Skip to content

Commit 439feaf

Browse files
authored
chore(apigateway): enhance code arounds method authorizers (#30978)
### Issue # (if applicable) ### Reason for this change Add comments and enhance the code around method authorizers. ### Description of changes No behavioural change, should only include comments, variables name update and re-ordering. ### Description of how you validated changes All existing tests pass. ### Checklist - [ ] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md) ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent d60aaa0 commit 439feaf

File tree

1 file changed

+43
-9
lines changed
  • packages/aws-cdk-lib/aws-apigateway/lib

1 file changed

+43
-9
lines changed

packages/aws-cdk-lib/aws-apigateway/lib/method.ts

+43-9
Original file line numberDiff line numberDiff line change
@@ -185,22 +185,35 @@ export class Method extends Resource {
185185
validateHttpMethod(this.httpMethod);
186186

187187
const options = props.options || {};
188-
189188
const defaultMethodOptions = props.resource.defaultMethodOptions || {};
189+
190190
// do not use the default authorizer config in case if the provided authorizer type is None
191191
const authorizer =
192192
options.authorizationType === AuthorizationType.NONE
193193
&& options.authorizer == undefined ? undefined : options.authorizer || defaultMethodOptions.authorizer;
194194
const authorizerId = authorizer?.authorizerId ? authorizer.authorizerId : undefined;
195195

196-
const authorizationTypeOption = options.authorizationType || defaultMethodOptions.authorizationType;
197-
const authorizationType = authorizer?.authorizationType || authorizationTypeOption || AuthorizationType.NONE;
198-
199-
// if the authorizer defines an authorization type and we also have an explicit option set, check that they are the same
200-
if (authorizer?.authorizationType && authorizationTypeOption && authorizer?.authorizationType !== authorizationTypeOption) {
201-
throw new Error(`${this.resource}/${this.httpMethod} - Authorization type is set to ${authorizationTypeOption} ` +
202-
`which is different from what is required by the authorizer [${authorizer.authorizationType}]`);
203-
}
196+
/**
197+
* Get and validate authorization type from the values set by API resource and method.
198+
*
199+
* REST API Resource
200+
* └── defaultMethodOptions: Method options to use as a default for all methods created within this API unless custom options are specified.
201+
* ├── authorizationType: Specifies the default authorization type unless custom options are specified, recommended to not be specified.
202+
* └── authorizer: Specifies the default authorizer for all methods created within this API unless custom options are specified.
203+
* └── authorizerType: The default authorization type of this authorizer.
204+
*
205+
* REST API Method
206+
* └── options: Method options.
207+
* ├── authorizationType: Specifies the authorization type, recommended to not be specified.
208+
* └── authorizer: Specifies an authorizer to use for this method.
209+
* └── authorizerType: The authorization type of this authorizer.
210+
*
211+
* Authorization type is first set to "authorizer.authorizerType", falling back to method's "authorizationType",
212+
* falling back to API resource's default "authorizationType", and lastly "Authorizer.NONE".
213+
*
214+
* Note that "authorizer.authorizerType" should match method or resource's "authorizationType" if exists.
215+
*/
216+
const authorizationType = this.getMethodAuthorizationType(options, defaultMethodOptions, authorizer);
204217

205218
// AuthorizationScope should only be applied to COGNITO_USER_POOLS AuthorizationType.
206219
const defaultScopes = options.authorizationScopes ?? defaultMethodOptions.authorizationScopes;
@@ -303,6 +316,27 @@ export class Method extends Resource {
303316
this.methodResponses.push(methodResponse);
304317
}
305318

319+
/**
320+
* Get API Gateway Method's authorization type
321+
* @param options API Gateway Method's options to use
322+
* @param defaultMethodOptions API Gateway resource's default Method's options to use
323+
* @param authorizer Authorizer used for API Gateway Method
324+
* @returns API Gateway Method's authorizer type
325+
*/
326+
private getMethodAuthorizationType(options: MethodOptions, defaultMethodOptions: MethodOptions, authorizer?: IAuthorizer): string {
327+
const authorizerAuthType = authorizer?.authorizationType;
328+
const optionsAuthType = options.authorizationType || defaultMethodOptions.authorizationType;
329+
const finalAuthType = authorizerAuthType || optionsAuthType || AuthorizationType.NONE;
330+
331+
// if the authorizer defines an authorization type and we also have an explicit option set, check that they are the same
332+
if (authorizerAuthType && optionsAuthType && authorizerAuthType !== optionsAuthType) {
333+
throw new Error(`${this.resource}/${this.httpMethod} - Authorization type is set to ${optionsAuthType} ` +
334+
`which is different from what is required by the authorizer [${authorizerAuthType}]`);
335+
}
336+
337+
return finalAuthType;
338+
}
339+
306340
private renderIntegration(bindResult: IntegrationConfig): CfnMethod.IntegrationProperty {
307341
const options = bindResult.options ?? {};
308342
let credentials;

0 commit comments

Comments
 (0)