Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(apigateway): authorizer is not attached to RestApi across projects #7596

Merged
merged 7 commits into from
Apr 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion packages/@aws-cdk/aws-apigateway/lib/authorizer.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,29 @@
import { Resource } from '@aws-cdk/core';
import { Construct, Resource, ResourceProps } from '@aws-cdk/core';
import { AuthorizationType } from './method';
import { RestApi } from './restapi';

const AUTHORIZER_SYMBOL = Symbol.for('@aws-cdk/aws-apigateway.Authorizer');

/**
* Base class for all custom authorizers
*/
export abstract class Authorizer extends Resource implements IAuthorizer {
/**
* Return whether the given object is an Authorizer.
*/
public static isAuthorizer(x: any): x is Authorizer {
return x !== null && typeof(x) === 'object' && AUTHORIZER_SYMBOL in x;
}

public readonly abstract authorizerId: string;
public readonly authorizationType?: AuthorizationType = AuthorizationType.CUSTOM;

public constructor(scope: Construct, id: string, props?: ResourceProps) {
super(scope, id, props);

Object.defineProperty(this, AUTHORIZER_SYMBOL, { value: true });
nija-at marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* Called when the authorizer is used from a specific REST API.
* @internal
Expand Down
2 changes: 1 addition & 1 deletion packages/@aws-cdk/aws-apigateway/lib/method.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ export class Method extends Resource {
`which is different from what is required by the authorizer [${authorizer.authorizationType}]`);
}

if (authorizer instanceof Authorizer) {
if (Authorizer.isAuthorizer(authorizer)) {
authorizer._attachToApi(this.restApi);
}

Expand Down
21 changes: 21 additions & 0 deletions packages/@aws-cdk/aws-apigateway/test/test.authorizer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Stack } from '@aws-cdk/core';
import { Test } from 'nodeunit';
import { Authorizer, RestApi } from '../lib';

export = {
'isAuthorizer correctly detects an instance of type Authorizer'(test: Test) {
class MyAuthorizer extends Authorizer {
public readonly authorizerId = 'test-authorizer-id';
public _attachToApi(_: RestApi): void {
// do nothing
}
}
const stack = new Stack();
const authorizer = new MyAuthorizer(stack, 'authorizer');

test.ok(Authorizer.isAuthorizer(authorizer), 'type Authorizer expected but is not');
test.ok(!Authorizer.isAuthorizer(stack), 'type Authorizer found, when not expected');

test.done();
},
};