Validate Jwt token in ALB

0

We have multiple API microservices and we are using cognito for login/sign up, The issue is how do we validate the jwt token obtained from cognito, seems like ALB is not validating the header, I don't want to implement jwt validation in all my microservices, Is there a better way to validate jwt token without making any code change? like authorizer in API gateway where the auth is validated and then the request forwarded to particular services.

Anser
asked a year ago1566 views
1 Answer
0

You can use an Amazon API Gateway Authorizer to validate the JWT tokens obtained from Amazon Cognito.

An API Gateway Authorizer is a Lambda function that performs authentication and authorization checks before allowing the request to be passed to the microservices. It can be configured to accept a JWT token, validate it, and return an IAM policy document that specifies the permissions for the user making the request.

To set up an API Gateway Authorizer for JWT validation, you can follow these steps:

Create a new Lambda function that will serve as the Authorizer. This function will receive the JWT token in the Authorization header and will validate it using the Cognito SDK. If the token is valid, the function will return an IAM policy document that specifies the permissions for the user. If the token is not valid, the function will return an error.

Create an API Gateway REST API and define the endpoints for your microservices.

Create an Authorizer for your API. You can select the Lambda function you created in step 1 as the Authorizer.

Add the Authorizer to the endpoints that require authentication. When a client makes a request to one of these endpoints, API Gateway will call the Authorizer Lambda function to validate the JWT token before forwarding the request to the microservice.

By using an API Gateway Authorizer, you can centralize the authentication and authorization logic and avoid duplicating it in each microservice. This approach can also simplify your microservice code and reduce the risk of security vulnerabilities.

Here are some links that may be useful to you:

profile pictureAWS
answered a year ago
profile pictureAWS
EXPERT
reviewed a year ago
  • While this is certainly the standard use case and flow for API Gateway, the OP is asking specifically about JWT validation using an ALB. ALB does integrate with Cognito User Pool, but it redirects unauthenticated requests to Cognito assuming that the client is a browser. If you implement a simple REST API backed by a Lambda behind an ALB (intentionally not using API Gateway as it's much lighter weight), and you invoke the API using Curl, the desired behavior is for ALB to either provide a go or no-go (401) check based on the signed JWT's properties before forwarding the request to the target group.

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions