Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// ------------------------------------------------------------------------
const api = new apigateway.RestApi(this, id + "API");
const integration = new apigateway.LambdaIntegration(apiFunction, {
// lambda proxy integration:
// see https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html#api-gateway-create-api-as-simple-proxy
proxy: true
});
// ------------------------------------------------------------------------
// Cognito Authorizer
// ------------------------------------------------------------------------
const cfnAuthorizer = new apigateway.CfnAuthorizer(this, id, {
name: "CognitoAuthorizer",
type: AuthorizationType.COGNITO,
identitySource: "method.request.header." + authorizationHeaderName,
restApiId: api.restApiId,
providerArns: [userPool.userPoolArn]
});
// ------------------------------------------------------------------------
// Root (/) - no authorization required
// ------------------------------------------------------------------------
const rootResource = api.root;
rootResource.addMethod("ANY", integration);
// ------------------------------------------------------------------------
// All Other Paths (/{proxy+}) - authorization required
const rootResource = api.root;
rootResource.addMethod("ANY", integration);
// ------------------------------------------------------------------------
// All Other Paths (/{proxy+}) - authorization required
// ------------------------------------------------------------------------
// all other paths require the cognito authorizer (validates the JWT and passes it to the lambda)
const proxyResource = rootResource.addResource("{proxy+}");
const method = proxyResource.addMethod("ANY", integration, {
authorizer: {authorizerId: cfnAuthorizer.ref},
authorizationType: AuthorizationType.COGNITO,
});
// uncomment to use an access token instead of an id token
// const cfnMethod = method.node.defaultChild as apigateway.CfnMethod;
// cfnMethod.authorizationScopes = ["openid"];
// ------------------------------------------------------------------------
// Add CORS support to all
// ------------------------------------------------------------------------
Utils.addCorsOptions(proxyResource, corsOrigin);
Utils.addCorsOptions(rootResource, corsOrigin);
// ========================================================================