Skip to content

Commit f2396ff

Browse files
authored
fix(auth): Cognito additional auth (#553)
1 parent ded4339 commit f2396ff

File tree

3 files changed

+29
-10
lines changed

3 files changed

+29
-10
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ jobs:
1515
runs-on: ubuntu-latest
1616
strategy:
1717
matrix:
18-
node: [16]
18+
node: [16, 18]
1919
steps:
2020
- uses: actions/setup-node@v3
2121
with:

src/__tests__/api.test.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,14 @@ describe('Api', () => {
7474
it('should compile the Api Resource with additional auths', () => {
7575
const api = new Api(
7676
given.appSyncConfig({
77+
authentication: {
78+
type: 'AMAZON_COGNITO_USER_POOLS',
79+
config: {
80+
userPoolId: 'pool123',
81+
awsRegion: 'us-east-1',
82+
appIdClientRegex: '[a-z]',
83+
},
84+
},
7785
additionalAuthentications: [
7886
{
7987
type: 'AMAZON_COGNITO_USER_POOLS',
@@ -117,7 +125,6 @@ describe('Api', () => {
117125
"UserPoolConfig": Object {
118126
"AppIdClientRegex": "[a-z]",
119127
"AwsRegion": "us-east-1",
120-
"DefaultAction": "ALLOW",
121128
"UserPoolId": "pool123",
122129
},
123130
},
@@ -147,14 +154,20 @@ describe('Api', () => {
147154
},
148155
},
149156
],
150-
"AuthenticationType": "API_KEY",
157+
"AuthenticationType": "AMAZON_COGNITO_USER_POOLS",
151158
"Name": "MyApi",
152159
"Tags": Array [
153160
Object {
154161
"Key": "stage",
155162
"Value": "Dev",
156163
},
157164
],
165+
"UserPoolConfig": Object {
166+
"AppIdClientRegex": "[a-z]",
167+
"AwsRegion": "us-east-1",
168+
"DefaultAction": "ALLOW",
169+
"UserPoolId": "pool123",
170+
},
158171
"XrayEnabled": false,
159172
},
160173
"Type": "AWS::AppSync::GraphQLApi",

src/resources/Api.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ export class Api {
8888
merge(endpointResource.Properties, {
8989
AdditionalAuthenticationProviders:
9090
this.config.additionalAuthentications?.map((provider) =>
91-
this.compileAuthenticationProvider(provider),
91+
this.compileAuthenticationProvider(provider, true),
9292
),
9393
});
9494
}
@@ -411,14 +411,18 @@ export class Api {
411411
};
412412
}
413413

414-
getUserPoolConfig(auth: CognitoAuth) {
414+
getUserPoolConfig(auth: CognitoAuth, isAdditionalAuth = false) {
415415
const userPoolConfig = {
416416
AwsRegion: auth.config.awsRegion || { 'Fn::Sub': '${AWS::Region}' },
417417
UserPoolId: auth.config.userPoolId,
418418
AppIdClientRegex: auth.config.appIdClientRegex,
419-
// Default action is the one passed in the config
420-
// or 'ALLOW'
421-
DefaultAction: auth.config.defaultAction || 'ALLOW',
419+
...(!isAdditionalAuth
420+
? {
421+
// Default action is the one passed in the config
422+
// or 'ALLOW'
423+
DefaultAction: auth.config.defaultAction || 'ALLOW',
424+
}
425+
: {}),
422426
};
423427

424428
return userPoolConfig;
@@ -468,14 +472,16 @@ export class Api {
468472
}));
469473
}
470474

471-
compileAuthenticationProvider(provider: Auth) {
475+
compileAuthenticationProvider(provider: Auth, isAdditionalAuth = false) {
472476
const { type } = provider;
473477
const authPrivider = {
474478
AuthenticationType: type,
475479
};
476480

477481
if (type === 'AMAZON_COGNITO_USER_POOLS') {
478-
merge(authPrivider, { UserPoolConfig: this.getUserPoolConfig(provider) });
482+
merge(authPrivider, {
483+
UserPoolConfig: this.getUserPoolConfig(provider, isAdditionalAuth),
484+
});
479485
} else if (type === 'OPENID_CONNECT') {
480486
merge(authPrivider, {
481487
OpenIDConnectConfig: this.getOpenIDConnectConfig(provider),

0 commit comments

Comments
 (0)