Search code examples
aws-lambdajwtamazon-cognitoaws-identitypools

How to retrieve custom JWT claims from within Lambda with Identity Pool?


I have the following scenario and am trying to understand the right way to implement it.

I have Okta as my IDP. Amazon API gateway for managing my APIs and some lambdas which handle the API requests. Identity Pool is used to provide AWS credentials to the client accessing the APIs.

When the client accesses the API, I need my lambda (which handles the request) to fetch the data from DynamoDB, and filter it based on a few attributes that are specific to the user that has logged in to the client. e.g. I need to retrieve accounts for a customer using the API, but the user only has access to certain accounts and so the lambda should filter the result.

I am thinking of having some custom claims defined for each user in Okta. When the client authenticates with Okta, it receives a JWT token with these claims. And it fetches the AWS credentials from Identity Pool with this token, to access the API. The API would trigger the lambda. Here, I would want to retrieve the claims and use them for filtering the data.

Any thoughts on how this can be achieved? Or is there a better way to address this?

Thank you.


Solution

  • We can use Lambda authorizers for such a scenario. Please refer one of the following documents based on your API type.

    1. REST APIs
    2. HTTP APIs

    (Conceptually both Lambda Authorizers are more or less same.)

    What you have to do is:

    • In the Lambda Authorizer validate the incoming JWT (which is generated by Okta). Then follow below steps only if the token is valid.
    • Based on the custom claim(s) (which you configured in the Okta for every user), create a key value pair(s) in the context of the output of the Lambda Authorizer (as mentioned in here or here)
    • Then those context details are available for your Lambda which does the DB lookup. With that you can do the filtering.