Skip to content

Commit acc2cd0

Browse files
authored
Merge pull request #207 from horike37/feature/Oauth_scope
feat: add support for AuthorizationScopes
2 parents f9c6cd0 + ba59a1e commit acc2cd0

File tree

3 files changed

+55
-17
lines changed

3 files changed

+55
-17
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,10 @@ stepFunctions:
497497
type: COGNITO_USER_POOLS # TOKEN, CUSTOM or COGNITO_USER_POOLS, same as AWS Cloudformation documentation
498498
authorizerId:
499499
Ref: ApiGatewayAuthorizer # or hard-code Authorizer ID
500+
# [Optional] you can also specify the OAuth scopes for Cognito
501+
scopes:
502+
- scope1
503+
...
500504
```
501505

502506
#### LAMBDA_PROXY request template

lib/deploy/events/apiGateway/methods.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,7 @@ module.exports = {
328328
Properties: {
329329
AuthorizationType: http.authorizer.type,
330330
AuthorizerId: http.authorizer.authorizerId,
331+
AuthorizationScopes: http.authorizer.scopes,
331332
},
332333
};
333334
}
@@ -336,10 +337,12 @@ module.exports = {
336337
.getAuthorizerLogicalId(http.authorizer.name || http.authorizer);
337338

338339
let authorizationType;
340+
let authorizationScopes;
339341
const authorizerArn = http.authorizer.arn;
340342
if (typeof authorizerArn === 'string'
341343
&& awsArnRegExs.cognitoIdpArnExpr.test(authorizerArn)) {
342344
authorizationType = 'COGNITO_USER_POOLS';
345+
authorizationScopes = http.authorizer.scopes;
343346
} else {
344347
authorizationType = 'CUSTOM';
345348
}
@@ -348,6 +351,7 @@ module.exports = {
348351
Properties: {
349352
AuthorizationType: authorizationType,
350353
AuthorizerId: { Ref: authorizerLogicalId },
354+
AuthorizationScopes: authorizationScopes,
351355
},
352356
DependsOn: authorizerLogicalId,
353357
};

lib/deploy/events/apiGateway/methods.test.js

Lines changed: 47 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -288,8 +288,10 @@ describe('#methods()', () => {
288288
method: 'post',
289289
};
290290

291-
expect(serverlessStepFunctions.getMethodAuthorization(event)
292-
.Properties.AuthorizationType).to.equal('NONE');
291+
const authorization = serverlessStepFunctions.getMethodAuthorization(event);
292+
293+
expect(authorization.Properties.AuthorizationType).to.equal('NONE');
294+
expect(authorization.Properties.AuthorizationScopes).to.equal(undefined);
293295
});
294296

295297
it('should return resource properties with AuthorizationType: AWS_IAM', () => {
@@ -300,8 +302,10 @@ describe('#methods()', () => {
300302
},
301303
};
302304

303-
expect(serverlessStepFunctions.getMethodAuthorization(event)
304-
.Properties.AuthorizationType).to.equal('AWS_IAM');
305+
const authorization = serverlessStepFunctions.getMethodAuthorization(event);
306+
307+
expect(authorization.Properties.AuthorizationType).to.equal('AWS_IAM');
308+
expect(authorization.Properties.AuthorizationScopes).to.equal(undefined);
305309
});
306310

307311
it('should return properties with AuthorizationType: CUSTOM and authotizerId', () => {
@@ -312,10 +316,11 @@ describe('#methods()', () => {
312316
},
313317
};
314318

315-
expect(serverlessStepFunctions.getMethodAuthorization(event)
316-
.Properties.AuthorizationType).to.equal('CUSTOM');
317-
expect(serverlessStepFunctions.getMethodAuthorization(event)
318-
.Properties.AuthorizerId).to.equal('foo12345');
319+
const authorization = serverlessStepFunctions.getMethodAuthorization(event);
320+
321+
expect(authorization.Properties.AuthorizationType).to.equal('CUSTOM');
322+
expect(authorization.Properties.AuthorizerId).to.equal('foo12345');
323+
expect(authorization.Properties.AuthorizationScopes).to.equal(undefined);
319324
});
320325

321326
it('should return properties with AuthorizationType: CUSTOM and resource reference', () => {
@@ -328,11 +333,9 @@ describe('#methods()', () => {
328333
},
329334
};
330335

331-
const autorization = serverlessStepFunctions.getMethodAuthorization(event);
332-
expect(autorization.Properties.AuthorizationType)
333-
.to.equal('CUSTOM');
334-
335-
expect(autorization.Properties.AuthorizerId)
336+
const authorization = serverlessStepFunctions.getMethodAuthorization(event);
337+
expect(authorization.Properties.AuthorizationType).to.equal('CUSTOM');
338+
expect(authorization.Properties.AuthorizerId)
336339
.to.deep.equal({ Ref: 'AuthorizerApiGatewayAuthorizer' });
337340
});
338341

@@ -341,14 +344,41 @@ describe('#methods()', () => {
341344
authorizer: {
342345
name: 'authorizer',
343346
arn: 'arn:aws:cognito-idp:us-east-1:xxx:userpool/us-east-1_ZZZ',
347+
scopes: [
348+
'scope1',
349+
'scope2',
350+
],
344351
},
345352
};
346353

347-
const autorization = serverlessStepFunctions.getMethodAuthorization(event);
348-
expect(autorization.Properties.AuthorizationType)
349-
.to.equal('COGNITO_USER_POOLS');
350-
expect(autorization.Properties.AuthorizerId)
354+
const authorization = serverlessStepFunctions.getMethodAuthorization(event);
355+
expect(authorization.Properties.AuthorizationType).to.equal('COGNITO_USER_POOLS');
356+
expect(authorization.Properties.AuthorizerId)
351357
.to.deep.equal({ Ref: 'AuthorizerApiGatewayAuthorizer' });
358+
expect(authorization.Properties.AuthorizationScopes)
359+
.to.deep.equal(['scope1', 'scope2']);
360+
});
361+
362+
it('should return properties with AuthorizationType when type is "COGNITO_USER_POOLS"', () => {
363+
const event = {
364+
authorizer: {
365+
type: 'COGNITO_USER_POOLS',
366+
authorizerId: {
367+
Ref: 'ApiGatewayAuthorizer',
368+
},
369+
scopes: [
370+
'scope1',
371+
'scope2',
372+
],
373+
},
374+
};
375+
376+
const authorization = serverlessStepFunctions.getMethodAuthorization(event);
377+
expect(authorization.Properties.AuthorizationType).to.equal('COGNITO_USER_POOLS');
378+
expect(authorization.Properties.AuthorizerId)
379+
.to.deep.equal({ Ref: 'ApiGatewayAuthorizer' });
380+
expect(authorization.Properties.AuthorizationScopes)
381+
.to.deep.equal(['scope1', 'scope2']);
352382
});
353383
});
354384
});

0 commit comments

Comments
 (0)