-
Notifications
You must be signed in to change notification settings - Fork 4.1k
/
Copy pathuser-pool-attr.ts
271 lines (235 loc) · 6.36 KB
/
user-pool-attr.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/**
* Standard attributes
*
* @see https://docs.aws.amazon.com/cognito/latest/developerguide/user-pool-settings-attributes.html#cognito-user-pools-standard-attributes
*/
export enum StandardAttribute {
/**
* End-User's preferred postal address.
*/
ADDRESS = 'address',
/**
* User's birthday, represented as an ISO 8601:2004 [ISO8601‑2004] YYYY-MM-DD format.
* The year MAY be 0000, indicating that it is omitted.
* To represent only the year, YYYY format is allowed.
*/
BIRTHDATE = 'birthdate',
/**
* User's preferred e-mail address.
* Its value MUST conform to the RFC 5322 [RFC5322] addr-spec syntax.
*/
EMAIL = 'email',
/**
* Surname or last name of the user.
*/
FAMILY_NAME = 'family_name',
/**
* User's gender.
*/
GENDER = 'gender',
/**
* Given name or first name of the user.
*/
GIVEN_NAME = 'given_name',
/**
* User's locale, represented as a BCP47 [RFC5646] language tag.
* This is typically an ISO 639-1 Alpha-2 [ISO639‑1] language code in lowercase
* and an ISO 3166-1 Alpha-2 [ISO3166‑1] country code in uppercase, separated by a dash.
* For example, en-US or fr-CA.
*/
LOCALE = 'locale',
/**
* Middle name of the user.
*/
MIDDLE_NAME = 'middle_name',
/**
* User's full name in displayable form including all name parts, possibly including titles and suffixes.
*/
NAME = 'name',
/**
* Casual name of the user that may or may not be the same as the given_name.
*/
NICKNAME = 'nickname',
/**
* User's preferred telephone number.
*
* Phone numbers must follow these formatting rules: A phone number must start with a plus (+) sign, followed
* immediately by the country code. A phone number can only contain the + sign and digits. You must remove any other
* characters from a phone number, such as parentheses, spaces, or dashes (-) before submitting the value to the
* service.
*/
PHONE_NUMBER = 'phone_number',
/**
* URL of the user's profile picture.
* This URL must refer to an image file (for example, a PNG, JPEG, or GIF image file).
*/
PICTURE = 'picture',
/**
* Shorthand name by which the user wishes to be referred to.
*/
PREFERRED_USERNAME = 'preferred_username',
/**
* URL of the user's profile page.
*/
PROFILE = 'profile',
/**
* The user's time zone
*/
TIMEZONE = 'zoneinfo',
/**
* Time the user's information was last updated.
* Its value is a JSON number representing the number of seconds from 1970-01-01T0:0:0Z
* as measured in UTC until the date/time.
*/
UPDATED_AT = 'updated_at',
/**
* URL of the user's web page or blog.
*/
WEBSITE = 'website'
}
/**
* Represents a custom attribute type.
*/
export interface ICustomAttribute {
/**
* Bind this custom attribute type to the values as expected by CloudFormation
*/
bind(): CustomAttributeConfig;
}
/**
* Configuration that will be fed into CloudFormation for any custom attribute type.
*/
export interface CustomAttributeConfig {
// tslint:disable:max-line-length
/**
* The data type of the custom attribute.
*
* @see https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_SchemaAttributeType.html#CognitoUserPools-Type-SchemaAttributeType-AttributeDataType
*/
readonly dataType: string;
// tslint:enable:max-line-length
/**
* The constraints for a custom attribute of 'String' data type.
* @default - None.
*/
readonly stringConstraints?: StringAttributeConstraints;
/**
* The constraints for a custom attribute of the 'Number' data type.
* @default - None.
*/
readonly numberConstraints?: NumberAttributeConstraints;
}
/**
* Constraints that can be applied to a custom attribute of string type.
*/
export interface StringAttributeConstraints {
/**
* Minimum length of this attribute.
* @default 0
*/
readonly minLen?: number;
/**
* Maximum length of this attribute.
* @default 2048
*/
readonly maxLen?: number;
}
/**
* Props for constructing a StringAttr
*/
export interface StringAttributeProps extends StringAttributeConstraints {
}
/**
* The String custom attribute type.
*/
export class StringAttribute implements ICustomAttribute {
private readonly minLen?: number;
private readonly maxLen?: number;
constructor(props: StringAttributeProps = {}) {
if (props.minLen && props.minLen < 0) {
throw new Error(`minLen cannot be less than 0 (value: ${props.minLen}).`);
}
if (props.maxLen && props.maxLen > 2048) {
throw new Error(`maxLen cannot be greater than 2048 (value: ${props.maxLen}).`);
}
this.minLen = props?.minLen;
this.maxLen = props?.maxLen;
}
public bind(): CustomAttributeConfig {
let stringConstraints: StringAttributeConstraints | undefined;
if (this.minLen || this.maxLen) {
stringConstraints = {
minLen: this.minLen,
maxLen: this.maxLen,
};
}
return {
dataType: 'String',
stringConstraints,
};
}
}
/**
* Constraints that can be applied to a custom attribute of number type.
*/
export interface NumberAttributeConstraints {
/**
* Minimum value of this attribute.
* @default - no minimum value
*/
readonly min?: number;
/**
* Maximum value of this attribute.
* @default - no maximum value
*/
readonly max?: number;
}
/**
* Props for NumberAttr
*/
export interface NumberAttributeProps extends NumberAttributeConstraints {
}
/**
* The Number custom attribute type.
*/
export class NumberAttribute implements ICustomAttribute {
private readonly min?: number;
private readonly max?: number;
constructor(props: NumberAttributeProps = {}) {
this.min = props?.min;
this.max = props?.max;
}
public bind(): CustomAttributeConfig {
let numberConstraints: NumberAttributeConstraints | undefined;
if (this.min || this.max) {
numberConstraints = {
min: this.min,
max: this.max,
};
}
return {
dataType: 'Number',
numberConstraints,
};
}
}
/**
* The Boolean custom attribute type.
*/
export class BooleanAttribute implements ICustomAttribute {
public bind(): CustomAttributeConfig {
return {
dataType: 'Boolean'
};
}
}
/**
* The DateTime custom attribute type.
*/
export class DateTimeAttribute implements ICustomAttribute {
public bind(): CustomAttributeConfig {
return {
dataType: 'DateTime'
};
}
}