How to get cognito user pool's custom attributes from access token? it shows only user attrs why?

221 Views Asked by At

def lambda_handler(event, context):
    if event["httpMethod"] == "GET":
            print('Event_Auth:', event)
            
            headers = event['headers']
            access_token = headers.get('Authorization')
            if access_token:
                pieces = access_token.split()
                if len(pieces) == 2 and pieces[0].lower() == 'bearer':
                    access_token = pieces[1]
            print('Without Bearer:', access_token)
            is_valid = validate_token(access_token)


def validate_token(access_token):
    try
        response = cognito.get_user(AccessToken=access_token)
        if response['Username']:
            print('Response:', response)
            print('Cognito Username:', response['Username'])
            return True
    except:
        return False

This is my lambda handler, what we are using? we have an authroizer in api-gateway which triggers a lambda function and obtain an Access Token... i have an intermediate knowlodge about aws. Problem: We haved added custom attrs in coginto user pool, and we need these attrs to build SaaS which is very much important for us. So, what is happening now, we are unable to retrieve custom attrs of user pool in the event because custom attrs doesn't exist in event, but user attrs are in the event, then why custom doesn't exists in the event? What will be the solution for that, kindly let me know we stuck there from last couple of days?

`
Response: {'Username': 'XXXXXXX', 'UserAttributes': [{'Name': 'sub', 'Value': 'ca2ae0ae-XXXXX-XXXXX'}, {'Name': 'email_verified', 'Value': 'true'}, {'Name': 'email', 'Value': '[email protected]'}], 'ResponseMetadata': {'RequestId': '87314820-7141-4089-a92c-XXXXXXXXXXXX', 'HTTPStatusCode': 200, 'HTTPHeaders': {'date': 'Wed, 09 Aug 2023 12:04:57 GMT', 'content-type': 'application/x-amz-json-1.1', 'content-length': '19X', 'connection': 'keep-alive', 'x-amzn-requestid': '87314820-xxxxxxxxx-xxxx'}, 'RetryAttempts': 0}}
`

Response: UserAttrs exists but custom attrs are not showing

0

There are 0 best solutions below