A developer is designing a serverless multi-tenant SaaS application. The frontend client sends requests to an Amazon API Gateway REST API backed by AWS Lambda. The application uses an external OpenID Connect (OIDC) identity provider for user authentication. The API must validate the signature and expiration of the incoming JSON Web Token (JWT). In addition, access to specific resource paths and HTTP methods must be dynamically controlled based on the user's tenant ID and user role claims embedded in the JWT. The backend Lambda function needs to receive these validated claims to perform tenant-specific business logic without re-decoding or re-validating the token. Which solution meets these requirements with the lowest latency and follows security best practices?
- Create a Lambda Request Authorizer in API Gateway. In the authorizer function, validate the JWT from the external identity provider, dynamically generate an IAM policy that allows or denies access to the specific API resource paths and methods based on the tenant ID and user role claims, and return the policy along with the claims in the context object of the authorizer's response to be accessed via the requestContext.authorizer object in the backend Lambda function.Answer
- BConfigure a Cognito User Pools Authorizer on API Gateway and federate the external OIDC identity provider with the user pool. In the backend Lambda function, extract the JWT from the authorization header, manually decode it, and validate its signature. Use custom code in the backend Lambda function to enforce path-based authorization and log tenant activity.
- CConfigure a Cognito Identity Pool and integrate the external OIDC identity provider. Have the client exchange the OIDC JWT for temporary AWS credentials using the Cognito Identity Pool, configure the API Gateway API to use IAM Authorization, and parse the OIDC JWT inside the backend Lambda function to retrieve the tenant ID and user role for logging.
- DCreate a Lambda Token Authorizer in API Gateway to validate the JWT. In the authorizer function, return an IAM policy allowing access. Since the Lambda Proxy Integration does not support forwarding custom authorizer metadata directly, configure a custom integration mapping template to extract the JWT claims from the header and map them to a custom JSON payload sent to the backend Lambda function.