@@ -185,22 +185,35 @@ export class Method extends Resource {
185
185
validateHttpMethod ( this . httpMethod ) ;
186
186
187
187
const options = props . options || { } ;
188
-
189
188
const defaultMethodOptions = props . resource . defaultMethodOptions || { } ;
189
+
190
190
// do not use the default authorizer config in case if the provided authorizer type is None
191
191
const authorizer =
192
192
options . authorizationType === AuthorizationType . NONE
193
193
&& options . authorizer == undefined ? undefined : options . authorizer || defaultMethodOptions . authorizer ;
194
194
const authorizerId = authorizer ?. authorizerId ? authorizer . authorizerId : undefined ;
195
195
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 ) ;
204
217
205
218
// AuthorizationScope should only be applied to COGNITO_USER_POOLS AuthorizationType.
206
219
const defaultScopes = options . authorizationScopes ?? defaultMethodOptions . authorizationScopes ;
@@ -303,6 +316,27 @@ export class Method extends Resource {
303
316
this . methodResponses . push ( methodResponse ) ;
304
317
}
305
318
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
+
306
340
private renderIntegration ( bindResult : IntegrationConfig ) : CfnMethod . IntegrationProperty {
307
341
const options = bindResult . options ?? { } ;
308
342
let credentials ;
0 commit comments