Skip to content

Commit e9f46da

Browse files
vas1468mergify[bot]
authored andcommitted
feat(cognito): add PreTokenGeneration lambda trigger support (#3910)
* feat(cognito): add PreTokenGeneration lambda trigger support fixes #2497 * Update to adhere LambdaConfigProperty * feat(cognito): add UserMigration lambda trigger support * feat(cognito): add different instances of lambda functions on the user pool test(if api correctly appends triggers)
1 parent bace776 commit e9f46da

File tree

4 files changed

+130
-17
lines changed

4 files changed

+130
-17
lines changed

packages/@aws-cdk/aws-cognito/lib/user-pool.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,18 @@ export interface UserPoolTriggers {
188188
*/
189189
readonly preSignUp?: lambda.IFunction;
190190

191+
/**
192+
* A pre-token-generation AWS Lambda trigger.
193+
* @see https://docs.aws.amazon.com/cognito/latest/developerguide/user-pool-lambda-pre-token-generation.html
194+
*/
195+
readonly preTokenGeneration?: lambda.IFunction;
196+
197+
/**
198+
* A user-migration AWS Lambda trigger.
199+
* @see https://docs.aws.amazon.com/cognito/latest/developerguide/user-pool-lambda-migrate-user.html
200+
*/
201+
readonly userMigration?: lambda.IFunction;
202+
191203
/**
192204
* Verifies the authentication challenge response.
193205
* @see https://docs.aws.amazon.com/cognito/latest/developerguide/user-pool-lambda-verify-auth-challenge-response.html
@@ -486,6 +498,28 @@ export class UserPool extends Resource implements IUserPool {
486498
this.triggers = { ...this.triggers, preSignUp: fn.functionArn };
487499
}
488500

501+
/**
502+
* Attach 'Pre Token Generation' trigger
503+
* Grants access from cognito-idp.amazonaws.com to the lambda
504+
* @see https://docs.aws.amazon.com/cognito/latest/developerguide/user-pool-lambda-pre-token-generation.html
505+
* @param fn the lambda function to attach
506+
*/
507+
public addPreTokenGenerationTrigger(fn: lambda.IFunction): void {
508+
this.addLambdaPermission(fn, 'PreTokenGeneration');
509+
this.triggers = { ...this.triggers, preTokenGeneration: fn.functionArn };
510+
}
511+
512+
/**
513+
* Attach 'User Migration' trigger
514+
* Grants access from cognito-idp.amazonaws.com to the lambda
515+
* @see https://docs.aws.amazon.com/cognito/latest/developerguide/user-pool-lambda-migrate-user.html
516+
* @param fn the lambda function to attach
517+
*/
518+
public addUserMigrationTrigger(fn: lambda.IFunction): void {
519+
this.addLambdaPermission(fn, 'UserMigration');
520+
this.triggers = { ...this.triggers, userMigration: fn.functionArn };
521+
}
522+
489523
/**
490524
* Attach 'Verify Auth Challenge Response' trigger
491525
* Grants access from cognito-idp.amazonaws.com to the lambda

packages/@aws-cdk/aws-cognito/test/test.user-pool.ts

Lines changed: 76 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -53,34 +53,93 @@ export = {
5353
'on* API correctly appends triggers'(test: Test) {
5454
// GIVEN
5555
const stack = new cdk.Stack();
56-
const fn = new lambda.Function(stack, 'MyLambda', {
56+
57+
const createAuthChallengeLambdaFn = new lambda.Function(stack, 'createAuthChallengeLambda', {
58+
code: new lambda.InlineCode('foo'),
59+
handler: 'index.handler',
60+
runtime: lambda.Runtime.NODEJS_8_10,
61+
});
62+
63+
const customMessageLambdaFn = new lambda.Function(stack, 'customMessageLambda', {
64+
code: new lambda.InlineCode('foo'),
65+
handler: 'index.handler',
66+
runtime: lambda.Runtime.NODEJS_8_10,
67+
});
68+
69+
const defineAuthChallengeLambdaFn = new lambda.Function(stack, 'defineAuthChallengeLambda', {
70+
code: new lambda.InlineCode('foo'),
71+
handler: 'index.handler',
72+
runtime: lambda.Runtime.NODEJS_8_10,
73+
});
74+
75+
const postAuthenticationLambdaFn = new lambda.Function(stack, 'postAuthenticationLambda', {
76+
code: new lambda.InlineCode('foo'),
77+
handler: 'index.handler',
78+
runtime: lambda.Runtime.NODEJS_8_10,
79+
});
80+
81+
const postConfirmationLambdaFn = new lambda.Function(stack, 'postConfirmationLambda', {
82+
code: new lambda.InlineCode('foo'),
83+
handler: 'index.handler',
84+
runtime: lambda.Runtime.NODEJS_8_10,
85+
});
86+
87+
const preAuthenticationLambdaFn = new lambda.Function(stack, 'preAuthenticationLambda', {
88+
code: new lambda.InlineCode('foo'),
89+
handler: 'index.handler',
90+
runtime: lambda.Runtime.NODEJS_8_10,
91+
});
92+
93+
const preSignUpLambdaFn = new lambda.Function(stack, 'preSignUpLambda', {
94+
code: new lambda.InlineCode('foo'),
95+
handler: 'index.handler',
96+
runtime: lambda.Runtime.NODEJS_8_10,
97+
});
98+
99+
const preTokenGenerationLambdaFn = new lambda.Function(stack, 'preTokenGenerationLambda', {
100+
code: new lambda.InlineCode('foo'),
101+
handler: 'index.handler',
102+
runtime: lambda.Runtime.NODEJS_8_10,
103+
});
104+
105+
const userMigrationLambdaFn = new lambda.Function(stack, 'userMigrationLambda', {
106+
code: new lambda.InlineCode('foo'),
107+
handler: 'index.handler',
108+
runtime: lambda.Runtime.NODEJS_8_10,
109+
});
110+
111+
const verifyAuthChallengeResponseLambdaFn = new lambda.Function(stack, 'verifyAuthChallengeResponseLambda', {
57112
code: new lambda.InlineCode('foo'),
58113
handler: 'index.handler',
59114
runtime: lambda.Runtime.NODEJS_8_10,
60115
});
61116

62117
// WHEN
63118
const pool = new cognito.UserPool(stack, 'Pool', { });
64-
pool.addCreateAuthChallengeTrigger(fn);
65-
pool.addCustomMessageTrigger(fn);
66-
pool.addDefineAuthChallengeTrigger(fn);
67-
pool.addPostAuthenticationTrigger(fn);
68-
pool.addPostConfirmationTrigger(fn);
69-
pool.addPreAuthenticationTrigger(fn);
70-
pool.addPreSignUpTrigger(fn);
71-
pool.addVerifyAuthChallengeResponseTrigger(fn);
119+
pool.addCreateAuthChallengeTrigger(createAuthChallengeLambdaFn);
120+
pool.addCustomMessageTrigger(customMessageLambdaFn);
121+
pool.addDefineAuthChallengeTrigger(defineAuthChallengeLambdaFn);
122+
pool.addPostAuthenticationTrigger(postAuthenticationLambdaFn);
123+
pool.addPostConfirmationTrigger(postConfirmationLambdaFn);
124+
pool.addPreAuthenticationTrigger(preAuthenticationLambdaFn);
125+
pool.addPreSignUpTrigger(preSignUpLambdaFn);
126+
pool.addPreTokenGenerationTrigger(preTokenGenerationLambdaFn);
127+
pool.addUserMigrationTrigger(userMigrationLambdaFn);
128+
pool.addVerifyAuthChallengeResponseTrigger(verifyAuthChallengeResponseLambdaFn);
72129

73130
// THEN
74131
expect(stack).to(haveResourceLike('AWS::Cognito::UserPool', {
75132
LambdaConfig: {
76-
CreateAuthChallenge: stack.resolve(fn.functionArn),
77-
CustomMessage: stack.resolve(fn.functionArn),
78-
DefineAuthChallenge: stack.resolve(fn.functionArn),
79-
PostAuthentication: stack.resolve(fn.functionArn),
80-
PostConfirmation: stack.resolve(fn.functionArn),
81-
PreAuthentication: stack.resolve(fn.functionArn),
82-
PreSignUp: stack.resolve(fn.functionArn),
83-
VerifyAuthChallengeResponse: stack.resolve(fn.functionArn)
133+
CreateAuthChallenge: stack.resolve(createAuthChallengeLambdaFn.functionArn),
134+
CustomMessage: stack.resolve(customMessageLambdaFn.functionArn),
135+
DefineAuthChallenge: stack.resolve(defineAuthChallengeLambdaFn.functionArn),
136+
PostAuthentication: stack.resolve(postAuthenticationLambdaFn.functionArn),
137+
PostConfirmation: stack.resolve(postConfirmationLambdaFn.functionArn),
138+
PreAuthentication: stack.resolve(preAuthenticationLambdaFn.functionArn),
139+
PreSignUp: stack.resolve(preSignUpLambdaFn.functionArn),
140+
PreTokenGeneration: stack.resolve(preTokenGenerationLambdaFn.functionArn),
141+
UserMigration: stack.resolve(userMigrationLambdaFn.functionArn),
142+
VerifyAuthChallengeResponse: stack.resolve(verifyAuthChallengeResponseLambdaFn.functionArn)
84143
}
85144
}));
86145

tools/cfn2ts/test/expected.cdk.output

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14344,6 +14344,16 @@ export namespace aws.cognito.UserPool {
1434414344
*/
1434514345
CustomMessage?: string | core.Token;
1434614346

14347+
/**
14348+
* @link https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-cognito-userpool-lambdaconfig.html#cfn-cognito-userpool-lambdaconfig-pretokengeneration
14349+
*/
14350+
PreTokenGeneration?: string | core.Token;
14351+
14352+
/**
14353+
* @link https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-cognito-userpool-lambdaconfig.html#cfn-cognito-userpool-lambdaconfig-usermigration
14354+
*/
14355+
UserMigration?: string | core.Token;
14356+
1434714357
/**
1434814358
* @link http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-cognito-userpool-lambdaconfig.html#cfn-cognito-userpool-lambdaconfig-verifyauthchallengeresponse
1434914359
*/

tools/cfn2ts/test/expected.cfnobjects.output

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15092,6 +15092,16 @@ export namespace aws.cognito.UserPool {
1509215092
*/
1509315093
CustomMessage?: string | core.Token;
1509415094

15095+
/**
15096+
* @link https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-cognito-userpool-lambdaconfig.html#cfn-cognito-userpool-lambdaconfig-pretokengeneration
15097+
*/
15098+
PreTokenGeneration?: string | core.Token;
15099+
15100+
/**
15101+
* @link https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-cognito-userpool-lambdaconfig.html#cfn-cognito-userpool-lambdaconfig-usermigration
15102+
*/
15103+
UserMigration?: string | core.Token;
15104+
1509515105
/**
1509615106
* @link http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-cognito-userpool-lambdaconfig.html#cfn-cognito-userpool-lambdaconfig-verifyauthchallengeresponse
1509715107
*/

0 commit comments

Comments
 (0)