@aws-amplify/auth 5.6.0 → 5.6.1-api-v6.4d00072.0
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/internals/package.json +8 -0
- package/lib/Auth.d.ts +4 -81
- package/lib/Auth.js +45 -2252
- package/lib/Auth.js.map +1 -1
- package/lib/internals/InternalAuth.d.ts +342 -0
- package/lib/internals/InternalAuth.js +2553 -0
- package/lib/internals/InternalAuth.js.map +1 -0
- package/lib/internals/index.d.ts +1 -0
- package/lib/internals/index.js +7 -0
- package/lib/internals/index.js.map +1 -0
- package/lib-esm/Auth.d.ts +4 -81
- package/lib-esm/Auth.js +47 -2254
- package/lib-esm/Auth.js.map +1 -1
- package/lib-esm/internals/InternalAuth.d.ts +342 -0
- package/lib-esm/internals/InternalAuth.js +2551 -0
- package/lib-esm/internals/InternalAuth.js.map +1 -0
- package/lib-esm/internals/index.d.ts +1 -0
- package/lib-esm/internals/index.js +4 -0
- package/lib-esm/internals/index.js.map +1 -0
- package/package.json +8 -6
- package/src/Auth.ts +76 -2410
- package/src/internals/InternalAuth.ts +2901 -0
- package/src/internals/index.ts +3 -0
|
@@ -0,0 +1,2553 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
3
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
5
|
+
var tslib_1 = require("tslib");
|
|
6
|
+
var types_1 = require("../types");
|
|
7
|
+
var core_1 = require("@aws-amplify/core");
|
|
8
|
+
var amazon_cognito_identity_js_1 = require("amazon-cognito-identity-js");
|
|
9
|
+
var internals_1 = require("amazon-cognito-identity-js/internals");
|
|
10
|
+
var url_1 = require("url");
|
|
11
|
+
var OAuth_1 = tslib_1.__importDefault(require("../OAuth/OAuth"));
|
|
12
|
+
var urlListener_1 = tslib_1.__importDefault(require("../urlListener"));
|
|
13
|
+
var Errors_1 = require("../Errors");
|
|
14
|
+
var Auth_1 = require("../types/Auth");
|
|
15
|
+
var logger = new core_1.ConsoleLogger('AuthClass');
|
|
16
|
+
var USER_ADMIN_SCOPE = 'aws.cognito.signin.user.admin';
|
|
17
|
+
// 10 sec, following this guide https://www.nngroup.com/articles/response-times-3-important-limits/
|
|
18
|
+
var OAUTH_FLOW_MS_TIMEOUT = 10 * 1000;
|
|
19
|
+
var AMPLIFY_SYMBOL = (typeof Symbol !== 'undefined' && typeof Symbol.for === 'function'
|
|
20
|
+
? Symbol.for('amplify_default')
|
|
21
|
+
: '@@amplify_default');
|
|
22
|
+
var dispatchAuthEvent = function (event, data, message) {
|
|
23
|
+
core_1.Hub.dispatch('auth', { event: event, data: data, message: message }, 'Auth', AMPLIFY_SYMBOL);
|
|
24
|
+
};
|
|
25
|
+
// Cognito Documentation for max device
|
|
26
|
+
// tslint:disable-next-line:max-line-length
|
|
27
|
+
// https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_ListDevices.html#API_ListDevices_RequestSyntax
|
|
28
|
+
var MAX_DEVICES = 60;
|
|
29
|
+
var MAX_AUTOSIGNIN_POLLING_MS = 3 * 60 * 1000;
|
|
30
|
+
/**
|
|
31
|
+
* Provide authentication steps
|
|
32
|
+
*/
|
|
33
|
+
var InternalAuthClass = /** @class */ (function () {
|
|
34
|
+
/**
|
|
35
|
+
* Initialize Auth with AWS configurations
|
|
36
|
+
* @param {Object} config - Configuration of the Auth
|
|
37
|
+
*/
|
|
38
|
+
function InternalAuthClass(config) {
|
|
39
|
+
var _this = this;
|
|
40
|
+
this.userPool = null;
|
|
41
|
+
this.user = null;
|
|
42
|
+
this.oAuthFlowInProgress = false;
|
|
43
|
+
this.autoSignInInitiated = false;
|
|
44
|
+
this.inflightSessionPromise = null;
|
|
45
|
+
this.inflightSessionPromiseCounter = 0;
|
|
46
|
+
this.Credentials = core_1.Credentials;
|
|
47
|
+
this.wrapRefreshSessionCallback = function (callback) {
|
|
48
|
+
var wrapped = function (error, data) {
|
|
49
|
+
if (data) {
|
|
50
|
+
dispatchAuthEvent('tokenRefresh', undefined, "New token retrieved");
|
|
51
|
+
}
|
|
52
|
+
else {
|
|
53
|
+
dispatchAuthEvent('tokenRefresh_failure', error, "Failed to retrieve new token");
|
|
54
|
+
}
|
|
55
|
+
return callback(error, data);
|
|
56
|
+
};
|
|
57
|
+
return wrapped;
|
|
58
|
+
}; // prettier-ignore
|
|
59
|
+
this.configure(config);
|
|
60
|
+
this.currentCredentials = this.currentCredentials.bind(this);
|
|
61
|
+
this.currentUserCredentials = this.currentUserCredentials.bind(this);
|
|
62
|
+
core_1.Hub.listen('auth', function (_a) {
|
|
63
|
+
var payload = _a.payload;
|
|
64
|
+
var event = payload.event;
|
|
65
|
+
switch (event) {
|
|
66
|
+
case 'verify':
|
|
67
|
+
case 'signIn':
|
|
68
|
+
_this._storage.setItem('amplify-signin-with-hostedUI', 'false');
|
|
69
|
+
break;
|
|
70
|
+
case 'signOut':
|
|
71
|
+
_this._storage.removeItem('amplify-signin-with-hostedUI');
|
|
72
|
+
break;
|
|
73
|
+
case 'cognitoHostedUI':
|
|
74
|
+
_this._storage.setItem('amplify-signin-with-hostedUI', 'true');
|
|
75
|
+
break;
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
internals_1.addAuthCategoryToCognitoUserAgent();
|
|
79
|
+
internals_1.addFrameworkToCognitoUserAgent(core_1.Platform.framework);
|
|
80
|
+
core_1.Platform.observeFrameworkChanges(function () {
|
|
81
|
+
internals_1.addFrameworkToCognitoUserAgent(core_1.Platform.framework);
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
InternalAuthClass.prototype.getModuleName = function () {
|
|
85
|
+
return 'InternalAuth';
|
|
86
|
+
};
|
|
87
|
+
InternalAuthClass.prototype.configure = function (config) {
|
|
88
|
+
var _this = this;
|
|
89
|
+
if (!config)
|
|
90
|
+
return this._config || {};
|
|
91
|
+
logger.debug('configure Auth');
|
|
92
|
+
var conf = Object.assign({}, this._config, core_1.parseAWSExports(config).Auth, config);
|
|
93
|
+
this._config = conf;
|
|
94
|
+
var _a = this._config, userPoolId = _a.userPoolId, userPoolWebClientId = _a.userPoolWebClientId, cookieStorage = _a.cookieStorage, oauth = _a.oauth, region = _a.region, identityPoolId = _a.identityPoolId, mandatorySignIn = _a.mandatorySignIn, refreshHandlers = _a.refreshHandlers, identityPoolRegion = _a.identityPoolRegion, clientMetadata = _a.clientMetadata, endpoint = _a.endpoint, storage = _a.storage;
|
|
95
|
+
if (!storage) {
|
|
96
|
+
// backward compatability
|
|
97
|
+
if (cookieStorage)
|
|
98
|
+
this._storage = new amazon_cognito_identity_js_1.CookieStorage(cookieStorage);
|
|
99
|
+
else {
|
|
100
|
+
this._storage = config.ssr
|
|
101
|
+
? new core_1.UniversalStorage()
|
|
102
|
+
: new core_1.StorageHelper().getStorage();
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
else {
|
|
106
|
+
if (!this._isValidAuthStorage(storage)) {
|
|
107
|
+
logger.error('The storage in the Auth config is not valid!');
|
|
108
|
+
throw new Error('Empty storage object');
|
|
109
|
+
}
|
|
110
|
+
this._storage = storage;
|
|
111
|
+
}
|
|
112
|
+
this._storageSync = Promise.resolve();
|
|
113
|
+
if (typeof this._storage['sync'] === 'function') {
|
|
114
|
+
this._storageSync = this._storage['sync']();
|
|
115
|
+
}
|
|
116
|
+
if (userPoolId) {
|
|
117
|
+
var userPoolData = {
|
|
118
|
+
UserPoolId: userPoolId,
|
|
119
|
+
ClientId: userPoolWebClientId,
|
|
120
|
+
endpoint: endpoint,
|
|
121
|
+
};
|
|
122
|
+
userPoolData.Storage = this._storage;
|
|
123
|
+
this.userPool = new amazon_cognito_identity_js_1.CognitoUserPool(userPoolData, this.wrapRefreshSessionCallback);
|
|
124
|
+
}
|
|
125
|
+
this.Credentials.configure({
|
|
126
|
+
mandatorySignIn: mandatorySignIn,
|
|
127
|
+
region: region,
|
|
128
|
+
userPoolId: userPoolId,
|
|
129
|
+
identityPoolId: identityPoolId,
|
|
130
|
+
refreshHandlers: refreshHandlers,
|
|
131
|
+
storage: this._storage,
|
|
132
|
+
identityPoolRegion: identityPoolRegion,
|
|
133
|
+
});
|
|
134
|
+
// initialize cognitoauth client if hosted ui options provided
|
|
135
|
+
// to keep backward compatibility:
|
|
136
|
+
var cognitoHostedUIConfig = oauth
|
|
137
|
+
? types_1.isCognitoHostedOpts(this._config.oauth)
|
|
138
|
+
? oauth
|
|
139
|
+
: oauth.awsCognito
|
|
140
|
+
: undefined;
|
|
141
|
+
if (cognitoHostedUIConfig) {
|
|
142
|
+
var cognitoAuthParams = Object.assign({
|
|
143
|
+
cognitoClientId: userPoolWebClientId,
|
|
144
|
+
UserPoolId: userPoolId,
|
|
145
|
+
domain: cognitoHostedUIConfig['domain'],
|
|
146
|
+
scopes: cognitoHostedUIConfig['scope'],
|
|
147
|
+
redirectSignIn: cognitoHostedUIConfig['redirectSignIn'],
|
|
148
|
+
redirectSignOut: cognitoHostedUIConfig['redirectSignOut'],
|
|
149
|
+
responseType: cognitoHostedUIConfig['responseType'],
|
|
150
|
+
Storage: this._storage,
|
|
151
|
+
urlOpener: cognitoHostedUIConfig['urlOpener'],
|
|
152
|
+
clientMetadata: clientMetadata,
|
|
153
|
+
}, cognitoHostedUIConfig['options']);
|
|
154
|
+
this._oAuthHandler = new OAuth_1.default({
|
|
155
|
+
scopes: cognitoAuthParams.scopes,
|
|
156
|
+
config: cognitoAuthParams,
|
|
157
|
+
cognitoClientId: cognitoAuthParams.cognitoClientId,
|
|
158
|
+
});
|
|
159
|
+
// **NOTE** - Remove this in a future major release as it is a breaking change
|
|
160
|
+
// Prevents _handleAuthResponse from being called multiple times in Expo
|
|
161
|
+
// See https://github.com/aws-amplify/amplify-js/issues/4388
|
|
162
|
+
var usedResponseUrls_1 = {};
|
|
163
|
+
urlListener_1.default(function (_a) {
|
|
164
|
+
var url = _a.url;
|
|
165
|
+
if (usedResponseUrls_1[url]) {
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
usedResponseUrls_1[url] = true;
|
|
169
|
+
_this._handleAuthResponse(url);
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
dispatchAuthEvent('configured', null, "The Auth category has been configured successfully");
|
|
173
|
+
if (!this.autoSignInInitiated &&
|
|
174
|
+
typeof this._storage['getItem'] === 'function') {
|
|
175
|
+
var pollingInitiated = this.isTrueStorageValue('amplify-polling-started');
|
|
176
|
+
if (pollingInitiated) {
|
|
177
|
+
dispatchAuthEvent('autoSignIn_failure', null, Auth_1.AuthErrorTypes.AutoSignInError);
|
|
178
|
+
this._storage.removeItem('amplify-auto-sign-in');
|
|
179
|
+
}
|
|
180
|
+
this._storage.removeItem('amplify-polling-started');
|
|
181
|
+
}
|
|
182
|
+
return this._config;
|
|
183
|
+
};
|
|
184
|
+
InternalAuthClass.prototype.signUp = function (params, restOfAttrs, customUserAgentDetails) {
|
|
185
|
+
var _this = this;
|
|
186
|
+
var _a, _b, _c;
|
|
187
|
+
if (!this.userPool) {
|
|
188
|
+
return this.rejectNoUserPool();
|
|
189
|
+
}
|
|
190
|
+
var username = null;
|
|
191
|
+
var password = null;
|
|
192
|
+
var attributes = [];
|
|
193
|
+
var validationData = null;
|
|
194
|
+
var clientMetadata;
|
|
195
|
+
var autoSignIn = { enabled: false };
|
|
196
|
+
var autoSignInValidationData = {};
|
|
197
|
+
var autoSignInClientMetaData = {};
|
|
198
|
+
if (params && typeof params === 'string') {
|
|
199
|
+
username = params;
|
|
200
|
+
password = restOfAttrs ? restOfAttrs[0] : null;
|
|
201
|
+
var email = restOfAttrs ? restOfAttrs[1] : null;
|
|
202
|
+
var phone_number = restOfAttrs ? restOfAttrs[2] : null;
|
|
203
|
+
if (email)
|
|
204
|
+
attributes.push(new amazon_cognito_identity_js_1.CognitoUserAttribute({ Name: 'email', Value: email }));
|
|
205
|
+
if (phone_number)
|
|
206
|
+
attributes.push(new amazon_cognito_identity_js_1.CognitoUserAttribute({
|
|
207
|
+
Name: 'phone_number',
|
|
208
|
+
Value: phone_number,
|
|
209
|
+
}));
|
|
210
|
+
}
|
|
211
|
+
else if (params && typeof params === 'object') {
|
|
212
|
+
username = params['username'];
|
|
213
|
+
password = params['password'];
|
|
214
|
+
if (params && params.clientMetadata) {
|
|
215
|
+
clientMetadata = params.clientMetadata;
|
|
216
|
+
}
|
|
217
|
+
else if (this._config.clientMetadata) {
|
|
218
|
+
clientMetadata = this._config.clientMetadata;
|
|
219
|
+
}
|
|
220
|
+
var attrs_1 = params['attributes'];
|
|
221
|
+
if (attrs_1) {
|
|
222
|
+
Object.keys(attrs_1).map(function (key) {
|
|
223
|
+
attributes.push(new amazon_cognito_identity_js_1.CognitoUserAttribute({ Name: key, Value: attrs_1[key] }));
|
|
224
|
+
});
|
|
225
|
+
}
|
|
226
|
+
var validationDataObject_1 = params['validationData'];
|
|
227
|
+
if (validationDataObject_1) {
|
|
228
|
+
validationData = [];
|
|
229
|
+
Object.keys(validationDataObject_1).map(function (key) {
|
|
230
|
+
validationData.push(new amazon_cognito_identity_js_1.CognitoUserAttribute({
|
|
231
|
+
Name: key,
|
|
232
|
+
Value: validationDataObject_1[key],
|
|
233
|
+
}));
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
autoSignIn = (_a = params.autoSignIn) !== null && _a !== void 0 ? _a : { enabled: false };
|
|
237
|
+
if (autoSignIn.enabled) {
|
|
238
|
+
this._storage.setItem('amplify-auto-sign-in', 'true');
|
|
239
|
+
autoSignInValidationData = (_b = autoSignIn.validationData) !== null && _b !== void 0 ? _b : {};
|
|
240
|
+
autoSignInClientMetaData = (_c = autoSignIn.clientMetaData) !== null && _c !== void 0 ? _c : {};
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
else {
|
|
244
|
+
return this.rejectAuthError(Auth_1.AuthErrorTypes.SignUpError);
|
|
245
|
+
}
|
|
246
|
+
if (!username) {
|
|
247
|
+
return this.rejectAuthError(Auth_1.AuthErrorTypes.EmptyUsername);
|
|
248
|
+
}
|
|
249
|
+
if (!password) {
|
|
250
|
+
return this.rejectAuthError(Auth_1.AuthErrorTypes.EmptyPassword);
|
|
251
|
+
}
|
|
252
|
+
logger.debug('signUp attrs:', attributes);
|
|
253
|
+
logger.debug('signUp validation data:', validationData);
|
|
254
|
+
return new Promise(function (resolve, reject) {
|
|
255
|
+
_this.userPool.signUp(username, password, attributes, validationData, function (err, data) {
|
|
256
|
+
if (err) {
|
|
257
|
+
dispatchAuthEvent('signUp_failure', err, username + " failed to signup");
|
|
258
|
+
reject(err);
|
|
259
|
+
}
|
|
260
|
+
else {
|
|
261
|
+
dispatchAuthEvent('signUp', data, username + " has signed up successfully");
|
|
262
|
+
if (autoSignIn.enabled) {
|
|
263
|
+
_this.handleAutoSignIn(username, password, autoSignInValidationData, autoSignInClientMetaData, data);
|
|
264
|
+
}
|
|
265
|
+
resolve(data);
|
|
266
|
+
}
|
|
267
|
+
}, clientMetadata);
|
|
268
|
+
});
|
|
269
|
+
};
|
|
270
|
+
InternalAuthClass.prototype.handleAutoSignIn = function (username, password, validationData, clientMetadata, data) {
|
|
271
|
+
this.autoSignInInitiated = true;
|
|
272
|
+
var authDetails = new amazon_cognito_identity_js_1.AuthenticationDetails({
|
|
273
|
+
Username: username,
|
|
274
|
+
Password: password,
|
|
275
|
+
ValidationData: validationData,
|
|
276
|
+
ClientMetadata: clientMetadata,
|
|
277
|
+
});
|
|
278
|
+
if (data.userConfirmed) {
|
|
279
|
+
this.signInAfterUserConfirmed(authDetails);
|
|
280
|
+
}
|
|
281
|
+
else if (this._config.signUpVerificationMethod === 'link') {
|
|
282
|
+
this.handleLinkAutoSignIn(authDetails);
|
|
283
|
+
}
|
|
284
|
+
else {
|
|
285
|
+
this.handleCodeAutoSignIn(authDetails);
|
|
286
|
+
}
|
|
287
|
+
};
|
|
288
|
+
InternalAuthClass.prototype.handleCodeAutoSignIn = function (authDetails) {
|
|
289
|
+
var _this = this;
|
|
290
|
+
var listenEvent = function (_a) {
|
|
291
|
+
var payload = _a.payload;
|
|
292
|
+
if (payload.event === 'confirmSignUp') {
|
|
293
|
+
_this.signInAfterUserConfirmed(authDetails, listenEvent);
|
|
294
|
+
}
|
|
295
|
+
};
|
|
296
|
+
core_1.Hub.listen('auth', listenEvent);
|
|
297
|
+
};
|
|
298
|
+
InternalAuthClass.prototype.handleLinkAutoSignIn = function (authDetails) {
|
|
299
|
+
var _this = this;
|
|
300
|
+
this._storage.setItem('amplify-polling-started', 'true');
|
|
301
|
+
var start = Date.now();
|
|
302
|
+
var autoSignInPollingIntervalId = setInterval(function () {
|
|
303
|
+
if (Date.now() - start > MAX_AUTOSIGNIN_POLLING_MS) {
|
|
304
|
+
clearInterval(autoSignInPollingIntervalId);
|
|
305
|
+
dispatchAuthEvent('autoSignIn_failure', null, 'Please confirm your account and use your credentials to sign in.');
|
|
306
|
+
_this._storage.removeItem('amplify-auto-sign-in');
|
|
307
|
+
}
|
|
308
|
+
else {
|
|
309
|
+
_this.signInAfterUserConfirmed(authDetails, null, autoSignInPollingIntervalId);
|
|
310
|
+
}
|
|
311
|
+
}, 5000);
|
|
312
|
+
};
|
|
313
|
+
InternalAuthClass.prototype.signInAfterUserConfirmed = function (authDetails, listenEvent, autoSignInPollingIntervalId) {
|
|
314
|
+
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
|
315
|
+
var user, error_1;
|
|
316
|
+
var _this = this;
|
|
317
|
+
return tslib_1.__generator(this, function (_a) {
|
|
318
|
+
switch (_a.label) {
|
|
319
|
+
case 0:
|
|
320
|
+
user = this.createCognitoUser(authDetails.getUsername());
|
|
321
|
+
_a.label = 1;
|
|
322
|
+
case 1:
|
|
323
|
+
_a.trys.push([1, 3, , 4]);
|
|
324
|
+
return [4 /*yield*/, user.authenticateUser(authDetails, this.authCallbacks(user, function (value) {
|
|
325
|
+
dispatchAuthEvent('autoSignIn', value, authDetails.getUsername() + " has signed in successfully");
|
|
326
|
+
if (listenEvent) {
|
|
327
|
+
core_1.Hub.remove('auth', listenEvent);
|
|
328
|
+
}
|
|
329
|
+
if (autoSignInPollingIntervalId) {
|
|
330
|
+
clearInterval(autoSignInPollingIntervalId);
|
|
331
|
+
_this._storage.removeItem('amplify-polling-started');
|
|
332
|
+
}
|
|
333
|
+
_this._storage.removeItem('amplify-auto-sign-in');
|
|
334
|
+
}, function (error) {
|
|
335
|
+
logger.error(error);
|
|
336
|
+
_this._storage.removeItem('amplify-auto-sign-in');
|
|
337
|
+
}))];
|
|
338
|
+
case 2:
|
|
339
|
+
_a.sent();
|
|
340
|
+
return [3 /*break*/, 4];
|
|
341
|
+
case 3:
|
|
342
|
+
error_1 = _a.sent();
|
|
343
|
+
logger.error(error_1);
|
|
344
|
+
return [3 /*break*/, 4];
|
|
345
|
+
case 4: return [2 /*return*/];
|
|
346
|
+
}
|
|
347
|
+
});
|
|
348
|
+
});
|
|
349
|
+
};
|
|
350
|
+
/**
|
|
351
|
+
* Send the verification code to confirm sign up
|
|
352
|
+
* @param {String} username - The username to be confirmed
|
|
353
|
+
* @param {String} code - The verification code
|
|
354
|
+
* @param {ConfirmSignUpOptions} options - other options for confirm signup
|
|
355
|
+
* @param {CustomUserAgentDetails} customUserAgentDetails - Optional parameter to send user agent details
|
|
356
|
+
* @return - A promise resolves callback data if success
|
|
357
|
+
*/
|
|
358
|
+
InternalAuthClass.prototype.confirmSignUp = function (username, code, options, customUserAgentDetails) {
|
|
359
|
+
var _this = this;
|
|
360
|
+
if (!this.userPool) {
|
|
361
|
+
return this.rejectNoUserPool();
|
|
362
|
+
}
|
|
363
|
+
if (!username) {
|
|
364
|
+
return this.rejectAuthError(Auth_1.AuthErrorTypes.EmptyUsername);
|
|
365
|
+
}
|
|
366
|
+
if (!code) {
|
|
367
|
+
return this.rejectAuthError(Auth_1.AuthErrorTypes.EmptyCode);
|
|
368
|
+
}
|
|
369
|
+
var user = this.createCognitoUser(username);
|
|
370
|
+
var forceAliasCreation = options && typeof options.forceAliasCreation === 'boolean'
|
|
371
|
+
? options.forceAliasCreation
|
|
372
|
+
: true;
|
|
373
|
+
var clientMetadata;
|
|
374
|
+
if (options && options.clientMetadata) {
|
|
375
|
+
clientMetadata = options.clientMetadata;
|
|
376
|
+
}
|
|
377
|
+
else if (this._config.clientMetadata) {
|
|
378
|
+
clientMetadata = this._config.clientMetadata;
|
|
379
|
+
}
|
|
380
|
+
return new Promise(function (resolve, reject) {
|
|
381
|
+
user.confirmRegistration(code, forceAliasCreation, function (err, data) {
|
|
382
|
+
if (err) {
|
|
383
|
+
reject(err);
|
|
384
|
+
}
|
|
385
|
+
else {
|
|
386
|
+
dispatchAuthEvent('confirmSignUp', data, username + " has been confirmed successfully");
|
|
387
|
+
var autoSignIn = _this.isTrueStorageValue('amplify-auto-sign-in');
|
|
388
|
+
if (autoSignIn && !_this.autoSignInInitiated) {
|
|
389
|
+
dispatchAuthEvent('autoSignIn_failure', null, Auth_1.AuthErrorTypes.AutoSignInError);
|
|
390
|
+
_this._storage.removeItem('amplify-auto-sign-in');
|
|
391
|
+
}
|
|
392
|
+
resolve(data);
|
|
393
|
+
}
|
|
394
|
+
}, clientMetadata);
|
|
395
|
+
});
|
|
396
|
+
};
|
|
397
|
+
InternalAuthClass.prototype.isTrueStorageValue = function (value) {
|
|
398
|
+
var item = this._storage.getItem(value);
|
|
399
|
+
return item ? item === 'true' : false;
|
|
400
|
+
};
|
|
401
|
+
/**
|
|
402
|
+
* Resend the verification code
|
|
403
|
+
* @param {String} username - The username to be confirmed
|
|
404
|
+
* @param {ClientMetadata} clientMetadata - Metadata to be passed to Cognito Lambda triggers
|
|
405
|
+
* @param {CustomUserAgentDetails} customUserAgentDetails - Optional parameter to send user agent details
|
|
406
|
+
* @return - A promise resolves code delivery details if successful
|
|
407
|
+
*/
|
|
408
|
+
InternalAuthClass.prototype.resendSignUp = function (username, clientMetadata, customUserAgentDetails) {
|
|
409
|
+
if (clientMetadata === void 0) { clientMetadata = this._config.clientMetadata; }
|
|
410
|
+
if (!this.userPool) {
|
|
411
|
+
return this.rejectNoUserPool();
|
|
412
|
+
}
|
|
413
|
+
if (!username) {
|
|
414
|
+
return this.rejectAuthError(Auth_1.AuthErrorTypes.EmptyUsername);
|
|
415
|
+
}
|
|
416
|
+
var user = this.createCognitoUser(username);
|
|
417
|
+
return new Promise(function (resolve, reject) {
|
|
418
|
+
user.resendConfirmationCode(function (err, data) {
|
|
419
|
+
if (err) {
|
|
420
|
+
reject(err);
|
|
421
|
+
}
|
|
422
|
+
else {
|
|
423
|
+
resolve(data);
|
|
424
|
+
}
|
|
425
|
+
}, clientMetadata);
|
|
426
|
+
});
|
|
427
|
+
};
|
|
428
|
+
/**
|
|
429
|
+
* Sign in
|
|
430
|
+
* @param {String | SignInOpts} usernameOrSignInOpts - The username to be signed in or the sign in options
|
|
431
|
+
* @param {String} pw - The password of the username
|
|
432
|
+
* @param {ClientMetaData} clientMetadata - Client metadata for custom workflows
|
|
433
|
+
* @return - A promise resolves the CognitoUser
|
|
434
|
+
*/
|
|
435
|
+
InternalAuthClass.prototype.signIn = function (usernameOrSignInOpts, pw, clientMetadata, customUserAgentDetails) {
|
|
436
|
+
if (clientMetadata === void 0) { clientMetadata = this._config.clientMetadata; }
|
|
437
|
+
if (!this.userPool) {
|
|
438
|
+
return this.rejectNoUserPool();
|
|
439
|
+
}
|
|
440
|
+
var username = null;
|
|
441
|
+
var password = null;
|
|
442
|
+
var validationData = {};
|
|
443
|
+
// for backward compatibility
|
|
444
|
+
if (typeof usernameOrSignInOpts === 'string') {
|
|
445
|
+
username = usernameOrSignInOpts;
|
|
446
|
+
password = pw;
|
|
447
|
+
}
|
|
448
|
+
else if (types_1.isUsernamePasswordOpts(usernameOrSignInOpts)) {
|
|
449
|
+
if (typeof pw !== 'undefined') {
|
|
450
|
+
logger.warn('The password should be defined under the first parameter object!');
|
|
451
|
+
}
|
|
452
|
+
username = usernameOrSignInOpts.username;
|
|
453
|
+
password = usernameOrSignInOpts.password;
|
|
454
|
+
validationData = usernameOrSignInOpts.validationData;
|
|
455
|
+
}
|
|
456
|
+
else {
|
|
457
|
+
return this.rejectAuthError(Auth_1.AuthErrorTypes.InvalidUsername);
|
|
458
|
+
}
|
|
459
|
+
if (!username) {
|
|
460
|
+
return this.rejectAuthError(Auth_1.AuthErrorTypes.EmptyUsername);
|
|
461
|
+
}
|
|
462
|
+
var authDetails = new amazon_cognito_identity_js_1.AuthenticationDetails({
|
|
463
|
+
Username: username,
|
|
464
|
+
Password: password,
|
|
465
|
+
ValidationData: validationData,
|
|
466
|
+
ClientMetadata: clientMetadata,
|
|
467
|
+
});
|
|
468
|
+
if (password) {
|
|
469
|
+
return this.signInWithPassword(authDetails);
|
|
470
|
+
}
|
|
471
|
+
else {
|
|
472
|
+
return this.signInWithoutPassword(authDetails);
|
|
473
|
+
}
|
|
474
|
+
};
|
|
475
|
+
/**
|
|
476
|
+
* Return an object with the authentication callbacks
|
|
477
|
+
* @param {CognitoUser} user - the cognito user object
|
|
478
|
+
* @param {} resolve - function called when resolving the current step
|
|
479
|
+
* @param {} reject - function called when rejecting the current step
|
|
480
|
+
* @return - an object with the callback methods for user authentication
|
|
481
|
+
*/
|
|
482
|
+
InternalAuthClass.prototype.authCallbacks = function (user, resolve, reject) {
|
|
483
|
+
var _this = this;
|
|
484
|
+
var that = this;
|
|
485
|
+
return {
|
|
486
|
+
onSuccess: function (session) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
|
|
487
|
+
var cred, e_1, currentUser, e_2;
|
|
488
|
+
return tslib_1.__generator(this, function (_a) {
|
|
489
|
+
switch (_a.label) {
|
|
490
|
+
case 0:
|
|
491
|
+
logger.debug(session);
|
|
492
|
+
delete user['challengeName'];
|
|
493
|
+
delete user['challengeParam'];
|
|
494
|
+
_a.label = 1;
|
|
495
|
+
case 1:
|
|
496
|
+
_a.trys.push([1, 4, 5, 9]);
|
|
497
|
+
return [4 /*yield*/, this.Credentials.clear()];
|
|
498
|
+
case 2:
|
|
499
|
+
_a.sent();
|
|
500
|
+
return [4 /*yield*/, this.Credentials.set(session, 'session')];
|
|
501
|
+
case 3:
|
|
502
|
+
cred = _a.sent();
|
|
503
|
+
logger.debug('succeed to get cognito credentials', cred);
|
|
504
|
+
return [3 /*break*/, 9];
|
|
505
|
+
case 4:
|
|
506
|
+
e_1 = _a.sent();
|
|
507
|
+
logger.debug('cannot get cognito credentials', e_1);
|
|
508
|
+
return [3 /*break*/, 9];
|
|
509
|
+
case 5:
|
|
510
|
+
_a.trys.push([5, 7, , 8]);
|
|
511
|
+
return [4 /*yield*/, this.currentUserPoolUser()];
|
|
512
|
+
case 6:
|
|
513
|
+
currentUser = _a.sent();
|
|
514
|
+
that.user = currentUser;
|
|
515
|
+
dispatchAuthEvent('signIn', currentUser, "A user " + user.getUsername() + " has been signed in");
|
|
516
|
+
resolve(currentUser);
|
|
517
|
+
return [3 /*break*/, 8];
|
|
518
|
+
case 7:
|
|
519
|
+
e_2 = _a.sent();
|
|
520
|
+
logger.error('Failed to get the signed in user', e_2);
|
|
521
|
+
reject(e_2);
|
|
522
|
+
return [3 /*break*/, 8];
|
|
523
|
+
case 8: return [7 /*endfinally*/];
|
|
524
|
+
case 9: return [2 /*return*/];
|
|
525
|
+
}
|
|
526
|
+
});
|
|
527
|
+
}); },
|
|
528
|
+
onFailure: function (err) {
|
|
529
|
+
logger.debug('signIn failure', err);
|
|
530
|
+
dispatchAuthEvent('signIn_failure', err, user.getUsername() + " failed to signin");
|
|
531
|
+
reject(err);
|
|
532
|
+
},
|
|
533
|
+
customChallenge: function (challengeParam) {
|
|
534
|
+
logger.debug('signIn custom challenge answer required');
|
|
535
|
+
user['challengeName'] = 'CUSTOM_CHALLENGE';
|
|
536
|
+
user['challengeParam'] = challengeParam;
|
|
537
|
+
resolve(user);
|
|
538
|
+
},
|
|
539
|
+
mfaRequired: function (challengeName, challengeParam) {
|
|
540
|
+
logger.debug('signIn MFA required');
|
|
541
|
+
user['challengeName'] = challengeName;
|
|
542
|
+
user['challengeParam'] = challengeParam;
|
|
543
|
+
resolve(user);
|
|
544
|
+
},
|
|
545
|
+
mfaSetup: function (challengeName, challengeParam) {
|
|
546
|
+
logger.debug('signIn mfa setup', challengeName);
|
|
547
|
+
user['challengeName'] = challengeName;
|
|
548
|
+
user['challengeParam'] = challengeParam;
|
|
549
|
+
resolve(user);
|
|
550
|
+
},
|
|
551
|
+
newPasswordRequired: function (userAttributes, requiredAttributes) {
|
|
552
|
+
logger.debug('signIn new password');
|
|
553
|
+
user['challengeName'] = 'NEW_PASSWORD_REQUIRED';
|
|
554
|
+
user['challengeParam'] = {
|
|
555
|
+
userAttributes: userAttributes,
|
|
556
|
+
requiredAttributes: requiredAttributes,
|
|
557
|
+
};
|
|
558
|
+
resolve(user);
|
|
559
|
+
},
|
|
560
|
+
totpRequired: function (challengeName, challengeParam) {
|
|
561
|
+
logger.debug('signIn totpRequired');
|
|
562
|
+
user['challengeName'] = challengeName;
|
|
563
|
+
user['challengeParam'] = challengeParam;
|
|
564
|
+
resolve(user);
|
|
565
|
+
},
|
|
566
|
+
selectMFAType: function (challengeName, challengeParam) {
|
|
567
|
+
logger.debug('signIn selectMFAType', challengeName);
|
|
568
|
+
user['challengeName'] = challengeName;
|
|
569
|
+
user['challengeParam'] = challengeParam;
|
|
570
|
+
resolve(user);
|
|
571
|
+
},
|
|
572
|
+
};
|
|
573
|
+
};
|
|
574
|
+
/**
|
|
575
|
+
* Sign in with a password
|
|
576
|
+
* @private
|
|
577
|
+
* @param {AuthenticationDetails} authDetails - the user sign in data
|
|
578
|
+
* @return - A promise resolves the CognitoUser object if success or mfa required
|
|
579
|
+
*/
|
|
580
|
+
InternalAuthClass.prototype.signInWithPassword = function (authDetails) {
|
|
581
|
+
var _this = this;
|
|
582
|
+
if (this.pendingSignIn) {
|
|
583
|
+
throw new Error('Pending sign-in attempt already in progress');
|
|
584
|
+
}
|
|
585
|
+
var user = this.createCognitoUser(authDetails.getUsername());
|
|
586
|
+
this.pendingSignIn = new Promise(function (resolve, reject) {
|
|
587
|
+
user.authenticateUser(authDetails, _this.authCallbacks(user, function (value) {
|
|
588
|
+
_this.pendingSignIn = null;
|
|
589
|
+
resolve(value);
|
|
590
|
+
}, function (error) {
|
|
591
|
+
_this.pendingSignIn = null;
|
|
592
|
+
reject(error);
|
|
593
|
+
}));
|
|
594
|
+
});
|
|
595
|
+
return this.pendingSignIn;
|
|
596
|
+
};
|
|
597
|
+
/**
|
|
598
|
+
* Sign in without a password
|
|
599
|
+
* @private
|
|
600
|
+
* @param {AuthenticationDetails} authDetails - the user sign in data
|
|
601
|
+
* @return - A promise resolves the CognitoUser object if success or mfa required
|
|
602
|
+
*/
|
|
603
|
+
InternalAuthClass.prototype.signInWithoutPassword = function (authDetails) {
|
|
604
|
+
var _this = this;
|
|
605
|
+
var user = this.createCognitoUser(authDetails.getUsername());
|
|
606
|
+
user.setAuthenticationFlowType('CUSTOM_AUTH');
|
|
607
|
+
return new Promise(function (resolve, reject) {
|
|
608
|
+
user.initiateAuth(authDetails, _this.authCallbacks(user, resolve, reject));
|
|
609
|
+
});
|
|
610
|
+
};
|
|
611
|
+
/**
|
|
612
|
+
* This was previously used by an authenticated user to get MFAOptions,
|
|
613
|
+
* but no longer returns a meaningful response. Refer to the documentation for
|
|
614
|
+
* how to setup and use MFA: https://docs.amplify.aws/lib/auth/mfa/q/platform/js
|
|
615
|
+
* @deprecated
|
|
616
|
+
* @param {CognitoUser} user - the current user
|
|
617
|
+
* @param {CustomUserAgentDetails} customUserAgentDetails - Optional parameter to send user agent details
|
|
618
|
+
* @return - A promise resolves the current preferred mfa option if success
|
|
619
|
+
*/
|
|
620
|
+
InternalAuthClass.prototype.getMFAOptions = function (user, customUserAgentDetails) {
|
|
621
|
+
return new Promise(function (res, rej) {
|
|
622
|
+
user.getMFAOptions(function (err, mfaOptions) {
|
|
623
|
+
if (err) {
|
|
624
|
+
logger.debug('get MFA Options failed', err);
|
|
625
|
+
rej(err);
|
|
626
|
+
return;
|
|
627
|
+
}
|
|
628
|
+
logger.debug('get MFA options success', mfaOptions);
|
|
629
|
+
res(mfaOptions);
|
|
630
|
+
return;
|
|
631
|
+
});
|
|
632
|
+
});
|
|
633
|
+
};
|
|
634
|
+
/**
|
|
635
|
+
* get preferred mfa method
|
|
636
|
+
* @param {CognitoUser} user - the current cognito user
|
|
637
|
+
* @param {GetPreferredMFAOpts} params - options for getting the current user preferred MFA
|
|
638
|
+
* @param {CustomUserAgentDetails} customUserAgentDetails - Optional parameter to send user agent details
|
|
639
|
+
*/
|
|
640
|
+
InternalAuthClass.prototype.getPreferredMFA = function (user, params, customUserAgentDetails) {
|
|
641
|
+
var _this = this;
|
|
642
|
+
var that = this;
|
|
643
|
+
return new Promise(function (res, rej) {
|
|
644
|
+
var clientMetadata = _this._config.clientMetadata; // TODO: verify behavior if this is override during signIn
|
|
645
|
+
var bypassCache = params ? params.bypassCache : false;
|
|
646
|
+
user.getUserData(function (err, data) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
|
|
647
|
+
var cleanUpError_1, mfaType;
|
|
648
|
+
return tslib_1.__generator(this, function (_a) {
|
|
649
|
+
switch (_a.label) {
|
|
650
|
+
case 0:
|
|
651
|
+
if (!err) return [3 /*break*/, 5];
|
|
652
|
+
logger.debug('getting preferred mfa failed', err);
|
|
653
|
+
if (!this.isSessionInvalid(err)) return [3 /*break*/, 4];
|
|
654
|
+
_a.label = 1;
|
|
655
|
+
case 1:
|
|
656
|
+
_a.trys.push([1, 3, , 4]);
|
|
657
|
+
return [4 /*yield*/, this.cleanUpInvalidSession(user)];
|
|
658
|
+
case 2:
|
|
659
|
+
_a.sent();
|
|
660
|
+
return [3 /*break*/, 4];
|
|
661
|
+
case 3:
|
|
662
|
+
cleanUpError_1 = _a.sent();
|
|
663
|
+
rej(new Error("Session is invalid due to: " + err.message + " and failed to clean up invalid session: " + cleanUpError_1.message));
|
|
664
|
+
return [2 /*return*/];
|
|
665
|
+
case 4:
|
|
666
|
+
rej(err);
|
|
667
|
+
return [2 /*return*/];
|
|
668
|
+
case 5:
|
|
669
|
+
mfaType = that._getMfaTypeFromUserData(data);
|
|
670
|
+
if (!mfaType) {
|
|
671
|
+
rej('invalid MFA Type');
|
|
672
|
+
return [2 /*return*/];
|
|
673
|
+
}
|
|
674
|
+
else {
|
|
675
|
+
res(mfaType);
|
|
676
|
+
return [2 /*return*/];
|
|
677
|
+
}
|
|
678
|
+
return [2 /*return*/];
|
|
679
|
+
}
|
|
680
|
+
});
|
|
681
|
+
}); }, { bypassCache: bypassCache, clientMetadata: clientMetadata });
|
|
682
|
+
});
|
|
683
|
+
};
|
|
684
|
+
InternalAuthClass.prototype._getMfaTypeFromUserData = function (data) {
|
|
685
|
+
var ret = null;
|
|
686
|
+
var preferredMFA = data.PreferredMfaSetting;
|
|
687
|
+
// if the user has used Auth.setPreferredMFA() to setup the mfa type
|
|
688
|
+
// then the "PreferredMfaSetting" would exist in the response
|
|
689
|
+
if (preferredMFA) {
|
|
690
|
+
ret = preferredMFA;
|
|
691
|
+
}
|
|
692
|
+
else {
|
|
693
|
+
// if mfaList exists but empty, then its noMFA
|
|
694
|
+
var mfaList = data.UserMFASettingList;
|
|
695
|
+
if (!mfaList) {
|
|
696
|
+
// if SMS was enabled by using Auth.enableSMS(),
|
|
697
|
+
// the response would contain MFAOptions
|
|
698
|
+
// as for now Cognito only supports for SMS, so we will say it is 'SMS_MFA'
|
|
699
|
+
// if it does not exist, then it should be NOMFA
|
|
700
|
+
var MFAOptions = data.MFAOptions;
|
|
701
|
+
if (MFAOptions) {
|
|
702
|
+
ret = 'SMS_MFA';
|
|
703
|
+
}
|
|
704
|
+
else {
|
|
705
|
+
ret = 'NOMFA';
|
|
706
|
+
}
|
|
707
|
+
}
|
|
708
|
+
else if (mfaList.length === 0) {
|
|
709
|
+
ret = 'NOMFA';
|
|
710
|
+
}
|
|
711
|
+
else {
|
|
712
|
+
logger.debug('invalid case for getPreferredMFA', data);
|
|
713
|
+
}
|
|
714
|
+
}
|
|
715
|
+
return ret;
|
|
716
|
+
};
|
|
717
|
+
InternalAuthClass.prototype._getUserData = function (user, params) {
|
|
718
|
+
var _this = this;
|
|
719
|
+
return new Promise(function (res, rej) {
|
|
720
|
+
user.getUserData(function (err, data) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
|
|
721
|
+
var cleanUpError_2;
|
|
722
|
+
return tslib_1.__generator(this, function (_a) {
|
|
723
|
+
switch (_a.label) {
|
|
724
|
+
case 0:
|
|
725
|
+
if (!err) return [3 /*break*/, 5];
|
|
726
|
+
logger.debug('getting user data failed', err);
|
|
727
|
+
if (!this.isSessionInvalid(err)) return [3 /*break*/, 4];
|
|
728
|
+
_a.label = 1;
|
|
729
|
+
case 1:
|
|
730
|
+
_a.trys.push([1, 3, , 4]);
|
|
731
|
+
return [4 /*yield*/, this.cleanUpInvalidSession(user)];
|
|
732
|
+
case 2:
|
|
733
|
+
_a.sent();
|
|
734
|
+
return [3 /*break*/, 4];
|
|
735
|
+
case 3:
|
|
736
|
+
cleanUpError_2 = _a.sent();
|
|
737
|
+
rej(new Error("Session is invalid due to: " + err.message + " and failed to clean up invalid session: " + cleanUpError_2.message));
|
|
738
|
+
return [2 /*return*/];
|
|
739
|
+
case 4:
|
|
740
|
+
rej(err);
|
|
741
|
+
return [2 /*return*/];
|
|
742
|
+
case 5:
|
|
743
|
+
res(data);
|
|
744
|
+
_a.label = 6;
|
|
745
|
+
case 6: return [2 /*return*/];
|
|
746
|
+
}
|
|
747
|
+
});
|
|
748
|
+
}); }, params);
|
|
749
|
+
});
|
|
750
|
+
};
|
|
751
|
+
/**
|
|
752
|
+
* set preferred MFA method
|
|
753
|
+
* @param {CognitoUser} user - the current Cognito user
|
|
754
|
+
* @param {string} mfaMethod - preferred mfa method
|
|
755
|
+
* @param {CustomUserAgentDetails} customUserAgentDetails - Optional parameter to send user agent details
|
|
756
|
+
* @return - A promise resolve if success
|
|
757
|
+
*/
|
|
758
|
+
InternalAuthClass.prototype.setPreferredMFA = function (user, mfaMethod, customUserAgentDetails) {
|
|
759
|
+
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
|
760
|
+
var clientMetadata, userData, smsMfaSettings, totpMfaSettings, _a, mfaList, currentMFAType, that;
|
|
761
|
+
var _this = this;
|
|
762
|
+
return tslib_1.__generator(this, function (_b) {
|
|
763
|
+
switch (_b.label) {
|
|
764
|
+
case 0:
|
|
765
|
+
clientMetadata = this._config.clientMetadata;
|
|
766
|
+
return [4 /*yield*/, this._getUserData(user, {
|
|
767
|
+
bypassCache: true,
|
|
768
|
+
clientMetadata: clientMetadata,
|
|
769
|
+
})];
|
|
770
|
+
case 1:
|
|
771
|
+
userData = _b.sent();
|
|
772
|
+
smsMfaSettings = null;
|
|
773
|
+
totpMfaSettings = null;
|
|
774
|
+
_a = mfaMethod;
|
|
775
|
+
switch (_a) {
|
|
776
|
+
case 'TOTP': return [3 /*break*/, 2];
|
|
777
|
+
case 'SOFTWARE_TOKEN_MFA': return [3 /*break*/, 2];
|
|
778
|
+
case 'SMS': return [3 /*break*/, 3];
|
|
779
|
+
case 'SMS_MFA': return [3 /*break*/, 3];
|
|
780
|
+
case 'NOMFA': return [3 /*break*/, 4];
|
|
781
|
+
}
|
|
782
|
+
return [3 /*break*/, 6];
|
|
783
|
+
case 2:
|
|
784
|
+
totpMfaSettings = {
|
|
785
|
+
PreferredMfa: true,
|
|
786
|
+
Enabled: true,
|
|
787
|
+
};
|
|
788
|
+
return [3 /*break*/, 7];
|
|
789
|
+
case 3:
|
|
790
|
+
smsMfaSettings = {
|
|
791
|
+
PreferredMfa: true,
|
|
792
|
+
Enabled: true,
|
|
793
|
+
};
|
|
794
|
+
return [3 /*break*/, 7];
|
|
795
|
+
case 4:
|
|
796
|
+
mfaList = userData['UserMFASettingList'];
|
|
797
|
+
return [4 /*yield*/, this._getMfaTypeFromUserData(userData)];
|
|
798
|
+
case 5:
|
|
799
|
+
currentMFAType = _b.sent();
|
|
800
|
+
if (currentMFAType === 'NOMFA') {
|
|
801
|
+
return [2 /*return*/, Promise.resolve('No change for mfa type')];
|
|
802
|
+
}
|
|
803
|
+
else if (currentMFAType === 'SMS_MFA') {
|
|
804
|
+
smsMfaSettings = {
|
|
805
|
+
PreferredMfa: false,
|
|
806
|
+
Enabled: false,
|
|
807
|
+
};
|
|
808
|
+
}
|
|
809
|
+
else if (currentMFAType === 'SOFTWARE_TOKEN_MFA') {
|
|
810
|
+
totpMfaSettings = {
|
|
811
|
+
PreferredMfa: false,
|
|
812
|
+
Enabled: false,
|
|
813
|
+
};
|
|
814
|
+
}
|
|
815
|
+
else {
|
|
816
|
+
return [2 /*return*/, this.rejectAuthError(Auth_1.AuthErrorTypes.InvalidMFA)];
|
|
817
|
+
}
|
|
818
|
+
// if there is a UserMFASettingList in the response
|
|
819
|
+
// we need to disable every mfa type in that list
|
|
820
|
+
if (mfaList && mfaList.length !== 0) {
|
|
821
|
+
// to disable SMS or TOTP if exists in that list
|
|
822
|
+
mfaList.forEach(function (mfaType) {
|
|
823
|
+
if (mfaType === 'SMS_MFA') {
|
|
824
|
+
smsMfaSettings = {
|
|
825
|
+
PreferredMfa: false,
|
|
826
|
+
Enabled: false,
|
|
827
|
+
};
|
|
828
|
+
}
|
|
829
|
+
else if (mfaType === 'SOFTWARE_TOKEN_MFA') {
|
|
830
|
+
totpMfaSettings = {
|
|
831
|
+
PreferredMfa: false,
|
|
832
|
+
Enabled: false,
|
|
833
|
+
};
|
|
834
|
+
}
|
|
835
|
+
});
|
|
836
|
+
}
|
|
837
|
+
return [3 /*break*/, 7];
|
|
838
|
+
case 6:
|
|
839
|
+
logger.debug('no validmfa method provided');
|
|
840
|
+
return [2 /*return*/, this.rejectAuthError(Auth_1.AuthErrorTypes.NoMFA)];
|
|
841
|
+
case 7:
|
|
842
|
+
that = this;
|
|
843
|
+
return [2 /*return*/, new Promise(function (res, rej) {
|
|
844
|
+
user.setUserMfaPreference(smsMfaSettings, totpMfaSettings, function (err, result) {
|
|
845
|
+
if (err) {
|
|
846
|
+
logger.debug('Set user mfa preference error', err);
|
|
847
|
+
return rej(err);
|
|
848
|
+
}
|
|
849
|
+
logger.debug('Set user mfa success', result);
|
|
850
|
+
logger.debug('Caching the latest user data into local');
|
|
851
|
+
// cache the latest result into user data
|
|
852
|
+
user.getUserData(function (err, data) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
|
|
853
|
+
var cleanUpError_3;
|
|
854
|
+
return tslib_1.__generator(this, function (_a) {
|
|
855
|
+
switch (_a.label) {
|
|
856
|
+
case 0:
|
|
857
|
+
if (!err) return [3 /*break*/, 5];
|
|
858
|
+
logger.debug('getting user data failed', err);
|
|
859
|
+
if (!this.isSessionInvalid(err)) return [3 /*break*/, 4];
|
|
860
|
+
_a.label = 1;
|
|
861
|
+
case 1:
|
|
862
|
+
_a.trys.push([1, 3, , 4]);
|
|
863
|
+
return [4 /*yield*/, this.cleanUpInvalidSession(user)];
|
|
864
|
+
case 2:
|
|
865
|
+
_a.sent();
|
|
866
|
+
return [3 /*break*/, 4];
|
|
867
|
+
case 3:
|
|
868
|
+
cleanUpError_3 = _a.sent();
|
|
869
|
+
rej(new Error("Session is invalid due to: " + err.message + " and failed to clean up invalid session: " + cleanUpError_3.message));
|
|
870
|
+
return [2 /*return*/];
|
|
871
|
+
case 4: return [2 /*return*/, rej(err)];
|
|
872
|
+
case 5: return [2 /*return*/, res(result)];
|
|
873
|
+
}
|
|
874
|
+
});
|
|
875
|
+
}); }, {
|
|
876
|
+
bypassCache: true,
|
|
877
|
+
clientMetadata: clientMetadata,
|
|
878
|
+
});
|
|
879
|
+
});
|
|
880
|
+
})];
|
|
881
|
+
}
|
|
882
|
+
});
|
|
883
|
+
});
|
|
884
|
+
};
|
|
885
|
+
/**
|
|
886
|
+
* disable SMS
|
|
887
|
+
* @deprecated
|
|
888
|
+
* @param {CognitoUser} user - the current user
|
|
889
|
+
* @param {CustomUserAgentDetails} customUserAgentDetails - Optional parameter to send user agent details
|
|
890
|
+
* @return - A promise resolves is success
|
|
891
|
+
*/
|
|
892
|
+
InternalAuthClass.prototype.disableSMS = function (user, customUserAgentDetails) {
|
|
893
|
+
return new Promise(function (res, rej) {
|
|
894
|
+
user.disableMFA(function (err, data) {
|
|
895
|
+
if (err) {
|
|
896
|
+
logger.debug('disable mfa failed', err);
|
|
897
|
+
rej(err);
|
|
898
|
+
return;
|
|
899
|
+
}
|
|
900
|
+
logger.debug('disable mfa succeed', data);
|
|
901
|
+
res(data);
|
|
902
|
+
return;
|
|
903
|
+
});
|
|
904
|
+
});
|
|
905
|
+
};
|
|
906
|
+
/**
|
|
907
|
+
* enable SMS
|
|
908
|
+
* @deprecated
|
|
909
|
+
* @param {CognitoUser} user - the current user
|
|
910
|
+
* @param {CustomUserAgentDetails} customUserAgentDetails - Optional parameter to send user agent details
|
|
911
|
+
* @return - A promise resolves is success
|
|
912
|
+
*/
|
|
913
|
+
InternalAuthClass.prototype.enableSMS = function (user, customUserAgentDetails) {
|
|
914
|
+
return new Promise(function (res, rej) {
|
|
915
|
+
user.enableMFA(function (err, data) {
|
|
916
|
+
if (err) {
|
|
917
|
+
logger.debug('enable mfa failed', err);
|
|
918
|
+
rej(err);
|
|
919
|
+
return;
|
|
920
|
+
}
|
|
921
|
+
logger.debug('enable mfa succeed', data);
|
|
922
|
+
res(data);
|
|
923
|
+
return;
|
|
924
|
+
});
|
|
925
|
+
});
|
|
926
|
+
};
|
|
927
|
+
/**
|
|
928
|
+
* Setup TOTP
|
|
929
|
+
* @param {CognitoUser} user - the current user
|
|
930
|
+
* @param {CustomUserAgentDetails} customUserAgentDetails - Optional parameter to send user agent details
|
|
931
|
+
* @return - A promise resolves with the secret code if success
|
|
932
|
+
*/
|
|
933
|
+
InternalAuthClass.prototype.setupTOTP = function (user, customUserAgentDetails) {
|
|
934
|
+
return new Promise(function (res, rej) {
|
|
935
|
+
user.associateSoftwareToken({
|
|
936
|
+
onFailure: function (err) {
|
|
937
|
+
logger.debug('associateSoftwareToken failed', err);
|
|
938
|
+
rej(err);
|
|
939
|
+
return;
|
|
940
|
+
},
|
|
941
|
+
associateSecretCode: function (secretCode) {
|
|
942
|
+
logger.debug('associateSoftwareToken success', secretCode);
|
|
943
|
+
res(secretCode);
|
|
944
|
+
return;
|
|
945
|
+
},
|
|
946
|
+
});
|
|
947
|
+
});
|
|
948
|
+
};
|
|
949
|
+
/**
|
|
950
|
+
* verify TOTP setup
|
|
951
|
+
* @param {CognitoUser} user - the current user
|
|
952
|
+
* @param {string} challengeAnswer - challenge answer
|
|
953
|
+
* @param {CustomUserAgentDetails} customUserAgentDetails - Optional parameter to send user agent details
|
|
954
|
+
* @return - A promise resolves is success
|
|
955
|
+
*/
|
|
956
|
+
InternalAuthClass.prototype.verifyTotpToken = function (user, challengeAnswer, customUserAgentDetails) {
|
|
957
|
+
logger.debug('verification totp token', user, challengeAnswer);
|
|
958
|
+
var signInUserSession;
|
|
959
|
+
if (user && typeof user.getSignInUserSession === 'function') {
|
|
960
|
+
signInUserSession = user.getSignInUserSession();
|
|
961
|
+
}
|
|
962
|
+
var isLoggedIn = signInUserSession === null || signInUserSession === void 0 ? void 0 : signInUserSession.isValid();
|
|
963
|
+
return new Promise(function (res, rej) {
|
|
964
|
+
user.verifySoftwareToken(challengeAnswer, 'My TOTP device', {
|
|
965
|
+
onFailure: function (err) {
|
|
966
|
+
logger.debug('verifyTotpToken failed', err);
|
|
967
|
+
rej(err);
|
|
968
|
+
return;
|
|
969
|
+
},
|
|
970
|
+
onSuccess: function (data) {
|
|
971
|
+
if (!isLoggedIn) {
|
|
972
|
+
dispatchAuthEvent('signIn', user, "A user " + user.getUsername() + " has been signed in");
|
|
973
|
+
}
|
|
974
|
+
dispatchAuthEvent('verify', user, "A user " + user.getUsername() + " has been verified");
|
|
975
|
+
logger.debug('verifyTotpToken success', data);
|
|
976
|
+
res(data);
|
|
977
|
+
return;
|
|
978
|
+
},
|
|
979
|
+
});
|
|
980
|
+
});
|
|
981
|
+
};
|
|
982
|
+
/**
|
|
983
|
+
* Send MFA code to confirm sign in
|
|
984
|
+
* @param {Object} user - The CognitoUser object
|
|
985
|
+
* @param {String} code - The confirmation code
|
|
986
|
+
* @param {string} mfaType - optional mfaType: 'SMS_MFA' | 'SOFTWARE_TOKEN_MFA'
|
|
987
|
+
* @param {ClientMetaData} clientMetadata - optional client metadata defaults to config
|
|
988
|
+
* @param {CustomUserAgentDetails} customUserAgentDetails - Optional parameter to send user agent details
|
|
989
|
+
*/
|
|
990
|
+
InternalAuthClass.prototype.confirmSignIn = function (user, code, mfaType, clientMetadata, customUserAgentDetails) {
|
|
991
|
+
var _this = this;
|
|
992
|
+
if (clientMetadata === void 0) { clientMetadata = this._config.clientMetadata; }
|
|
993
|
+
if (!code) {
|
|
994
|
+
return this.rejectAuthError(Auth_1.AuthErrorTypes.EmptyCode);
|
|
995
|
+
}
|
|
996
|
+
var that = this;
|
|
997
|
+
return new Promise(function (resolve, reject) {
|
|
998
|
+
user.sendMFACode(code, {
|
|
999
|
+
onSuccess: function (session) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
|
|
1000
|
+
var cred, e_3, currentUser, e_4;
|
|
1001
|
+
return tslib_1.__generator(this, function (_a) {
|
|
1002
|
+
switch (_a.label) {
|
|
1003
|
+
case 0:
|
|
1004
|
+
logger.debug(session);
|
|
1005
|
+
_a.label = 1;
|
|
1006
|
+
case 1:
|
|
1007
|
+
_a.trys.push([1, 4, 5, 10]);
|
|
1008
|
+
return [4 /*yield*/, this.Credentials.clear()];
|
|
1009
|
+
case 2:
|
|
1010
|
+
_a.sent();
|
|
1011
|
+
return [4 /*yield*/, this.Credentials.set(session, 'session')];
|
|
1012
|
+
case 3:
|
|
1013
|
+
cred = _a.sent();
|
|
1014
|
+
logger.debug('succeed to get cognito credentials', cred);
|
|
1015
|
+
return [3 /*break*/, 10];
|
|
1016
|
+
case 4:
|
|
1017
|
+
e_3 = _a.sent();
|
|
1018
|
+
logger.debug('cannot get cognito credentials', e_3);
|
|
1019
|
+
return [3 /*break*/, 10];
|
|
1020
|
+
case 5:
|
|
1021
|
+
that.user = user;
|
|
1022
|
+
_a.label = 6;
|
|
1023
|
+
case 6:
|
|
1024
|
+
_a.trys.push([6, 8, , 9]);
|
|
1025
|
+
return [4 /*yield*/, this.currentUserPoolUser()];
|
|
1026
|
+
case 7:
|
|
1027
|
+
currentUser = _a.sent();
|
|
1028
|
+
user.attributes = currentUser.attributes;
|
|
1029
|
+
return [3 /*break*/, 9];
|
|
1030
|
+
case 8:
|
|
1031
|
+
e_4 = _a.sent();
|
|
1032
|
+
logger.debug('cannot get updated Cognito User', e_4);
|
|
1033
|
+
return [3 /*break*/, 9];
|
|
1034
|
+
case 9:
|
|
1035
|
+
dispatchAuthEvent('signIn', user, "A user " + user.getUsername() + " has been signed in");
|
|
1036
|
+
resolve(user);
|
|
1037
|
+
return [7 /*endfinally*/];
|
|
1038
|
+
case 10: return [2 /*return*/];
|
|
1039
|
+
}
|
|
1040
|
+
});
|
|
1041
|
+
}); },
|
|
1042
|
+
onFailure: function (err) {
|
|
1043
|
+
logger.debug('confirm signIn failure', err);
|
|
1044
|
+
reject(err);
|
|
1045
|
+
},
|
|
1046
|
+
}, mfaType, clientMetadata);
|
|
1047
|
+
});
|
|
1048
|
+
};
|
|
1049
|
+
InternalAuthClass.prototype.completeNewPassword = function (user, password, requiredAttributes, clientMetadata, customUserAgentDetails) {
|
|
1050
|
+
var _this = this;
|
|
1051
|
+
if (requiredAttributes === void 0) { requiredAttributes = {}; }
|
|
1052
|
+
if (clientMetadata === void 0) { clientMetadata = this._config.clientMetadata; }
|
|
1053
|
+
if (!password) {
|
|
1054
|
+
return this.rejectAuthError(Auth_1.AuthErrorTypes.EmptyPassword);
|
|
1055
|
+
}
|
|
1056
|
+
var that = this;
|
|
1057
|
+
return new Promise(function (resolve, reject) {
|
|
1058
|
+
user.completeNewPasswordChallenge(password, requiredAttributes, {
|
|
1059
|
+
onSuccess: function (session) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
|
|
1060
|
+
var cred, e_5;
|
|
1061
|
+
return tslib_1.__generator(this, function (_a) {
|
|
1062
|
+
switch (_a.label) {
|
|
1063
|
+
case 0:
|
|
1064
|
+
logger.debug(session);
|
|
1065
|
+
_a.label = 1;
|
|
1066
|
+
case 1:
|
|
1067
|
+
_a.trys.push([1, 4, 5, 6]);
|
|
1068
|
+
return [4 /*yield*/, this.Credentials.clear()];
|
|
1069
|
+
case 2:
|
|
1070
|
+
_a.sent();
|
|
1071
|
+
return [4 /*yield*/, this.Credentials.set(session, 'session')];
|
|
1072
|
+
case 3:
|
|
1073
|
+
cred = _a.sent();
|
|
1074
|
+
logger.debug('succeed to get cognito credentials', cred);
|
|
1075
|
+
return [3 /*break*/, 6];
|
|
1076
|
+
case 4:
|
|
1077
|
+
e_5 = _a.sent();
|
|
1078
|
+
logger.debug('cannot get cognito credentials', e_5);
|
|
1079
|
+
return [3 /*break*/, 6];
|
|
1080
|
+
case 5:
|
|
1081
|
+
that.user = user;
|
|
1082
|
+
dispatchAuthEvent('signIn', user, "A user " + user.getUsername() + " has been signed in");
|
|
1083
|
+
resolve(user);
|
|
1084
|
+
return [7 /*endfinally*/];
|
|
1085
|
+
case 6: return [2 /*return*/];
|
|
1086
|
+
}
|
|
1087
|
+
});
|
|
1088
|
+
}); },
|
|
1089
|
+
onFailure: function (err) {
|
|
1090
|
+
logger.debug('completeNewPassword failure', err);
|
|
1091
|
+
dispatchAuthEvent('completeNewPassword_failure', err, _this.user + " failed to complete the new password flow");
|
|
1092
|
+
reject(err);
|
|
1093
|
+
},
|
|
1094
|
+
mfaRequired: function (challengeName, challengeParam) {
|
|
1095
|
+
logger.debug('signIn MFA required');
|
|
1096
|
+
user['challengeName'] = challengeName;
|
|
1097
|
+
user['challengeParam'] = challengeParam;
|
|
1098
|
+
resolve(user);
|
|
1099
|
+
},
|
|
1100
|
+
mfaSetup: function (challengeName, challengeParam) {
|
|
1101
|
+
logger.debug('signIn mfa setup', challengeName);
|
|
1102
|
+
user['challengeName'] = challengeName;
|
|
1103
|
+
user['challengeParam'] = challengeParam;
|
|
1104
|
+
resolve(user);
|
|
1105
|
+
},
|
|
1106
|
+
totpRequired: function (challengeName, challengeParam) {
|
|
1107
|
+
logger.debug('signIn mfa setup', challengeName);
|
|
1108
|
+
user['challengeName'] = challengeName;
|
|
1109
|
+
user['challengeParam'] = challengeParam;
|
|
1110
|
+
resolve(user);
|
|
1111
|
+
},
|
|
1112
|
+
}, clientMetadata);
|
|
1113
|
+
});
|
|
1114
|
+
};
|
|
1115
|
+
/**
|
|
1116
|
+
* Send the answer to a custom challenge
|
|
1117
|
+
* @param {CognitoUser} user - The CognitoUser object
|
|
1118
|
+
* @param {String} challengeResponses - The confirmation code
|
|
1119
|
+
* @param {ClientMetaData} clientMetadata - optional client metadata defaults to config
|
|
1120
|
+
* @param {CustomUserAgentDetails} customUserAgentDetails - Optional parameter to send user agent details
|
|
1121
|
+
*/
|
|
1122
|
+
InternalAuthClass.prototype.sendCustomChallengeAnswer = function (user, challengeResponses, clientMetadata, customUserAgentDetails) {
|
|
1123
|
+
var _this = this;
|
|
1124
|
+
if (clientMetadata === void 0) { clientMetadata = this._config.clientMetadata; }
|
|
1125
|
+
if (!this.userPool) {
|
|
1126
|
+
return this.rejectNoUserPool();
|
|
1127
|
+
}
|
|
1128
|
+
if (!challengeResponses) {
|
|
1129
|
+
return this.rejectAuthError(Auth_1.AuthErrorTypes.EmptyChallengeResponse);
|
|
1130
|
+
}
|
|
1131
|
+
var that = this;
|
|
1132
|
+
return new Promise(function (resolve, reject) {
|
|
1133
|
+
user.sendCustomChallengeAnswer(challengeResponses, _this.authCallbacks(user, resolve, reject), clientMetadata);
|
|
1134
|
+
});
|
|
1135
|
+
};
|
|
1136
|
+
/**
|
|
1137
|
+
* Delete an authenticated users' attributes
|
|
1138
|
+
* @param {CognitoUser} user - The currently logged in user object
|
|
1139
|
+
* @param {string[]} attributeNames - Attributes to delete
|
|
1140
|
+
* @param {CustomUserAgentDetails} customUserAgentDetails - Optional parameter to send user agent details
|
|
1141
|
+
* @return {Promise}
|
|
1142
|
+
**/
|
|
1143
|
+
InternalAuthClass.prototype.deleteUserAttributes = function (user, attributeNames, customUserAgentDetails) {
|
|
1144
|
+
var that = this;
|
|
1145
|
+
return new Promise(function (resolve, reject) {
|
|
1146
|
+
that.userSession(user).then(function (session) {
|
|
1147
|
+
user.deleteAttributes(attributeNames, function (err, result) {
|
|
1148
|
+
if (err) {
|
|
1149
|
+
return reject(err);
|
|
1150
|
+
}
|
|
1151
|
+
else {
|
|
1152
|
+
return resolve(result);
|
|
1153
|
+
}
|
|
1154
|
+
});
|
|
1155
|
+
});
|
|
1156
|
+
});
|
|
1157
|
+
};
|
|
1158
|
+
/**
|
|
1159
|
+
* Delete the current authenticated user
|
|
1160
|
+
* @param {CustomUserAgentDetails} customUserAgentDetails - Optional parameter to send user agent details
|
|
1161
|
+
* @return {Promise}
|
|
1162
|
+
**/
|
|
1163
|
+
// TODO: Check return type void
|
|
1164
|
+
InternalAuthClass.prototype.deleteUser = function (customUserAgentDetails) {
|
|
1165
|
+
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
|
1166
|
+
var e_6, isSignedInHostedUI;
|
|
1167
|
+
var _this = this;
|
|
1168
|
+
return tslib_1.__generator(this, function (_a) {
|
|
1169
|
+
switch (_a.label) {
|
|
1170
|
+
case 0:
|
|
1171
|
+
_a.trys.push([0, 2, , 3]);
|
|
1172
|
+
return [4 /*yield*/, this._storageSync];
|
|
1173
|
+
case 1:
|
|
1174
|
+
_a.sent();
|
|
1175
|
+
return [3 /*break*/, 3];
|
|
1176
|
+
case 2:
|
|
1177
|
+
e_6 = _a.sent();
|
|
1178
|
+
logger.debug('Failed to sync cache info into memory', e_6);
|
|
1179
|
+
throw new Error(e_6);
|
|
1180
|
+
case 3:
|
|
1181
|
+
isSignedInHostedUI = this._oAuthHandler &&
|
|
1182
|
+
this._storage.getItem('amplify-signin-with-hostedUI') === 'true';
|
|
1183
|
+
return [2 /*return*/, new Promise(function (res, rej) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
|
|
1184
|
+
var user_1;
|
|
1185
|
+
var _this = this;
|
|
1186
|
+
return tslib_1.__generator(this, function (_a) {
|
|
1187
|
+
if (this.userPool) {
|
|
1188
|
+
user_1 = this.userPool.getCurrentUser();
|
|
1189
|
+
if (!user_1) {
|
|
1190
|
+
logger.debug('Failed to get user from user pool');
|
|
1191
|
+
return [2 /*return*/, rej(new Error('No current user.'))];
|
|
1192
|
+
}
|
|
1193
|
+
else {
|
|
1194
|
+
user_1.getSession(function (err, session) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
|
|
1195
|
+
var cleanUpError_4;
|
|
1196
|
+
var _this = this;
|
|
1197
|
+
return tslib_1.__generator(this, function (_a) {
|
|
1198
|
+
switch (_a.label) {
|
|
1199
|
+
case 0:
|
|
1200
|
+
if (!err) return [3 /*break*/, 5];
|
|
1201
|
+
logger.debug('Failed to get the user session', err);
|
|
1202
|
+
if (!this.isSessionInvalid(err)) return [3 /*break*/, 4];
|
|
1203
|
+
_a.label = 1;
|
|
1204
|
+
case 1:
|
|
1205
|
+
_a.trys.push([1, 3, , 4]);
|
|
1206
|
+
return [4 /*yield*/, this.cleanUpInvalidSession(user_1)];
|
|
1207
|
+
case 2:
|
|
1208
|
+
_a.sent();
|
|
1209
|
+
return [3 /*break*/, 4];
|
|
1210
|
+
case 3:
|
|
1211
|
+
cleanUpError_4 = _a.sent();
|
|
1212
|
+
rej(new Error("Session is invalid due to: " + err.message + " and failed to clean up invalid session: " + cleanUpError_4.message));
|
|
1213
|
+
return [2 /*return*/];
|
|
1214
|
+
case 4: return [2 /*return*/, rej(err)];
|
|
1215
|
+
case 5:
|
|
1216
|
+
user_1.deleteUser(function (err, result) {
|
|
1217
|
+
if (err) {
|
|
1218
|
+
rej(err);
|
|
1219
|
+
}
|
|
1220
|
+
else {
|
|
1221
|
+
dispatchAuthEvent('userDeleted', result, 'The authenticated user has been deleted.');
|
|
1222
|
+
user_1.signOut();
|
|
1223
|
+
_this.user = null;
|
|
1224
|
+
try {
|
|
1225
|
+
_this.cleanCachedItems(); // clean aws credentials
|
|
1226
|
+
}
|
|
1227
|
+
catch (e) {
|
|
1228
|
+
// TODO: change to rejects in refactor
|
|
1229
|
+
logger.debug('failed to clear cached items');
|
|
1230
|
+
}
|
|
1231
|
+
if (isSignedInHostedUI) {
|
|
1232
|
+
_this.oAuthSignOutRedirect(res, rej);
|
|
1233
|
+
}
|
|
1234
|
+
else {
|
|
1235
|
+
dispatchAuthEvent('signOut', _this.user, "A user has been signed out");
|
|
1236
|
+
res(result);
|
|
1237
|
+
}
|
|
1238
|
+
}
|
|
1239
|
+
});
|
|
1240
|
+
_a.label = 6;
|
|
1241
|
+
case 6: return [2 /*return*/];
|
|
1242
|
+
}
|
|
1243
|
+
});
|
|
1244
|
+
}); });
|
|
1245
|
+
}
|
|
1246
|
+
}
|
|
1247
|
+
else {
|
|
1248
|
+
logger.debug('no Congito User pool');
|
|
1249
|
+
rej(new Error('Cognito User pool does not exist'));
|
|
1250
|
+
}
|
|
1251
|
+
return [2 /*return*/];
|
|
1252
|
+
});
|
|
1253
|
+
}); })];
|
|
1254
|
+
}
|
|
1255
|
+
});
|
|
1256
|
+
});
|
|
1257
|
+
};
|
|
1258
|
+
/**
|
|
1259
|
+
* Update an authenticated users' attributes
|
|
1260
|
+
* @param {CognitoUser} user - The currently logged in user object
|
|
1261
|
+
* @param {object} attributes - attributes to update
|
|
1262
|
+
* @param {ClientMetaData} clientMetadata - optional client metadata, defaults to config
|
|
1263
|
+
* @param {CustomUserAgentDetails} customUserAgentDetails - Optional parameter to send user agent details
|
|
1264
|
+
* @return {Promise}
|
|
1265
|
+
**/
|
|
1266
|
+
InternalAuthClass.prototype.updateUserAttributes = function (user, attributes, clientMetadata, customUserAgentDetails) {
|
|
1267
|
+
var _this = this;
|
|
1268
|
+
if (clientMetadata === void 0) { clientMetadata = this._config.clientMetadata; }
|
|
1269
|
+
var attributeList = [];
|
|
1270
|
+
var that = this;
|
|
1271
|
+
return new Promise(function (resolve, reject) {
|
|
1272
|
+
that.userSession(user).then(function (session) {
|
|
1273
|
+
for (var key in attributes) {
|
|
1274
|
+
if (key !== 'sub' && key.indexOf('_verified') < 0) {
|
|
1275
|
+
var attr = {
|
|
1276
|
+
Name: key,
|
|
1277
|
+
Value: attributes[key],
|
|
1278
|
+
};
|
|
1279
|
+
attributeList.push(attr);
|
|
1280
|
+
}
|
|
1281
|
+
}
|
|
1282
|
+
user.updateAttributes(attributeList, function (err, result, details) {
|
|
1283
|
+
if (err) {
|
|
1284
|
+
dispatchAuthEvent('updateUserAttributes_failure', err, 'Failed to update attributes');
|
|
1285
|
+
return reject(err);
|
|
1286
|
+
}
|
|
1287
|
+
else {
|
|
1288
|
+
var attrs = _this.createUpdateAttributesResultList(attributes, details === null || details === void 0 ? void 0 : details.CodeDeliveryDetailsList);
|
|
1289
|
+
dispatchAuthEvent('updateUserAttributes', attrs, 'Attributes successfully updated');
|
|
1290
|
+
return resolve(result);
|
|
1291
|
+
}
|
|
1292
|
+
}, clientMetadata);
|
|
1293
|
+
});
|
|
1294
|
+
});
|
|
1295
|
+
};
|
|
1296
|
+
InternalAuthClass.prototype.createUpdateAttributesResultList = function (attributes, codeDeliveryDetailsList) {
|
|
1297
|
+
var attrs = {};
|
|
1298
|
+
Object.keys(attributes).forEach(function (key) {
|
|
1299
|
+
attrs[key] = {
|
|
1300
|
+
isUpdated: true,
|
|
1301
|
+
};
|
|
1302
|
+
var codeDeliveryDetails = codeDeliveryDetailsList === null || codeDeliveryDetailsList === void 0 ? void 0 : codeDeliveryDetailsList.find(function (value) { return value.AttributeName === key; });
|
|
1303
|
+
if (codeDeliveryDetails) {
|
|
1304
|
+
attrs[key].isUpdated = false;
|
|
1305
|
+
attrs[key].codeDeliveryDetails = codeDeliveryDetails;
|
|
1306
|
+
}
|
|
1307
|
+
});
|
|
1308
|
+
return attrs;
|
|
1309
|
+
};
|
|
1310
|
+
/**
|
|
1311
|
+
* Return user attributes
|
|
1312
|
+
* @param {Object} user - The CognitoUser object
|
|
1313
|
+
* @param {CustomUserAgentDetails} customUserAgentDetails - Optional parameter to send user agent details
|
|
1314
|
+
* @return - A promise resolves to user attributes if success
|
|
1315
|
+
*/
|
|
1316
|
+
InternalAuthClass.prototype.userAttributes = function (user, customUserAgentDetails) {
|
|
1317
|
+
var _this = this;
|
|
1318
|
+
return new Promise(function (resolve, reject) {
|
|
1319
|
+
_this.userSession(user).then(function (session) {
|
|
1320
|
+
user.getUserAttributes(function (err, attributes) {
|
|
1321
|
+
if (err) {
|
|
1322
|
+
reject(err);
|
|
1323
|
+
}
|
|
1324
|
+
else {
|
|
1325
|
+
resolve(attributes);
|
|
1326
|
+
}
|
|
1327
|
+
});
|
|
1328
|
+
});
|
|
1329
|
+
});
|
|
1330
|
+
};
|
|
1331
|
+
InternalAuthClass.prototype.verifiedContact = function (user, customUserAgentDetails) {
|
|
1332
|
+
var that = this;
|
|
1333
|
+
return this.userAttributes(user).then(function (attributes) {
|
|
1334
|
+
var attrs = that.attributesToObject(attributes);
|
|
1335
|
+
var unverified = {};
|
|
1336
|
+
var verified = {};
|
|
1337
|
+
if (attrs['email']) {
|
|
1338
|
+
if (attrs['email_verified']) {
|
|
1339
|
+
verified['email'] = attrs['email'];
|
|
1340
|
+
}
|
|
1341
|
+
else {
|
|
1342
|
+
unverified['email'] = attrs['email'];
|
|
1343
|
+
}
|
|
1344
|
+
}
|
|
1345
|
+
if (attrs['phone_number']) {
|
|
1346
|
+
if (attrs['phone_number_verified']) {
|
|
1347
|
+
verified['phone_number'] = attrs['phone_number'];
|
|
1348
|
+
}
|
|
1349
|
+
else {
|
|
1350
|
+
unverified['phone_number'] = attrs['phone_number'];
|
|
1351
|
+
}
|
|
1352
|
+
}
|
|
1353
|
+
return {
|
|
1354
|
+
verified: verified,
|
|
1355
|
+
unverified: unverified,
|
|
1356
|
+
};
|
|
1357
|
+
});
|
|
1358
|
+
};
|
|
1359
|
+
InternalAuthClass.prototype.isErrorWithMessage = function (err) {
|
|
1360
|
+
return (typeof err === 'object' &&
|
|
1361
|
+
Object.prototype.hasOwnProperty.call(err, 'message'));
|
|
1362
|
+
};
|
|
1363
|
+
// Session revoked by another app
|
|
1364
|
+
InternalAuthClass.prototype.isTokenRevokedError = function (err) {
|
|
1365
|
+
return (this.isErrorWithMessage(err) &&
|
|
1366
|
+
err.message === 'Access Token has been revoked');
|
|
1367
|
+
};
|
|
1368
|
+
InternalAuthClass.prototype.isRefreshTokenRevokedError = function (err) {
|
|
1369
|
+
return (this.isErrorWithMessage(err) &&
|
|
1370
|
+
err.message === 'Refresh Token has been revoked');
|
|
1371
|
+
};
|
|
1372
|
+
InternalAuthClass.prototype.isUserDisabledError = function (err) {
|
|
1373
|
+
return this.isErrorWithMessage(err) && err.message === 'User is disabled.';
|
|
1374
|
+
};
|
|
1375
|
+
InternalAuthClass.prototype.isUserDoesNotExistError = function (err) {
|
|
1376
|
+
return (this.isErrorWithMessage(err) && err.message === 'User does not exist.');
|
|
1377
|
+
};
|
|
1378
|
+
InternalAuthClass.prototype.isRefreshTokenExpiredError = function (err) {
|
|
1379
|
+
return (this.isErrorWithMessage(err) &&
|
|
1380
|
+
err.message === 'Refresh Token has expired');
|
|
1381
|
+
};
|
|
1382
|
+
InternalAuthClass.prototype.isPasswordResetRequiredError = function (err) {
|
|
1383
|
+
return (this.isErrorWithMessage(err) &&
|
|
1384
|
+
err.message === 'Password reset required for the user');
|
|
1385
|
+
};
|
|
1386
|
+
InternalAuthClass.prototype.isSignedInHostedUI = function () {
|
|
1387
|
+
return (this._oAuthHandler &&
|
|
1388
|
+
this._storage.getItem('amplify-signin-with-hostedUI') === 'true');
|
|
1389
|
+
};
|
|
1390
|
+
InternalAuthClass.prototype.isSessionInvalid = function (err) {
|
|
1391
|
+
return (this.isUserDisabledError(err) ||
|
|
1392
|
+
this.isUserDoesNotExistError(err) ||
|
|
1393
|
+
this.isTokenRevokedError(err) ||
|
|
1394
|
+
this.isRefreshTokenRevokedError(err) ||
|
|
1395
|
+
this.isRefreshTokenExpiredError(err) ||
|
|
1396
|
+
this.isPasswordResetRequiredError(err));
|
|
1397
|
+
};
|
|
1398
|
+
InternalAuthClass.prototype.cleanUpInvalidSession = function (user) {
|
|
1399
|
+
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
|
1400
|
+
var e_7;
|
|
1401
|
+
var _this = this;
|
|
1402
|
+
return tslib_1.__generator(this, function (_a) {
|
|
1403
|
+
switch (_a.label) {
|
|
1404
|
+
case 0:
|
|
1405
|
+
user.signOut();
|
|
1406
|
+
this.user = null;
|
|
1407
|
+
_a.label = 1;
|
|
1408
|
+
case 1:
|
|
1409
|
+
_a.trys.push([1, 3, , 4]);
|
|
1410
|
+
return [4 /*yield*/, this.cleanCachedItems()];
|
|
1411
|
+
case 2:
|
|
1412
|
+
_a.sent(); // clean aws credentials
|
|
1413
|
+
return [3 /*break*/, 4];
|
|
1414
|
+
case 3:
|
|
1415
|
+
e_7 = _a.sent();
|
|
1416
|
+
logger.debug('failed to clear cached items');
|
|
1417
|
+
return [3 /*break*/, 4];
|
|
1418
|
+
case 4:
|
|
1419
|
+
if (this.isSignedInHostedUI()) {
|
|
1420
|
+
return [2 /*return*/, new Promise(function (res, rej) {
|
|
1421
|
+
_this.oAuthSignOutRedirect(res, rej);
|
|
1422
|
+
})];
|
|
1423
|
+
}
|
|
1424
|
+
else {
|
|
1425
|
+
dispatchAuthEvent('signOut', this.user, "A user has been signed out");
|
|
1426
|
+
}
|
|
1427
|
+
return [2 /*return*/];
|
|
1428
|
+
}
|
|
1429
|
+
});
|
|
1430
|
+
});
|
|
1431
|
+
};
|
|
1432
|
+
/**
|
|
1433
|
+
* Get current authenticated user
|
|
1434
|
+
* @param {CurrentUserOpts} params - options for getting the current user
|
|
1435
|
+
* @param {CustomUserAgentDetails} customUserAgentDetails - Optional parameter to send user agent details
|
|
1436
|
+
* @return - A promise resolves to current authenticated CognitoUser if success
|
|
1437
|
+
*/
|
|
1438
|
+
InternalAuthClass.prototype.currentUserPoolUser = function (params, customUserAgentDetails) {
|
|
1439
|
+
var _this = this;
|
|
1440
|
+
if (!this.userPool) {
|
|
1441
|
+
return this.rejectNoUserPool();
|
|
1442
|
+
}
|
|
1443
|
+
return new Promise(function (res, rej) {
|
|
1444
|
+
_this._storageSync
|
|
1445
|
+
.then(function () { return tslib_1.__awaiter(_this, void 0, void 0, function () {
|
|
1446
|
+
var user, session, bypassCache, clientMetadata, _a, scope, err_1;
|
|
1447
|
+
var _this = this;
|
|
1448
|
+
return tslib_1.__generator(this, function (_b) {
|
|
1449
|
+
switch (_b.label) {
|
|
1450
|
+
case 0:
|
|
1451
|
+
if (!this.isOAuthInProgress()) return [3 /*break*/, 2];
|
|
1452
|
+
logger.debug('OAuth signIn in progress, waiting for resolution...');
|
|
1453
|
+
return [4 /*yield*/, new Promise(function (res) {
|
|
1454
|
+
var timeoutId = setTimeout(function () {
|
|
1455
|
+
logger.debug('OAuth signIn in progress timeout');
|
|
1456
|
+
core_1.Hub.remove('auth', hostedUISignCallback);
|
|
1457
|
+
res();
|
|
1458
|
+
}, OAUTH_FLOW_MS_TIMEOUT);
|
|
1459
|
+
core_1.Hub.listen('auth', hostedUISignCallback);
|
|
1460
|
+
function hostedUISignCallback(_a) {
|
|
1461
|
+
var payload = _a.payload;
|
|
1462
|
+
var event = payload.event;
|
|
1463
|
+
if (event === 'cognitoHostedUI' ||
|
|
1464
|
+
event === 'cognitoHostedUI_failure') {
|
|
1465
|
+
logger.debug("OAuth signIn resolved: " + event);
|
|
1466
|
+
clearTimeout(timeoutId);
|
|
1467
|
+
core_1.Hub.remove('auth', hostedUISignCallback);
|
|
1468
|
+
res();
|
|
1469
|
+
}
|
|
1470
|
+
}
|
|
1471
|
+
})];
|
|
1472
|
+
case 1:
|
|
1473
|
+
_b.sent();
|
|
1474
|
+
_b.label = 2;
|
|
1475
|
+
case 2:
|
|
1476
|
+
user = this.userPool.getCurrentUser();
|
|
1477
|
+
if (!user) {
|
|
1478
|
+
logger.debug('Failed to get user from user pool');
|
|
1479
|
+
rej('No current user');
|
|
1480
|
+
return [2 /*return*/];
|
|
1481
|
+
}
|
|
1482
|
+
_b.label = 3;
|
|
1483
|
+
case 3:
|
|
1484
|
+
_b.trys.push([3, 7, , 8]);
|
|
1485
|
+
return [4 /*yield*/, this._userSession(user)];
|
|
1486
|
+
case 4:
|
|
1487
|
+
session = _b.sent();
|
|
1488
|
+
bypassCache = params ? params.bypassCache : false;
|
|
1489
|
+
if (!bypassCache) return [3 /*break*/, 6];
|
|
1490
|
+
return [4 /*yield*/, this.Credentials.clear()];
|
|
1491
|
+
case 5:
|
|
1492
|
+
_b.sent();
|
|
1493
|
+
_b.label = 6;
|
|
1494
|
+
case 6:
|
|
1495
|
+
clientMetadata = this._config.clientMetadata;
|
|
1496
|
+
_a = session.getAccessToken().decodePayload().scope, scope = _a === void 0 ? '' : _a;
|
|
1497
|
+
if (scope.split(' ').includes(USER_ADMIN_SCOPE)) {
|
|
1498
|
+
user.getUserData(function (err, data) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
|
|
1499
|
+
var cleanUpError_5, preferredMFA, attributeList, i, attribute, userAttribute, attributes;
|
|
1500
|
+
return tslib_1.__generator(this, function (_a) {
|
|
1501
|
+
switch (_a.label) {
|
|
1502
|
+
case 0:
|
|
1503
|
+
if (!err) return [3 /*break*/, 7];
|
|
1504
|
+
logger.debug('getting user data failed', err);
|
|
1505
|
+
if (!this.isSessionInvalid(err)) return [3 /*break*/, 5];
|
|
1506
|
+
_a.label = 1;
|
|
1507
|
+
case 1:
|
|
1508
|
+
_a.trys.push([1, 3, , 4]);
|
|
1509
|
+
return [4 /*yield*/, this.cleanUpInvalidSession(user)];
|
|
1510
|
+
case 2:
|
|
1511
|
+
_a.sent();
|
|
1512
|
+
return [3 /*break*/, 4];
|
|
1513
|
+
case 3:
|
|
1514
|
+
cleanUpError_5 = _a.sent();
|
|
1515
|
+
rej(new Error("Session is invalid due to: " + err.message + " and failed to clean up invalid session: " + cleanUpError_5.message));
|
|
1516
|
+
return [2 /*return*/];
|
|
1517
|
+
case 4:
|
|
1518
|
+
rej(err);
|
|
1519
|
+
return [3 /*break*/, 6];
|
|
1520
|
+
case 5:
|
|
1521
|
+
res(user);
|
|
1522
|
+
_a.label = 6;
|
|
1523
|
+
case 6: return [2 /*return*/];
|
|
1524
|
+
case 7:
|
|
1525
|
+
preferredMFA = data.PreferredMfaSetting || 'NOMFA';
|
|
1526
|
+
attributeList = [];
|
|
1527
|
+
for (i = 0; i < data.UserAttributes.length; i++) {
|
|
1528
|
+
attribute = {
|
|
1529
|
+
Name: data.UserAttributes[i].Name,
|
|
1530
|
+
Value: data.UserAttributes[i].Value,
|
|
1531
|
+
};
|
|
1532
|
+
userAttribute = new amazon_cognito_identity_js_1.CognitoUserAttribute(attribute);
|
|
1533
|
+
attributeList.push(userAttribute);
|
|
1534
|
+
}
|
|
1535
|
+
attributes = this.attributesToObject(attributeList);
|
|
1536
|
+
Object.assign(user, { attributes: attributes, preferredMFA: preferredMFA });
|
|
1537
|
+
return [2 /*return*/, res(user)];
|
|
1538
|
+
}
|
|
1539
|
+
});
|
|
1540
|
+
}); }, { bypassCache: bypassCache, clientMetadata: clientMetadata });
|
|
1541
|
+
}
|
|
1542
|
+
else {
|
|
1543
|
+
logger.debug("Unable to get the user data because the " + USER_ADMIN_SCOPE + " " +
|
|
1544
|
+
"is not in the scopes of the access token");
|
|
1545
|
+
return [2 /*return*/, res(user)];
|
|
1546
|
+
}
|
|
1547
|
+
return [3 /*break*/, 8];
|
|
1548
|
+
case 7:
|
|
1549
|
+
err_1 = _b.sent();
|
|
1550
|
+
rej(err_1);
|
|
1551
|
+
return [3 /*break*/, 8];
|
|
1552
|
+
case 8: return [2 /*return*/];
|
|
1553
|
+
}
|
|
1554
|
+
});
|
|
1555
|
+
}); })
|
|
1556
|
+
.catch(function (e) {
|
|
1557
|
+
logger.debug('Failed to sync cache info into memory', e);
|
|
1558
|
+
return rej(e);
|
|
1559
|
+
});
|
|
1560
|
+
});
|
|
1561
|
+
};
|
|
1562
|
+
InternalAuthClass.prototype.isOAuthInProgress = function () {
|
|
1563
|
+
return this.oAuthFlowInProgress;
|
|
1564
|
+
};
|
|
1565
|
+
/**
|
|
1566
|
+
* Get current authenticated user
|
|
1567
|
+
* @param {CurrentUserOpts} - options for getting the current user
|
|
1568
|
+
* @param {CustomUserAgentDetails} customUserAgentDetails - Optional parameter to send user agent details
|
|
1569
|
+
* @return - A promise resolves to current authenticated CognitoUser if success
|
|
1570
|
+
*/
|
|
1571
|
+
InternalAuthClass.prototype.currentAuthenticatedUser = function (params, customUserAgentDetails) {
|
|
1572
|
+
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
|
1573
|
+
var federatedUser, e_8, federatedInfo, user, e_9;
|
|
1574
|
+
return tslib_1.__generator(this, function (_a) {
|
|
1575
|
+
switch (_a.label) {
|
|
1576
|
+
case 0:
|
|
1577
|
+
logger.debug('getting current authenticated user');
|
|
1578
|
+
federatedUser = null;
|
|
1579
|
+
_a.label = 1;
|
|
1580
|
+
case 1:
|
|
1581
|
+
_a.trys.push([1, 3, , 4]);
|
|
1582
|
+
return [4 /*yield*/, this._storageSync];
|
|
1583
|
+
case 2:
|
|
1584
|
+
_a.sent();
|
|
1585
|
+
return [3 /*break*/, 4];
|
|
1586
|
+
case 3:
|
|
1587
|
+
e_8 = _a.sent();
|
|
1588
|
+
logger.debug('Failed to sync cache info into memory', e_8);
|
|
1589
|
+
throw e_8;
|
|
1590
|
+
case 4:
|
|
1591
|
+
try {
|
|
1592
|
+
federatedInfo = JSON.parse(this._storage.getItem('aws-amplify-federatedInfo'));
|
|
1593
|
+
if (federatedInfo) {
|
|
1594
|
+
federatedUser = tslib_1.__assign(tslib_1.__assign({}, federatedInfo.user), { token: federatedInfo.token });
|
|
1595
|
+
}
|
|
1596
|
+
}
|
|
1597
|
+
catch (e) {
|
|
1598
|
+
logger.debug('cannot load federated user from auth storage');
|
|
1599
|
+
}
|
|
1600
|
+
if (!federatedUser) return [3 /*break*/, 5];
|
|
1601
|
+
this.user = federatedUser;
|
|
1602
|
+
logger.debug('get current authenticated federated user', this.user);
|
|
1603
|
+
return [2 /*return*/, this.user];
|
|
1604
|
+
case 5:
|
|
1605
|
+
logger.debug('get current authenticated userpool user');
|
|
1606
|
+
user = null;
|
|
1607
|
+
_a.label = 6;
|
|
1608
|
+
case 6:
|
|
1609
|
+
_a.trys.push([6, 8, , 9]);
|
|
1610
|
+
return [4 /*yield*/, this.currentUserPoolUser(params)];
|
|
1611
|
+
case 7:
|
|
1612
|
+
user = _a.sent();
|
|
1613
|
+
return [3 /*break*/, 9];
|
|
1614
|
+
case 8:
|
|
1615
|
+
e_9 = _a.sent();
|
|
1616
|
+
if (e_9 === 'No userPool') {
|
|
1617
|
+
logger.error('Cannot get the current user because the user pool is missing. ' +
|
|
1618
|
+
'Please make sure the Auth module is configured with a valid Cognito User Pool ID');
|
|
1619
|
+
}
|
|
1620
|
+
logger.debug('The user is not authenticated by the error', e_9);
|
|
1621
|
+
return [2 /*return*/, Promise.reject('The user is not authenticated')];
|
|
1622
|
+
case 9:
|
|
1623
|
+
this.user = user;
|
|
1624
|
+
return [2 /*return*/, this.user];
|
|
1625
|
+
}
|
|
1626
|
+
});
|
|
1627
|
+
});
|
|
1628
|
+
};
|
|
1629
|
+
/**
|
|
1630
|
+
* Get current user's session
|
|
1631
|
+
* @param {CustomUserAgentDetails} customUserAgentDetails - Optional parameter to send user agent details
|
|
1632
|
+
* @return - A promise resolves to session object if success
|
|
1633
|
+
*/
|
|
1634
|
+
InternalAuthClass.prototype.currentSession = function (customUserAgentDetails) {
|
|
1635
|
+
var that = this;
|
|
1636
|
+
logger.debug('Getting current session');
|
|
1637
|
+
// Purposely not calling the reject method here because we don't need a console error
|
|
1638
|
+
if (!this.userPool) {
|
|
1639
|
+
return Promise.reject(new Error('No User Pool in the configuration.'));
|
|
1640
|
+
}
|
|
1641
|
+
return new Promise(function (res, rej) {
|
|
1642
|
+
that
|
|
1643
|
+
.currentUserPoolUser()
|
|
1644
|
+
.then(function (user) {
|
|
1645
|
+
that
|
|
1646
|
+
.userSession(user)
|
|
1647
|
+
.then(function (session) {
|
|
1648
|
+
res(session);
|
|
1649
|
+
return;
|
|
1650
|
+
})
|
|
1651
|
+
.catch(function (e) {
|
|
1652
|
+
logger.debug('Failed to get the current session', e);
|
|
1653
|
+
rej(e);
|
|
1654
|
+
return;
|
|
1655
|
+
});
|
|
1656
|
+
})
|
|
1657
|
+
.catch(function (e) {
|
|
1658
|
+
logger.debug('Failed to get the current user', e);
|
|
1659
|
+
rej(e);
|
|
1660
|
+
return;
|
|
1661
|
+
});
|
|
1662
|
+
});
|
|
1663
|
+
};
|
|
1664
|
+
InternalAuthClass.prototype._userSession = function (user) {
|
|
1665
|
+
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
|
1666
|
+
var clientMetadata, userSession;
|
|
1667
|
+
var _this = this;
|
|
1668
|
+
return tslib_1.__generator(this, function (_a) {
|
|
1669
|
+
switch (_a.label) {
|
|
1670
|
+
case 0:
|
|
1671
|
+
if (!user) {
|
|
1672
|
+
logger.debug('the user is null');
|
|
1673
|
+
return [2 /*return*/, this.rejectAuthError(Auth_1.AuthErrorTypes.NoUserSession)];
|
|
1674
|
+
}
|
|
1675
|
+
clientMetadata = this._config.clientMetadata;
|
|
1676
|
+
// Debouncing the concurrent userSession calls by caching the promise.
|
|
1677
|
+
// This solution assumes users will always call this function with the same CognitoUser instance.
|
|
1678
|
+
if (this.inflightSessionPromiseCounter === 0) {
|
|
1679
|
+
this.inflightSessionPromise = new Promise(function (res, rej) {
|
|
1680
|
+
user.getSession(function (err, session) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
|
|
1681
|
+
var cleanUpError_6;
|
|
1682
|
+
return tslib_1.__generator(this, function (_a) {
|
|
1683
|
+
switch (_a.label) {
|
|
1684
|
+
case 0:
|
|
1685
|
+
if (!err) return [3 /*break*/, 5];
|
|
1686
|
+
logger.debug('Failed to get the session from user', user);
|
|
1687
|
+
if (!this.isSessionInvalid(err)) return [3 /*break*/, 4];
|
|
1688
|
+
_a.label = 1;
|
|
1689
|
+
case 1:
|
|
1690
|
+
_a.trys.push([1, 3, , 4]);
|
|
1691
|
+
return [4 /*yield*/, this.cleanUpInvalidSession(user)];
|
|
1692
|
+
case 2:
|
|
1693
|
+
_a.sent();
|
|
1694
|
+
return [3 /*break*/, 4];
|
|
1695
|
+
case 3:
|
|
1696
|
+
cleanUpError_6 = _a.sent();
|
|
1697
|
+
rej(new Error("Session is invalid due to: " + err.message + " and failed to clean up invalid session: " + cleanUpError_6.message));
|
|
1698
|
+
return [2 /*return*/];
|
|
1699
|
+
case 4:
|
|
1700
|
+
rej(err);
|
|
1701
|
+
return [2 /*return*/];
|
|
1702
|
+
case 5:
|
|
1703
|
+
logger.debug('Succeed to get the user session', session);
|
|
1704
|
+
res(session);
|
|
1705
|
+
return [2 /*return*/];
|
|
1706
|
+
}
|
|
1707
|
+
});
|
|
1708
|
+
}); }, { clientMetadata: clientMetadata });
|
|
1709
|
+
});
|
|
1710
|
+
}
|
|
1711
|
+
this.inflightSessionPromiseCounter++;
|
|
1712
|
+
_a.label = 1;
|
|
1713
|
+
case 1:
|
|
1714
|
+
_a.trys.push([1, , 3, 4]);
|
|
1715
|
+
return [4 /*yield*/, this.inflightSessionPromise];
|
|
1716
|
+
case 2:
|
|
1717
|
+
userSession = _a.sent();
|
|
1718
|
+
// Set private member. Avoid user.setSignInUserSession() to prevent excessive localstorage refresh.
|
|
1719
|
+
// @ts-ignore
|
|
1720
|
+
user.signInUserSession = userSession;
|
|
1721
|
+
return [2 /*return*/, userSession];
|
|
1722
|
+
case 3:
|
|
1723
|
+
this.inflightSessionPromiseCounter--;
|
|
1724
|
+
return [7 /*endfinally*/];
|
|
1725
|
+
case 4: return [2 /*return*/];
|
|
1726
|
+
}
|
|
1727
|
+
});
|
|
1728
|
+
});
|
|
1729
|
+
};
|
|
1730
|
+
/**
|
|
1731
|
+
* Get the corresponding user session
|
|
1732
|
+
* @param {Object} user - The CognitoUser object
|
|
1733
|
+
* @param {CustomUserAgentDetails} customUserAgentDetails - Optional parameter to send user agent details
|
|
1734
|
+
* @return - A promise resolves to the session
|
|
1735
|
+
*/
|
|
1736
|
+
InternalAuthClass.prototype.userSession = function (user, customUserAgentDetails) {
|
|
1737
|
+
return this._userSession(user);
|
|
1738
|
+
};
|
|
1739
|
+
/**
|
|
1740
|
+
* Get authenticated credentials of current user.
|
|
1741
|
+
* @param {CustomUserAgentDetails} customUserAgentDetails - Optional parameter to send user agent details
|
|
1742
|
+
* @return - A promise resolves to be current user's credentials
|
|
1743
|
+
*/
|
|
1744
|
+
InternalAuthClass.prototype.currentUserCredentials = function (customUserAgentDetails) {
|
|
1745
|
+
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
|
1746
|
+
var e_10, federatedInfo;
|
|
1747
|
+
var _this = this;
|
|
1748
|
+
return tslib_1.__generator(this, function (_a) {
|
|
1749
|
+
switch (_a.label) {
|
|
1750
|
+
case 0:
|
|
1751
|
+
logger.debug('Getting current user credentials');
|
|
1752
|
+
_a.label = 1;
|
|
1753
|
+
case 1:
|
|
1754
|
+
_a.trys.push([1, 3, , 4]);
|
|
1755
|
+
return [4 /*yield*/, this._storageSync];
|
|
1756
|
+
case 2:
|
|
1757
|
+
_a.sent();
|
|
1758
|
+
return [3 /*break*/, 4];
|
|
1759
|
+
case 3:
|
|
1760
|
+
e_10 = _a.sent();
|
|
1761
|
+
logger.debug('Failed to sync cache info into memory', e_10);
|
|
1762
|
+
throw e_10;
|
|
1763
|
+
case 4:
|
|
1764
|
+
federatedInfo = null;
|
|
1765
|
+
try {
|
|
1766
|
+
federatedInfo = JSON.parse(this._storage.getItem('aws-amplify-federatedInfo'));
|
|
1767
|
+
}
|
|
1768
|
+
catch (e) {
|
|
1769
|
+
logger.debug('failed to get or parse item aws-amplify-federatedInfo', e);
|
|
1770
|
+
}
|
|
1771
|
+
if (federatedInfo) {
|
|
1772
|
+
// refresh the jwt token here if necessary
|
|
1773
|
+
return [2 /*return*/, this.Credentials.refreshFederatedToken(federatedInfo)];
|
|
1774
|
+
}
|
|
1775
|
+
else {
|
|
1776
|
+
return [2 /*return*/, this.currentSession()
|
|
1777
|
+
.then(function (session) {
|
|
1778
|
+
logger.debug('getting session success', session);
|
|
1779
|
+
return _this.Credentials.set(session, 'session');
|
|
1780
|
+
})
|
|
1781
|
+
.catch(function () {
|
|
1782
|
+
logger.debug('getting guest credentials');
|
|
1783
|
+
return _this.Credentials.set(null, 'guest');
|
|
1784
|
+
})];
|
|
1785
|
+
}
|
|
1786
|
+
return [2 /*return*/];
|
|
1787
|
+
}
|
|
1788
|
+
});
|
|
1789
|
+
});
|
|
1790
|
+
};
|
|
1791
|
+
InternalAuthClass.prototype.currentCredentials = function (customUserAgentDetails) {
|
|
1792
|
+
logger.debug('getting current credentials');
|
|
1793
|
+
return this.Credentials.get();
|
|
1794
|
+
};
|
|
1795
|
+
/**
|
|
1796
|
+
* Initiate an attribute confirmation request
|
|
1797
|
+
* @param {Object} user - The CognitoUser
|
|
1798
|
+
* @param {Object} attr - The attributes to be verified
|
|
1799
|
+
* @return - A promise resolves to callback data if success
|
|
1800
|
+
*/
|
|
1801
|
+
InternalAuthClass.prototype.verifyUserAttribute = function (user, attr, clientMetadata, customUserAgentDetails) {
|
|
1802
|
+
if (clientMetadata === void 0) { clientMetadata = this._config.clientMetadata; }
|
|
1803
|
+
return new Promise(function (resolve, reject) {
|
|
1804
|
+
user.getAttributeVerificationCode(attr, {
|
|
1805
|
+
onSuccess: function (success) {
|
|
1806
|
+
return resolve(success);
|
|
1807
|
+
},
|
|
1808
|
+
onFailure: function (err) {
|
|
1809
|
+
return reject(err);
|
|
1810
|
+
},
|
|
1811
|
+
}, clientMetadata);
|
|
1812
|
+
});
|
|
1813
|
+
};
|
|
1814
|
+
/**
|
|
1815
|
+
* Confirm an attribute using a confirmation code
|
|
1816
|
+
* @param {Object} user - The CognitoUser
|
|
1817
|
+
* @param {Object} attr - The attribute to be verified
|
|
1818
|
+
* @param {String} code - The confirmation code
|
|
1819
|
+
* @param {CustomUserAgentDetails} customUserAgentDetails - Optional parameter to send user agent details
|
|
1820
|
+
* @return - A promise resolves to callback data if success
|
|
1821
|
+
*/
|
|
1822
|
+
InternalAuthClass.prototype.verifyUserAttributeSubmit = function (user, attr, code, customUserAgentDetails) {
|
|
1823
|
+
if (!code) {
|
|
1824
|
+
return this.rejectAuthError(Auth_1.AuthErrorTypes.EmptyCode);
|
|
1825
|
+
}
|
|
1826
|
+
return new Promise(function (resolve, reject) {
|
|
1827
|
+
user.verifyAttribute(attr, code, {
|
|
1828
|
+
onSuccess: function (data) {
|
|
1829
|
+
resolve(data);
|
|
1830
|
+
return;
|
|
1831
|
+
},
|
|
1832
|
+
onFailure: function (err) {
|
|
1833
|
+
reject(err);
|
|
1834
|
+
return;
|
|
1835
|
+
},
|
|
1836
|
+
});
|
|
1837
|
+
});
|
|
1838
|
+
};
|
|
1839
|
+
InternalAuthClass.prototype.verifyCurrentUserAttribute = function (attr, customUserAgentDetails) {
|
|
1840
|
+
var that = this;
|
|
1841
|
+
return that
|
|
1842
|
+
.currentUserPoolUser()
|
|
1843
|
+
.then(function (user) { return that.verifyUserAttribute(user, attr); });
|
|
1844
|
+
};
|
|
1845
|
+
/**
|
|
1846
|
+
* Confirm current user's attribute using a confirmation code
|
|
1847
|
+
* @param {Object} attr - The attribute to be verified
|
|
1848
|
+
* @param {String} code - The confirmation code
|
|
1849
|
+
* @param {CustomUserAgentDetails} customUserAgentDetails - Optional parameter to send user agent details
|
|
1850
|
+
* @return - A promise resolves to callback data if success
|
|
1851
|
+
*/
|
|
1852
|
+
InternalAuthClass.prototype.verifyCurrentUserAttributeSubmit = function (attr, code, customUserAgentDetails) {
|
|
1853
|
+
var that = this;
|
|
1854
|
+
return that
|
|
1855
|
+
.currentUserPoolUser()
|
|
1856
|
+
.then(function (user) { return that.verifyUserAttributeSubmit(user, attr, code); });
|
|
1857
|
+
};
|
|
1858
|
+
InternalAuthClass.prototype.cognitoIdentitySignOut = function (opts, user) {
|
|
1859
|
+
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
|
1860
|
+
var e_11, isSignedInHostedUI;
|
|
1861
|
+
var _this = this;
|
|
1862
|
+
return tslib_1.__generator(this, function (_a) {
|
|
1863
|
+
switch (_a.label) {
|
|
1864
|
+
case 0:
|
|
1865
|
+
_a.trys.push([0, 2, , 3]);
|
|
1866
|
+
return [4 /*yield*/, this._storageSync];
|
|
1867
|
+
case 1:
|
|
1868
|
+
_a.sent();
|
|
1869
|
+
return [3 /*break*/, 3];
|
|
1870
|
+
case 2:
|
|
1871
|
+
e_11 = _a.sent();
|
|
1872
|
+
logger.debug('Failed to sync cache info into memory', e_11);
|
|
1873
|
+
throw e_11;
|
|
1874
|
+
case 3:
|
|
1875
|
+
isSignedInHostedUI = this._oAuthHandler &&
|
|
1876
|
+
this._storage.getItem('amplify-signin-with-hostedUI') === 'true';
|
|
1877
|
+
return [2 /*return*/, new Promise(function (res, rej) {
|
|
1878
|
+
if (opts && opts.global) {
|
|
1879
|
+
logger.debug('user global sign out', user);
|
|
1880
|
+
// in order to use global signout
|
|
1881
|
+
// we must validate the user as an authenticated user by using getSession
|
|
1882
|
+
var clientMetadata = _this._config.clientMetadata; // TODO: verify behavior if this is override during signIn
|
|
1883
|
+
user.getSession(function (err, result) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
|
|
1884
|
+
var cleanUpError_7;
|
|
1885
|
+
var _this = this;
|
|
1886
|
+
return tslib_1.__generator(this, function (_a) {
|
|
1887
|
+
switch (_a.label) {
|
|
1888
|
+
case 0:
|
|
1889
|
+
if (!err) return [3 /*break*/, 5];
|
|
1890
|
+
logger.debug('failed to get the user session', err);
|
|
1891
|
+
if (!this.isSessionInvalid(err)) return [3 /*break*/, 4];
|
|
1892
|
+
_a.label = 1;
|
|
1893
|
+
case 1:
|
|
1894
|
+
_a.trys.push([1, 3, , 4]);
|
|
1895
|
+
return [4 /*yield*/, this.cleanUpInvalidSession(user)];
|
|
1896
|
+
case 2:
|
|
1897
|
+
_a.sent();
|
|
1898
|
+
return [3 /*break*/, 4];
|
|
1899
|
+
case 3:
|
|
1900
|
+
cleanUpError_7 = _a.sent();
|
|
1901
|
+
rej(new Error("Session is invalid due to: " + err.message + " and failed to clean up invalid session: " + cleanUpError_7.message));
|
|
1902
|
+
return [2 /*return*/];
|
|
1903
|
+
case 4: return [2 /*return*/, rej(err)];
|
|
1904
|
+
case 5:
|
|
1905
|
+
user.globalSignOut({
|
|
1906
|
+
onSuccess: function (data) {
|
|
1907
|
+
logger.debug('global sign out success');
|
|
1908
|
+
if (isSignedInHostedUI) {
|
|
1909
|
+
_this.oAuthSignOutRedirect(res, rej);
|
|
1910
|
+
}
|
|
1911
|
+
else {
|
|
1912
|
+
return res();
|
|
1913
|
+
}
|
|
1914
|
+
},
|
|
1915
|
+
onFailure: function (err) {
|
|
1916
|
+
logger.debug('global sign out failed', err);
|
|
1917
|
+
return rej(err);
|
|
1918
|
+
},
|
|
1919
|
+
});
|
|
1920
|
+
return [2 /*return*/];
|
|
1921
|
+
}
|
|
1922
|
+
});
|
|
1923
|
+
}); }, { clientMetadata: clientMetadata });
|
|
1924
|
+
}
|
|
1925
|
+
else {
|
|
1926
|
+
logger.debug('user sign out', user);
|
|
1927
|
+
user.signOut(function () {
|
|
1928
|
+
if (isSignedInHostedUI) {
|
|
1929
|
+
_this.oAuthSignOutRedirect(res, rej);
|
|
1930
|
+
}
|
|
1931
|
+
else {
|
|
1932
|
+
return res();
|
|
1933
|
+
}
|
|
1934
|
+
});
|
|
1935
|
+
}
|
|
1936
|
+
})];
|
|
1937
|
+
}
|
|
1938
|
+
});
|
|
1939
|
+
});
|
|
1940
|
+
};
|
|
1941
|
+
InternalAuthClass.prototype.oAuthSignOutRedirect = function (resolve, reject) {
|
|
1942
|
+
var isBrowser = core_1.browserOrNode().isBrowser;
|
|
1943
|
+
if (isBrowser) {
|
|
1944
|
+
this.oAuthSignOutRedirectOrReject(reject);
|
|
1945
|
+
}
|
|
1946
|
+
else {
|
|
1947
|
+
this.oAuthSignOutAndResolve(resolve);
|
|
1948
|
+
}
|
|
1949
|
+
};
|
|
1950
|
+
InternalAuthClass.prototype.oAuthSignOutAndResolve = function (resolve) {
|
|
1951
|
+
this._oAuthHandler.signOut();
|
|
1952
|
+
resolve();
|
|
1953
|
+
};
|
|
1954
|
+
InternalAuthClass.prototype.oAuthSignOutRedirectOrReject = function (reject) {
|
|
1955
|
+
this._oAuthHandler.signOut(); // this method redirects url
|
|
1956
|
+
// App should be redirected to another url otherwise it will reject
|
|
1957
|
+
setTimeout(function () { return reject(Error('Signout timeout fail')); }, 3000);
|
|
1958
|
+
};
|
|
1959
|
+
/**
|
|
1960
|
+
* Sign out method
|
|
1961
|
+
* @param {SignOutOpts} opts - options for sign out
|
|
1962
|
+
* @param {CustomUserAgentDetails} customUserAgentDetails - Optional parameter to send user agent details
|
|
1963
|
+
* @return - A promise resolved if success
|
|
1964
|
+
*/
|
|
1965
|
+
InternalAuthClass.prototype.signOut = function (opts, customUserAgentDetails) {
|
|
1966
|
+
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
|
1967
|
+
var e_12, user;
|
|
1968
|
+
return tslib_1.__generator(this, function (_a) {
|
|
1969
|
+
switch (_a.label) {
|
|
1970
|
+
case 0:
|
|
1971
|
+
_a.trys.push([0, 2, , 3]);
|
|
1972
|
+
return [4 /*yield*/, this.cleanCachedItems()];
|
|
1973
|
+
case 1:
|
|
1974
|
+
_a.sent();
|
|
1975
|
+
return [3 /*break*/, 3];
|
|
1976
|
+
case 2:
|
|
1977
|
+
e_12 = _a.sent();
|
|
1978
|
+
logger.debug('failed to clear cached items');
|
|
1979
|
+
return [3 /*break*/, 3];
|
|
1980
|
+
case 3:
|
|
1981
|
+
if (!this.userPool) return [3 /*break*/, 7];
|
|
1982
|
+
user = this.userPool.getCurrentUser();
|
|
1983
|
+
if (!user) return [3 /*break*/, 5];
|
|
1984
|
+
return [4 /*yield*/, this.cognitoIdentitySignOut(opts, user)];
|
|
1985
|
+
case 4:
|
|
1986
|
+
_a.sent();
|
|
1987
|
+
return [3 /*break*/, 6];
|
|
1988
|
+
case 5:
|
|
1989
|
+
logger.debug('no current Cognito user');
|
|
1990
|
+
_a.label = 6;
|
|
1991
|
+
case 6: return [3 /*break*/, 8];
|
|
1992
|
+
case 7:
|
|
1993
|
+
logger.debug('no Cognito User pool');
|
|
1994
|
+
_a.label = 8;
|
|
1995
|
+
case 8:
|
|
1996
|
+
/**
|
|
1997
|
+
* Note for future refactor - no reliable way to get username with
|
|
1998
|
+
* Cognito User Pools vs Identity when federating with Social Providers
|
|
1999
|
+
* This is why we need a well structured session object that can be inspected
|
|
2000
|
+
* and information passed back in the message below for Hub dispatch
|
|
2001
|
+
*/
|
|
2002
|
+
dispatchAuthEvent('signOut', this.user, "A user has been signed out");
|
|
2003
|
+
this.user = null;
|
|
2004
|
+
return [2 /*return*/];
|
|
2005
|
+
}
|
|
2006
|
+
});
|
|
2007
|
+
});
|
|
2008
|
+
};
|
|
2009
|
+
InternalAuthClass.prototype.cleanCachedItems = function () {
|
|
2010
|
+
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
|
2011
|
+
return tslib_1.__generator(this, function (_a) {
|
|
2012
|
+
switch (_a.label) {
|
|
2013
|
+
case 0:
|
|
2014
|
+
// clear cognito cached item
|
|
2015
|
+
return [4 /*yield*/, this.Credentials.clear()];
|
|
2016
|
+
case 1:
|
|
2017
|
+
// clear cognito cached item
|
|
2018
|
+
_a.sent();
|
|
2019
|
+
return [2 /*return*/];
|
|
2020
|
+
}
|
|
2021
|
+
});
|
|
2022
|
+
});
|
|
2023
|
+
};
|
|
2024
|
+
/**
|
|
2025
|
+
* Change a password for an authenticated user
|
|
2026
|
+
* @param {Object} user - The CognitoUser object
|
|
2027
|
+
* @param {String} oldPassword - the current password
|
|
2028
|
+
* @param {String} newPassword - the requested new password
|
|
2029
|
+
* @param {ClientMetaData} clientMetadata - optional client metadata, defaults to config
|
|
2030
|
+
* @param {CustomUserAgentDetails} customUserAgentDetails - Optional parameter to send user agent details
|
|
2031
|
+
* @return - A promise resolves if success
|
|
2032
|
+
*/
|
|
2033
|
+
InternalAuthClass.prototype.changePassword = function (user, oldPassword, newPassword, clientMetadata, customUserAgentDetails) {
|
|
2034
|
+
var _this = this;
|
|
2035
|
+
if (clientMetadata === void 0) { clientMetadata = this._config.clientMetadata; }
|
|
2036
|
+
return new Promise(function (resolve, reject) {
|
|
2037
|
+
_this.userSession(user).then(function (session) {
|
|
2038
|
+
user.changePassword(oldPassword, newPassword, function (err, data) {
|
|
2039
|
+
if (err) {
|
|
2040
|
+
logger.debug('change password failure', err);
|
|
2041
|
+
return reject(err);
|
|
2042
|
+
}
|
|
2043
|
+
else {
|
|
2044
|
+
return resolve(data);
|
|
2045
|
+
}
|
|
2046
|
+
}, clientMetadata);
|
|
2047
|
+
});
|
|
2048
|
+
});
|
|
2049
|
+
};
|
|
2050
|
+
/**
|
|
2051
|
+
* Initiate a forgot password request
|
|
2052
|
+
* @param {String} username - the username to change password
|
|
2053
|
+
* @param {ClientMetaData} clientMetadata - optional client metadata, defaults to config
|
|
2054
|
+
* @param {CustomUserAgentDetails} customUserAgentDetails - Optional parameter to send user agent details
|
|
2055
|
+
* @return - A promise resolves if success
|
|
2056
|
+
*/
|
|
2057
|
+
InternalAuthClass.prototype.forgotPassword = function (username, clientMetadata, customUserAgentDetails) {
|
|
2058
|
+
if (clientMetadata === void 0) { clientMetadata = this._config.clientMetadata; }
|
|
2059
|
+
if (!this.userPool) {
|
|
2060
|
+
return this.rejectNoUserPool();
|
|
2061
|
+
}
|
|
2062
|
+
if (!username) {
|
|
2063
|
+
return this.rejectAuthError(Auth_1.AuthErrorTypes.EmptyUsername);
|
|
2064
|
+
}
|
|
2065
|
+
var user = this.createCognitoUser(username);
|
|
2066
|
+
return new Promise(function (resolve, reject) {
|
|
2067
|
+
user.forgotPassword({
|
|
2068
|
+
onSuccess: function () {
|
|
2069
|
+
resolve();
|
|
2070
|
+
return;
|
|
2071
|
+
},
|
|
2072
|
+
onFailure: function (err) {
|
|
2073
|
+
logger.debug('forgot password failure', err);
|
|
2074
|
+
dispatchAuthEvent('forgotPassword_failure', err, username + " forgotPassword failed");
|
|
2075
|
+
reject(err);
|
|
2076
|
+
return;
|
|
2077
|
+
},
|
|
2078
|
+
inputVerificationCode: function (data) {
|
|
2079
|
+
dispatchAuthEvent('forgotPassword', user, username + " has initiated forgot password flow");
|
|
2080
|
+
resolve(data);
|
|
2081
|
+
return;
|
|
2082
|
+
},
|
|
2083
|
+
}, clientMetadata);
|
|
2084
|
+
});
|
|
2085
|
+
};
|
|
2086
|
+
/**
|
|
2087
|
+
* Confirm a new password using a confirmation Code
|
|
2088
|
+
* @param {String} username - The username
|
|
2089
|
+
* @param {String} code - The confirmation code
|
|
2090
|
+
* @param {String} password - The new password
|
|
2091
|
+
* @param {ClientMetaData} clientMetadata - optional client metadata, defaults to config
|
|
2092
|
+
* @param {CustomUserAgentDetails} customUserAgentDetails - Optional parameter to send user agent details
|
|
2093
|
+
* @return - A promise that resolves if success
|
|
2094
|
+
*/
|
|
2095
|
+
InternalAuthClass.prototype.forgotPasswordSubmit = function (username, code, password, clientMetadata, customUserAgentDetails) {
|
|
2096
|
+
if (clientMetadata === void 0) { clientMetadata = this._config.clientMetadata; }
|
|
2097
|
+
if (!this.userPool) {
|
|
2098
|
+
return this.rejectNoUserPool();
|
|
2099
|
+
}
|
|
2100
|
+
if (!username) {
|
|
2101
|
+
return this.rejectAuthError(Auth_1.AuthErrorTypes.EmptyUsername);
|
|
2102
|
+
}
|
|
2103
|
+
if (!code) {
|
|
2104
|
+
return this.rejectAuthError(Auth_1.AuthErrorTypes.EmptyCode);
|
|
2105
|
+
}
|
|
2106
|
+
if (!password) {
|
|
2107
|
+
return this.rejectAuthError(Auth_1.AuthErrorTypes.EmptyPassword);
|
|
2108
|
+
}
|
|
2109
|
+
var user = this.createCognitoUser(username);
|
|
2110
|
+
return new Promise(function (resolve, reject) {
|
|
2111
|
+
user.confirmPassword(code, password, {
|
|
2112
|
+
onSuccess: function (success) {
|
|
2113
|
+
dispatchAuthEvent('forgotPasswordSubmit', user, username + " forgotPasswordSubmit successful");
|
|
2114
|
+
resolve(success);
|
|
2115
|
+
return;
|
|
2116
|
+
},
|
|
2117
|
+
onFailure: function (err) {
|
|
2118
|
+
dispatchAuthEvent('forgotPasswordSubmit_failure', err, username + " forgotPasswordSubmit failed");
|
|
2119
|
+
reject(err);
|
|
2120
|
+
return;
|
|
2121
|
+
},
|
|
2122
|
+
}, clientMetadata);
|
|
2123
|
+
});
|
|
2124
|
+
};
|
|
2125
|
+
/**
|
|
2126
|
+
* Get user information
|
|
2127
|
+
* @async
|
|
2128
|
+
* @param {CustomUserAgentDetails} customUserAgentDetails - Optional parameter to send user agent details
|
|
2129
|
+
* @return {Object }- current User's information
|
|
2130
|
+
*/
|
|
2131
|
+
InternalAuthClass.prototype.currentUserInfo = function (customUserAgentDetails) {
|
|
2132
|
+
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
|
2133
|
+
var source, user, attributes, userAttrs, credentials, e_13, info, err_2, user;
|
|
2134
|
+
return tslib_1.__generator(this, function (_a) {
|
|
2135
|
+
switch (_a.label) {
|
|
2136
|
+
case 0:
|
|
2137
|
+
source = this.Credentials.getCredSource();
|
|
2138
|
+
if (!(!source || source === 'aws' || source === 'userPool')) return [3 /*break*/, 9];
|
|
2139
|
+
return [4 /*yield*/, this.currentUserPoolUser().catch(function (err) {
|
|
2140
|
+
return logger.error(err);
|
|
2141
|
+
})];
|
|
2142
|
+
case 1:
|
|
2143
|
+
user = _a.sent();
|
|
2144
|
+
if (!user) {
|
|
2145
|
+
return [2 /*return*/, null];
|
|
2146
|
+
}
|
|
2147
|
+
_a.label = 2;
|
|
2148
|
+
case 2:
|
|
2149
|
+
_a.trys.push([2, 8, , 9]);
|
|
2150
|
+
return [4 /*yield*/, this.userAttributes(user)];
|
|
2151
|
+
case 3:
|
|
2152
|
+
attributes = _a.sent();
|
|
2153
|
+
userAttrs = this.attributesToObject(attributes);
|
|
2154
|
+
credentials = null;
|
|
2155
|
+
_a.label = 4;
|
|
2156
|
+
case 4:
|
|
2157
|
+
_a.trys.push([4, 6, , 7]);
|
|
2158
|
+
return [4 /*yield*/, this.currentCredentials()];
|
|
2159
|
+
case 5:
|
|
2160
|
+
credentials = _a.sent();
|
|
2161
|
+
return [3 /*break*/, 7];
|
|
2162
|
+
case 6:
|
|
2163
|
+
e_13 = _a.sent();
|
|
2164
|
+
logger.debug('Failed to retrieve credentials while getting current user info', e_13);
|
|
2165
|
+
return [3 /*break*/, 7];
|
|
2166
|
+
case 7:
|
|
2167
|
+
info = {
|
|
2168
|
+
id: credentials ? credentials.identityId : undefined,
|
|
2169
|
+
username: user.getUsername(),
|
|
2170
|
+
attributes: userAttrs,
|
|
2171
|
+
};
|
|
2172
|
+
return [2 /*return*/, info];
|
|
2173
|
+
case 8:
|
|
2174
|
+
err_2 = _a.sent();
|
|
2175
|
+
logger.error('currentUserInfo error', err_2);
|
|
2176
|
+
return [2 /*return*/, {}];
|
|
2177
|
+
case 9:
|
|
2178
|
+
if (source === 'federated') {
|
|
2179
|
+
user = this.user;
|
|
2180
|
+
return [2 /*return*/, user ? user : {}];
|
|
2181
|
+
}
|
|
2182
|
+
return [2 /*return*/];
|
|
2183
|
+
}
|
|
2184
|
+
});
|
|
2185
|
+
});
|
|
2186
|
+
};
|
|
2187
|
+
InternalAuthClass.prototype.federatedSignIn = function (providerOrOptions, response, user, customUserAgentDetails) {
|
|
2188
|
+
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
|
2189
|
+
var options, provider, customState, client_id, redirect_uri, provider, loggedInUser, token, identity_id, expires_at, credentials, currentUser;
|
|
2190
|
+
return tslib_1.__generator(this, function (_a) {
|
|
2191
|
+
switch (_a.label) {
|
|
2192
|
+
case 0:
|
|
2193
|
+
if (!this._config.identityPoolId && !this._config.userPoolId) {
|
|
2194
|
+
throw new Error("Federation requires either a User Pool or Identity Pool in config");
|
|
2195
|
+
}
|
|
2196
|
+
// Ensure backwards compatability
|
|
2197
|
+
if (typeof providerOrOptions === 'undefined') {
|
|
2198
|
+
if (this._config.identityPoolId && !this._config.userPoolId) {
|
|
2199
|
+
throw new Error("Federation with Identity Pools requires tokens passed as arguments");
|
|
2200
|
+
}
|
|
2201
|
+
}
|
|
2202
|
+
if (!(types_1.isFederatedSignInOptions(providerOrOptions) ||
|
|
2203
|
+
types_1.isFederatedSignInOptionsCustom(providerOrOptions) ||
|
|
2204
|
+
types_1.hasCustomState(providerOrOptions) ||
|
|
2205
|
+
typeof providerOrOptions === 'undefined')) return [3 /*break*/, 1];
|
|
2206
|
+
options = providerOrOptions || {
|
|
2207
|
+
provider: Auth_1.CognitoHostedUIIdentityProvider.Cognito,
|
|
2208
|
+
};
|
|
2209
|
+
provider = types_1.isFederatedSignInOptions(options)
|
|
2210
|
+
? options.provider
|
|
2211
|
+
: options.customProvider;
|
|
2212
|
+
customState = types_1.isFederatedSignInOptions(options)
|
|
2213
|
+
? options.customState
|
|
2214
|
+
: options.customState;
|
|
2215
|
+
if (this._config.userPoolId) {
|
|
2216
|
+
client_id = types_1.isCognitoHostedOpts(this._config.oauth)
|
|
2217
|
+
? this._config.userPoolWebClientId
|
|
2218
|
+
: this._config.oauth.clientID;
|
|
2219
|
+
redirect_uri = types_1.isCognitoHostedOpts(this._config.oauth)
|
|
2220
|
+
? this._config.oauth.redirectSignIn
|
|
2221
|
+
: this._config.oauth.redirectUri;
|
|
2222
|
+
this._oAuthHandler.oauthSignIn(this._config.oauth.responseType, this._config.oauth.domain, redirect_uri, client_id, provider, customState);
|
|
2223
|
+
}
|
|
2224
|
+
return [3 /*break*/, 4];
|
|
2225
|
+
case 1:
|
|
2226
|
+
provider = providerOrOptions;
|
|
2227
|
+
// To check if the user is already logged in
|
|
2228
|
+
try {
|
|
2229
|
+
loggedInUser = JSON.stringify(JSON.parse(this._storage.getItem('aws-amplify-federatedInfo')).user);
|
|
2230
|
+
if (loggedInUser) {
|
|
2231
|
+
logger.warn("There is already a signed in user: " + loggedInUser + " in your app.\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tYou should not call Auth.federatedSignIn method again as it may cause unexpected behavior.");
|
|
2232
|
+
}
|
|
2233
|
+
}
|
|
2234
|
+
catch (e) { }
|
|
2235
|
+
token = response.token, identity_id = response.identity_id, expires_at = response.expires_at;
|
|
2236
|
+
return [4 /*yield*/, this.Credentials.set({ provider: provider, token: token, identity_id: identity_id, user: user, expires_at: expires_at }, 'federation')];
|
|
2237
|
+
case 2:
|
|
2238
|
+
credentials = _a.sent();
|
|
2239
|
+
return [4 /*yield*/, this.currentAuthenticatedUser()];
|
|
2240
|
+
case 3:
|
|
2241
|
+
currentUser = _a.sent();
|
|
2242
|
+
dispatchAuthEvent('signIn', currentUser, "A user " + currentUser.username + " has been signed in");
|
|
2243
|
+
logger.debug('federated sign in credentials', credentials);
|
|
2244
|
+
return [2 /*return*/, credentials];
|
|
2245
|
+
case 4: return [2 /*return*/];
|
|
2246
|
+
}
|
|
2247
|
+
});
|
|
2248
|
+
});
|
|
2249
|
+
};
|
|
2250
|
+
/**
|
|
2251
|
+
* Used to complete the OAuth flow with or without the Cognito Hosted UI
|
|
2252
|
+
* @param {String} URL - optional parameter for customers to pass in the response URL
|
|
2253
|
+
*/
|
|
2254
|
+
InternalAuthClass.prototype._handleAuthResponse = function (URL) {
|
|
2255
|
+
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
|
2256
|
+
var currentUrl, hasCodeOrError, hasTokenOrError, _a, accessToken, idToken, refreshToken, state, session, credentials, isCustomStateIncluded, currentUser, customState, err_3;
|
|
2257
|
+
return tslib_1.__generator(this, function (_b) {
|
|
2258
|
+
switch (_b.label) {
|
|
2259
|
+
case 0:
|
|
2260
|
+
if (this.oAuthFlowInProgress) {
|
|
2261
|
+
logger.debug("Skipping URL " + URL + " current flow in progress");
|
|
2262
|
+
return [2 /*return*/];
|
|
2263
|
+
}
|
|
2264
|
+
_b.label = 1;
|
|
2265
|
+
case 1:
|
|
2266
|
+
_b.trys.push([1, , 8, 9]);
|
|
2267
|
+
this.oAuthFlowInProgress = true;
|
|
2268
|
+
if (!this._config.userPoolId) {
|
|
2269
|
+
throw new Error("OAuth responses require a User Pool defined in config");
|
|
2270
|
+
}
|
|
2271
|
+
dispatchAuthEvent('parsingCallbackUrl', { url: URL }, "The callback url is being parsed");
|
|
2272
|
+
currentUrl = URL || (core_1.browserOrNode().isBrowser ? window.location.href : '');
|
|
2273
|
+
hasCodeOrError = !!(url_1.parse(currentUrl).query || '')
|
|
2274
|
+
.split('&')
|
|
2275
|
+
.map(function (entry) { return entry.split('='); })
|
|
2276
|
+
.find(function (_a) {
|
|
2277
|
+
var _b = tslib_1.__read(_a, 1), k = _b[0];
|
|
2278
|
+
return k === 'code' || k === 'error';
|
|
2279
|
+
});
|
|
2280
|
+
hasTokenOrError = !!(url_1.parse(currentUrl).hash || '#')
|
|
2281
|
+
.substr(1)
|
|
2282
|
+
.split('&')
|
|
2283
|
+
.map(function (entry) { return entry.split('='); })
|
|
2284
|
+
.find(function (_a) {
|
|
2285
|
+
var _b = tslib_1.__read(_a, 1), k = _b[0];
|
|
2286
|
+
return k === 'access_token' || k === 'error';
|
|
2287
|
+
});
|
|
2288
|
+
if (!(hasCodeOrError || hasTokenOrError)) return [3 /*break*/, 7];
|
|
2289
|
+
this._storage.setItem('amplify-redirected-from-hosted-ui', 'true');
|
|
2290
|
+
_b.label = 2;
|
|
2291
|
+
case 2:
|
|
2292
|
+
_b.trys.push([2, 6, , 7]);
|
|
2293
|
+
return [4 /*yield*/, this._oAuthHandler.handleAuthResponse(currentUrl)];
|
|
2294
|
+
case 3:
|
|
2295
|
+
_a = _b.sent(), accessToken = _a.accessToken, idToken = _a.idToken, refreshToken = _a.refreshToken, state = _a.state;
|
|
2296
|
+
session = new amazon_cognito_identity_js_1.CognitoUserSession({
|
|
2297
|
+
IdToken: new amazon_cognito_identity_js_1.CognitoIdToken({ IdToken: idToken }),
|
|
2298
|
+
RefreshToken: new amazon_cognito_identity_js_1.CognitoRefreshToken({
|
|
2299
|
+
RefreshToken: refreshToken,
|
|
2300
|
+
}),
|
|
2301
|
+
AccessToken: new amazon_cognito_identity_js_1.CognitoAccessToken({
|
|
2302
|
+
AccessToken: accessToken,
|
|
2303
|
+
}),
|
|
2304
|
+
});
|
|
2305
|
+
credentials = void 0;
|
|
2306
|
+
if (!this._config.identityPoolId) return [3 /*break*/, 5];
|
|
2307
|
+
return [4 /*yield*/, this.Credentials.set(session, 'session')];
|
|
2308
|
+
case 4:
|
|
2309
|
+
credentials = _b.sent();
|
|
2310
|
+
logger.debug('AWS credentials', credentials);
|
|
2311
|
+
_b.label = 5;
|
|
2312
|
+
case 5:
|
|
2313
|
+
isCustomStateIncluded = /-/.test(state);
|
|
2314
|
+
currentUser = this.createCognitoUser(session.getIdToken().decodePayload()['cognito:username']);
|
|
2315
|
+
// This calls cacheTokens() in Cognito SDK
|
|
2316
|
+
currentUser.setSignInUserSession(session);
|
|
2317
|
+
if (window && typeof window.history !== 'undefined') {
|
|
2318
|
+
window.history.replaceState({}, null, this._config.oauth.redirectSignIn);
|
|
2319
|
+
}
|
|
2320
|
+
dispatchAuthEvent('signIn', currentUser, "A user " + currentUser.getUsername() + " has been signed in");
|
|
2321
|
+
dispatchAuthEvent('cognitoHostedUI', currentUser, "A user " + currentUser.getUsername() + " has been signed in via Cognito Hosted UI");
|
|
2322
|
+
if (isCustomStateIncluded) {
|
|
2323
|
+
customState = state.split('-').splice(1).join('-');
|
|
2324
|
+
dispatchAuthEvent('customOAuthState', core_1.urlSafeDecode(customState), "State for user " + currentUser.getUsername());
|
|
2325
|
+
}
|
|
2326
|
+
//#endregion
|
|
2327
|
+
return [2 /*return*/, credentials];
|
|
2328
|
+
case 6:
|
|
2329
|
+
err_3 = _b.sent();
|
|
2330
|
+
logger.debug('Error in cognito hosted auth response', err_3);
|
|
2331
|
+
// Just like a successful handling of `?code`, replace the window history to "dispose" of the `code`.
|
|
2332
|
+
// Otherwise, reloading the page will throw errors as the `code` has already been spent.
|
|
2333
|
+
if (window && typeof window.history !== 'undefined') {
|
|
2334
|
+
window.history.replaceState({}, null, this._config.oauth.redirectSignIn);
|
|
2335
|
+
}
|
|
2336
|
+
dispatchAuthEvent('signIn_failure', err_3, "The OAuth response flow failed");
|
|
2337
|
+
dispatchAuthEvent('cognitoHostedUI_failure', err_3, "A failure occurred when returning to the Cognito Hosted UI");
|
|
2338
|
+
dispatchAuthEvent('customState_failure', err_3, "A failure occurred when returning state");
|
|
2339
|
+
return [3 /*break*/, 7];
|
|
2340
|
+
case 7: return [3 /*break*/, 9];
|
|
2341
|
+
case 8:
|
|
2342
|
+
this.oAuthFlowInProgress = false;
|
|
2343
|
+
return [7 /*endfinally*/];
|
|
2344
|
+
case 9: return [2 /*return*/];
|
|
2345
|
+
}
|
|
2346
|
+
});
|
|
2347
|
+
});
|
|
2348
|
+
};
|
|
2349
|
+
/**
|
|
2350
|
+
* Compact version of credentials
|
|
2351
|
+
* @param {Object} credentials
|
|
2352
|
+
* @return {Object} - Credentials
|
|
2353
|
+
*/
|
|
2354
|
+
InternalAuthClass.prototype.essentialCredentials = function (credentials) {
|
|
2355
|
+
return {
|
|
2356
|
+
accessKeyId: credentials.accessKeyId,
|
|
2357
|
+
sessionToken: credentials.sessionToken,
|
|
2358
|
+
secretAccessKey: credentials.secretAccessKey,
|
|
2359
|
+
identityId: credentials.identityId,
|
|
2360
|
+
authenticated: credentials.authenticated,
|
|
2361
|
+
};
|
|
2362
|
+
};
|
|
2363
|
+
InternalAuthClass.prototype.attributesToObject = function (attributes) {
|
|
2364
|
+
var _this = this;
|
|
2365
|
+
var obj = {};
|
|
2366
|
+
if (attributes) {
|
|
2367
|
+
attributes.map(function (attribute) {
|
|
2368
|
+
if (attribute.Name === 'email_verified' ||
|
|
2369
|
+
attribute.Name === 'phone_number_verified') {
|
|
2370
|
+
obj[attribute.Name] =
|
|
2371
|
+
_this.isTruthyString(attribute.Value) || attribute.Value === true;
|
|
2372
|
+
}
|
|
2373
|
+
else {
|
|
2374
|
+
obj[attribute.Name] = attribute.Value;
|
|
2375
|
+
}
|
|
2376
|
+
});
|
|
2377
|
+
}
|
|
2378
|
+
return obj;
|
|
2379
|
+
};
|
|
2380
|
+
InternalAuthClass.prototype.isTruthyString = function (value) {
|
|
2381
|
+
return (typeof value.toLowerCase === 'function' && value.toLowerCase() === 'true');
|
|
2382
|
+
};
|
|
2383
|
+
InternalAuthClass.prototype.createCognitoUser = function (username) {
|
|
2384
|
+
var userData = {
|
|
2385
|
+
Username: username,
|
|
2386
|
+
Pool: this.userPool,
|
|
2387
|
+
};
|
|
2388
|
+
userData.Storage = this._storage;
|
|
2389
|
+
var authenticationFlowType = this._config.authenticationFlowType;
|
|
2390
|
+
var user = new amazon_cognito_identity_js_1.CognitoUser(userData);
|
|
2391
|
+
if (authenticationFlowType) {
|
|
2392
|
+
user.setAuthenticationFlowType(authenticationFlowType);
|
|
2393
|
+
}
|
|
2394
|
+
return user;
|
|
2395
|
+
};
|
|
2396
|
+
InternalAuthClass.prototype._isValidAuthStorage = function (obj) {
|
|
2397
|
+
// We need to check if the obj has the functions of Storage
|
|
2398
|
+
return (!!obj &&
|
|
2399
|
+
typeof obj.getItem === 'function' &&
|
|
2400
|
+
typeof obj.setItem === 'function' &&
|
|
2401
|
+
typeof obj.removeItem === 'function' &&
|
|
2402
|
+
typeof obj.clear === 'function');
|
|
2403
|
+
};
|
|
2404
|
+
InternalAuthClass.prototype.noUserPoolErrorHandler = function (config) {
|
|
2405
|
+
if (config) {
|
|
2406
|
+
if (!config.userPoolId || !config.identityPoolId) {
|
|
2407
|
+
return Auth_1.AuthErrorTypes.MissingAuthConfig;
|
|
2408
|
+
}
|
|
2409
|
+
}
|
|
2410
|
+
return Auth_1.AuthErrorTypes.NoConfig;
|
|
2411
|
+
};
|
|
2412
|
+
InternalAuthClass.prototype.rejectAuthError = function (type) {
|
|
2413
|
+
return Promise.reject(new Errors_1.AuthError(type));
|
|
2414
|
+
};
|
|
2415
|
+
InternalAuthClass.prototype.rejectNoUserPool = function () {
|
|
2416
|
+
var type = this.noUserPoolErrorHandler(this._config);
|
|
2417
|
+
return Promise.reject(new Errors_1.NoUserPoolError(type));
|
|
2418
|
+
};
|
|
2419
|
+
InternalAuthClass.prototype.rememberDevice = function (customUserAgentDetails) {
|
|
2420
|
+
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
|
2421
|
+
var currUser, error_2;
|
|
2422
|
+
return tslib_1.__generator(this, function (_a) {
|
|
2423
|
+
switch (_a.label) {
|
|
2424
|
+
case 0:
|
|
2425
|
+
_a.trys.push([0, 2, , 3]);
|
|
2426
|
+
return [4 /*yield*/, this.currentUserPoolUser()];
|
|
2427
|
+
case 1:
|
|
2428
|
+
currUser = _a.sent();
|
|
2429
|
+
return [3 /*break*/, 3];
|
|
2430
|
+
case 2:
|
|
2431
|
+
error_2 = _a.sent();
|
|
2432
|
+
logger.debug('The user is not authenticated by the error', error_2);
|
|
2433
|
+
return [2 /*return*/, Promise.reject('The user is not authenticated')];
|
|
2434
|
+
case 3:
|
|
2435
|
+
currUser.getCachedDeviceKeyAndPassword();
|
|
2436
|
+
return [2 /*return*/, new Promise(function (res, rej) {
|
|
2437
|
+
currUser.setDeviceStatusRemembered({
|
|
2438
|
+
onSuccess: function (data) {
|
|
2439
|
+
res(data);
|
|
2440
|
+
},
|
|
2441
|
+
onFailure: function (err) {
|
|
2442
|
+
if (err.code === 'InvalidParameterException') {
|
|
2443
|
+
rej(new Errors_1.AuthError(Auth_1.AuthErrorTypes.DeviceConfig));
|
|
2444
|
+
}
|
|
2445
|
+
else if (err.code === 'NetworkError') {
|
|
2446
|
+
rej(new Errors_1.AuthError(Auth_1.AuthErrorTypes.NetworkError));
|
|
2447
|
+
}
|
|
2448
|
+
else {
|
|
2449
|
+
rej(err);
|
|
2450
|
+
}
|
|
2451
|
+
},
|
|
2452
|
+
});
|
|
2453
|
+
})];
|
|
2454
|
+
}
|
|
2455
|
+
});
|
|
2456
|
+
});
|
|
2457
|
+
};
|
|
2458
|
+
InternalAuthClass.prototype.forgetDevice = function (customUserAgentDetails) {
|
|
2459
|
+
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
|
2460
|
+
var currUser, error_3;
|
|
2461
|
+
return tslib_1.__generator(this, function (_a) {
|
|
2462
|
+
switch (_a.label) {
|
|
2463
|
+
case 0:
|
|
2464
|
+
_a.trys.push([0, 2, , 3]);
|
|
2465
|
+
return [4 /*yield*/, this.currentUserPoolUser()];
|
|
2466
|
+
case 1:
|
|
2467
|
+
currUser = _a.sent();
|
|
2468
|
+
return [3 /*break*/, 3];
|
|
2469
|
+
case 2:
|
|
2470
|
+
error_3 = _a.sent();
|
|
2471
|
+
logger.debug('The user is not authenticated by the error', error_3);
|
|
2472
|
+
return [2 /*return*/, Promise.reject('The user is not authenticated')];
|
|
2473
|
+
case 3:
|
|
2474
|
+
currUser.getCachedDeviceKeyAndPassword();
|
|
2475
|
+
return [2 /*return*/, new Promise(function (res, rej) {
|
|
2476
|
+
currUser.forgetDevice({
|
|
2477
|
+
onSuccess: function (data) {
|
|
2478
|
+
res(data);
|
|
2479
|
+
},
|
|
2480
|
+
onFailure: function (err) {
|
|
2481
|
+
if (err.code === 'InvalidParameterException') {
|
|
2482
|
+
rej(new Errors_1.AuthError(Auth_1.AuthErrorTypes.DeviceConfig));
|
|
2483
|
+
}
|
|
2484
|
+
else if (err.code === 'NetworkError') {
|
|
2485
|
+
rej(new Errors_1.AuthError(Auth_1.AuthErrorTypes.NetworkError));
|
|
2486
|
+
}
|
|
2487
|
+
else {
|
|
2488
|
+
rej(err);
|
|
2489
|
+
}
|
|
2490
|
+
},
|
|
2491
|
+
});
|
|
2492
|
+
})];
|
|
2493
|
+
}
|
|
2494
|
+
});
|
|
2495
|
+
});
|
|
2496
|
+
};
|
|
2497
|
+
InternalAuthClass.prototype.fetchDevices = function (customUserAgentDetails) {
|
|
2498
|
+
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
|
2499
|
+
var currUser, error_4;
|
|
2500
|
+
return tslib_1.__generator(this, function (_a) {
|
|
2501
|
+
switch (_a.label) {
|
|
2502
|
+
case 0:
|
|
2503
|
+
_a.trys.push([0, 2, , 3]);
|
|
2504
|
+
return [4 /*yield*/, this.currentUserPoolUser()];
|
|
2505
|
+
case 1:
|
|
2506
|
+
currUser = _a.sent();
|
|
2507
|
+
return [3 /*break*/, 3];
|
|
2508
|
+
case 2:
|
|
2509
|
+
error_4 = _a.sent();
|
|
2510
|
+
logger.debug('The user is not authenticated by the error', error_4);
|
|
2511
|
+
throw new Error('The user is not authenticated');
|
|
2512
|
+
case 3:
|
|
2513
|
+
currUser.getCachedDeviceKeyAndPassword();
|
|
2514
|
+
return [2 /*return*/, new Promise(function (res, rej) {
|
|
2515
|
+
var cb = {
|
|
2516
|
+
onSuccess: function (data) {
|
|
2517
|
+
var deviceList = data.Devices.map(function (device) {
|
|
2518
|
+
var deviceName = device.DeviceAttributes.find(function (_a) {
|
|
2519
|
+
var Name = _a.Name;
|
|
2520
|
+
return Name === 'device_name';
|
|
2521
|
+
}) || {};
|
|
2522
|
+
var deviceInfo = {
|
|
2523
|
+
id: device.DeviceKey,
|
|
2524
|
+
name: deviceName.Value,
|
|
2525
|
+
};
|
|
2526
|
+
return deviceInfo;
|
|
2527
|
+
});
|
|
2528
|
+
res(deviceList);
|
|
2529
|
+
},
|
|
2530
|
+
onFailure: function (err) {
|
|
2531
|
+
if (err.code === 'InvalidParameterException') {
|
|
2532
|
+
rej(new Errors_1.AuthError(Auth_1.AuthErrorTypes.DeviceConfig));
|
|
2533
|
+
}
|
|
2534
|
+
else if (err.code === 'NetworkError') {
|
|
2535
|
+
rej(new Errors_1.AuthError(Auth_1.AuthErrorTypes.NetworkError));
|
|
2536
|
+
}
|
|
2537
|
+
else {
|
|
2538
|
+
rej(err);
|
|
2539
|
+
}
|
|
2540
|
+
},
|
|
2541
|
+
};
|
|
2542
|
+
currUser.listDevices(MAX_DEVICES, null, cb);
|
|
2543
|
+
})];
|
|
2544
|
+
}
|
|
2545
|
+
});
|
|
2546
|
+
});
|
|
2547
|
+
};
|
|
2548
|
+
return InternalAuthClass;
|
|
2549
|
+
}());
|
|
2550
|
+
exports.InternalAuthClass = InternalAuthClass;
|
|
2551
|
+
exports.InternalAuth = new InternalAuthClass(null);
|
|
2552
|
+
core_1.Amplify.register(exports.InternalAuth);
|
|
2553
|
+
//# sourceMappingURL=InternalAuth.js.map
|