idea-aws 4.4.1 → 4.4.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/src/attachments.d.ts +27 -0
- package/dist/src/attachments.js +39 -0
- package/dist/src/cognito.d.ts +177 -0
- package/dist/src/cognito.js +412 -0
- package/dist/src/comprehend.d.ts +34 -0
- package/dist/src/comprehend.js +58 -0
- package/dist/src/dynamoDB.d.ts +108 -0
- package/dist/src/dynamoDB.js +296 -0
- package/dist/src/genericController.d.ts +60 -0
- package/dist/src/genericController.js +89 -0
- package/dist/src/lambdaLogger.d.ts +13 -0
- package/dist/src/lambdaLogger.js +43 -0
- package/dist/src/logger.d.ts +10 -0
- package/dist/src/logger.js +16 -0
- package/dist/src/metrics.d.ts +31 -0
- package/dist/src/metrics.js +45 -0
- package/dist/src/resourceController.d.ts +232 -0
- package/dist/src/resourceController.js +561 -0
- package/dist/src/s3.d.ts +225 -0
- package/dist/src/s3.js +180 -0
- package/dist/src/secretsManager.d.ts +15 -0
- package/dist/src/secretsManager.js +48 -0
- package/dist/src/ses.d.ts +161 -0
- package/dist/src/ses.js +196 -0
- package/dist/src/sns.d.ts +60 -0
- package/dist/src/sns.js +94 -0
- package/dist/src/ssm.d.ts +22 -0
- package/dist/src/ssm.js +54 -0
- package/dist/src/streamController.d.ts +11 -0
- package/dist/src/streamController.js +20 -0
- package/dist/src/translate.d.ts +61 -0
- package/dist/src/translate.js +155 -0
- package/package.json +2 -2
- package/src/attachments.ts +41 -0
- package/src/cognito.ts +511 -0
- package/src/comprehend.ts +52 -0
- package/src/dynamoDB.ts +311 -0
- package/src/genericController.ts +103 -0
- package/src/lambdaLogger.ts +39 -0
- package/src/metrics.ts +45 -0
- package/src/resourceController.ts +645 -0
- package/src/s3.ts +334 -0
- package/src/secretsManager.ts +24 -0
- package/src/ses.ts +313 -0
- package/src/sns.ts +118 -0
- package/src/ssm.ts +33 -0
- package/src/streamController.ts +25 -0
- package/src/translate.ts +174 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "idea-aws",
|
|
3
|
-
"version": "4.4.
|
|
3
|
+
"version": "4.4.2",
|
|
4
4
|
"description": "AWS wrappers to use in IDEA's back-ends",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -39,7 +39,7 @@
|
|
|
39
39
|
"dependencies": {
|
|
40
40
|
"@aws-lambda-powertools/metrics": "^1.17.0",
|
|
41
41
|
"@aws-lambda-powertools/tracer": "^1.18.0",
|
|
42
|
-
"idea-toolbox": "^7.0.
|
|
42
|
+
"idea-toolbox": "^7.0.3",
|
|
43
43
|
"nanoid": "^3.3.7",
|
|
44
44
|
"nodemailer": "^6.9.8",
|
|
45
45
|
"source-map-support": "^0.5.21"
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { SignedURL } from 'idea-toolbox';
|
|
2
|
+
|
|
3
|
+
import { DynamoDB } from './dynamoDB';
|
|
4
|
+
import { S3 } from './s3';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* A custom class that takes advantage of DynamoDB and S3 to easily manage attachments.
|
|
8
|
+
*/
|
|
9
|
+
export class Attachments {
|
|
10
|
+
/**
|
|
11
|
+
* The bucket where from to retrieve the attachments. Fallback to IDEA's default one.
|
|
12
|
+
*/
|
|
13
|
+
protected S3_ATTACHMENTS_BUCKET = process.env.S3_ATTACHMENTS_BUCKET ?? 'idea-attachments';
|
|
14
|
+
/**
|
|
15
|
+
* The prefix for attachment IDs. Fallback to IDEA's default one.
|
|
16
|
+
*/
|
|
17
|
+
protected IUID_ATTACHMENTS_PREFIX = process.env.IUID_ATTACHMENTS_PREFIX ?? 'ATT';
|
|
18
|
+
|
|
19
|
+
constructor(protected ddb: DynamoDB, protected s3: S3) {}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Get a signedURL to put an attachment.
|
|
23
|
+
*/
|
|
24
|
+
async put(project: string, teamId: string): Promise<SignedURL> {
|
|
25
|
+
const attachmentIdPrefix = this.IUID_ATTACHMENTS_PREFIX.concat('_', project, '_', teamId);
|
|
26
|
+
const attachmentId = await this.ddb.IUNID(attachmentIdPrefix);
|
|
27
|
+
|
|
28
|
+
const signedURL = await this.s3.signedURLPut(this.S3_ATTACHMENTS_BUCKET, attachmentId);
|
|
29
|
+
signedURL.id = attachmentId;
|
|
30
|
+
return signedURL;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Get a signedURL to retrieve an attachment.
|
|
35
|
+
*/
|
|
36
|
+
async get(attachmentId: string): Promise<SignedURL> {
|
|
37
|
+
const signedURL = await this.s3.signedURLGet(this.S3_ATTACHMENTS_BUCKET, attachmentId);
|
|
38
|
+
signedURL.id = attachmentId;
|
|
39
|
+
return signedURL;
|
|
40
|
+
}
|
|
41
|
+
}
|
package/src/cognito.ts
ADDED
|
@@ -0,0 +1,511 @@
|
|
|
1
|
+
import * as CognitoIP from '@aws-sdk/client-cognito-identity-provider';
|
|
2
|
+
import { CognitoUser, isEmpty } from 'idea-toolbox';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* A wrapper for AWS Cognito.
|
|
6
|
+
*/
|
|
7
|
+
export class Cognito {
|
|
8
|
+
protected cognito: CognitoIP.CognitoIdentityProviderClient;
|
|
9
|
+
|
|
10
|
+
constructor(options: { region?: string } = {}) {
|
|
11
|
+
this.cognito = new CognitoIP.CognitoIdentityProviderClient({ region: options.region });
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Change the region in which to find the user pool.
|
|
16
|
+
* Default: the runner's (e.g. Lambda function) region.
|
|
17
|
+
*/
|
|
18
|
+
setRegion(region: string): void {
|
|
19
|
+
// there is no quick way to change the region without re-creating the object
|
|
20
|
+
this.cognito = new CognitoIP.CognitoIdentityProviderClient({ region });
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Get the attributes of the user, from the authorizer claims.
|
|
25
|
+
* @param claims authorizer claims
|
|
26
|
+
* @return user's data
|
|
27
|
+
* @deprecated use idea-toolbox's CognitoUser instead
|
|
28
|
+
*/
|
|
29
|
+
getUserByClaims(claims: Record<string, any>): CognitoUserGeneric {
|
|
30
|
+
if (!claims) return null;
|
|
31
|
+
const user: Record<string, any> = {};
|
|
32
|
+
// add any additional cognito attribute available in cognito
|
|
33
|
+
for (const p in claims) if (p.startsWith('cognito:')) user[p.slice(8)] = claims[p];
|
|
34
|
+
// map the important attributes with reserved names
|
|
35
|
+
user.userId = claims.sub;
|
|
36
|
+
user.email = claims.email;
|
|
37
|
+
return user as CognitoUserGeneric;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Map the complex structure returned by Cognito for a user's attributes in a simple key-value object.
|
|
42
|
+
*/
|
|
43
|
+
private mapCognitoUserAttributesAsPlainObject(user: Record<string, any>): CognitoUserGeneric {
|
|
44
|
+
const userAttributes: Record<string, any> = {};
|
|
45
|
+
(user.Attributes ?? user.UserAttributes ?? []).forEach((a: any): void => (userAttributes[a.Name] = a.Value));
|
|
46
|
+
|
|
47
|
+
if (!userAttributes.userId) userAttributes.userId = userAttributes.sub;
|
|
48
|
+
return userAttributes as CognitoUserGeneric;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
//
|
|
52
|
+
// ADMIN
|
|
53
|
+
//
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Identify a user by its email address, returning its attributes.
|
|
57
|
+
*/
|
|
58
|
+
async getUserByEmail(email: string, userPoolId: string): Promise<CognitoUserGeneric> {
|
|
59
|
+
const command = new CognitoIP.AdminGetUserCommand({ UserPoolId: userPoolId, Username: email });
|
|
60
|
+
try {
|
|
61
|
+
const user = await this.cognito.send(command);
|
|
62
|
+
return this.mapCognitoUserAttributesAsPlainObject(user);
|
|
63
|
+
} catch (error) {
|
|
64
|
+
if ((error as Error).name === 'UserNotFoundException') throw new Error('User not found');
|
|
65
|
+
throw error;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Identify a user by its userId (sub), returning its attributes.
|
|
71
|
+
*/
|
|
72
|
+
async getUserBySub(sub: string, userPoolId: string): Promise<CognitoUserGeneric> {
|
|
73
|
+
// as of today, there is no a direct way to find a user by its sub: we need to run a query against the users base
|
|
74
|
+
const command = new CognitoIP.ListUsersCommand({ UserPoolId: userPoolId, Filter: `sub = "${sub}"`, Limit: 1 });
|
|
75
|
+
const { Users } = await this.cognito.send(command);
|
|
76
|
+
if (Users.length < 1) throw new Error('User not found');
|
|
77
|
+
return this.mapCognitoUserAttributesAsPlainObject(Users[0]);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* List all the users of the pool.
|
|
82
|
+
*/
|
|
83
|
+
async listUsers(
|
|
84
|
+
userPoolId: string,
|
|
85
|
+
options: { pagination?: string; users: CognitoUser[] } = { users: [] }
|
|
86
|
+
): Promise<CognitoUser[]> {
|
|
87
|
+
const params: CognitoIP.ListUsersCommandInput = { UserPoolId: userPoolId };
|
|
88
|
+
if (options.pagination) params.PaginationToken = options.pagination;
|
|
89
|
+
|
|
90
|
+
const { Users, PaginationToken: pagination } = await this.cognito.send(new CognitoIP.ListUsersCommand(params));
|
|
91
|
+
|
|
92
|
+
const users = options.users.concat(Users.map(u => new CognitoUser(this.mapCognitoUserAttributesAsPlainObject(u))));
|
|
93
|
+
|
|
94
|
+
if (pagination) return await this.listUsers(userPoolId, { pagination, users });
|
|
95
|
+
else return users;
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* List all the users of the pool, including the information about the groups they're in.
|
|
99
|
+
* Note: it's slower than the alternative `getAllUsers`: use it only when needed.
|
|
100
|
+
*/
|
|
101
|
+
async listUsersWithGroupsDetail(cognitoUserPoolId: string): Promise<CognitoUser[]> {
|
|
102
|
+
const groups = await this.listGroups(cognitoUserPoolId);
|
|
103
|
+
|
|
104
|
+
const users: CognitoUser[] = [];
|
|
105
|
+
for (const group of groups) {
|
|
106
|
+
const usersOfGroup = await this.listUsersInGroup(group.name, cognitoUserPoolId);
|
|
107
|
+
usersOfGroup.forEach(userInGroup => {
|
|
108
|
+
const userAlreadyInOutputList = users.find(u => u.userId === userInGroup.userId);
|
|
109
|
+
if (userAlreadyInOutputList) userAlreadyInOutputList.groups.push(group.name);
|
|
110
|
+
else {
|
|
111
|
+
userInGroup.groups.push(group.name);
|
|
112
|
+
users.push(userInGroup);
|
|
113
|
+
}
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
return users;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Create a new user (by its email) in the pool specified.
|
|
122
|
+
* @return userId of the new user
|
|
123
|
+
*/
|
|
124
|
+
async createUser(
|
|
125
|
+
cognitoUserOrEmail: CognitoUser | string,
|
|
126
|
+
userPoolId: string,
|
|
127
|
+
options: CreateUserOptions = {}
|
|
128
|
+
): Promise<string> {
|
|
129
|
+
const email =
|
|
130
|
+
typeof cognitoUserOrEmail === 'string'
|
|
131
|
+
? (cognitoUserOrEmail as string)
|
|
132
|
+
: (cognitoUserOrEmail as CognitoUser).email;
|
|
133
|
+
|
|
134
|
+
if (isEmpty(email, 'email')) throw new Error('INVALID_EMAIL');
|
|
135
|
+
|
|
136
|
+
const UserAttributes = [
|
|
137
|
+
{ Name: 'email', Value: email },
|
|
138
|
+
{ Name: 'email_verified', Value: 'true' }
|
|
139
|
+
];
|
|
140
|
+
|
|
141
|
+
if (typeof cognitoUserOrEmail === 'object') {
|
|
142
|
+
const user = cognitoUserOrEmail as CognitoUser;
|
|
143
|
+
|
|
144
|
+
UserAttributes.push({ Name: 'name', Value: user.name });
|
|
145
|
+
UserAttributes.push({ Name: 'picture', Value: user.picture || '' });
|
|
146
|
+
|
|
147
|
+
Object.keys(user.attributes).forEach(a =>
|
|
148
|
+
UserAttributes.push({ Name: 'custom:'.concat(a), Value: String(user.attributes[a]) })
|
|
149
|
+
);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
const params: CognitoIP.AdminCreateUserCommandInput = { UserPoolId: userPoolId, Username: email, UserAttributes };
|
|
153
|
+
if (options.skipNotification) params.MessageAction = 'SUPPRESS';
|
|
154
|
+
if (options.temporaryPassword) params.TemporaryPassword = options.temporaryPassword;
|
|
155
|
+
|
|
156
|
+
const { User } = await this.cognito.send(new CognitoIP.AdminCreateUserCommand(params));
|
|
157
|
+
|
|
158
|
+
const userId = this.mapCognitoUserAttributesAsPlainObject(User).sub;
|
|
159
|
+
|
|
160
|
+
if (!userId) throw new Error('Creation failed');
|
|
161
|
+
return userId;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Resend the password to a user who never logged in.
|
|
166
|
+
*/
|
|
167
|
+
async resendPassword(email: string, userPoolId: string, options: CreateUserOptions = {}): Promise<void> {
|
|
168
|
+
if (isEmpty(email, 'email')) throw new Error('Invalid email');
|
|
169
|
+
|
|
170
|
+
const params: CognitoIP.AdminCreateUserCommandInput = {
|
|
171
|
+
UserPoolId: userPoolId,
|
|
172
|
+
Username: email,
|
|
173
|
+
MessageAction: 'RESEND'
|
|
174
|
+
};
|
|
175
|
+
if (options.temporaryPassword) params.TemporaryPassword = options.temporaryPassword;
|
|
176
|
+
|
|
177
|
+
await this.cognito.send(new CognitoIP.AdminCreateUserCommand(params));
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Set a new password for a specific user identified by its email (admin-only).
|
|
182
|
+
* If not specified, the password is generated randomly, and the user must change it at the first login.
|
|
183
|
+
*/
|
|
184
|
+
async setPassword(
|
|
185
|
+
email: string,
|
|
186
|
+
userPoolId: string,
|
|
187
|
+
options: { password?: string; permanent?: boolean } = {}
|
|
188
|
+
): Promise<void> {
|
|
189
|
+
if (isEmpty(email, 'email')) throw new Error('Invalid email');
|
|
190
|
+
|
|
191
|
+
const RANDOM_PASSWORD_LENGTH = 8;
|
|
192
|
+
const password =
|
|
193
|
+
options.password ??
|
|
194
|
+
Math.random()
|
|
195
|
+
.toString(36)
|
|
196
|
+
.slice(2, 2 + RANDOM_PASSWORD_LENGTH);
|
|
197
|
+
|
|
198
|
+
const params: CognitoIP.AdminSetUserPasswordCommandInput = {
|
|
199
|
+
UserPoolId: userPoolId,
|
|
200
|
+
Username: email,
|
|
201
|
+
Password: password,
|
|
202
|
+
Permanent: options.permanent
|
|
203
|
+
};
|
|
204
|
+
|
|
205
|
+
await this.cognito.send(new CognitoIP.AdminSetUserPasswordCommand(params));
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* Delete a user by its email (username), in the pool specified.
|
|
210
|
+
*/
|
|
211
|
+
async deleteUser(email: string, userPoolId: string): Promise<void> {
|
|
212
|
+
if (isEmpty(email, 'email')) throw new Error('Invalid email');
|
|
213
|
+
|
|
214
|
+
const command = new CognitoIP.AdminDeleteUserCommand({ UserPoolId: userPoolId, Username: email });
|
|
215
|
+
await this.cognito.send(command);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
//
|
|
219
|
+
// USER
|
|
220
|
+
//
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* Sign in a user of a specific pool through username and password.
|
|
224
|
+
*/
|
|
225
|
+
async signIn(
|
|
226
|
+
email: string,
|
|
227
|
+
password: string,
|
|
228
|
+
userPoolId: string,
|
|
229
|
+
userPoolClientId: string
|
|
230
|
+
): Promise<CognitoIP.AuthenticationResultType> {
|
|
231
|
+
const command = new CognitoIP.AdminInitiateAuthCommand({
|
|
232
|
+
UserPoolId: userPoolId,
|
|
233
|
+
ClientId: userPoolClientId,
|
|
234
|
+
AuthFlow: 'ADMIN_NO_SRP_AUTH',
|
|
235
|
+
AuthParameters: { USERNAME: email, PASSWORD: password }
|
|
236
|
+
});
|
|
237
|
+
const { AuthenticationResult } = await this.cognito.send(command);
|
|
238
|
+
|
|
239
|
+
if (!AuthenticationResult) throw new Error('Sign-in failed');
|
|
240
|
+
return AuthenticationResult;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* Given a username and a refresh token (and pool data), refresh the session and return the new tokens.
|
|
245
|
+
*/
|
|
246
|
+
async refreshSession(
|
|
247
|
+
email: string,
|
|
248
|
+
refreshToken: string,
|
|
249
|
+
userPoolId: string,
|
|
250
|
+
userPoolClientId: string
|
|
251
|
+
): Promise<CognitoIP.AuthenticationResultType> {
|
|
252
|
+
const command = new CognitoIP.AdminInitiateAuthCommand({
|
|
253
|
+
UserPoolId: userPoolId,
|
|
254
|
+
ClientId: userPoolClientId,
|
|
255
|
+
AuthFlow: 'REFRESH_TOKEN_AUTH',
|
|
256
|
+
AuthParameters: { USERNAME: email, REFRESH_TOKEN: refreshToken }
|
|
257
|
+
});
|
|
258
|
+
const { AuthenticationResult } = await this.cognito.send(command);
|
|
259
|
+
|
|
260
|
+
if (!AuthenticationResult) throw new Error('Refresh failed');
|
|
261
|
+
return AuthenticationResult;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
/**
|
|
265
|
+
* Change the email address (== username) associated to a user.
|
|
266
|
+
*/
|
|
267
|
+
async updateEmail(email: string, newEmail: string, userPoolId: string): Promise<void> {
|
|
268
|
+
if (isEmpty(newEmail, 'email')) throw new Error('Invalid new email');
|
|
269
|
+
|
|
270
|
+
const command = new CognitoIP.AdminUpdateUserAttributesCommand({
|
|
271
|
+
UserPoolId: userPoolId,
|
|
272
|
+
Username: email,
|
|
273
|
+
UserAttributes: [
|
|
274
|
+
{ Name: 'email', Value: newEmail },
|
|
275
|
+
{ Name: 'email_verified', Value: 'true' }
|
|
276
|
+
]
|
|
277
|
+
});
|
|
278
|
+
await this.cognito.send(command);
|
|
279
|
+
|
|
280
|
+
// sign out the user from all its devices and resolve
|
|
281
|
+
await this.globalSignOut(newEmail, userPoolId);
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
/**
|
|
285
|
+
* Change the password to sign in for a user.
|
|
286
|
+
*/
|
|
287
|
+
async updatePassword(
|
|
288
|
+
email: string,
|
|
289
|
+
oldPassword: string,
|
|
290
|
+
newPassword: string,
|
|
291
|
+
userPoolId: string,
|
|
292
|
+
userPoolClientId: string
|
|
293
|
+
): Promise<void> {
|
|
294
|
+
if (newPassword.length < 8) throw new Error('Invalid new password');
|
|
295
|
+
|
|
296
|
+
const tokensForPasswordChange = await this.signIn(email, oldPassword, userPoolId, userPoolClientId);
|
|
297
|
+
|
|
298
|
+
const command = new CognitoIP.ChangePasswordCommand({
|
|
299
|
+
AccessToken: tokensForPasswordChange.AccessToken,
|
|
300
|
+
PreviousPassword: oldPassword,
|
|
301
|
+
ProposedPassword: newPassword
|
|
302
|
+
});
|
|
303
|
+
await this.cognito.send(command);
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
/**
|
|
307
|
+
* Send to a user the instructions to change the password.
|
|
308
|
+
*/
|
|
309
|
+
async forgotPassword(email: string, userPoolClientId: string): Promise<CognitoIP.CodeDeliveryDetailsType> {
|
|
310
|
+
const command = new CognitoIP.ForgotPasswordCommand({ Username: email, ClientId: userPoolClientId });
|
|
311
|
+
const { CodeDeliveryDetails } = await this.cognito.send(command);
|
|
312
|
+
return CodeDeliveryDetails;
|
|
313
|
+
}
|
|
314
|
+
/**
|
|
315
|
+
* Complete the flow of a password forgot.
|
|
316
|
+
*/
|
|
317
|
+
async confirmForgotPassword(
|
|
318
|
+
email: string,
|
|
319
|
+
newPassword: string,
|
|
320
|
+
confirmationCode: string,
|
|
321
|
+
userPoolClientId: string
|
|
322
|
+
): Promise<void> {
|
|
323
|
+
const command = new CognitoIP.ConfirmForgotPasswordCommand({
|
|
324
|
+
ClientId: userPoolClientId,
|
|
325
|
+
Username: email,
|
|
326
|
+
ConfirmationCode: confirmationCode,
|
|
327
|
+
Password: newPassword
|
|
328
|
+
});
|
|
329
|
+
await this.cognito.send(command);
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
/**
|
|
333
|
+
* Update a (Cognito)User's attributes, excluding the attributes that require specific methods.
|
|
334
|
+
*/
|
|
335
|
+
async updateUser(user: CognitoUser, userPoolId: string): Promise<void> {
|
|
336
|
+
const UserAttributes = [
|
|
337
|
+
{ Name: 'name', Value: user.name },
|
|
338
|
+
{ Name: 'picture', Value: user.picture ?? '' }
|
|
339
|
+
];
|
|
340
|
+
|
|
341
|
+
Object.keys(user.attributes).forEach(customAttribute =>
|
|
342
|
+
UserAttributes.push({
|
|
343
|
+
Name: 'custom:'.concat(customAttribute),
|
|
344
|
+
Value: String(user.attributes[customAttribute])
|
|
345
|
+
})
|
|
346
|
+
);
|
|
347
|
+
|
|
348
|
+
const command = new CognitoIP.AdminUpdateUserAttributesCommand({
|
|
349
|
+
UserPoolId: userPoolId,
|
|
350
|
+
Username: user.email,
|
|
351
|
+
UserAttributes
|
|
352
|
+
});
|
|
353
|
+
await this.cognito.send(command);
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
/**
|
|
357
|
+
* Sign out the user from all devices.
|
|
358
|
+
*/
|
|
359
|
+
async globalSignOut(email: string, userPoolId: string): Promise<void> {
|
|
360
|
+
const command = new CognitoIP.AdminUserGlobalSignOutCommand({ Username: email, UserPoolId: userPoolId });
|
|
361
|
+
await this.cognito.send(command);
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
/**
|
|
365
|
+
* Confirm and conclude a registration, usign a confirmation code.
|
|
366
|
+
*/
|
|
367
|
+
async confirmSignUp(email: string, confirmationCode: string, userPoolClientId: string): Promise<void> {
|
|
368
|
+
if (!email) throw new Error('Invalid email');
|
|
369
|
+
if (!confirmationCode) throw new Error('Invalid confirmation code');
|
|
370
|
+
if (!userPoolClientId) throw new Error('Invalid client ID');
|
|
371
|
+
|
|
372
|
+
const command = new CognitoIP.ConfirmSignUpCommand({
|
|
373
|
+
Username: email,
|
|
374
|
+
ConfirmationCode: confirmationCode,
|
|
375
|
+
ClientId: userPoolClientId
|
|
376
|
+
});
|
|
377
|
+
await this.cognito.send(command);
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
/**
|
|
381
|
+
* List the groups of the user pool.
|
|
382
|
+
*/
|
|
383
|
+
async listGroups(
|
|
384
|
+
userPoolId: string,
|
|
385
|
+
options: { pagination?: string; groups: CognitoGroup[] } = { groups: [] }
|
|
386
|
+
): Promise<CognitoGroup[]> {
|
|
387
|
+
const params: CognitoIP.ListGroupsRequest = { UserPoolId: userPoolId };
|
|
388
|
+
if (options.pagination) params.NextToken = options.pagination;
|
|
389
|
+
|
|
390
|
+
const res = await this.cognito.send(new CognitoIP.ListGroupsCommand(params));
|
|
391
|
+
|
|
392
|
+
const pagination = res.NextToken;
|
|
393
|
+
const groups = options.groups.concat(
|
|
394
|
+
res.Groups.map(g => ({ name: g.GroupName, description: g.Description } as CognitoGroup))
|
|
395
|
+
);
|
|
396
|
+
|
|
397
|
+
if (pagination) return await this.listGroups(userPoolId, { pagination, groups });
|
|
398
|
+
else return groups;
|
|
399
|
+
}
|
|
400
|
+
/**
|
|
401
|
+
* Create a new group in the user pool.
|
|
402
|
+
*/
|
|
403
|
+
async createGroup(groupName: string, userPoolId: string): Promise<void> {
|
|
404
|
+
const command = new CognitoIP.CreateGroupCommand({ GroupName: groupName, UserPoolId: userPoolId });
|
|
405
|
+
await this.cognito.send(command);
|
|
406
|
+
}
|
|
407
|
+
/**
|
|
408
|
+
* Delete a group from the user pool.
|
|
409
|
+
*/
|
|
410
|
+
async deleteGroup(groupName: string, userPoolId: string): Promise<void> {
|
|
411
|
+
const command = new CognitoIP.DeleteGroupCommand({ GroupName: groupName, UserPoolId: userPoolId });
|
|
412
|
+
await this.cognito.send(command);
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
/**
|
|
416
|
+
* List the users part of a group in the user pool.
|
|
417
|
+
*/
|
|
418
|
+
async listUsersInGroup(
|
|
419
|
+
group: string,
|
|
420
|
+
userPoolId: string,
|
|
421
|
+
options: { pagination?: string; users: CognitoUser[] } = { users: [] }
|
|
422
|
+
): Promise<CognitoUser[]> {
|
|
423
|
+
const params: CognitoIP.ListUsersInGroupRequest = {
|
|
424
|
+
UserPoolId: userPoolId,
|
|
425
|
+
GroupName: group
|
|
426
|
+
};
|
|
427
|
+
if (options.pagination) params.NextToken = options.pagination;
|
|
428
|
+
|
|
429
|
+
const res = await this.cognito.send(new CognitoIP.ListUsersInGroupCommand(params));
|
|
430
|
+
|
|
431
|
+
const pagination = res.NextToken;
|
|
432
|
+
const users = options.users.concat(
|
|
433
|
+
res.Users.map(u => new CognitoUser(this.mapCognitoUserAttributesAsPlainObject(u)))
|
|
434
|
+
);
|
|
435
|
+
|
|
436
|
+
if (pagination) return await this.listUsersInGroup(group, userPoolId, { pagination, users });
|
|
437
|
+
else return users;
|
|
438
|
+
}
|
|
439
|
+
/**
|
|
440
|
+
* Add a user (by email) to a group in the user pool.
|
|
441
|
+
*/
|
|
442
|
+
async addUserToGroup(email: string, group: string, userPoolId: string): Promise<void> {
|
|
443
|
+
const user = new CognitoUser(await this.getUserByEmail(email, userPoolId));
|
|
444
|
+
|
|
445
|
+
const command = new CognitoIP.AdminAddUserToGroupCommand({
|
|
446
|
+
UserPoolId: userPoolId,
|
|
447
|
+
GroupName: group,
|
|
448
|
+
Username: user.userId
|
|
449
|
+
});
|
|
450
|
+
await this.cognito.send(command);
|
|
451
|
+
}
|
|
452
|
+
/**
|
|
453
|
+
* Remove a user (by email) from a group in the user pool.
|
|
454
|
+
*/
|
|
455
|
+
async removeUserFromGroup(email: string, group: string, userPoolId: string): Promise<void> {
|
|
456
|
+
const user = new CognitoUser(await this.getUserByEmail(email, userPoolId));
|
|
457
|
+
|
|
458
|
+
const command = new CognitoIP.AdminRemoveUserFromGroupCommand({
|
|
459
|
+
UserPoolId: userPoolId,
|
|
460
|
+
GroupName: group,
|
|
461
|
+
Username: user.userId
|
|
462
|
+
});
|
|
463
|
+
await this.cognito.send(command);
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
/**
|
|
468
|
+
* The attributes of a generic Cognito user of which we don't know the custom attributes.
|
|
469
|
+
*/
|
|
470
|
+
export interface CognitoUserGeneric {
|
|
471
|
+
/**
|
|
472
|
+
* The user id (sub).
|
|
473
|
+
*/
|
|
474
|
+
userId: string;
|
|
475
|
+
/**
|
|
476
|
+
* The email (=== username).
|
|
477
|
+
*/
|
|
478
|
+
email: string;
|
|
479
|
+
/**
|
|
480
|
+
* Cognito can have custom attributes.
|
|
481
|
+
*/
|
|
482
|
+
[attribute: string]: string;
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
/**
|
|
486
|
+
* Options when creating a new user.
|
|
487
|
+
*/
|
|
488
|
+
export interface CreateUserOptions {
|
|
489
|
+
/**
|
|
490
|
+
* Uf true, don't send the default Cognito email notification
|
|
491
|
+
*/
|
|
492
|
+
skipNotification?: boolean;
|
|
493
|
+
/**
|
|
494
|
+
* If null, randomly generated
|
|
495
|
+
*/
|
|
496
|
+
temporaryPassword?: string;
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
/**
|
|
500
|
+
* The attributes of a Cognito group.
|
|
501
|
+
*/
|
|
502
|
+
export interface CognitoGroup {
|
|
503
|
+
/**
|
|
504
|
+
* The name (and id) of the group.
|
|
505
|
+
*/
|
|
506
|
+
name: string;
|
|
507
|
+
/**
|
|
508
|
+
* The description of the group.
|
|
509
|
+
*/
|
|
510
|
+
description: string;
|
|
511
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import * as AmazonComprehend from '@aws-sdk/client-comprehend';
|
|
2
|
+
import { Sentiment } from 'idea-toolbox';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* A wrapper for Amazon Comprehend.
|
|
6
|
+
*/
|
|
7
|
+
export class Comprehend {
|
|
8
|
+
protected comprehend: AmazonComprehend.ComprehendClient;
|
|
9
|
+
|
|
10
|
+
constructor(options: { region?: string } = {}) {
|
|
11
|
+
this.comprehend = new AmazonComprehend.ComprehendClient({ region: options.region });
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Inspects text and returns an inference of the prevailing sentiment (POSITIVE, NEUTRAL, MIXED, or NEGATIVE).
|
|
16
|
+
*/
|
|
17
|
+
async detectSentiment(params: DetectSentimentParameters): Promise<Sentiment> {
|
|
18
|
+
if (!params.language || !params.text) throw new Error('Missing some parameters');
|
|
19
|
+
|
|
20
|
+
const command = new AmazonComprehend.DetectSentimentCommand({ LanguageCode: params.language, Text: params.text });
|
|
21
|
+
const { Sentiment } = await this.comprehend.send(command);
|
|
22
|
+
|
|
23
|
+
return Sentiment as Sentiment;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Determines the dominant language of the input text.
|
|
28
|
+
*/
|
|
29
|
+
async detectDominantLanguage(params: { text: string }): Promise<string> {
|
|
30
|
+
if (!params.text) throw new Error('Missing text');
|
|
31
|
+
|
|
32
|
+
const command = new AmazonComprehend.DetectDominantLanguageCommand({ Text: params.text });
|
|
33
|
+
const { Languages } = await this.comprehend.send(command);
|
|
34
|
+
if (!Languages.length) throw new Error('Not found');
|
|
35
|
+
|
|
36
|
+
return Languages[0].LanguageCode;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export interface DetectSentimentParameters {
|
|
41
|
+
/**
|
|
42
|
+
* The language of the input contents. You can specify any of the primary languages supported by Amazon Comprehend.
|
|
43
|
+
* All contents must be in the same language. Required.
|
|
44
|
+
* Valid Values: en | es | fr | de | it | pt | ar | hi | ja | ko | zh | zh-TW
|
|
45
|
+
*/
|
|
46
|
+
language: string;
|
|
47
|
+
/**
|
|
48
|
+
* The text to analyze. Required.
|
|
49
|
+
* A UTF-8 text string. Each string must contain fewer that 5,000 bytes of UTF-8 encoded characters.
|
|
50
|
+
*/
|
|
51
|
+
text: string;
|
|
52
|
+
}
|