@aws-amplify/auth 5.0.2-unstable.5 → 5.0.2-unstable.7
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/lib/Auth.d.ts +3 -0
- package/lib/Auth.js +155 -143
- package/lib/Auth.js.map +1 -1
- package/lib-esm/Auth.d.ts +3 -0
- package/lib-esm/Auth.js +155 -143
- package/lib-esm/Auth.js.map +1 -1
- package/package.json +4 -4
- package/src/Auth.ts +113 -112
package/lib-esm/Auth.js
CHANGED
|
@@ -38,6 +38,8 @@ var AuthClass = /** @class */ (function () {
|
|
|
38
38
|
this.user = null;
|
|
39
39
|
this.oAuthFlowInProgress = false;
|
|
40
40
|
this.autoSignInInitiated = false;
|
|
41
|
+
this.inflightSessionPromise = null;
|
|
42
|
+
this.inflightSessionPromiseCounter = 0;
|
|
41
43
|
this.Credentials = Credentials;
|
|
42
44
|
this.wrapRefreshSessionCallback = function (callback) {
|
|
43
45
|
var wrapped = function (error, data) {
|
|
@@ -1374,10 +1376,10 @@ var AuthClass = /** @class */ (function () {
|
|
|
1374
1376
|
return new Promise(function (res, rej) {
|
|
1375
1377
|
_this._storageSync
|
|
1376
1378
|
.then(function () { return __awaiter(_this, void 0, void 0, function () {
|
|
1377
|
-
var user, clientMetadata;
|
|
1379
|
+
var user, session, bypassCache, clientMetadata, _a, scope, err_1;
|
|
1378
1380
|
var _this = this;
|
|
1379
|
-
return __generator(this, function (
|
|
1380
|
-
switch (
|
|
1381
|
+
return __generator(this, function (_b) {
|
|
1382
|
+
switch (_b.label) {
|
|
1381
1383
|
case 0:
|
|
1382
1384
|
if (!this.isOAuthInProgress()) return [3 /*break*/, 2];
|
|
1383
1385
|
logger.debug('OAuth signIn in progress, waiting for resolution...');
|
|
@@ -1401,8 +1403,8 @@ var AuthClass = /** @class */ (function () {
|
|
|
1401
1403
|
}
|
|
1402
1404
|
})];
|
|
1403
1405
|
case 1:
|
|
1404
|
-
|
|
1405
|
-
|
|
1406
|
+
_b.sent();
|
|
1407
|
+
_b.label = 2;
|
|
1406
1408
|
case 2:
|
|
1407
1409
|
user = this.userPool.getCurrentUser();
|
|
1408
1410
|
if (!user) {
|
|
@@ -1410,96 +1412,77 @@ var AuthClass = /** @class */ (function () {
|
|
|
1410
1412
|
rej('No current user');
|
|
1411
1413
|
return [2 /*return*/];
|
|
1412
1414
|
}
|
|
1415
|
+
_b.label = 3;
|
|
1416
|
+
case 3:
|
|
1417
|
+
_b.trys.push([3, 7, , 8]);
|
|
1418
|
+
return [4 /*yield*/, this._userSession(user)];
|
|
1419
|
+
case 4:
|
|
1420
|
+
session = _b.sent();
|
|
1421
|
+
bypassCache = params ? params.bypassCache : false;
|
|
1422
|
+
if (!bypassCache) return [3 /*break*/, 6];
|
|
1423
|
+
return [4 /*yield*/, this.Credentials.clear()];
|
|
1424
|
+
case 5:
|
|
1425
|
+
_b.sent();
|
|
1426
|
+
_b.label = 6;
|
|
1427
|
+
case 6:
|
|
1413
1428
|
clientMetadata = this._config.clientMetadata;
|
|
1414
|
-
|
|
1415
|
-
|
|
1416
|
-
|
|
1417
|
-
|
|
1418
|
-
|
|
1419
|
-
|
|
1420
|
-
|
|
1421
|
-
|
|
1422
|
-
|
|
1423
|
-
|
|
1424
|
-
|
|
1425
|
-
|
|
1426
|
-
|
|
1427
|
-
|
|
1428
|
-
|
|
1429
|
-
|
|
1430
|
-
|
|
1431
|
-
|
|
1432
|
-
|
|
1433
|
-
|
|
1434
|
-
|
|
1435
|
-
|
|
1436
|
-
|
|
1437
|
-
|
|
1438
|
-
|
|
1439
|
-
|
|
1440
|
-
|
|
1441
|
-
return [
|
|
1442
|
-
|
|
1443
|
-
|
|
1444
|
-
|
|
1445
|
-
|
|
1446
|
-
|
|
1447
|
-
|
|
1448
|
-
|
|
1449
|
-
|
|
1450
|
-
|
|
1451
|
-
|
|
1452
|
-
|
|
1453
|
-
|
|
1454
|
-
|
|
1455
|
-
logger.debug('getting user data failed', err);
|
|
1456
|
-
if (!this.isSessionInvalid(err)) return [3 /*break*/, 5];
|
|
1457
|
-
_a.label = 1;
|
|
1458
|
-
case 1:
|
|
1459
|
-
_a.trys.push([1, 3, , 4]);
|
|
1460
|
-
return [4 /*yield*/, this.cleanUpInvalidSession(user)];
|
|
1461
|
-
case 2:
|
|
1462
|
-
_a.sent();
|
|
1463
|
-
return [3 /*break*/, 4];
|
|
1464
|
-
case 3:
|
|
1465
|
-
cleanUpError_6 = _a.sent();
|
|
1466
|
-
rej(new Error("Session is invalid due to: " + err.message + " and failed to clean up invalid session: " + cleanUpError_6.message));
|
|
1467
|
-
return [2 /*return*/];
|
|
1468
|
-
case 4:
|
|
1469
|
-
rej(err);
|
|
1470
|
-
return [3 /*break*/, 6];
|
|
1471
|
-
case 5:
|
|
1472
|
-
res(user);
|
|
1473
|
-
_a.label = 6;
|
|
1474
|
-
case 6: return [2 /*return*/];
|
|
1475
|
-
case 7:
|
|
1476
|
-
preferredMFA = data.PreferredMfaSetting || 'NOMFA';
|
|
1477
|
-
attributeList = [];
|
|
1478
|
-
for (i = 0; i < data.UserAttributes.length; i++) {
|
|
1479
|
-
attribute = {
|
|
1480
|
-
Name: data.UserAttributes[i].Name,
|
|
1481
|
-
Value: data.UserAttributes[i].Value,
|
|
1482
|
-
};
|
|
1483
|
-
userAttribute = new CognitoUserAttribute(attribute);
|
|
1484
|
-
attributeList.push(userAttribute);
|
|
1485
|
-
}
|
|
1486
|
-
attributes = this.attributesToObject(attributeList);
|
|
1487
|
-
Object.assign(user, { attributes: attributes, preferredMFA: preferredMFA });
|
|
1488
|
-
return [2 /*return*/, res(user)];
|
|
1489
|
-
}
|
|
1490
|
-
});
|
|
1491
|
-
}); }, { bypassCache: bypassCache, clientMetadata: clientMetadata });
|
|
1492
|
-
}
|
|
1493
|
-
else {
|
|
1494
|
-
logger.debug("Unable to get the user data because the " + USER_ADMIN_SCOPE + " " +
|
|
1495
|
-
"is not in the scopes of the access token");
|
|
1429
|
+
_a = session.getAccessToken().decodePayload().scope, scope = _a === void 0 ? '' : _a;
|
|
1430
|
+
if (scope.split(' ').includes(USER_ADMIN_SCOPE)) {
|
|
1431
|
+
user.getUserData(function (err, data) { return __awaiter(_this, void 0, void 0, function () {
|
|
1432
|
+
var cleanUpError_5, preferredMFA, attributeList, i, attribute, userAttribute, attributes;
|
|
1433
|
+
return __generator(this, function (_a) {
|
|
1434
|
+
switch (_a.label) {
|
|
1435
|
+
case 0:
|
|
1436
|
+
if (!err) return [3 /*break*/, 7];
|
|
1437
|
+
logger.debug('getting user data failed', err);
|
|
1438
|
+
if (!this.isSessionInvalid(err)) return [3 /*break*/, 5];
|
|
1439
|
+
_a.label = 1;
|
|
1440
|
+
case 1:
|
|
1441
|
+
_a.trys.push([1, 3, , 4]);
|
|
1442
|
+
return [4 /*yield*/, this.cleanUpInvalidSession(user)];
|
|
1443
|
+
case 2:
|
|
1444
|
+
_a.sent();
|
|
1445
|
+
return [3 /*break*/, 4];
|
|
1446
|
+
case 3:
|
|
1447
|
+
cleanUpError_5 = _a.sent();
|
|
1448
|
+
rej(new Error("Session is invalid due to: " + err.message + " and failed to clean up invalid session: " + cleanUpError_5.message));
|
|
1449
|
+
return [2 /*return*/];
|
|
1450
|
+
case 4:
|
|
1451
|
+
rej(err);
|
|
1452
|
+
return [3 /*break*/, 6];
|
|
1453
|
+
case 5:
|
|
1454
|
+
res(user);
|
|
1455
|
+
_a.label = 6;
|
|
1456
|
+
case 6: return [2 /*return*/];
|
|
1457
|
+
case 7:
|
|
1458
|
+
preferredMFA = data.PreferredMfaSetting || 'NOMFA';
|
|
1459
|
+
attributeList = [];
|
|
1460
|
+
for (i = 0; i < data.UserAttributes.length; i++) {
|
|
1461
|
+
attribute = {
|
|
1462
|
+
Name: data.UserAttributes[i].Name,
|
|
1463
|
+
Value: data.UserAttributes[i].Value,
|
|
1464
|
+
};
|
|
1465
|
+
userAttribute = new CognitoUserAttribute(attribute);
|
|
1466
|
+
attributeList.push(userAttribute);
|
|
1467
|
+
}
|
|
1468
|
+
attributes = this.attributesToObject(attributeList);
|
|
1469
|
+
Object.assign(user, { attributes: attributes, preferredMFA: preferredMFA });
|
|
1496
1470
|
return [2 /*return*/, res(user)];
|
|
1497
|
-
|
|
1498
|
-
|
|
1499
|
-
|
|
1500
|
-
|
|
1501
|
-
|
|
1502
|
-
|
|
1471
|
+
}
|
|
1472
|
+
});
|
|
1473
|
+
}); }, { bypassCache: bypassCache, clientMetadata: clientMetadata });
|
|
1474
|
+
}
|
|
1475
|
+
else {
|
|
1476
|
+
logger.debug("Unable to get the user data because the " + USER_ADMIN_SCOPE + " " +
|
|
1477
|
+
"is not in the scopes of the access token");
|
|
1478
|
+
return [2 /*return*/, res(user)];
|
|
1479
|
+
}
|
|
1480
|
+
return [3 /*break*/, 8];
|
|
1481
|
+
case 7:
|
|
1482
|
+
err_1 = _b.sent();
|
|
1483
|
+
rej(err_1);
|
|
1484
|
+
return [3 /*break*/, 8];
|
|
1485
|
+
case 8: return [2 /*return*/];
|
|
1503
1486
|
}
|
|
1504
1487
|
});
|
|
1505
1488
|
}); })
|
|
@@ -1609,50 +1592,79 @@ var AuthClass = /** @class */ (function () {
|
|
|
1609
1592
|
});
|
|
1610
1593
|
});
|
|
1611
1594
|
};
|
|
1595
|
+
AuthClass.prototype._userSession = function (user) {
|
|
1596
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
1597
|
+
var clientMetadata, userSession;
|
|
1598
|
+
var _this = this;
|
|
1599
|
+
return __generator(this, function (_a) {
|
|
1600
|
+
switch (_a.label) {
|
|
1601
|
+
case 0:
|
|
1602
|
+
if (!user) {
|
|
1603
|
+
logger.debug('the user is null');
|
|
1604
|
+
return [2 /*return*/, this.rejectAuthError(AuthErrorTypes.NoUserSession)];
|
|
1605
|
+
}
|
|
1606
|
+
clientMetadata = this._config.clientMetadata;
|
|
1607
|
+
// Debouncing the concurrent userSession calls by caching the promise.
|
|
1608
|
+
// This solution assumes users will always call this function with the same CognitoUser instance.
|
|
1609
|
+
if (this.inflightSessionPromiseCounter === 0) {
|
|
1610
|
+
this.inflightSessionPromise = new Promise(function (res, rej) {
|
|
1611
|
+
user.getSession(function (err, session) { return __awaiter(_this, void 0, void 0, function () {
|
|
1612
|
+
var cleanUpError_6;
|
|
1613
|
+
return __generator(this, function (_a) {
|
|
1614
|
+
switch (_a.label) {
|
|
1615
|
+
case 0:
|
|
1616
|
+
if (!err) return [3 /*break*/, 5];
|
|
1617
|
+
logger.debug('Failed to get the session from user', user);
|
|
1618
|
+
if (!this.isSessionInvalid(err)) return [3 /*break*/, 4];
|
|
1619
|
+
_a.label = 1;
|
|
1620
|
+
case 1:
|
|
1621
|
+
_a.trys.push([1, 3, , 4]);
|
|
1622
|
+
return [4 /*yield*/, this.cleanUpInvalidSession(user)];
|
|
1623
|
+
case 2:
|
|
1624
|
+
_a.sent();
|
|
1625
|
+
return [3 /*break*/, 4];
|
|
1626
|
+
case 3:
|
|
1627
|
+
cleanUpError_6 = _a.sent();
|
|
1628
|
+
rej(new Error("Session is invalid due to: " + err.message + " and failed to clean up invalid session: " + cleanUpError_6.message));
|
|
1629
|
+
return [2 /*return*/];
|
|
1630
|
+
case 4:
|
|
1631
|
+
rej(err);
|
|
1632
|
+
return [2 /*return*/];
|
|
1633
|
+
case 5:
|
|
1634
|
+
logger.debug('Succeed to get the user session', session);
|
|
1635
|
+
res(session);
|
|
1636
|
+
return [2 /*return*/];
|
|
1637
|
+
}
|
|
1638
|
+
});
|
|
1639
|
+
}); }, { clientMetadata: clientMetadata });
|
|
1640
|
+
});
|
|
1641
|
+
}
|
|
1642
|
+
this.inflightSessionPromiseCounter++;
|
|
1643
|
+
_a.label = 1;
|
|
1644
|
+
case 1:
|
|
1645
|
+
_a.trys.push([1, , 3, 4]);
|
|
1646
|
+
return [4 /*yield*/, this.inflightSessionPromise];
|
|
1647
|
+
case 2:
|
|
1648
|
+
userSession = _a.sent();
|
|
1649
|
+
// Set private member. Avoid user.setSignInUserSession() to prevent excessive localstorage refresh.
|
|
1650
|
+
// @ts-ignore
|
|
1651
|
+
user.signInUserSession = userSession;
|
|
1652
|
+
return [2 /*return*/, userSession];
|
|
1653
|
+
case 3:
|
|
1654
|
+
this.inflightSessionPromiseCounter--;
|
|
1655
|
+
return [7 /*endfinally*/];
|
|
1656
|
+
case 4: return [2 /*return*/];
|
|
1657
|
+
}
|
|
1658
|
+
});
|
|
1659
|
+
});
|
|
1660
|
+
};
|
|
1612
1661
|
/**
|
|
1613
1662
|
* Get the corresponding user session
|
|
1614
1663
|
* @param {Object} user - The CognitoUser object
|
|
1615
1664
|
* @return - A promise resolves to the session
|
|
1616
1665
|
*/
|
|
1617
1666
|
AuthClass.prototype.userSession = function (user) {
|
|
1618
|
-
|
|
1619
|
-
if (!user) {
|
|
1620
|
-
logger.debug('the user is null');
|
|
1621
|
-
return this.rejectAuthError(AuthErrorTypes.NoUserSession);
|
|
1622
|
-
}
|
|
1623
|
-
var clientMetadata = this._config.clientMetadata; // TODO: verify behavior if this is override during signIn
|
|
1624
|
-
return new Promise(function (res, rej) {
|
|
1625
|
-
logger.debug('Getting the session from this user:', user);
|
|
1626
|
-
user.getSession(function (err, session) { return __awaiter(_this, void 0, void 0, function () {
|
|
1627
|
-
var cleanUpError_7;
|
|
1628
|
-
return __generator(this, function (_a) {
|
|
1629
|
-
switch (_a.label) {
|
|
1630
|
-
case 0:
|
|
1631
|
-
if (!err) return [3 /*break*/, 5];
|
|
1632
|
-
logger.debug('Failed to get the session from user', user);
|
|
1633
|
-
if (!this.isSessionInvalid(err)) return [3 /*break*/, 4];
|
|
1634
|
-
_a.label = 1;
|
|
1635
|
-
case 1:
|
|
1636
|
-
_a.trys.push([1, 3, , 4]);
|
|
1637
|
-
return [4 /*yield*/, this.cleanUpInvalidSession(user)];
|
|
1638
|
-
case 2:
|
|
1639
|
-
_a.sent();
|
|
1640
|
-
return [3 /*break*/, 4];
|
|
1641
|
-
case 3:
|
|
1642
|
-
cleanUpError_7 = _a.sent();
|
|
1643
|
-
rej(new Error("Session is invalid due to: " + err.message + " and failed to clean up invalid session: " + cleanUpError_7.message));
|
|
1644
|
-
return [2 /*return*/];
|
|
1645
|
-
case 4:
|
|
1646
|
-
rej(err);
|
|
1647
|
-
return [2 /*return*/];
|
|
1648
|
-
case 5:
|
|
1649
|
-
logger.debug('Succeed to get the user session', session);
|
|
1650
|
-
res(session);
|
|
1651
|
-
return [2 /*return*/];
|
|
1652
|
-
}
|
|
1653
|
-
});
|
|
1654
|
-
}); }, { clientMetadata: clientMetadata });
|
|
1655
|
-
});
|
|
1667
|
+
return this._userSession(user);
|
|
1656
1668
|
};
|
|
1657
1669
|
/**
|
|
1658
1670
|
* Get authenticated credentials of current user.
|
|
@@ -1796,7 +1808,7 @@ var AuthClass = /** @class */ (function () {
|
|
|
1796
1808
|
// we must validate the user as an authenticated user by using getSession
|
|
1797
1809
|
var clientMetadata = _this._config.clientMetadata; // TODO: verify behavior if this is override during signIn
|
|
1798
1810
|
user.getSession(function (err, result) { return __awaiter(_this, void 0, void 0, function () {
|
|
1799
|
-
var
|
|
1811
|
+
var cleanUpError_7;
|
|
1800
1812
|
var _this = this;
|
|
1801
1813
|
return __generator(this, function (_a) {
|
|
1802
1814
|
switch (_a.label) {
|
|
@@ -1812,8 +1824,8 @@ var AuthClass = /** @class */ (function () {
|
|
|
1812
1824
|
_a.sent();
|
|
1813
1825
|
return [3 /*break*/, 4];
|
|
1814
1826
|
case 3:
|
|
1815
|
-
|
|
1816
|
-
rej(new Error("Session is invalid due to: " + err.message + " and failed to clean up invalid session: " +
|
|
1827
|
+
cleanUpError_7 = _a.sent();
|
|
1828
|
+
rej(new Error("Session is invalid due to: " + err.message + " and failed to clean up invalid session: " + cleanUpError_7.message));
|
|
1817
1829
|
return [2 /*return*/];
|
|
1818
1830
|
case 4: return [2 /*return*/, rej(err)];
|
|
1819
1831
|
case 5:
|
|
@@ -2037,7 +2049,7 @@ var AuthClass = /** @class */ (function () {
|
|
|
2037
2049
|
*/
|
|
2038
2050
|
AuthClass.prototype.currentUserInfo = function () {
|
|
2039
2051
|
return __awaiter(this, void 0, void 0, function () {
|
|
2040
|
-
var source, user, attributes, userAttrs, credentials, e_12, info,
|
|
2052
|
+
var source, user, attributes, userAttrs, credentials, e_12, info, err_2, user;
|
|
2041
2053
|
return __generator(this, function (_a) {
|
|
2042
2054
|
switch (_a.label) {
|
|
2043
2055
|
case 0:
|
|
@@ -2078,8 +2090,8 @@ var AuthClass = /** @class */ (function () {
|
|
|
2078
2090
|
};
|
|
2079
2091
|
return [2 /*return*/, info];
|
|
2080
2092
|
case 8:
|
|
2081
|
-
|
|
2082
|
-
logger.error('currentUserInfo error',
|
|
2093
|
+
err_2 = _a.sent();
|
|
2094
|
+
logger.error('currentUserInfo error', err_2);
|
|
2083
2095
|
return [2 /*return*/, {}];
|
|
2084
2096
|
case 9:
|
|
2085
2097
|
if (source === 'federated') {
|
|
@@ -2160,7 +2172,7 @@ var AuthClass = /** @class */ (function () {
|
|
|
2160
2172
|
*/
|
|
2161
2173
|
AuthClass.prototype._handleAuthResponse = function (URL) {
|
|
2162
2174
|
return __awaiter(this, void 0, void 0, function () {
|
|
2163
|
-
var currentUrl, hasCodeOrError, hasTokenOrError, _a, accessToken, idToken, refreshToken, state, session, credentials, isCustomStateIncluded, currentUser, customState,
|
|
2175
|
+
var currentUrl, hasCodeOrError, hasTokenOrError, _a, accessToken, idToken, refreshToken, state, session, credentials, isCustomStateIncluded, currentUser, customState, err_3;
|
|
2164
2176
|
return __generator(this, function (_b) {
|
|
2165
2177
|
switch (_b.label) {
|
|
2166
2178
|
case 0:
|
|
@@ -2233,16 +2245,16 @@ var AuthClass = /** @class */ (function () {
|
|
|
2233
2245
|
//#endregion
|
|
2234
2246
|
return [2 /*return*/, credentials];
|
|
2235
2247
|
case 6:
|
|
2236
|
-
|
|
2237
|
-
logger.debug('Error in cognito hosted auth response',
|
|
2248
|
+
err_3 = _b.sent();
|
|
2249
|
+
logger.debug('Error in cognito hosted auth response', err_3);
|
|
2238
2250
|
// Just like a successful handling of `?code`, replace the window history to "dispose" of the `code`.
|
|
2239
2251
|
// Otherwise, reloading the page will throw errors as the `code` has already been spent.
|
|
2240
2252
|
if (window && typeof window.history !== 'undefined') {
|
|
2241
2253
|
window.history.replaceState({}, null, this._config.oauth.redirectSignIn);
|
|
2242
2254
|
}
|
|
2243
|
-
dispatchAuthEvent('signIn_failure',
|
|
2244
|
-
dispatchAuthEvent('cognitoHostedUI_failure',
|
|
2245
|
-
dispatchAuthEvent('customState_failure',
|
|
2255
|
+
dispatchAuthEvent('signIn_failure', err_3, "The OAuth response flow failed");
|
|
2256
|
+
dispatchAuthEvent('cognitoHostedUI_failure', err_3, "A failure occurred when returning to the Cognito Hosted UI");
|
|
2257
|
+
dispatchAuthEvent('customState_failure', err_3, "A failure occurred when returning state");
|
|
2246
2258
|
return [3 /*break*/, 7];
|
|
2247
2259
|
case 7: return [3 /*break*/, 9];
|
|
2248
2260
|
case 8:
|