A developer is building a web application that uses Amazon Cognito User Pools for user authentication and Amazon API Gateway REST APIs for the backend. The developer needs to restrict access to a specific API resource so that only users who have a custom user attribute `custom:membership` set to `Gold` can access it. The client application must be able to call the API by passing the Cognito ID token in the `Authorization` header, without having to sign the requests using AWS Signature Version 4. Which solution should the developer implement to meet these requirements?
- Create a custom API Gateway Lambda authorizer that decodes the Cognito ID token, verifies its signature, validates the custom membership claim value, and returns an IAM policy to allow or deny the request.Answer
- BConfigure an API Gateway Cognito User Pool authorizer and specify the custom membership attribute as an authorization scope on the API Gateway method request.
- CUse an Amazon Cognito Identity Pool to federate authentication, map the custom membership claim to an IAM role, and configure the API Gateway method to use AWS_IAM authorization.
- DCreate a custom API Gateway Lambda authorizer that calls the Cognito AdminGetUser API using the AWS SDK to retrieve the user's attributes and check the custom membership value on each request.
Answer
Create a custom API Gateway Lambda authorizer that decodes the Cognito ID token, verifies its signature, validates the custom membership claim value, and returns an IAM policy to allow or deny the request.
The correct solution uses a custom API Gateway Lambda authorizer to decode the Cognito ID token, verify its signature, and inspect the custom membership claim value. Because Cognito ID tokens are JSON Web Tokens (JWTs) that carry custom user attributes in their payload, the Lambda authorizer can perform this check offline without calling Cognito APIs, and then return the appropriate IAM policy to allow or deny access. This achieves the desired authorization logic without requiring the client to perform Signature Version 4 signing.
Step-by-Step Solution
Key Concept
Fine-grained API Gateway authorization using Cognito ID token claims with a custom Lambda Authorizer.