Skip to content

Instantly share code, notes, and snippets.

@leegilmorecode
Created July 21, 2022 14:45
Show Gist options
  • Select an option

  • Save leegilmorecode/8ad913c7671f89455a0eff89bc7c7b3c to your computer and use it in GitHub Desktop.

Select an option

Save leegilmorecode/8ad913c7671f89455a0eff89bc7c7b3c to your computer and use it in GitHub Desktop.
Example of a Cognito Authorizer being added to an endpoint on API Gateway
// add the cognito authorizer to our api which validates our tokens using cognito
const cognitoAuthorizer: apigw.CfnAuthorizer = new apigw.CfnAuthorizer(
this,
"APIGatewayAuthorizer",
{
name: "sushi-orders-authorizer",
identitySource: "method.request.header.Authorization",
providerArns: [userPool.userPoolArn],
restApiId: ordersAPI.restApiId,
type: apigw.AuthorizationType.COGNITO,
}
);
// add the endpoint for getting all orders
orders.addMethod(
"GET",
new apigw.LambdaIntegration(getOrders, {
proxy: true,
allowTestInvoke: true,
}),
{
authorizationType: apigw.AuthorizationType.COGNITO,
apiKeyRequired: true, // ensure that the consumer needs to send the api key
authorizer: { authorizerId: cognitoAuthorizer.ref }, // the cognito authoriser will ensure we have a token
authorizationScopes: [`orders/${getOrdersScope.scopeName}`], // ensure the token has the correct scope
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment