So i’m working on a mobile app and need to call my backend services through the AWS API Gateway, and for logged in users the request will have a JWT token in the request header. WHat I want to do is whenever a request is sent to gateway, I want a lambda function to check if the header exists if so then do the logic to validate the JWT token will be executed and then add a header User-ID corresponding the user with the JWT token and finally forward the reqeust to the final service or endpoint. Is this possible to do with AWS ?
I tried to use lambda integration but didn’t really get how it’s really working
The Technique you should use here (if you want to build it yourself and not use for example AWS Cognito) is “Lambda Authorizer”. You can create it in the console if you go to:
API Gateway -> APIs -> [YOUR API] ([YOUR API ID]) -> Authorization
and go to the tab “Manage authorizers”.
You can also choose to use some managed authorizer to validate the JWT. Then each consumer down the line can uppack and grab the User-ID directly from the JWT, now trusting that it is valid. Use a library like python-jose
to get get_unverified_claims
from the JWT. (Because now you know that your API Gateway actually verified the JWT.)