Skip to content

Commit 745358e

Browse files
committed
revamp cognito docs
1 parent ef2ee3a commit 745358e

File tree

1 file changed

+79
-50
lines changed

1 file changed

+79
-50
lines changed

src/content/docs/aws/services/cognito.md

Lines changed: 79 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
---
22
title: "Cognito"
3-
linkTitle: "Cognito"
43
description: Get started with Cognito on LocalStack
54
tags: ["Base"]
65
persistence: supported
@@ -13,7 +12,7 @@ Cognito enables developers to add user sign-up, sign-in, and access control func
1312
Cognito supports various authentication methods, including social identity providers, SAML-based identity providers, and custom authentication flows.
1413

1514
LocalStack allows you to use the Cognito APIs in your local environment to manage authentication and access control for your local application and resources.
16-
The supported APIs are available on our [Cognito Identity coverage page]({{< ref "coverage_cognito-identity" >}}) and [Cognito User Pools coverage page]({{< ref "coverage_cognito-idp" >}}), which provides information on the extent of Cognito's integration with LocalStack.
15+
The supported APIs are available on our [Cognito Identity coverage page]() and [Cognito User Pools coverage page](), which provides information on the extent of Cognito's integration with LocalStack.
1716

1817
## Getting started
1918

@@ -27,9 +26,9 @@ We will demonstrate how you can create a Cognito user pool and client, and then
2726
To create a user pool, you can use the [`CreateUserPool`](https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_CreateUserPool.html) API call.
2827
The following command creates a user pool named `test`:
2928

30-
{{< command >}}
31-
$ awslocal cognito-idp create-user-pool --pool-name test
32-
{{< /command >}}
29+
```bash
30+
awslocal cognito-idp create-user-pool --pool-name test
31+
```
3332

3433
You can see an output similar to the following:
3534

@@ -66,15 +65,15 @@ You can see an output similar to the following:
6665
You will need the user pool's `id` for further operations.
6766
Save it in a `pool_id` variable:
6867

69-
{{< command >}}
70-
$ pool_id=<your-pool-id>
71-
{{< /command >}}
68+
```bash
69+
pool_id=<your-pool-id>
70+
```
7271

7372
Alternatively, you can use JSON processor like [`jq`](https://stedolan.github.io/jq/) to extract the essential information right from the outset when creating a pool.
7473

75-
{{< command >}}
76-
$ pool_id=$(awslocal cognito-idp create-user-pool --pool-name test | jq -rc ".UserPool.Id")
77-
{{< /command >}}
74+
```bash
75+
pool_id=$(awslocal cognito-idp create-user-pool --pool-name test | jq -rc ".UserPool.Id")
76+
```
7877

7978
### Adding a Client
8079

@@ -83,9 +82,9 @@ You will require the ID of the newly created client for the subsequent steps.
8382
You can use the [`CreateUserPoolClient`](https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_CreateUserPoolClient.html) for both client creation and extraction of the corresponding ID.
8483
Run the following command:
8584

86-
{{< command >}}
87-
$ client_id=$(awslocal cognito-idp create-user-pool-client --user-pool-id $pool_id --client-name test-client | jq -rc ".UserPoolClient.ClientId")
88-
{{< /command >}}
85+
```bash
86+
client_id=$(awslocal cognito-idp create-user-pool-client --user-pool-id $pool_id --client-name test-client | jq -rc ".UserPoolClient.ClientId")
87+
```
8988

9089
### Using Predefined IDs for Pool Creation
9190

@@ -95,39 +94,49 @@ Please note that a valid custom id must be in the format `<region>_<custom_pool_
9594

9695
Run the following command to create a user pool with a predefined ID:
9796

98-
{{< command >}}
99-
$ awslocal cognito-idp create-user-pool --pool-name p1 --user-pool-tags "_custom_id_=us-east-1_myid123"
97+
```bash
98+
awslocal cognito-idp create-user-pool --pool-name p1 --user-pool-tags "_custom_id_=us-east-1_myid123"
99+
```
100+
101+
The output will be:
102+
103+
```json
100104
{
101105
"UserPool": {
102106
"Id": "myid123",
103107
"Name": "p1",
104108
...
105-
{{< /command >}}
109+
```
106110

107111
You also have the possibility to create a Cognito user pool client with a predefined ID by specifying a `ClientName` with the specific format: `_custom_id_:<custom_client_id>`.
108112

109-
{{< command >}}
110-
$ awslocal cognito-idp create-user-pool-client --user-pool-id us-east-1_myid123 --client-name _custom_id_:myclient123
113+
```bash
114+
awslocal cognito-idp create-user-pool-client --user-pool-id us-east-1_myid123 --client-name _custom_id_:myclient123
115+
```
116+
117+
The output will be:
118+
119+
```json
111120
{
112121
"UserPoolClient": {
113122
"UserPoolId": "us-east-1_myid123",
114123
"ClientName": "_custom_id_:myclient123",
115124
"ClientId": "myclient123",
116125
...
117-
{{< /command >}}
126+
```
118127

119128
### Signing up and confirming a user
120129

121130
You can now use the [`SignUp`](https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_SignUp.html) API to sign up a user.
122131
Run the following command:
123132

124-
{{< command >}}
125-
$ awslocal cognito-idp sign-up \
133+
```bash
134+
awslocal cognito-idp sign-up \
126135
--client-id $client_id \
127136
--username example_user \
128137
--password 12345678Aa! \
129138
--user-attributes Name=email,Value=<[email protected]>
130-
{{< /command >}}
139+
```
131140

132141
You can see an output similar to the following:
133142

@@ -140,7 +149,7 @@ You can see an output similar to the following:
140149

141150
Once the user is successfully created, a confirmation code will be generated.
142151
This code can be found in the LocalStack container logs (as shown below).
143-
Additionally, if you have [SMTP configured]({{< ref "configuration#emails" >}}), the confirmation code can be optionally sent via email for enhanced convenience and user experience.
152+
Additionally, if you have [SMTP configured](/aws/capabilities/config/configuration/#emails), the confirmation code can be optionally sent via email for enhanced convenience and user experience.
144153

145154
```bash
146155
INFO:localstack_ext.services.cognito.cognito_idp_api: Confirmation code for Cognito user example_user: 125796
@@ -150,18 +159,23 @@ DEBUG:localstack_ext.bootstrap.email_utils: Sending confirmation code via email
150159
You can confirm the user with the activation code, using the [`ConfirmSignUp`](https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_ConfirmSignUp.html) API.
151160
Execute the following command:
152161

153-
{{< command >}}
154-
$ awslocal cognito-idp confirm-sign-up \
162+
```bash
163+
awslocal cognito-idp confirm-sign-up \
155164
--client-id $client_id \
156165
--username example_user \
157166
--confirmation-code <received-confirmation-code>
158-
{{< /command >}}
167+
```
159168

160169
Since the above command does not provide a direct response, we need to verify the success of the request by checking the pool.
161170
Run the following command to use the [`ListUsers`](https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_ListUsers.html) API to list the users in the pool:
162171

163-
{{< command "hl_lines=21" >}}
164-
$ awslocal cognito-idp list-users --user-pool-id $pool_id
172+
```bash
173+
awslocal cognito-idp list-users --user-pool-id $pool_id
174+
```
175+
176+
The output will be similar to the following:
177+
178+
```json
165179
{
166180
"Users": [
167181
{
@@ -185,7 +199,7 @@ $ awslocal cognito-idp list-users --user-pool-id $pool_id
185199
}
186200
]
187201
}
188-
{{< /command >}}
202+
```
189203

190204
## JWT Token Issuer and JSON Web Key Sets (JWKS) endpoints
191205

@@ -204,17 +218,27 @@ https://cognito-idp.localhost.localstack.cloud/<pool_id>
204218

205219
To access the JSON Web Key Sets (JWKS) configuration for each user pool, you can use the standardized well-known URL below:
206220

207-
{{< command >}}
208-
$ curl 'http://localhost:4566/<pool_id>/.well-known/jwks.json'
221+
```bash
222+
curl 'http://localhost:4566/<pool_id>/.well-known/jwks.json'
223+
```
224+
225+
The output will be similar to the following:
226+
227+
```json
209228
{"keys": [{"kty": "RSA", "alg": "RS256", "use": "sig", "kid": "test-key", "n": "k6lrbEH..."]}
210-
{{</ command >}}
229+
```
211230

212231
Moreover, you can retrieve the global region-specific public keys for Cognito Identity Pools using the following endpoint:
213232

214-
{{< command >}}
215-
$ curl http://localhost:4566/.well-known/jwks_uri
233+
```bash
234+
curl http://localhost:4566/.well-known/jwks_uri
235+
```
236+
237+
The output will be similar to the following:
238+
239+
```bash
216240
{"keys": [{"kty": "RSA", "alg": "RS512", "use": "sig", "kid": "ap-northeast-11", "n": "AI7mc1assO5..."]}
217-
{{</ command >}}
241+
```
218242

219243
## Cognito Lambda Triggers
220244

@@ -278,23 +302,23 @@ exports.handler = async (event) => {
278302

279303
Enter the following commands to create the Lambda function:
280304

281-
{{< command >}}
282-
$ zip function.zip index.js
283-
$ awslocal lambda create-function \
305+
```bash
306+
zip function.zip index.js
307+
awslocal lambda create-function \
284308
--function-name migrate_users \
285309
--runtime nodejs18.x \
286310
--zip-file fileb://function.zip \
287311
--handler index.handler \
288312
--role arn:aws:iam::000000000000:role/lambda-role
289-
{{</ command >}}
313+
```
290314

291315
Subsequently, you can define the corresponding `--lambda-config` when creating the user pool to link it with the Lambda function:
292316

293-
{{< command >}}
294-
$ awslocal cognito-idp create-user-pool \
317+
```bash
318+
awslocal cognito-idp create-user-pool \
295319
--pool-name test2 \
296320
--lambda-config '{"UserMigration":"arn:aws:lambda:us-east-1:000000000000:function:migrate_users"}'
297-
{{</ command >}}
321+
```
298322

299323
Upon successful authentication of a non-registered user, Cognito will automatically trigger the migration Lambda function, allowing the user to be added to the pool after migration.
300324

@@ -310,7 +334,7 @@ Replace `<client_id>` with the ID of your existing user pool client (for example
310334

311335
The login form should look similar to the screenshot below:
312336

313-
{{< figure src="cognitoLogin.png" width="320" >}}
337+
![Cognito Login Form](/images/aws/cognito-login-form.png)
314338

315339
Upon successful login, the page will automatically redirect to the designated `<redirect_uri>`, with an appended path parameter `?code=<code>`.
316340
For instance, the redirect URL might look like `http://example.com?code=test123`.
@@ -320,13 +344,18 @@ To obtain a token, you need to submit the received code using `grant_type=author
320344
Note that the value of the `redirect_uri` parameter in your token request must match the value provided during the login process.
321345
Ensuring this match is crucial for the proper functioning of the authentication flow.
322346

323-
```sh
324-
% curl \
347+
```bash
348+
curl \
325349
--data-urlencode 'grant_type=authorization_code' \
326350
--data-urlencode 'redirect_uri=http://example.com' \
327351
--data-urlencode "client_id=${client_id}" \
328352
--data-urlencode 'code=test123' \
329353
'http://localhost:4566/_aws/cognito-idp/oauth2/token'
354+
```
355+
356+
The output will be similar to the following:
357+
358+
```json
330359
{"access_token": "eyJ0eXAi…lKaHx44Q", "expires_in": 86400, "token_type": "Bearer", "refresh_token": "e3f08304", "id_token": "eyJ0eXAi…ADTXv5mA"}
331360
```
332361

@@ -338,13 +367,13 @@ The client credentials grant allows for scope-based authorization from a non-int
338367
Your app can directly request client credentials from the token endpoint to receive an access token.
339368

340369
To request the token from the LocalStack URL, use the following endpoint: `://cognito-idp.localhost.localstack.cloud:4566/_aws/cognito-idp/oauth2/token`.
341-
For additional information on our endpoints, refer to our [Internal Endpoints]({{< ref "/references/internal-endpoints" >}}) documentation.
370+
For additional information on our endpoints, refer to our [Internal Endpoints]() documentation.
342371

343372
If there are multiple user pools, LocalStack identifies the appropriate one by examining the `clientid` of the request.
344373

345374
To get started, follow the example below:
346375

347-
```sh
376+
```bash
348377
#Create client user pool with a client.
349378
export client_id=$(awslocal cognito-idp create-user-pool-client --user-pool-id $pool_id --client-name test-client --generate-secret | jq -rc ".UserPoolClient.ClientId")
350379

@@ -449,7 +478,7 @@ Authentication: AWS4-HMAC-SHA256 Credential=test-1234567/20190821/us-east-1/cogn
449478
The LocalStack Web Application provides a Resource Browser for managing Cognito User Pools, and more.
450479
You can access the Resource Browser by opening the LocalStack Web Application in your browser, navigating to the **Resources** section, and then clicking on **Cognito** under the **Security Identity Compliance** section.
451480

452-
<img src="cognito-resource-browser.png" alt="Cognito Resource Browser" title="Cognito Resource Browser" width="900" />
481+
![Cognito Resource Browser](/images/aws/cognito-resource-browser.png)
453482

454483
The Resource Browser allows you to perform the following actions:
455484

@@ -472,4 +501,4 @@ The following code snippets and sample applications provide practical examples o
472501

473502
By default, LocalStack's Cognito does not send actual email messages.
474503
However, if you wish to enable this feature, you will need to provide an email address and configure the corresponding SMTP settings.
475-
The instructions on configuring the connection parameters of your SMTP server can be found in the [Configuration]({{< ref "configuration#emails" >}}) guide to allow your local Cognito environment to send email notifications.
504+
The instructions on configuring the connection parameters of your SMTP server can be found in the [Configuration](/aws/capabilities/config/configuration/#emails) guide to allow your local Cognito environment to send email notifications.

0 commit comments

Comments
 (0)