Skip to content

Can AWS Cognito Access Token can contain preferred_username attribute in its claims?

0

I want to know whether preferred_username claim is only present in AWS Cognito ID token only or can it be present inside Access token as well? If not, How I can bring preferred username in access token?

asked a month ago156 views

1 Answer
0

Hello.

preferred_username is typically not included in the standard claims of a Cognito Access Token. However, I believe it can be included as a user attribute in the ID Token.
For Access Tokens, it may be possible to add it using the Pre Token Generation Lambda trigger.
https://docs.aws.amazon.com/cognito/latest/developerguide/user-pool-lambda-pre-token-generation.html

This Lambda trigger can add, remove, and modify some claims in identity and access tokens before Amazon Cognito issues them to your app. To use this feature, associate a Lambda function from the Amazon Cognito user pools console or update your user pool LambdaConfig through the AWS Command Line Interface (AWS CLI).

EXPERT

answered a month ago

EXPERT

reviewed a month ago

  • Please note that you should use V2.0 or later when testing. Under AWS specifications, V1.0 is primarily intended for customizing ID tokens, whereas V2.0 enables the addition or modification of access token claims via accessTokenGeneration.claimsToAddOrOverride. In Cognito, V2.0 is available with the Essentials or Plus feature plans, while V3.0 is available if M2M functionality is included.

  • Thanks it is useful. I have a further question, then who decides/sets the value of preferred_username in case we choose the option of lamda ?

  • Generally speaking, I believe the preferred_username attribute stored in the Cognito User Pool can be copied directly into the access token. The user's User Pool attributes are passed to the Pre Token Generation Lambda function via event.request.userAttributes. AWS also describes userAttributes as "attributes of the user profile in the user pool." I don't think Lambda is generating the username; I believe it is simply reading a value that is already stored in the Cognito User Pool.
    Therefore, I believe you can retrieve it from the Lambda event as follows. https://docs.aws.amazon.com/en_en/cognito/latest/developerguide/user-pool-lambda-pre-token-generation.html https://docs.aws.amazon.com/hi_in/cognito/latest/developerguide/user-pool-settings-attributes.html

    def lambda_handler(event, context):
        preferred_username = event["request"]["userAttributes"].get(
            "preferred_username"
        )
    
  • Thanks it is useful as I am new to this Lamda Trigger. This answer my question. I have follow up question, does "username" claim appears in access token by default? I see AWS official documentation shows that username is part of access token payload.

  • Yes, in this case, the username claim is included in the user's access token by default. As you noted, the access token sample in the current official AWS documentation also includes "username": "my-test-user", which AWS describes as "the user's username in the user pool." https://docs.aws.amazon.com/cognito/latest/developerguide/amazon-cognito-user-pools-using-the-access-token.html

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.