|
1 | 1 | import { Construct } from 'constructs'; |
2 | 2 | import * as apiGateway from 'aws-cdk-lib/aws-apigateway'; |
3 | | -import { RestApiConstructProps } from './types.js'; |
| 3 | +import { IUserPool } from 'aws-cdk-lib/aws-cognito'; |
| 4 | +import { MethodsProps, RestApiConstructProps } from './types.js'; |
4 | 5 | import { validateRestApiPaths } from './validate_paths.js'; |
5 | 6 |
|
6 | 7 | /** |
7 | | - * Rest API construct for Amplify Backend |
| 8 | + * |
8 | 9 | */ |
9 | 10 | export class RestApiConstruct extends Construct { |
10 | 11 | public readonly api: apiGateway.RestApi; |
| 12 | + private readonly userPoolAuthorizer?: apiGateway.CognitoUserPoolsAuthorizer; |
| 13 | + |
11 | 14 | /** |
12 | | - * Create a new RestApiConstruct |
| 15 | + * Creates a new REST API in API Gateway with optional Cognito User Pool authorization. |
| 16 | + * |
| 17 | + * If a user pool is provided, all routes default to using it for authentication unless overridden. |
| 18 | + * @param scope - The scope in which this construct is defined. |
| 19 | + * @param id - The unique identifier for this construct. |
| 20 | + * @param props - Configuration options for the REST API, including name and route definitions. |
| 21 | + * @param userPool - Optional Cognito User Pool to use as the default authorizer. |
13 | 22 | */ |
14 | | - constructor(scope: Construct, id: string, props: RestApiConstructProps) { |
| 23 | + constructor( |
| 24 | + scope: Construct, |
| 25 | + id: string, |
| 26 | + props: RestApiConstructProps, |
| 27 | + userPool?: IUserPool, |
| 28 | + ) { |
15 | 29 | super(scope, id); |
16 | 30 |
|
17 | 31 | //check that the paths are valid before creating the API |
18 | 32 | const paths: string[] = []; |
19 | 33 | props.apiProps.forEach((value) => paths.push(value.path)); |
20 | 34 | validateRestApiPaths(paths); |
21 | 35 |
|
22 | | - // Create a new API Gateway REST API with the specified name |
| 36 | + // Create API |
23 | 37 | this.api = new apiGateway.RestApi(this, 'RestApi', { |
24 | 38 | restApiName: props.apiName, |
25 | 39 | }); |
26 | 40 |
|
27 | | - // Iterate over each path configuration |
| 41 | + // If userPool exists, create default authorizer |
| 42 | + if (userPool) { |
| 43 | + this.userPoolAuthorizer = new apiGateway.CognitoUserPoolsAuthorizer( |
| 44 | + this, |
| 45 | + 'DefaultUserPoolAuth', |
| 46 | + { |
| 47 | + cognitoUserPools: [userPool], |
| 48 | + }, |
| 49 | + ); |
| 50 | + } |
| 51 | + |
28 | 52 | for (const pathConfig of props.apiProps) { |
29 | 53 | const { path, methods, lambdaEntry } = pathConfig; |
30 | 54 | // Add resource and methods for this route |
31 | 55 | const resource = this.addNestedResource(this.api.root, path); |
| 56 | + |
32 | 57 | for (const method of methods) { |
33 | | - resource.addMethod( |
34 | | - method.method, |
35 | | - new apiGateway.LambdaIntegration(lambdaEntry.resources.lambda), |
| 58 | + const integration = new apiGateway.LambdaIntegration( |
| 59 | + lambdaEntry.resources.lambda, |
36 | 60 | ); |
| 61 | + |
| 62 | + resource.addMethod(method.method, integration, { |
| 63 | + authorizer: this.getAuthorizerForMethod(method), |
| 64 | + authorizationType: this.getAuthorizationType(method), |
| 65 | + }); |
37 | 66 | } |
38 | 67 | } |
39 | 68 | } |
40 | 69 |
|
| 70 | + /** |
| 71 | + * |
| 72 | + * If the method specifies a user pool authorizer, it returns the default user pool authorizer. |
| 73 | + * If no authorizer is specified but a user pool exists, it returns the default user pool authorizer. |
| 74 | + * Otherwise, it returns undefined. |
| 75 | + */ |
| 76 | + |
| 77 | + private getAuthorizerForMethod(method: MethodsProps) { |
| 78 | + if (method.authorizer?.type === 'userPool' && this.userPoolAuthorizer) { |
| 79 | + return this.userPoolAuthorizer; |
| 80 | + } |
| 81 | + if (!method.authorizer && this.userPoolAuthorizer) { |
| 82 | + return this.userPoolAuthorizer; |
| 83 | + } |
| 84 | + return undefined; |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Determines the authorization type based on the method's authorizer configuration. |
| 89 | + * If a user pool authorizer is set or if no authorizer is specified but a user pool exists, it returns COGNITO. |
| 90 | + * Otherwise, it returns NONE. |
| 91 | + */ |
| 92 | + |
| 93 | + private getAuthorizationType(method: MethodsProps) { |
| 94 | + if ( |
| 95 | + method.authorizer?.type === 'userPool' || |
| 96 | + (!method.authorizer && this.userPoolAuthorizer) |
| 97 | + ) { |
| 98 | + return apiGateway.AuthorizationType.COGNITO; |
| 99 | + } |
| 100 | + return apiGateway.AuthorizationType.NONE; |
| 101 | + } |
| 102 | + |
41 | 103 | /** |
42 | 104 | * Adds nested resources to the API based on the provided path. |
43 | 105 | */ |
|
0 commit comments