Daniel Birowsky Popeski
Daniel Birowsky Popeski

Reputation: 9286

Use Cognito authorizer just as a JWT translator

If the token is invalid, I don't want the request to fail with 'Unauthorized'. Instead, I still want to get the request within my lambda, albeit with no claims data, and from there, decide what to do onwards.

This means that I want the Cognito Authorizer to act just as a translator of the JWT token, when it's available and valid.

Upvotes: 0

Views: 835

Answers (1)

agent420
agent420

Reputation: 3521

You can not use API Gateway's built-in Cognito Userpool authorizer this way. It will always return Unauthorized on invalid credentials. For your use, case you would have to use the Custom Authorizer. You can always return an Allow policy and return some claims data only on valid token (using Enhanced context).

I must say, authorizers are not designed for this. Seems like you just want the claims data in your backend and use them if they are valid & ignore them if the token is invalid. If so, you do not need to use Authorizers for this. You can pass the token to backend directly & there write a code (same as Custom Authorizer's code snippet) to chech the validity of JWT & extract valid claims. There are multiple third-party libraries which can make this process easy & this token verification (& claims extraction) can be done using just a few lines of code.

EDIT

To verify the token, you need to:

Here is an AWS blog with the code you want: https://aws.amazon.com/blogs/mobile/integrating-amazon-cognito-user-pools-with-api-gateway/

Upvotes: 2

Related Questions