Skip to content

Bug: Unable to add custom claims to authentication token with Powertools v3.9.0 and later #7651

Closed

Description

@ogis-takeuchi

The following process worked correctly with Powertools for AWS Lambda (Python) v3.8.0 and earlier, but since v3.9.0, the response in the returned raw_event no longer contains the custom claims.

I would like to avoid directly editing the raw_event dictionary. Is there any recommended way to add custom claims to the authentication token in v3.9.0 or later?

Here is a simplified version of my handler:

    pre_token_generation.response.claims_scope_override_details.access_token_generation = {
        'claimsToAddOrOverride': {
            'custom_user_id': pre_token_generation.request.user_attributes['preferred_username'],
        }
    }
    # Return the raw event
    return pre_token_generation.raw_event

Expected Behaviour

■v3.8.0 raw_event example:

{
  'version': '2',
  'region': 'example-region-1',
  ...,
  'response': {
    'claimsAndScopeOverrideDetails': {
      'idTokenGeneration': {
        'claimsToAddOrOverride': {
          'custom_user_id': 'user123'
        }
      },
      'accessTokenGeneration': {
        'claimsToAddOrOverride': {
          'custom_user_id': 'user123'
        },
        'scopesToAdd': ['access:example_scope']
      }
    }
  }
}

Current Behaviour

In this case, response is empty.
■v3.9.0 raw_event example:

{
  'version': '2',
  'region': 'example-region-1',
  ...,
  'response': {}
}

Code snippet

    pre_token_generation.response.claims_scope_override_details.access_token_generation = {
        'claimsToAddOrOverride': {
            'custom_user_id': pre_token_generation.request.user_attributes['preferred_username'],
        }
    }
    # Return the raw event
    return pre_token_generation.raw_event

Possible Solution

No response

Steps to Reproduce

We send the following request to this function.

{
    "version": "2",
    "region": "region",
    "userPoolId": "example",
    "userName": "example",
    "triggerSource": "TokenGeneration_HostedAuth",
    "request": {
        "userAttributes": {
            "sub": "example",
            "email": "example@example.com",
            "preferred_username": "example",
            "family_name": "example",
            "given_name": "example",
            "phone_number": "example",
            "custom:family_name_kana": "example",
            "custom:given_name_kana": "example",
            "custom:company_name": "example",
            "custom:created_by": "test",
            "custom:updated_by": "test"
        }
    },
    "response": {}
}

Powertools for AWS Lambda (Python) version

latest

AWS Lambda function runtime

3.13

Packaging format used

PyPi

Debugging logs

Activity

  1. added
    bug
    triage
    on Nov 10, 2025
  2. boring-cyborg commented on Nov 10, 2025

    @boring-cyborg

    Thanks for opening your first issue here! We'll come back to you as soon as we can.
    In the meantime, check out the #python channel on our Powertools for AWS Lambda Discord: Invite link

  3. nc-dirknilius commented on Nov 10, 2025

    @nc-dirknilius
    Contributor

    I can confirm the issue as we were running into the same issue and found that by downgrading to 3.8 restores the expected behavior.

    Handler code (adding all claims prefixed with custom:):

    @event_source(data_class=PreTokenGenerationV2TriggerEvent)
    def handler(event: PreTokenGenerationV2TriggerEvent, _context) -> dict:
        custom_attributes = {
            key: value
            for key, value in event.request.user_attributes.items()
            if key.startswith("custom:")
        }
        
        event.response.claims_scope_override_details.access_token_generation = {
            "claimsToAddOrOverride": custom_attributes
        }
        
        return event.raw_event

    Example test failing (pytest):

    def test_handler_with_single_custom_attribute(base_event, context):
        """Test handler extracts and adds a single custom attribute to access token."""
        base_event.request.user_attributes["custom:role"] = "admin" # PreTokenGenerationV2TriggerEvent
    
        result = handler(base_event, context)
    
        claims = result["response"]["claimsAndScopeOverrideDetails"]["accessTokenGeneration"]["claimsToAddOrOverride"]
        assert claims == {"custom:role": "admin"}
  4. anafalcao commented on Nov 10, 2025

    @anafalcao
    Contributor

    Hi @ogis-takeuchi and @nc-dirknilius! Thanks for reporting this issue! I've confirmed the bug exists in v3.9.0 through the latest.
    Would you any of you be interested in submitting a PR? I'll add this to our backlog.

  5. added
    event_sources
    and removed
    triage
    on Nov 10, 2025
  6. nc-dirknilius commented on Nov 10, 2025

    @nc-dirknilius
    Contributor

    Hi @ogis-takeuchi and @nc-dirknilius! Thanks for reporting this issue! I've confirmed the bug exists in v3.9.0 through the latest. Would you any of you be interested in submitting a PR? I'll add this to our backlog.

    I checked briefly the code. I might be able to submit a PR with a fix as well as additional tests covering the usage.

  7. nc-dirknilius commented on Nov 10, 2025

    @nc-dirknilius
    Contributor

    Hi @anafalcao, PR is created and waiting for approval.

  8. changed the title [-]Bug: Unable to add custom claims to authentication token with PowerTools v3.9.0 and later[/-] [+]Bug: Unable to add custom claims to authentication token with Powertools v3.9.0 and later[/+] on Nov 11, 2025
  9. powertools-for-aws-oss-automation commented on Nov 19, 2025

    @powertools-for-aws-oss-automation

    Warning

    This issue is now closed. Please be mindful that future comments are hard for our team to see.
    If you need more assistance, please either reopen the issue, or open a new issue referencing this one.
    If you wish to keep having a conversation with other community members under this issue feel free to do so.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugevent_sources

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions