Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass cognito authentication token in the headers through api gateway to a lambda function

I am working on a full-stack project. And I use AWS cognito to do the Authentication part. And on my front-end, I can get the idToken successfully and put into the method headers. Here is the get method code:

          Axios.get(`url`, {
                headers: {
                    'Authorization': Token
                }
            })

On the backend, I use AWS api gateway and lambda. As I understand, if I want to get the token in the lamdba, I have to set up the mapping template in the Integration Request of APIgateway. So here is what I write, but I can't get the idToken. Anyone knows the correct way to get the idToken?

{
  
  "idToken":"$context.authorizer.claims.authorization"
}
like image 550
LiLian Avatar asked Oct 21 '25 20:10

LiLian


1 Answers

I close this question by myself. After my search, if you use AWS Auth cognit and you want to get the properties in the token, please refer to this doc:

idToken palyload https://docs.aws.amazon.com/cognito/latest/developerguide/amazon-cognito-user-pools-using-tokens-with-identity-providers.html api gateway mapping template: https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-mapping-template-reference.html#context-variable-reference.

ex: if you want to get the userName from the id token, on the mapping template, it should be:

{
    "userName" : "$context.authorizer.claims['cognito:username']"
}
like image 189
LiLian Avatar answered Oct 25 '25 11:10

LiLian



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!