@@ -330,6 +330,66 @@ Ensuring this match is crucial for the proper functioning of the authentication
330330 ' http://localhost:4566/_aws/cognito-idp/oauth2/token'
331331{" access_token" : " eyJ0eXAi…lKaHx44Q" , " expires_in" : 86400, " token_type" : " Bearer" , " refresh_token" : " e3f08304" , " id_token" : " eyJ0eXAi…ADTXv5mA" }
332332```
333+ ### Client credentials grant
334+
335+ The client credentials grant is designed for machine-to-machine (M2M) communication.
336+ In contrast, the authorization code and implicit grants provide tokens to authenticated human users.
337+ The client credentials grant allows for scope-based authorization from a non-interactive system to an API.
338+ Your app can directly request client credentials from the token endpoint to receive an access token.
339+
340+ To request the token from LocalStack the correct URL is ` http://cognito-idp.localhost.localstack-test.cloud:4566/_aws/cognito-idp/oauth2/token ` .
341+ In case that there is more than one user pool, LocalStack detects the right one by inspecting the ` clientId ` of the requests.
342+
343+ Here is an example on how to set it up:
344+ ``` sh
345+ # Create user pool.
346+ export pool_id=$( awslocal cognito-idp create-user-pool --pool-name test --username-configuration " CaseSensitive=False" | jq -rc " .UserPool.Id" )
347+
348+ # Create client user pool with a client.
349+ 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" )
350+
351+ # Retrieve secret.
352+ export client_secret=$( awslocal cognito-idp describe-user-pool-client --user-pool-id $pool_id --client-id $client_id | jq -r ' .UserPoolClient.ClientSecret' )
353+
354+ # Create resource server
355+ awslocal cognito-idp create-resource-server \
356+ --user-pool-id $pool_id \
357+ --identifier " api-client-organizations" \
358+ --name " vitalera API Clients Organizations Resource Server" \
359+ --scopes ' [{"ScopeName":"read","ScopeDescription":"Read access to Organizations"}]'
360+ ```
361+
362+ Then you could retrieve the token from your application like this:
363+
364+ ``` python
365+ import requests, os
366+ from requests.auth import HTTPBasicAuth
367+
368+ def get_access_token_with_secret ():
369+ client_id = os.environ[' client_id' ]
370+ client_secret = os.environ[' client_secret' ]
371+ scope = ' api-client-organizations/read'
372+ url = ' http://cognito-idp.localhost.localstack.cloud:4566/_aws/cognito-idp/oauth2/token'
373+
374+
375+ headers = {
376+ ' Content-Type' : ' application/x-www-form-urlencoded'
377+ }
378+
379+ auth = HTTPBasicAuth(client_id,client_secret)
380+
381+ payload = {
382+ ' grant_type' : ' client_credentials' ,
383+ ' client_id' :client_id,
384+ ' scope' :scope
385+ }
386+ response = requests.post(url,headers = headers,auth = auth,data = payload)
387+
388+ print (response.content)
389+
390+ if __name__ == " __main__" :
391+ get_access_token_with_secret()
392+ ```
333393
334394## Serverless and Cognito
335395
0 commit comments