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
질문됨 일 년 전1666회 조회
1개 답변
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
답변함 일 년 전
profile pictureAWS
전문가
검토됨 일 년 전
  • 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.

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인