forked from open-constructs/cdk-serverless
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.ts
184 lines (160 loc) · 5.02 KB
/
auth.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import * as fs from 'fs';
import * as cognito from '@aws-cdk/aws-cognito';
import * as cdk from '@aws-cdk/core';
import { LambdaFunction } from './func';
export interface IAuthentication {
readonly userpool: cognito.IUserPool;
}
export interface AuthenticationProps {
/**
* Configure Cognito Lambda triggers
*/
triggers?: {
/**
* Attaches a lambda function to the custom message trigger
*
* Code has to reside in './src/lambda/cognito.custom-message.ts' with a method 'handler'
*
* @default false
*/
customMessages?: boolean;
/**
* Attaches a lambda function to the pre token generation trigger
*
* Code has to reside in './src/lambda/cognito.pre-token-generation.ts' with a method 'handler'
*
* @default false
*/
preTokenGeneration?: boolean;
};
/**
* Properties of the Cognito user pool
*/
userPoolProps?: cognito.UserPoolProps;
/**
* Configure SES mail sending
*/
sesEmailSender?: {
/**
* AWS region to use for SES
*/
region: string;
/**
* Sender name
*/
name: string;
/**
* Sender email. Needs to be verified in SES
*/
email: string;
};
/**
* Groups to create in Cognito user pool
* Key: group name
* Value: group description
*/
groups?: {
[name: string]: string;
};
}
export class Authentication extends cdk.Construct implements IAuthentication {
public readonly userpool: cognito.UserPool;
public readonly customMessageFunction?: LambdaFunction;
public readonly preTokenGenerationFunction?: LambdaFunction;
constructor(scope: cdk.Construct, id: string, props: AuthenticationProps) {
super(scope, id);
this.userpool = new cognito.UserPool(this, 'UserPool', {
autoVerify: { email: true },
passwordPolicy: {
minLength: 12,
requireDigits: false,
requireLowercase: false,
requireSymbols: false,
requireUppercase: false,
},
standardAttributes: {
fullname: {
mutable: true,
required: true,
},
email: {
mutable: false,
required: true,
},
},
selfSignUpEnabled: false,
signInAliases: {
email: true,
},
signInCaseSensitive: false,
...props.userPoolProps,
});
if (props.triggers?.customMessages) {
const entryFile = './src/lambda/cognito.custom-message.ts';
if (!fs.existsSync(entryFile)) {
fs.writeFileSync(entryFile, `export async function handler(event: AWSLambda.CustomMessageTriggerEvent): Promise<AWSLambda.CustomMessageTriggerEvent> {
console.log(event);
if (event.triggerSource === 'CustomMessage_AdminCreateUser') {
// event.response.emailSubject = '';
// event.response.emailMessage = '';
} else if (event.triggerSource === 'CustomMessage_ForgotPassword') {
// event.response.emailSubject = '';
// event.response.emailMessage = '';
} // ...
return event;
}`, {
encoding: 'utf-8',
});
}
this.customMessageFunction = new LambdaFunction(this, 'CustomMessageFunction', {
entry: entryFile,
lambdaOptions: {
bundling: {
loader: {
'.html': 'text',
},
},
timeout: cdk.Duration.seconds(5),
},
});
this.userpool.addTrigger(cognito.UserPoolOperation.CUSTOM_MESSAGE, this.customMessageFunction);
}
if (props.triggers?.preTokenGeneration) {
const entryFile = './src/lambda/cognito.pre-token-generation.ts';
if (!fs.existsSync(entryFile)) {
fs.writeFileSync(entryFile, `export async function handler(event: AWSLambda.PreTokenGenerationTriggerEvent): Promise<AWSLambda.PreTokenGenerationTriggerEvent> {
console.log(JSON.stringify(event));
// modify event.response here ...
return event;
}`, {
encoding: 'utf-8',
});
}
this.preTokenGenerationFunction = new LambdaFunction(this, 'PreTokenGenerationFunction', {
lambdaOptions: {
timeout: cdk.Duration.seconds(5),
},
entry: entryFile,
});
this.userpool.addTrigger(cognito.UserPoolOperation.PRE_TOKEN_GENERATION, this.preTokenGenerationFunction);
}
if (props.sesEmailSender) {
(this.userpool.node.defaultChild as cognito.CfnUserPool).emailConfiguration = {
emailSendingAccount: 'DEVELOPER',
from: `${props.sesEmailSender.name} <${props.sesEmailSender.email}>`,
sourceArn: `arn:aws:ses:${props.sesEmailSender.region}:${cdk.Stack.of(this).account}:identity/${props.sesEmailSender.email}`,
};
}
for (const groupName in props.groups) {
if (Object.prototype.hasOwnProperty.call(props.groups, groupName)) {
const groupDescription = props.groups[groupName];
new cognito.CfnUserPoolGroup(this, `Group${groupName}`, {
userPoolId: this.userpool.userPoolId,
groupName,
description: groupDescription,
});
}
}
new cdk.CfnOutput(this, 'UserPoolId', { value: this.userpool.userPoolId });
}
}