1 Answer
- Newest
- Most votes
- Most comments
0
Please have a look into this blog post about Cognito token customization features that might help with third-party authenticators like Keycloak. https://aws.amazon.com/blogs/security/how-to-customize-access-tokens-in-amazon-cognito-user-pools/
Implementation Approach
Pre-Token Generation Flow
- Keycloak handles the initial authentication
- Cognito receives the authentication response
- The pre-token generation Lambda trigger can then customize the Cognito-issued tokens
Token Customization Capabilities
- Add application-specific claims to access tokens
- Refine scope claims for access control
- Include custom attributes for authorization decisions
Needs to be tested to be sure, but flow seems viable. Hope this helps. Thanks!
answered a year ago
Relevant content
asked 3 years ago

Thanks Vardan. Just to check if I have understood correctly, will(could) Cognito in this situation, issue an ID token and an access/refresh token pair? We have use cases for both. And which one would include the scopes? If we update the scopes directly as user-attributes in cognito, do we need to trigger a pre-token lambda? Wouldn't the tokens already carry the scope information as user-attributes? Please let me know your thoughts on this.
Yes, Cognito will issue all three tokens (ID token, access token, and refresh token) even when using Keycloak as the external authenticator. Here's how the token issuance works: Upon successful authentication through Keycloak, Cognito will issue:
https://docs.aws.amazon.com/cognito/latest/developerguide/amazon-cognito-user-pools-using-tokens-with-identity-providers.html
The access token includes the scopes in its scope claim. This includes:
The ID token does not contain scopes - it only contains identity information about the user. The access token is specifically designed for authorization purposes and is the one that carries the scope information for validating access rights
https://docs.aws.amazon.com/cognito/latest/developerguide/cognito-user-pools-define-resource-servers.html https://docs.aws.amazon.com/cognito/latest/developerguide/amazon-cognito-user-pools-using-the-access-token.html
Hello, If the answer was satisfactory to you please consider marking it as accepted. Thanks!