Skip to content

Commit 2661129

Browse files
tmokmssTikiTDO
authored andcommitted
feat(apigatewayv2-authorizers): http api - allow multiple user pool clients per HttpUserPoolAuthorizer (aws#16903)
closes aws#15431 BREAKING CHANGE: `userPoolClient` property in `UserPoolAuthorizerProps` is now renamed to `userPoolClients`. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent ccbd9c8 commit 2661129

File tree

4 files changed

+49
-9
lines changed

4 files changed

+49
-9
lines changed

Diff for: packages/@aws-cdk/aws-apigatewayv2-authorizers/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ const userPoolClient = userPool.addClient('UserPoolClient');
150150

151151
const authorizer = new HttpUserPoolAuthorizer({
152152
userPool,
153-
userPoolClient,
153+
userPoolClients: [userPoolClient],
154154
});
155155

156156
const api = new HttpApi(stack, 'HttpApi');

Diff for: packages/@aws-cdk/aws-apigatewayv2-authorizers/lib/http/user-pool.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ import { Stack, Token } from '@aws-cdk/core';
77
*/
88
export interface UserPoolAuthorizerProps {
99
/**
10-
* The user pool client that should be used to authorize requests with the user pool.
10+
* The user pool clients that should be used to authorize requests with the user pool.
1111
*/
12-
readonly userPoolClient: IUserPoolClient;
12+
readonly userPoolClients: IUserPoolClient[];
1313

1414
/**
1515
* The associated user pool
@@ -33,7 +33,7 @@ export interface UserPoolAuthorizerProps {
3333
*
3434
* @default ['$request.header.Authorization']
3535
*/
36-
readonly identitySource?: string[],
36+
readonly identitySource?: string[];
3737
}
3838

3939
/**
@@ -56,7 +56,7 @@ export class HttpUserPoolAuthorizer implements IHttpRouteAuthorizer {
5656
identitySource: this.props.identitySource ?? ['$request.header.Authorization'],
5757
type: HttpAuthorizerType.JWT,
5858
authorizerName: this.props.authorizerName,
59-
jwtAudience: [this.props.userPoolClient.userPoolClientId],
59+
jwtAudience: this.props.userPoolClients.map((c) => c.userPoolClientId),
6060
jwtIssuer: `https://cognito-idp.${region}.amazonaws.com/${this.props.userPool.userPoolId}`,
6161
});
6262
}
@@ -66,4 +66,4 @@ export class HttpUserPoolAuthorizer implements IHttpRouteAuthorizer {
6666
authorizationType: 'JWT',
6767
};
6868
}
69-
}
69+
}

Diff for: packages/@aws-cdk/aws-apigatewayv2-authorizers/test/http/integ.user-pool.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const userPoolClient = userPool.addClient('my-client');
2525

2626
const authorizer = new HttpUserPoolAuthorizer({
2727
userPool,
28-
userPoolClient,
28+
userPoolClients: [userPoolClient],
2929
});
3030

3131
const handler = new lambda.Function(stack, 'lambda', {

Diff for: packages/@aws-cdk/aws-apigatewayv2-authorizers/test/http/user-pool.test.ts

+42-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ describe('HttpUserPoolAuthorizer', () => {
1313
const userPoolClient = userPool.addClient('UserPoolClient');
1414
const authorizer = new HttpUserPoolAuthorizer({
1515
userPool,
16-
userPoolClient,
16+
userPoolClients: [userPoolClient],
1717
});
1818

1919
// WHEN
@@ -52,7 +52,7 @@ describe('HttpUserPoolAuthorizer', () => {
5252
const userPoolClient = userPool.addClient('UserPoolClient');
5353
const authorizer = new HttpUserPoolAuthorizer({
5454
userPool,
55-
userPoolClient,
55+
userPoolClients: [userPoolClient],
5656
});
5757

5858
// WHEN
@@ -70,6 +70,46 @@ describe('HttpUserPoolAuthorizer', () => {
7070
// THEN
7171
Template.fromStack(stack).resourceCountIs('AWS::ApiGatewayV2::Authorizer', 1);
7272
});
73+
74+
test('multiple userPoolClients are attached', () => {
75+
// GIVEN
76+
const stack = new Stack();
77+
const api = new HttpApi(stack, 'HttpApi');
78+
const userPool = new UserPool(stack, 'UserPool');
79+
const userPoolClient1 = userPool.addClient('UserPoolClient1');
80+
const userPoolClient2 = userPool.addClient('UserPoolClient2');
81+
const authorizer = new HttpUserPoolAuthorizer({
82+
userPool,
83+
userPoolClients: [userPoolClient1, userPoolClient2],
84+
});
85+
86+
// WHEN
87+
api.addRoutes({
88+
integration: new DummyRouteIntegration(),
89+
path: '/books',
90+
authorizer,
91+
});
92+
93+
// THEN
94+
Template.fromStack(stack).hasResourceProperties('AWS::ApiGatewayV2::Authorizer', {
95+
AuthorizerType: 'JWT',
96+
IdentitySource: ['$request.header.Authorization'],
97+
JwtConfiguration: {
98+
Audience: [stack.resolve(userPoolClient1.userPoolClientId), stack.resolve(userPoolClient2.userPoolClientId)],
99+
Issuer: {
100+
'Fn::Join': [
101+
'',
102+
[
103+
'https://cognito-idp.',
104+
{ Ref: 'AWS::Region' },
105+
'.amazonaws.com/',
106+
stack.resolve(userPool.userPoolId),
107+
],
108+
],
109+
},
110+
},
111+
});
112+
});
73113
});
74114

75115
class DummyRouteIntegration implements IHttpRouteIntegration {

0 commit comments

Comments
 (0)