@iyio/aws-credential-providers 0.6.5 → 0.7.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/esm/src/lib/CognitoAuthProvider.js +309 -355
- package/esm/src/lib/CognitoAuthProvider.js.map +1 -1
- package/esm/src/lib/CognitoJwtValidator.js +22 -29
- package/esm/src/lib/CognitoJwtValidator.js.map +1 -1
- package/esm/src/lib/cognito-test-helper.js +5 -6
- package/esm/src/lib/cognito-test-helper.js.map +1 -1
- package/package.json +3 -3
- package/src/lib/CognitoAuthProvider.js +309 -355
- package/src/lib/CognitoAuthProvider.js.map +1 -1
- package/src/lib/CognitoJwtValidator.js +22 -29
- package/src/lib/CognitoJwtValidator.js.map +1 -1
- package/src/lib/cognito-test-helper.js +5 -6
- package/src/lib/cognito-test-helper.js.map +1 -1
|
@@ -1,14 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var _a;
|
|
3
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
3
|
exports.CognitoAuthProvider = exports.CognitoAuthProviderType = exports._allIssuedCognitoCreds = void 0;
|
|
5
|
-
const tslib_1 = require("tslib");
|
|
6
4
|
const credential_providers_1 = require("@aws-sdk/credential-providers");
|
|
7
5
|
const aws_1 = require("@iyio/aws");
|
|
8
6
|
const common_1 = require("@iyio/common");
|
|
9
7
|
const amazon_cognito_identity_js_1 = require("amazon-cognito-identity-js");
|
|
10
8
|
const _types_aws_credential_providers_1 = require("./_types.aws-credential-providers");
|
|
11
|
-
const trackIssuedCreds = (0, common_1.parseConfigBool)(
|
|
9
|
+
const trackIssuedCreds = (0, common_1.parseConfigBool)(process.env['NX_TRACK_COGNITO_ISSUED_CREDS'] ?? process.env['NX_PUBLIC_TRACK_COGNITO_ISSUED_CREDS']);
|
|
12
10
|
/**
|
|
13
11
|
* Used for testing
|
|
14
12
|
*/
|
|
@@ -30,28 +28,27 @@ class CognitoAuthProvider {
|
|
|
30
28
|
constructor(config) {
|
|
31
29
|
this.type = exports.CognitoAuthProviderType;
|
|
32
30
|
this.unCreds = null;
|
|
33
|
-
this.getCredentials = () =>
|
|
34
|
-
var _a, _b;
|
|
31
|
+
this.getCredentials = async () => {
|
|
35
32
|
const user = this.currentUser.value;
|
|
36
33
|
if (!user) {
|
|
37
34
|
if (_types_aws_credential_providers_1.disableCognitoUnauthenticatedParam.get()) {
|
|
38
35
|
throw new Error('Unauthenticated cognito users disabled');
|
|
39
36
|
}
|
|
40
|
-
return
|
|
37
|
+
return await this.getUnauthenticatedCredentials()();
|
|
41
38
|
}
|
|
42
|
-
let session =
|
|
39
|
+
let session = user.providerData.providerData?.[sessionKey];
|
|
43
40
|
if (!session) {
|
|
44
41
|
if (_types_aws_credential_providers_1.disableCognitoUnauthenticatedParam.get()) {
|
|
45
42
|
throw new Error('Unauthenticated cognito users disabled');
|
|
46
43
|
}
|
|
47
|
-
return
|
|
44
|
+
return await this.getUnauthenticatedCredentials()();
|
|
48
45
|
}
|
|
49
46
|
if (!session.isValid()) {
|
|
50
47
|
console.info('getAuthProvider', 'refresh session');
|
|
51
|
-
const cognitoUser =
|
|
48
|
+
const cognitoUser = user.providerData.providerData?.[userKey];
|
|
52
49
|
if (cognitoUser) {
|
|
53
50
|
const refreshToken = session.getRefreshToken();
|
|
54
|
-
const newSession =
|
|
51
|
+
const newSession = await (0, common_1.promiseFromErrorResultCallback)(cb => cognitoUser.refreshSession(refreshToken, cb));
|
|
55
52
|
if (newSession) {
|
|
56
53
|
session = newSession;
|
|
57
54
|
if (user.providerData.providerData) {
|
|
@@ -73,8 +70,8 @@ class CognitoAuthProvider {
|
|
|
73
70
|
if (trackIssuedCreds) {
|
|
74
71
|
exports._allIssuedCognitoCreds.push(provider);
|
|
75
72
|
}
|
|
76
|
-
return
|
|
77
|
-
}
|
|
73
|
+
return await provider();
|
|
74
|
+
};
|
|
78
75
|
this.userPool = new amazon_cognito_identity_js_1.CognitoUserPool(config);
|
|
79
76
|
this.config = config;
|
|
80
77
|
this.currentUser = config.currentUser;
|
|
@@ -93,227 +90,200 @@ class CognitoAuthProvider {
|
|
|
93
90
|
getAuthProvider() {
|
|
94
91
|
return this.getCredentials;
|
|
95
92
|
}
|
|
96
|
-
getCurrentUser() {
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
return user !== null && user !== void 0 ? user : null;
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
catch (ex) {
|
|
106
|
-
console.error('User initialization failed', ex);
|
|
93
|
+
async getCurrentUser() {
|
|
94
|
+
try {
|
|
95
|
+
const cognitoUser = this.userPool.getCurrentUser();
|
|
96
|
+
if (cognitoUser) {
|
|
97
|
+
const user = await this.convertCognitoUserAsync(cognitoUser);
|
|
98
|
+
return user ?? null;
|
|
107
99
|
}
|
|
108
|
-
|
|
109
|
-
|
|
100
|
+
}
|
|
101
|
+
catch (ex) {
|
|
102
|
+
console.error('User initialization failed', ex);
|
|
103
|
+
}
|
|
104
|
+
return null;
|
|
110
105
|
}
|
|
111
|
-
convertCognitoUserAsync(user) {
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
if (newSession) {
|
|
120
|
-
session = newSession;
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
const atts = yield (0, common_1.promiseFromErrorResultCallback)(cb => user.getUserAttributes(cb));
|
|
124
|
-
const id = user.getUsername();
|
|
125
|
-
const data = {};
|
|
126
|
-
if (atts) {
|
|
127
|
-
for (const att of atts) {
|
|
128
|
-
data[att.Name] = att.Value;
|
|
129
|
-
}
|
|
106
|
+
async convertCognitoUserAsync(user) {
|
|
107
|
+
let session = await (0, common_1.promiseFromErrorResultCallback)(cb => user.getSession(cb));
|
|
108
|
+
if (session) {
|
|
109
|
+
if (!session.isValid()) {
|
|
110
|
+
const refreshToken = session.getRefreshToken();
|
|
111
|
+
const newSession = await (0, common_1.promiseFromErrorResultCallback)(cb => user.refreshSession(refreshToken, cb));
|
|
112
|
+
if (newSession) {
|
|
113
|
+
session = newSession;
|
|
130
114
|
}
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
phoneNumber,
|
|
139
|
-
data,
|
|
140
|
-
providerData: {
|
|
141
|
-
type: this.type,
|
|
142
|
-
userId: id,
|
|
143
|
-
providerData: {
|
|
144
|
-
[sessionKey]: session,
|
|
145
|
-
[userKey]: user
|
|
146
|
-
}
|
|
147
|
-
},
|
|
148
|
-
provider: this
|
|
149
|
-
};
|
|
150
|
-
const baseUser = this.config.userFactory.generate(options);
|
|
151
|
-
if (!baseUser) {
|
|
152
|
-
throw new Error('No user factory provided a user');
|
|
115
|
+
}
|
|
116
|
+
const atts = await (0, common_1.promiseFromErrorResultCallback)(cb => user.getUserAttributes(cb));
|
|
117
|
+
const id = user.getUsername();
|
|
118
|
+
const data = {};
|
|
119
|
+
if (atts) {
|
|
120
|
+
for (const att of atts) {
|
|
121
|
+
data[att.Name] = att.Value;
|
|
153
122
|
}
|
|
154
|
-
return baseUser;
|
|
155
123
|
}
|
|
156
|
-
|
|
157
|
-
|
|
124
|
+
const name = data['nickname'] ?? id;
|
|
125
|
+
const email = data['email'];
|
|
126
|
+
const phoneNumber = data['phone'] ?? data['phone_number'] ?? data['phoneNumber'];
|
|
127
|
+
const options = {
|
|
128
|
+
id,
|
|
129
|
+
name,
|
|
130
|
+
email,
|
|
131
|
+
phoneNumber,
|
|
132
|
+
data,
|
|
133
|
+
providerData: {
|
|
134
|
+
type: this.type,
|
|
135
|
+
userId: id,
|
|
136
|
+
providerData: {
|
|
137
|
+
[sessionKey]: session,
|
|
138
|
+
[userKey]: user
|
|
139
|
+
}
|
|
140
|
+
},
|
|
141
|
+
provider: this
|
|
142
|
+
};
|
|
143
|
+
const baseUser = this.config.userFactory.generate(options);
|
|
144
|
+
if (!baseUser) {
|
|
145
|
+
throw new Error('No user factory provided a user');
|
|
158
146
|
}
|
|
159
|
-
|
|
147
|
+
return baseUser;
|
|
148
|
+
}
|
|
149
|
+
else {
|
|
150
|
+
throw new Error('Unable to get user session');
|
|
151
|
+
}
|
|
160
152
|
}
|
|
161
|
-
getIdentitiesAsync(user) {
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
return undefined;
|
|
167
|
-
}
|
|
168
|
-
const idAtt = atts.find(a => a.Name === 'identities');
|
|
169
|
-
if (!idAtt) {
|
|
170
|
-
return undefined;
|
|
171
|
-
}
|
|
172
|
-
try {
|
|
173
|
-
const identities = JSON.parse(idAtt.Value);
|
|
174
|
-
return identities !== null && identities !== void 0 ? identities : ((typeof identities === 'object') ? identities : undefined);
|
|
175
|
-
}
|
|
176
|
-
catch (_a) {
|
|
177
|
-
return undefined;
|
|
178
|
-
}
|
|
153
|
+
async getIdentitiesAsync(user) {
|
|
154
|
+
try {
|
|
155
|
+
const atts = await (0, common_1.promiseFromErrorResultCallback)(cb => user.getUserAttributes(cb));
|
|
156
|
+
if (!atts) {
|
|
157
|
+
return undefined;
|
|
179
158
|
}
|
|
180
|
-
|
|
159
|
+
const idAtt = atts.find(a => a.Name === 'identities');
|
|
160
|
+
if (!idAtt) {
|
|
181
161
|
return undefined;
|
|
182
162
|
}
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
187
|
-
var _a;
|
|
188
|
-
const user = (_a = data.providerData) === null || _a === void 0 ? void 0 : _a[userKey];
|
|
189
|
-
if (!user) {
|
|
190
|
-
if (!data.userId) {
|
|
191
|
-
return null;
|
|
192
|
-
}
|
|
193
|
-
const currentUser = yield this.getCurrentUser();
|
|
194
|
-
return (currentUser === null || currentUser === void 0 ? void 0 : currentUser.id) === data.userId ? currentUser : null;
|
|
163
|
+
try {
|
|
164
|
+
const identities = JSON.parse(idAtt.Value);
|
|
165
|
+
return identities ?? ((typeof identities === 'object') ? identities : undefined);
|
|
195
166
|
}
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
}
|
|
199
|
-
deleteAsync(user) {
|
|
200
|
-
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
201
|
-
var _a;
|
|
202
|
-
const cUser = (_a = user.providerData.providerData) === null || _a === void 0 ? void 0 : _a[userKey];
|
|
203
|
-
if (!cUser) {
|
|
204
|
-
return;
|
|
167
|
+
catch {
|
|
168
|
+
return undefined;
|
|
205
169
|
}
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
status: 'error',
|
|
211
|
-
message: 'Delete user failed',
|
|
212
|
-
error: err
|
|
213
|
-
});
|
|
214
|
-
}
|
|
215
|
-
else {
|
|
216
|
-
resolve({
|
|
217
|
-
status: 'success'
|
|
218
|
-
});
|
|
219
|
-
}
|
|
220
|
-
});
|
|
221
|
-
});
|
|
222
|
-
});
|
|
170
|
+
}
|
|
171
|
+
catch {
|
|
172
|
+
return undefined;
|
|
173
|
+
}
|
|
223
174
|
}
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
return;
|
|
175
|
+
async getUserAsync(data) {
|
|
176
|
+
const user = data.providerData?.[userKey];
|
|
177
|
+
if (!user) {
|
|
178
|
+
if (!data.userId) {
|
|
179
|
+
return null;
|
|
230
180
|
}
|
|
231
|
-
const
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
181
|
+
const currentUser = await this.getCurrentUser();
|
|
182
|
+
return currentUser?.id === data.userId ? currentUser : null;
|
|
183
|
+
}
|
|
184
|
+
return await this.convertCognitoUserAsync(user);
|
|
185
|
+
}
|
|
186
|
+
async deleteAsync(user) {
|
|
187
|
+
const cUser = user.providerData.providerData?.[userKey];
|
|
188
|
+
if (!cUser) {
|
|
189
|
+
return;
|
|
190
|
+
}
|
|
191
|
+
return await new Promise((resolve) => {
|
|
192
|
+
cUser.deleteUser((err) => {
|
|
193
|
+
if (err) {
|
|
194
|
+
resolve({
|
|
195
|
+
status: 'error',
|
|
196
|
+
message: 'Delete user failed',
|
|
197
|
+
error: err
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
else {
|
|
201
|
+
resolve({
|
|
202
|
+
status: 'success'
|
|
241
203
|
});
|
|
242
204
|
}
|
|
243
|
-
}
|
|
244
|
-
yield new Promise((resolve) => {
|
|
245
|
-
cUser.signOut(() => resolve());
|
|
246
205
|
});
|
|
247
206
|
});
|
|
248
207
|
}
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
return
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
208
|
+
async signOutAsync(user) {
|
|
209
|
+
const cUser = user.providerData.providerData?.[userKey];
|
|
210
|
+
if (!cUser) {
|
|
211
|
+
return;
|
|
212
|
+
}
|
|
213
|
+
const prefix = _types_aws_credential_providers_1.cognitoDomainPrefixParam.get();
|
|
214
|
+
if (prefix) {
|
|
215
|
+
const ids = await this.getIdentitiesAsync(cUser);
|
|
216
|
+
if (ids) {
|
|
217
|
+
const url = (`https://${prefix}.auth.${(0, aws_1.awsRegionParam)()}.amazoncognito.com/logout?client_id=${(0, _types_aws_credential_providers_1.cognitoUserPoolClientIdParam)()}&logout_uri=${this.getOAuthRedirectUri()}`);
|
|
218
|
+
await (0, common_1.getPopupWindowMessageAsync)({
|
|
219
|
+
url,
|
|
220
|
+
triggerOnSameHost: true,
|
|
221
|
+
timeoutMs: 30000,
|
|
222
|
+
messageId: common_1.authServicePopupOAuthSignOutMessageId
|
|
223
|
+
});
|
|
260
224
|
}
|
|
261
|
-
|
|
225
|
+
}
|
|
226
|
+
await new Promise((resolve) => {
|
|
227
|
+
cUser.signOut(() => resolve());
|
|
262
228
|
});
|
|
263
229
|
}
|
|
264
|
-
|
|
265
|
-
return
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
});
|
|
285
|
-
cognitoUser.setSignInUserSession(session);
|
|
286
|
-
const user = yield this.convertCognitoUserAsync(cognitoUser);
|
|
230
|
+
async getJwtAsync(data) {
|
|
231
|
+
return data.providerData?.[sessionKey]?.getIdToken()?.getJwtToken() ?? null;
|
|
232
|
+
}
|
|
233
|
+
async getClaimsAsync(data) {
|
|
234
|
+
const jwt = await this.getJwtAsync(data);
|
|
235
|
+
if (!jwt) {
|
|
236
|
+
return null;
|
|
237
|
+
}
|
|
238
|
+
return (0, common_1.parseJwt)(jwt);
|
|
239
|
+
}
|
|
240
|
+
async signInWithTokenAsync(token) {
|
|
241
|
+
const idToken = token?.IdToken ?? token?.id_token;
|
|
242
|
+
const accessToken = token?.AccessToken ?? token?.access_token;
|
|
243
|
+
const refreshToken = token?.RefreshToken ?? token?.refresh_token;
|
|
244
|
+
const session = new amazon_cognito_identity_js_1.CognitoUserSession({
|
|
245
|
+
IdToken: (typeof idToken === 'string') ? new amazon_cognito_identity_js_1.CognitoIdToken({ IdToken: idToken }) : idToken,
|
|
246
|
+
AccessToken: (typeof accessToken === 'string') ? new amazon_cognito_identity_js_1.CognitoAccessToken({ AccessToken: accessToken }) : accessToken,
|
|
247
|
+
RefreshToken: (typeof refreshToken === 'string') ? new amazon_cognito_identity_js_1.CognitoRefreshToken({ RefreshToken: refreshToken }) : refreshToken,
|
|
248
|
+
});
|
|
249
|
+
if (!session.isValid()) {
|
|
287
250
|
return {
|
|
288
|
-
success:
|
|
289
|
-
|
|
251
|
+
success: false,
|
|
252
|
+
message: 'Invalid session from token'
|
|
290
253
|
};
|
|
254
|
+
}
|
|
255
|
+
const cognitoUser = new amazon_cognito_identity_js_1.CognitoUser({
|
|
256
|
+
Username: session.getAccessToken().payload['username'] ?? 'user',
|
|
257
|
+
Pool: this.userPool,
|
|
291
258
|
});
|
|
259
|
+
cognitoUser.setSignInUserSession(session);
|
|
260
|
+
const user = await this.convertCognitoUserAsync(cognitoUser);
|
|
261
|
+
return {
|
|
262
|
+
success: true,
|
|
263
|
+
user,
|
|
264
|
+
};
|
|
292
265
|
}
|
|
293
266
|
getOAuthRedirectUri() {
|
|
294
|
-
var _a, _b, _c, _d;
|
|
295
267
|
let redirectUri = (0, _types_aws_credential_providers_1.cognitoOAuthRedirectUriParam)();
|
|
296
268
|
if (!(0, common_1.getUriProtocol)(redirectUri)) {
|
|
297
269
|
if (redirectUri.startsWith('/')) {
|
|
298
270
|
redirectUri = redirectUri.substring(1);
|
|
299
271
|
}
|
|
300
|
-
redirectUri = `${
|
|
272
|
+
redirectUri = `${globalThis.location?.protocol ?? 'https:'}//${globalThis.location?.host ?? 'localhost'}/${redirectUri}`;
|
|
301
273
|
}
|
|
302
274
|
return redirectUri;
|
|
303
275
|
}
|
|
304
|
-
signInWithCodeAsync(code) {
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
noAuth: true,
|
|
314
|
-
});
|
|
315
|
-
return yield this.signInWithTokenAsync(token);
|
|
276
|
+
async signInWithCodeAsync(code) {
|
|
277
|
+
const token = await (0, common_1.httpClient)().postAsync(`https://${(0, _types_aws_credential_providers_1.cognitoDomainPrefixParam)()}.auth.${(0, aws_1.awsRegionParam)()}.amazoncognito.com/oauth2/token`, {
|
|
278
|
+
code,
|
|
279
|
+
client_id: (0, _types_aws_credential_providers_1.cognitoUserPoolClientIdParam)(),
|
|
280
|
+
grant_type: 'authorization_code',
|
|
281
|
+
redirect_uri: this.getOAuthRedirectUri(),
|
|
282
|
+
}, {
|
|
283
|
+
urlEncodeBody: true,
|
|
284
|
+
noAuth: true,
|
|
316
285
|
});
|
|
286
|
+
return await this.signInWithTokenAsync(token);
|
|
317
287
|
}
|
|
318
288
|
getOAuthSignInLinkUrl(provider, options) {
|
|
319
289
|
const prefix = _types_aws_credential_providers_1.cognitoDomainPrefixParam.get();
|
|
@@ -321,109 +291,104 @@ class CognitoAuthProvider {
|
|
|
321
291
|
return undefined;
|
|
322
292
|
}
|
|
323
293
|
const scopes = (typeof options['scopes'] === 'string') ? options['scopes'] : undefined;
|
|
324
|
-
return (`https://${prefix}.auth.${(0, aws_1.awsRegionParam)()}.amazoncognito.com/oauth2/authorize?client_id=${(0, _types_aws_credential_providers_1.cognitoUserPoolClientIdParam)()}&response_type=code&scope=${encodeURIComponent((scopes
|
|
294
|
+
return (`https://${prefix}.auth.${(0, aws_1.awsRegionParam)()}.amazoncognito.com/oauth2/authorize?client_id=${(0, _types_aws_credential_providers_1.cognitoUserPoolClientIdParam)()}&response_type=code&scope=${encodeURIComponent((scopes ?? (0, _types_aws_credential_providers_1.cognitoOAuthScopesParams)()).split(',').map(s => s.trim()).join(' '))}&redirect_uri=${encodeURIComponent(this.getOAuthRedirectUri())}`);
|
|
325
295
|
}
|
|
326
|
-
signInEmailPasswordAsync(email, password, newPassword) {
|
|
327
|
-
return
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
if (confirmationNeeded) {
|
|
340
|
-
resolve({
|
|
341
|
-
success: false,
|
|
342
|
-
message: 'Confirmation required'
|
|
343
|
-
});
|
|
344
|
-
return;
|
|
345
|
-
}
|
|
346
|
-
const user = yield this.convertCognitoUserAsync(cognitoUser);
|
|
296
|
+
async signInEmailPasswordAsync(email, password, newPassword) {
|
|
297
|
+
return new Promise((resolve) => {
|
|
298
|
+
const details = new amazon_cognito_identity_js_1.AuthenticationDetails({
|
|
299
|
+
Username: email,
|
|
300
|
+
Password: password
|
|
301
|
+
});
|
|
302
|
+
const cognitoUser = new amazon_cognito_identity_js_1.CognitoUser({
|
|
303
|
+
Username: email,
|
|
304
|
+
Pool: this.userPool,
|
|
305
|
+
});
|
|
306
|
+
const callbacks = {
|
|
307
|
+
onSuccess: async (session, confirmationNeeded) => {
|
|
308
|
+
if (confirmationNeeded) {
|
|
347
309
|
resolve({
|
|
348
|
-
success:
|
|
349
|
-
|
|
310
|
+
success: false,
|
|
311
|
+
message: 'Confirmation required'
|
|
350
312
|
});
|
|
351
|
-
|
|
352
|
-
|
|
313
|
+
return;
|
|
314
|
+
}
|
|
315
|
+
const user = await this.convertCognitoUserAsync(cognitoUser);
|
|
316
|
+
resolve({
|
|
317
|
+
success: true,
|
|
318
|
+
user
|
|
319
|
+
});
|
|
320
|
+
},
|
|
321
|
+
onFailure: (err) => {
|
|
322
|
+
resolve({
|
|
323
|
+
success: false,
|
|
324
|
+
message: 'Sign-in failed',
|
|
325
|
+
error: err
|
|
326
|
+
});
|
|
327
|
+
},
|
|
328
|
+
newPasswordRequired: () => {
|
|
329
|
+
if (newPassword) {
|
|
330
|
+
cognitoUser.completeNewPasswordChallenge(newPassword, {}, callbacks);
|
|
331
|
+
}
|
|
332
|
+
else {
|
|
353
333
|
resolve({
|
|
354
334
|
success: false,
|
|
355
|
-
message: '
|
|
356
|
-
error: err
|
|
335
|
+
message: 'New password required'
|
|
357
336
|
});
|
|
358
|
-
},
|
|
359
|
-
newPasswordRequired: () => {
|
|
360
|
-
if (newPassword) {
|
|
361
|
-
cognitoUser.completeNewPasswordChallenge(newPassword, {}, callbacks);
|
|
362
|
-
}
|
|
363
|
-
else {
|
|
364
|
-
resolve({
|
|
365
|
-
success: false,
|
|
366
|
-
message: 'New password required'
|
|
367
|
-
});
|
|
368
|
-
}
|
|
369
337
|
}
|
|
370
|
-
}
|
|
371
|
-
|
|
372
|
-
|
|
338
|
+
}
|
|
339
|
+
};
|
|
340
|
+
cognitoUser.authenticateUser(details, callbacks);
|
|
373
341
|
});
|
|
374
342
|
}
|
|
375
|
-
registerEmailPasswordAsync(email, password, userData) {
|
|
376
|
-
return
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
Value: (_b = (_a = userData[e]) === null || _a === void 0 ? void 0 : _a.toString()) !== null && _b !== void 0 ? _b : '',
|
|
391
|
-
}));
|
|
392
|
-
}
|
|
343
|
+
async registerEmailPasswordAsync(email, password, userData) {
|
|
344
|
+
return new Promise((resolve) => {
|
|
345
|
+
const atts = [
|
|
346
|
+
new amazon_cognito_identity_js_1.CognitoUserAttribute({
|
|
347
|
+
Name: 'email',
|
|
348
|
+
Value: email
|
|
349
|
+
})
|
|
350
|
+
];
|
|
351
|
+
if (userData) {
|
|
352
|
+
for (const e in userData) {
|
|
353
|
+
if (!atts.find(a => a.Name === e)) {
|
|
354
|
+
atts.push(new amazon_cognito_identity_js_1.CognitoUserAttribute({
|
|
355
|
+
Name: e,
|
|
356
|
+
Value: userData[e]?.toString() ?? '',
|
|
357
|
+
}));
|
|
393
358
|
}
|
|
394
359
|
}
|
|
395
|
-
|
|
396
|
-
|
|
360
|
+
}
|
|
361
|
+
this.userPool.signUp(email, password, atts, [], async (err, result) => {
|
|
362
|
+
if (err || !result?.user) {
|
|
363
|
+
resolve({
|
|
364
|
+
status: 'error',
|
|
365
|
+
message: 'Register failed',
|
|
366
|
+
error: err
|
|
367
|
+
});
|
|
368
|
+
}
|
|
369
|
+
else if (result.userConfirmed) {
|
|
370
|
+
const result = await this.signInEmailPasswordAsync(email, password);
|
|
371
|
+
if (result.success) {
|
|
397
372
|
resolve({
|
|
398
|
-
status: '
|
|
399
|
-
|
|
400
|
-
error: err
|
|
373
|
+
status: 'success',
|
|
374
|
+
user: result.user
|
|
401
375
|
});
|
|
402
376
|
}
|
|
403
|
-
else if (result.userConfirmed) {
|
|
404
|
-
const result = yield this.signInEmailPasswordAsync(email, password);
|
|
405
|
-
if (result.success) {
|
|
406
|
-
resolve({
|
|
407
|
-
status: 'success',
|
|
408
|
-
user: result.user
|
|
409
|
-
});
|
|
410
|
-
}
|
|
411
|
-
else {
|
|
412
|
-
resolve({
|
|
413
|
-
status: 'error',
|
|
414
|
-
message: result.success === false ? result.message : 'error',
|
|
415
|
-
error: result.success === false ? result.error : null
|
|
416
|
-
});
|
|
417
|
-
}
|
|
418
|
-
}
|
|
419
377
|
else {
|
|
420
378
|
resolve({
|
|
421
|
-
status: '
|
|
422
|
-
message:
|
|
423
|
-
|
|
379
|
+
status: 'error',
|
|
380
|
+
message: result.success === false ? result.message : 'error',
|
|
381
|
+
error: result.success === false ? result.error : null
|
|
424
382
|
});
|
|
425
383
|
}
|
|
426
|
-
}
|
|
384
|
+
}
|
|
385
|
+
else {
|
|
386
|
+
resolve({
|
|
387
|
+
status: 'verificationRequired',
|
|
388
|
+
message: `A verification code has been sent to ${email}`,
|
|
389
|
+
verificationDestination: email
|
|
390
|
+
});
|
|
391
|
+
}
|
|
427
392
|
});
|
|
428
393
|
});
|
|
429
394
|
}
|
|
@@ -464,90 +429,79 @@ class CognitoAuthProvider {
|
|
|
464
429
|
});
|
|
465
430
|
});
|
|
466
431
|
}
|
|
467
|
-
updateAsync(user, update) {
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
if (!atts.some(a => a.Name === name)) {
|
|
477
|
-
atts.push({ Name: name, Value: value });
|
|
478
|
-
}
|
|
479
|
-
};
|
|
480
|
-
if (update.name) {
|
|
481
|
-
add('nickname', update.name);
|
|
482
|
-
}
|
|
483
|
-
if (update.data) {
|
|
484
|
-
for (const e in update.data) {
|
|
485
|
-
add(e, update.data[e]);
|
|
486
|
-
}
|
|
487
|
-
}
|
|
488
|
-
if (!atts.length) {
|
|
489
|
-
return false;
|
|
490
|
-
}
|
|
491
|
-
yield (0, common_1.promiseFromErrorResultCallback)(cb => cUser.updateAttributes(atts, cb));
|
|
492
|
-
return true;
|
|
493
|
-
});
|
|
494
|
-
}
|
|
495
|
-
resetPasswordAsync(identity) {
|
|
496
|
-
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
497
|
-
try {
|
|
498
|
-
const cognitoUser = new amazon_cognito_identity_js_1.CognitoUser({
|
|
499
|
-
Username: identity,
|
|
500
|
-
Pool: this.userPool,
|
|
501
|
-
});
|
|
502
|
-
const { codeSentTo } = yield (0, common_1.promiseFromErrorResultCallback)(cb => cognitoUser.forgotPassword({
|
|
503
|
-
onSuccess: () => { },
|
|
504
|
-
onFailure: err => cb(err, undefined),
|
|
505
|
-
inputVerificationCode: code => {
|
|
506
|
-
var _a;
|
|
507
|
-
cb(null, {
|
|
508
|
-
codeSentTo: (_a = code === null || code === void 0 ? void 0 : code.CodeDeliveryDetails) === null || _a === void 0 ? void 0 : _a.Destination
|
|
509
|
-
});
|
|
510
|
-
}
|
|
511
|
-
}));
|
|
512
|
-
return {
|
|
513
|
-
codeSent: true,
|
|
514
|
-
codeSentTo
|
|
515
|
-
};
|
|
432
|
+
async updateAsync(user, update) {
|
|
433
|
+
const cUser = user.providerData.providerData?.[userKey];
|
|
434
|
+
if (!cUser) {
|
|
435
|
+
return false;
|
|
436
|
+
}
|
|
437
|
+
const atts = [];
|
|
438
|
+
const add = (name, value) => {
|
|
439
|
+
if (!atts.some(a => a.Name === name)) {
|
|
440
|
+
atts.push({ Name: name, Value: value });
|
|
516
441
|
}
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
442
|
+
};
|
|
443
|
+
if (update.name) {
|
|
444
|
+
add('nickname', update.name);
|
|
445
|
+
}
|
|
446
|
+
if (update.data) {
|
|
447
|
+
for (const e in update.data) {
|
|
448
|
+
add(e, update.data[e]);
|
|
522
449
|
}
|
|
523
|
-
}
|
|
450
|
+
}
|
|
451
|
+
if (!atts.length) {
|
|
452
|
+
return false;
|
|
453
|
+
}
|
|
454
|
+
await (0, common_1.promiseFromErrorResultCallback)(cb => cUser.updateAttributes(atts, cb));
|
|
455
|
+
return true;
|
|
524
456
|
}
|
|
525
|
-
|
|
526
|
-
|
|
457
|
+
async resetPasswordAsync(identity) {
|
|
458
|
+
try {
|
|
527
459
|
const cognitoUser = new amazon_cognito_identity_js_1.CognitoUser({
|
|
528
460
|
Username: identity,
|
|
529
461
|
Pool: this.userPool,
|
|
530
462
|
});
|
|
531
|
-
|
|
532
|
-
onSuccess: () =>
|
|
463
|
+
const { codeSentTo } = await (0, common_1.promiseFromErrorResultCallback)(cb => cognitoUser.forgotPassword({
|
|
464
|
+
onSuccess: () => { },
|
|
533
465
|
onFailure: err => cb(err, undefined),
|
|
466
|
+
inputVerificationCode: code => {
|
|
467
|
+
cb(null, {
|
|
468
|
+
codeSentTo: code?.CodeDeliveryDetails?.Destination
|
|
469
|
+
});
|
|
470
|
+
}
|
|
534
471
|
}));
|
|
535
|
-
|
|
472
|
+
return {
|
|
473
|
+
codeSent: true,
|
|
474
|
+
codeSentTo
|
|
475
|
+
};
|
|
476
|
+
}
|
|
477
|
+
catch (ex) {
|
|
478
|
+
return {
|
|
479
|
+
error: ex,
|
|
480
|
+
codeSent: false
|
|
481
|
+
};
|
|
482
|
+
}
|
|
536
483
|
}
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
if (!cUser) {
|
|
542
|
-
return false;
|
|
543
|
-
}
|
|
544
|
-
const session = yield (0, common_1.promiseFromErrorResultCallback)(cb => cUser.getSession(cb));
|
|
545
|
-
if (!session) {
|
|
546
|
-
return false;
|
|
547
|
-
}
|
|
548
|
-
yield (0, common_1.promiseFromErrorResultCallback)(cb => cUser.refreshSession(session.getRefreshToken(), cb));
|
|
549
|
-
return true;
|
|
484
|
+
async setNewPasswordAsync(identity, code, newPassword) {
|
|
485
|
+
const cognitoUser = new amazon_cognito_identity_js_1.CognitoUser({
|
|
486
|
+
Username: identity,
|
|
487
|
+
Pool: this.userPool,
|
|
550
488
|
});
|
|
489
|
+
return await (0, common_1.promiseFromErrorResultCallback)(cb => cognitoUser.confirmPassword(code, newPassword, {
|
|
490
|
+
onSuccess: () => cb(null, true),
|
|
491
|
+
onFailure: err => cb(err, undefined),
|
|
492
|
+
}));
|
|
493
|
+
}
|
|
494
|
+
async refreshTokenAsync(user) {
|
|
495
|
+
const cUser = user.providerData.providerData?.[userKey];
|
|
496
|
+
if (!cUser) {
|
|
497
|
+
return false;
|
|
498
|
+
}
|
|
499
|
+
const session = await (0, common_1.promiseFromErrorResultCallback)(cb => cUser.getSession(cb));
|
|
500
|
+
if (!session) {
|
|
501
|
+
return false;
|
|
502
|
+
}
|
|
503
|
+
await (0, common_1.promiseFromErrorResultCallback)(cb => cUser.refreshSession(session.getRefreshToken(), cb));
|
|
504
|
+
return true;
|
|
551
505
|
}
|
|
552
506
|
}
|
|
553
507
|
exports.CognitoAuthProvider = CognitoAuthProvider;
|