Soru

Zorluk: Çok zorAPI Development and Integration with Amazon API Gateway

A developer is building a serverless web application. The frontend, hosted on a static website in an Amazon S3 bucket, needs to call a secure backend API hosted on Amazon API Gateway. The API has a resource with `OPTIONS` and `POST` methods. The `POST` method is integrated with an AWS Lambda function using a Lambda custom integration (`AWS` integration type). To secure the API, the developer configures an Amazon Cognito User Pool authorizer and applies it to both the `OPTIONS` and `POST` methods. During testing, the frontend application fails to make requests, and the browser console displays a CORS error during the preflight phase. Additionally, the developer notes that the Lambda function cannot access the Cognito group membership claims of the authenticated user to perform fine-grained authorization. How should the developer resolve these issues?

  1. A
    Keep the Cognito User Pool authorizer on both the `OPTIONS` and `POST` methods. Update the authorizer configuration's 'Identity Sources' to include both the `Authorization` header and the `Origin` header to authenticate the preflight request.
  2. B
    Change the `POST` method integration to a Lambda proxy integration (`AWS_PROXY`). In the API Gateway integration request settings, define a request mapping template to extract the Cognito group claims from `$context.authorizer.claims` and inject them into the proxy request body.
  3. C
    Set the authorization type of the `OPTIONS` method to `NONE`. In the Lambda function's code, add the `Access-Control-Allow-Origin` header to the returned JSON response to satisfy the browser's CORS requirements, and retrieve the group claims from the `event.headers` object.
  4. Set the authorization type of the `OPTIONS` method to `NONE`. In the request mapping template for the `POST` method integration request, map the `$context.authorizer.claims['cognito:groups']` context variable to a JSON property in the payload sent to the Lambda function.Cevap

Cevap

Set the authorization type of the `OPTIONS` method to `NONE`. In the request mapping template for the `POST` method integration request, map the `$context.authorizer.claims['cognito:groups']` context variable to a JSON property in the payload sent to the Lambda function.
Setting the authorization type of the OPTIONS method to NONE allows the browser to perform the preflight CORS handshake without authorization tokens. Additionally, because the API uses a custom integration, request mapping templates are required to transform the incoming request and pass context variables such as `$context.authorizer.claims['cognito:groups']` to the Lambda function.

Adım Adım Çözüm

1
Address the CORS preflight authorization failure.
Identify that browsers do not include credentials in CORS preflight (OPTIONS) requests, causing the authorizer on the OPTIONS method to fail. Set the OPTIONS method authorization type to NONE.
This allows the browser's preflight check to complete successfully without requiring authentication.
2
Configure the custom integration to forward user claims to the backend.
Recognize that Lambda custom integrations (unlike proxy integrations) do not forward the full request context or Cognito claims by default. Define an API Gateway request mapping template for the POST method.
The mapping template extracts the claims from `$context.authorizer.claims['cognito:groups']` and injects them into the JSON payload sent to the Lambda function, giving the function access to the group membership details.

Anahtar Kavram

Handling CORS preflight authorization and mapping Cognito authorizer context in API Gateway custom integrations
Tahmini Süre:3m 0s
Bu soruyu puanla