Created
July 21, 2022 14:45
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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