Skip to content
This repository was archived by the owner on Aug 7, 2025. It is now read-only.

Commit 81af892

Browse files
authored
Cognito client crendetials flow (#1528)
1 parent 8bae102 commit 81af892

File tree

1 file changed

+66
-0
lines changed
  • content/en/user-guide/aws/cognito

1 file changed

+66
-0
lines changed

content/en/user-guide/aws/cognito/index.md

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,72 @@ Ensuring this match is crucial for the proper functioning of the authentication
331331
{"access_token": "eyJ0eXAi…lKaHx44Q", "expires_in": 86400, "token_type": "Bearer", "refresh_token": "e3f08304", "id_token": "eyJ0eXAi…ADTXv5mA"}
332332
```
333333

334+
### Client credentials grant
335+
336+
The client credentials grant is designed for machine-to-machine (M2M) communication.
337+
The Client Credentials Grant allows the machine (client) to authenticate itself directly with the authorization server using its credentials, such as a client ID and client secret.
338+
The client credentials grant allows for scope-based authorization from a non-interactive system to an API.
339+
Your app can directly request client credentials from the token endpoint to receive an access token.
340+
341+
To request the token from the LocalStack URL, use the following endpoint: `://cognito-idp.localhost.localstack.cloud:4566/_aws/cognito-idp/oauth2/token`.
342+
For additional information on our endpoints, refer to our [Internal Endpoints](https://docs.localstack.cloud/references/internal-endpoints/) documentation.
343+
344+
If there are multiple user pools, LocalStack identifies the appropriate one by examining the `clientid` of the request.
345+
346+
To get started, follow the example below:
347+
348+
```sh
349+
#Create client user pool with a client.
350+
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")
351+
352+
#Retrieve secret.
353+
export client_secret=$(awslocal cognito-idp describe-user-pool-client --user-pool-id $pool_id --client-id $client_id | jq -r '.UserPoolClient.ClientSecret')
354+
355+
#Create resource server
356+
awslocal cognito-idp create-resource-server \
357+
--user-pool-id $pool_id \
358+
--identifier "api-client-organizations" \
359+
--name "Resource Server Name" \
360+
--scopes '[{"ScopeName":"read","ScopeDescription":"Read access to Organizations"}]'
361+
362+
```
363+
364+
You can retrieve the token from your application using the specified endpoint: `http://cognito-idp.localhost.localstack.cloud:4566/_aws/cognito-idp/oauth2/token`.
365+
366+
```javascript
367+
require('dotenv').config();
368+
const axios = require('axios');
369+
370+
async function getAccessTokenWithSecret() {
371+
const clientId = process.env.client_id;
372+
const clientSecret = process.env.client_secret;
373+
const scope = 'api-client-organizations/read';
374+
const url = 'http://cognito-idp.localhost.localstack.cloud:4566/_aws/cognito-idp/oauth2/token';
375+
376+
const authHeader = Buffer.from(`${clientId}:${clientSecret}`).toString('base64');
377+
378+
const headers = {
379+
'Content-Type': 'application/x-www-form-urlencoded',
380+
'Authorization': `Basic ${authHeader}`
381+
};
382+
383+
const payload = new URLSearchParams({
384+
grant_type: 'client_credentials',
385+
client_id: clientId,
386+
scope: scope
387+
});
388+
389+
try {
390+
const response = await axios.post(url, payload, { headers });
391+
console.log(response.data);
392+
} catch (error) {
393+
console.error('Error fetching access token:', error.response ? error.response.data : error.message);
394+
}
395+
}
396+
397+
getAccessTokenWithSecret();
398+
```
399+
334400
## Serverless and Cognito
335401

336402
Furthermore, you have the option to combine Cognito and LocalStack seamlessly with the [Serverless framework](https://www.serverless.com/).

0 commit comments

Comments
 (0)