forked from aws/aws-cdk
/
user-pool-email.ts
215 lines (189 loc) · 5.82 KB
/
user-pool-email.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import { Stack, Token } from '@aws-cdk/core';
import { Construct } from 'constructs';
import { toASCII as punycodeEncode } from 'punycode/';
/**
* Configuration for Cognito sending emails via Amazon SES
*/
export interface UserPoolSESOptions {
/**
* The verified Amazon SES email address that Cognito should
* use to send emails.
*
* The email address used must be a verified email address
* in Amazon SES and must be configured to allow Cognito to
* send emails.
*
* @see https://docs.aws.amazon.com/cognito/latest/developerguide/user-pool-email.html
*/
readonly fromEmail: string;
/**
* An optional name that should be used as the sender's name
* along with the email.
*
* @default - no name
*/
readonly fromName?: string;
/**
* The destination to which the receiver of the email should reploy to.
*
* @default - same as the fromEmail
*/
readonly replyTo?: string;
/**
* The name of a configuration set in Amazon SES that should
* be applied to emails sent via Cognito.
*
* @see https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-cognito-userpool-emailconfiguration.html#cfn-cognito-userpool-emailconfiguration-configurationset
*
* @default - no configuration set
*/
readonly configurationSetName?: string;
/**
* Required if the UserPool region is different than the SES region.
*
* If sending emails with a Amazon SES verified email address,
* and the region that SES is configured is different than the
* region in which the UserPool is deployed, you must specify that
* region here.
*
* @default - The same region as the Cognito UserPool
*/
readonly sesRegion?: string;
/**
* SES Verified custom domain to be used to verify the identity
*
* @default - no domain
*/
readonly sesVerifiedDomain?: string
}
/**
* Result of binding email settings with a user pool
*/
interface UserPoolEmailConfig {
/**
* The name of the configuration set in SES.
*
* @default - none
*/
readonly configurationSet?: string;
/**
* Specifies whether to use Cognito's built in email functionality
* or SES.
*
* @default - Cognito built in email functionality
*/
readonly emailSendingAccount?: string;
/**
* Identifies either the sender's email address or the sender's
* name with their email address.
*
* If emailSendingAccount is DEVELOPER then this cannot be specified.
*
* @default 'no-reply@verificationemail.com'
*/
readonly from?: string;
/**
* The destination to which the receiver of the email should reply to.
*
* @default - same as `from`
*/
readonly replyToEmailAddress?: string;
/**
* The ARN of a verified email address in Amazon SES.
*
* required if emailSendingAccount is DEVELOPER or if
* 'from' is provided.
*
* @default - none
*/
readonly sourceArn?: string;
}
/**
* Configure how Cognito sends emails
*/
export abstract class UserPoolEmail {
/**
* Send email using Cognito
*/
public static withCognito(replyTo?: string): UserPoolEmail {
return new CognitoEmail(replyTo);
}
/**
* Send email using SES
*/
public static withSES(options: UserPoolSESOptions): UserPoolEmail {
return new SESEmail(options);
}
/**
* Returns the email configuration for a Cognito UserPool
* that controls how Cognito will send emails
* @internal
*/
public abstract _bind(scope: Construct): UserPoolEmailConfig;
}
class CognitoEmail extends UserPoolEmail {
constructor(private readonly replyTo?: string) {
super();
}
public _bind(_scope: Construct): UserPoolEmailConfig {
return {
replyToEmailAddress: encodeAndTest(this.replyTo),
emailSendingAccount: 'COGNITO_DEFAULT',
};
}
}
class SESEmail extends UserPoolEmail {
constructor(private readonly options: UserPoolSESOptions) {
super();
}
public _bind(scope: Construct): UserPoolEmailConfig {
const region = Stack.of(scope).region;
if (Token.isUnresolved(region) && !this.options.sesRegion) {
throw new Error('Your stack region cannot be determined so "sesRegion" is required in SESOptions');
}
let from = encodeAndTest(this.options.fromEmail);
if (this.options.fromName) {
let fromName = this.options.fromName;
// see RFC 5322 Section 3.4 for details
if (/[^\u0020-\u007E]/u.test(fromName)) {
// mime encoding for non US-ASCII characters (RFC 2047)
fromName = `=?UTF-8?B?${Buffer.from(fromName, 'utf-8').toString('base64')}?=`;
} else if (/^(?:"(?:[^\\"]|\\.)+"|[\w !#$%&'*+-\/=?^_`{|}~]+)$/.test(fromName)) {
// atom or quoted-string
} else {
// make quoted-string
fromName = `"${fromName.replace(/["\\]/g, '\\$&')}"`;
}
from = `${fromName} <${from}>`;
}
if (this.options.sesVerifiedDomain) {
const domainFromEmail = this.options.fromEmail.split('@').pop();
if (domainFromEmail !== this.options.sesVerifiedDomain) {
throw new Error('"fromEmail" contains a different domain than the "sesVerifiedDomain"');
}
}
return {
from,
replyToEmailAddress: encodeAndTest(this.options.replyTo),
configurationSet: this.options.configurationSetName,
emailSendingAccount: 'DEVELOPER',
sourceArn: Stack.of(scope).formatArn({
service: 'ses',
resource: 'identity',
resourceName: encodeAndTest(this.options.sesVerifiedDomain ?? this.options.fromEmail),
region: this.options.sesRegion ?? region,
}),
};
}
}
function encodeAndTest(input: string | undefined): string | undefined {
if (input) {
const local = input.split('@')[0];
if (!/[\p{ASCII}]+/u.test(local)) {
throw new Error('the local part of the email address must use ASCII characters only');
}
return punycodeEncode(input);
} else {
return undefined;
}
}