A company is building a multi-tenant REST API using Amazon API Gateway. The API must validate incoming calls from clients using a custom JSON Web Token (JWT) sent in the X-Custom-Auth header. The token validation requires checking the token's signature against a public key, verifying that the tenant_id claim matches a list of active tenants, and dynamically generating an IAM policy to restrict access to only the tenant's specific resources. The authorization decision needs to be cached for 300 seconds to optimize performance. Which solution should a developer implement to meet these requirements?
- Create a Lambda authorizer of type REQUEST. Configure method.request.header.X-Custom-Auth as the identity source. In the Lambda function, validate the JWT, extract the tenant_id claim, construct an IAM policy targeting the tenant's specific resource path, and return the policy.Answer
- BConfigure an Amazon Cognito User Pool authorizer. Set the token source header to X-Custom-Auth. Configure a Cognito post-authentication Lambda trigger to validate the JWT signature, extract the tenant_id claim, and dynamically generate the required IAM policy for API Gateway.
- CConfigure a Cognito Identity Pool to authenticate clients using the custom JWT. Enable AWS_IAM authorization on the API Gateway methods. Instruct clients to exchange their JWT for temporary AWS credentials and sign their API Gateway requests using Signature Version 4 (SigV4).
- DConfigure a Lambda Proxy integration to pass the X-Custom-Auth header directly to the backend Lambda function. In the backend Lambda function, validate the JWT, extract the tenant_id claim, and use the AWS Security Token Service (STS) to dynamically apply the generated IAM policy to the current execution context.
Answer
Create a Lambda authorizer of type REQUEST. Configure method.request.header.X-Custom-Auth as the identity source. In the Lambda function, validate the JWT, extract the tenant_id claim, construct an IAM policy targeting the tenant's specific resource path, and return the policy.
The correct solution uses a REQUEST-type Lambda authorizer. By defining the identity source as `method.request.header.X-Custom-Auth`, API Gateway can cache the generated IAM policy for 300 seconds. The Lambda authorizer executes code to validate the third-party JWT, check the tenant_id claim, and return a custom IAM policy that limits access to only the tenant's specific API paths, complying with least-privilege security principles.
Step-by-Step Solution
Key Concept
API Gateway Lambda Authorizer with Token Caching and Scoped IAM Policies