@schibsted/account-sdk-browser 5.0.0 → 5.0.1-beta.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/es5/index.js CHANGED
@@ -988,7 +988,7 @@ function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.g
988
988
  * `password` (will force password confirmation, even if user is already logged in), `eid`. Those values might
989
989
  * be mixed as space-separated string. To make sure that user has authenticated with 2FA you need
990
990
  * to verify AMR (Authentication Methods References) claim in ID token.
991
- * Might also be used to ensure additional acr (sms, otp) for already logged in users.
991
+ * Might also be used to ensure additional acr (sms, otp) for already logged-in users.
992
992
  * Supported value is also 'otp-email' means one time password using email.
993
993
  * @property {string} [scope] - The OAuth scopes for the tokens. This is a list of
994
994
  * scopes, separated by space. If the list of scopes contains `openid`, the generated tokens
@@ -1022,7 +1022,7 @@ function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.g
1022
1022
  * `password` (will force password confirmation, even if user is already logged in). Those values might
1023
1023
  * be mixed as space-separated string. To make sure that user has authenticated with 2FA you need
1024
1024
  * to verify AMR (Authentication Methods References) claim in ID token.
1025
- * Might also be used to ensure additional acr (sms, otp) for already logged in users.
1025
+ * Might also be used to ensure additional acr (sms, otp) for already logged-in users.
1026
1026
  * Supported value is also 'otp-email' means one time password using email.
1027
1027
  * @property {string} [scope] - The OAuth scopes for the tokens. This is a list of
1028
1028
  * scopes, separated by space. If the list of scopes contains `openid`, the generated tokens
@@ -1096,7 +1096,7 @@ function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.g
1096
1096
 
1097
1097
  /**
1098
1098
  * @typedef {object} SimplifiedLoginData
1099
- * @property {string} identifier - Deprecated: User UUID, to be be used as `loginHint` for {@link Identity#login}
1099
+ * @property {string} identifier - Deprecated: User UUID, to be as `loginHint` for {@link Identity#login}
1100
1100
  * @property {string} display_text - Human-readable user identifier
1101
1101
  * @property {string} client_name - Client name
1102
1102
  */
@@ -1108,13 +1108,17 @@ function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.g
1108
1108
 
1109
1109
  var HAS_SESSION_CACHE_KEY = 'hasSession-cache';
1110
1110
  var SESSION_CALL_BLOCKED_CACHE_KEY = 'sessionCallBlocked-cache';
1111
- var SESSION_CALL_BLOCKED_TTL = 1000 * 60 * 5;
1111
+ var SESSION_CALL_BLOCKED_TTL = 1000 * 30; //set to 30s, the default period for a request timeout
1112
+
1113
+ var TAB_ID_KEY = 'tab-id-cache';
1114
+ var TAB_ID = Math.floor(Math.random() * 100000);
1115
+ var TAB_ID_TTL = 1000 * 60 * 60 * 24 * 30;
1112
1116
  var globalWindow = function globalWindow() {
1113
1117
  return window;
1114
1118
  };
1115
1119
 
1116
1120
  /**
1117
- * Provides Identity functionalty to a web page
1121
+ * Provides Identity functionality to a web page
1118
1122
  */
1119
1123
  var Identity = /*#__PURE__*/function (_EventEmitter) {
1120
1124
  _inherits(Identity, _EventEmitter);
@@ -1150,20 +1154,24 @@ var Identity = /*#__PURE__*/function (_EventEmitter) {
1150
1154
  Object(_validate_js__WEBPACK_IMPORTED_MODULE_55__["assert"])(!redirectUri || Object(_validate_js__WEBPACK_IMPORTED_MODULE_55__["isUrl"])(redirectUri), 'redirectUri parameter is invalid');
1151
1155
  Object(_validate_js__WEBPACK_IMPORTED_MODULE_55__["assert"])(sessionDomain && Object(_validate_js__WEBPACK_IMPORTED_MODULE_55__["isUrl"])(sessionDomain), 'sessionDomain parameter is not a valid URL');
1152
1156
  _spidTalk_js__WEBPACK_IMPORTED_MODULE_64__["emulate"](window);
1157
+
1158
+ // Internal hack: set as false to always refresh from hasSession
1159
+ _this._enableSessionCaching = true;
1153
1160
  _this._sessionInitiatedSent = false;
1154
1161
  _this.window = window;
1155
1162
  _this.clientId = clientId;
1156
- _this.cache = new _cache_js__WEBPACK_IMPORTED_MODULE_60__["default"](function () {
1163
+ _this.sessionStorageCache = new _cache_js__WEBPACK_IMPORTED_MODULE_60__["default"](function () {
1157
1164
  return _this.window && _this.window.sessionStorage;
1158
1165
  });
1166
+ _this.localStorageCache = new _cache_js__WEBPACK_IMPORTED_MODULE_60__["default"](function () {
1167
+ return _this.window && _this.window.localStorage;
1168
+ });
1159
1169
  _this.redirectUri = redirectUri;
1160
1170
  _this.env = env;
1161
1171
  _this.log = log;
1162
1172
  _this.callbackBeforeRedirect = callbackBeforeRedirect;
1163
1173
  _this._sessionDomain = sessionDomain;
1164
-
1165
- // Internal hack: set to false to always refresh from hassession
1166
- _this._enableSessionCaching = true;
1174
+ _this._tabId = _this._getTabId();
1167
1175
 
1168
1176
  // Old session
1169
1177
  _this._session = {};
@@ -1172,50 +1180,61 @@ var Identity = /*#__PURE__*/function (_EventEmitter) {
1172
1180
  _this._setBffServerUrl(env);
1173
1181
  _this._setOauthServerUrl(env);
1174
1182
  _this._setGlobalSessionServiceUrl(env);
1175
- _this._unblockSessionCall();
1183
+ _this._unblockSessionCallByTab();
1176
1184
  return _this;
1177
1185
  }
1178
1186
 
1179
1187
  /**
1180
- * Checks if getting session is blocked
1188
+ * Read tabId from session storage if possible, otherwise save tabId to session storage and return it
1189
+ * @returns {number}
1181
1190
  * @private
1182
- *
1183
- * @returns {boolean|void}
1184
1191
  */
1185
1192
  _createClass(Identity, [{
1186
- key: "_isSessionCallBlocked",
1187
- value: function _isSessionCallBlocked() {
1193
+ key: "_getTabId",
1194
+ value: function _getTabId() {
1188
1195
  if (this._enableSessionCaching) {
1189
- return this.cache.get(SESSION_CALL_BLOCKED_CACHE_KEY);
1196
+ var tabId = this.sessionStorageCache.get(TAB_ID_KEY);
1197
+ if (!tabId) {
1198
+ this.sessionStorageCache.set(TAB_ID_KEY, TAB_ID, TAB_ID_TTL);
1199
+ return TAB_ID;
1200
+ }
1201
+ return tabId;
1190
1202
  }
1203
+ return TAB_ID;
1191
1204
  }
1192
1205
 
1193
1206
  /**
1194
- * Block calls to get session
1207
+ * Checks if calling GET session is blocked
1208
+ * @private
1209
+ * @returns {number|null}
1210
+ */
1211
+ }, {
1212
+ key: "_isSessionCallBlocked",
1213
+ value: function _isSessionCallBlocked() {
1214
+ return this.localStorageCache.get(SESSION_CALL_BLOCKED_CACHE_KEY);
1215
+ }
1216
+
1217
+ /**
1218
+ * Block calls to get session. This is done to prevent concurrent calls which can log user out if session is refreshed by one of them
1195
1219
  * @private
1196
- *
1197
1220
  * @returns {void}
1198
1221
  */
1199
1222
  }, {
1200
1223
  key: "_blockSessionCall",
1201
1224
  value: function _blockSessionCall() {
1202
- if (this._enableSessionCaching) {
1203
- var SESSION_CALL_BLOCKED = true;
1204
- this.cache.set(SESSION_CALL_BLOCKED_CACHE_KEY, SESSION_CALL_BLOCKED, SESSION_CALL_BLOCKED_TTL);
1205
- }
1225
+ this.localStorageCache.set(SESSION_CALL_BLOCKED_CACHE_KEY, this._tabId, SESSION_CALL_BLOCKED_TTL);
1206
1226
  }
1207
1227
 
1208
1228
  /**
1209
- * Unblocks calls to get session
1229
+ * Unblocks calls to get session if the lock was put by the same tab
1210
1230
  * @private
1211
- *
1212
1231
  * @returns {void}
1213
1232
  */
1214
1233
  }, {
1215
- key: "_unblockSessionCall",
1216
- value: function _unblockSessionCall() {
1217
- if (this._enableSessionCaching) {
1218
- this.cache.delete(SESSION_CALL_BLOCKED_CACHE_KEY);
1234
+ key: "_unblockSessionCallByTab",
1235
+ value: function _unblockSessionCallByTab() {
1236
+ if (this._isSessionCallBlocked() === this._tabId) {
1237
+ this.localStorageCache.delete(SESSION_CALL_BLOCKED_CACHE_KEY);
1219
1238
  }
1220
1239
  }
1221
1240
 
@@ -1323,7 +1342,7 @@ var Identity = /*#__PURE__*/function (_EventEmitter) {
1323
1342
  }
1324
1343
 
1325
1344
  /**
1326
- * Emits the relevant events based on the previous and new reply from hassession
1345
+ * Emits the relevant events based on the previous and new reply from {@link Identity#hasSession}
1327
1346
  * @private
1328
1347
  * @param {object} previous
1329
1348
  * @param {object} current
@@ -1407,7 +1426,7 @@ var Identity = /*#__PURE__*/function (_EventEmitter) {
1407
1426
  }
1408
1427
 
1409
1428
  /**
1410
- * Set the Varnish cookie (`SP_ID`) when hasSession() is called. Note that most browsers require
1429
+ * Set the Varnish cookie (`SP_ID`) when {@link Identity#hasSession} is called. Note that most browsers require
1411
1430
  * that you are on a "real domain" for this to work — so, **not** `localhost`
1412
1431
  * @param {object} [options]
1413
1432
  * @param {number} [options.expiresIn] Override this to set number of seconds before the varnish
@@ -1528,10 +1547,6 @@ var Identity = /*#__PURE__*/function (_EventEmitter) {
1528
1547
  key: "hasSession",
1529
1548
  value: function hasSession() {
1530
1549
  var _this2 = this;
1531
- var isSessionCallBlocked = this._isSessionCallBlocked();
1532
- if (isSessionCallBlocked) {
1533
- return this._session;
1534
- }
1535
1550
  if (this._hasSessionInProgress) {
1536
1551
  return this._hasSessionInProgress;
1537
1552
  }
@@ -1560,57 +1575,67 @@ var Identity = /*#__PURE__*/function (_EventEmitter) {
1560
1575
  break;
1561
1576
  }
1562
1577
  // Try to resolve from cache (it has a TTL)
1563
- cachedSession = _this2.cache.get(HAS_SESSION_CACHE_KEY);
1578
+ cachedSession = _this2.sessionStorageCache.get(HAS_SESSION_CACHE_KEY);
1564
1579
  if (!cachedSession) {
1565
1580
  _context.next = 4;
1566
1581
  break;
1567
1582
  }
1568
1583
  return _context.abrupt("return", _postProcess(cachedSession));
1569
1584
  case 4:
1585
+ if (!_this2._isSessionCallBlocked()) {
1586
+ _context.next = 6;
1587
+ break;
1588
+ }
1589
+ return _context.abrupt("return", _this2._session);
1590
+ case 6:
1570
1591
  sessionData = null;
1571
- _context.prev = 5;
1572
- _context.next = 8;
1573
- return _this2._sessionService.get('/v2/session');
1574
- case 8:
1592
+ _context.prev = 7;
1593
+ _this2._blockSessionCall();
1594
+ _context.next = 11;
1595
+ return _this2._sessionService.get('/v2/session', {
1596
+ tabId: _this2._tabId
1597
+ });
1598
+ case 11:
1575
1599
  sessionData = _context.sent;
1576
- _context.next = 15;
1600
+ _context.next = 18;
1577
1601
  break;
1578
- case 11:
1579
- _context.prev = 11;
1580
- _context.t0 = _context["catch"](5);
1602
+ case 14:
1603
+ _context.prev = 14;
1604
+ _context.t0 = _context["catch"](7);
1581
1605
  if (_context.t0 && _context.t0.code === 400 && _this2._enableSessionCaching) {
1582
1606
  expiresIn = 1000 * (_context.t0.expiresIn || 300);
1583
- _this2.cache.set(HAS_SESSION_CACHE_KEY, {
1607
+ _this2.sessionStorageCache.set(HAS_SESSION_CACHE_KEY, {
1584
1608
  error: _context.t0
1585
1609
  }, expiresIn);
1586
1610
  }
1587
1611
  throw _context.t0;
1588
- case 15:
1612
+ case 18:
1589
1613
  if (!sessionData) {
1590
- _context.next = 22;
1614
+ _context.next = 24;
1591
1615
  break;
1592
1616
  }
1593
1617
  if (!_checkRedirectionNeed(sessionData)) {
1594
- _context.next = 21;
1618
+ _context.next = 23;
1595
1619
  break;
1596
1620
  }
1597
- _this2._blockSessionCall();
1598
- _context.next = 20;
1621
+ _context.next = 22;
1599
1622
  return _this2.callbackBeforeRedirect();
1600
- case 20:
1601
- return _context.abrupt("return", _this2._sessionService.makeUrl(sessionData.redirectURL));
1602
- case 21:
1623
+ case 22:
1624
+ _this2.window.location.href = _this2._sessionService.makeUrl(sessionData.redirectURL, {
1625
+ tabId: _this2._getTabId()
1626
+ });
1627
+ case 23:
1603
1628
  if (_this2._enableSessionCaching) {
1604
1629
  _expiresIn = 1000 * (sessionData.expiresIn || 300);
1605
- _this2.cache.set(HAS_SESSION_CACHE_KEY, sessionData, _expiresIn);
1630
+ _this2.sessionStorageCache.set(HAS_SESSION_CACHE_KEY, sessionData, _expiresIn);
1606
1631
  }
1607
- case 22:
1632
+ case 24:
1608
1633
  return _context.abrupt("return", _postProcess(sessionData));
1609
- case 23:
1634
+ case 25:
1610
1635
  case "end":
1611
1636
  return _context.stop();
1612
1637
  }
1613
- }, _callee, null, [[5, 11]]);
1638
+ }, _callee, null, [[7, 14]]);
1614
1639
  }));
1615
1640
  return function _getSession() {
1616
1641
  return _ref2.apply(this, arguments);
@@ -1618,15 +1643,13 @@ var Identity = /*#__PURE__*/function (_EventEmitter) {
1618
1643
  }();
1619
1644
  this._hasSessionInProgress = _getSession().then(function (sessionData) {
1620
1645
  _this2._hasSessionInProgress = false;
1621
- if (Object(_validate_js__WEBPACK_IMPORTED_MODULE_55__["isUrl"])(sessionData)) {
1622
- return _this2.window.location.href = sessionData;
1623
- }
1624
1646
  return sessionData;
1625
1647
  }, function (err) {
1626
1648
  _this2.emit('error', err);
1627
1649
  _this2._hasSessionInProgress = false;
1628
1650
  throw new _SDKError_js__WEBPACK_IMPORTED_MODULE_63__["default"]('HasSession failed', err);
1629
1651
  });
1652
+ this._unblockSessionCallByTab();
1630
1653
  return this._hasSessionInProgress;
1631
1654
  }
1632
1655
 
@@ -1673,7 +1696,7 @@ var Identity = /*#__PURE__*/function (_EventEmitter) {
1673
1696
  }, {
1674
1697
  key: "clearCachedUserSession",
1675
1698
  value: function clearCachedUserSession() {
1676
- this.cache.delete(HAS_SESSION_CACHE_KEY);
1699
+ this.sessionStorageCache.delete(HAS_SESSION_CACHE_KEY);
1677
1700
  }
1678
1701
 
1679
1702
  /**
@@ -1767,7 +1790,7 @@ var Identity = /*#__PURE__*/function (_EventEmitter) {
1767
1790
  * @description This function calls {@link Identity#hasSession} internally and thus has the side
1768
1791
  * effect that it might perform an auto-login on the user
1769
1792
  * @throws {SDKError} If the user isn't connected to the merchant
1770
- * @return {Promise<string>} The `userId` field (not to be confused with the `uuid`)
1793
+ * @return {number} The `userId` field (not to be confused with the `uuid`)
1771
1794
  */
1772
1795
  }, {
1773
1796
  key: "getUserId",
@@ -2068,7 +2091,7 @@ var Identity = /*#__PURE__*/function (_EventEmitter) {
2068
2091
  _ref4$prompt = _ref4.prompt,
2069
2092
  prompt = _ref4$prompt === void 0 ? 'select_account' : _ref4$prompt;
2070
2093
  this._closePopup();
2071
- this.cache.delete(HAS_SESSION_CACHE_KEY);
2094
+ this.sessionStorageCache.delete(HAS_SESSION_CACHE_KEY);
2072
2095
  var url = this.loginUrl({
2073
2096
  state: state,
2074
2097
  acrValues: acrValues,
@@ -2141,7 +2164,7 @@ var Identity = /*#__PURE__*/function (_EventEmitter) {
2141
2164
  key: "logout",
2142
2165
  value: function logout() {
2143
2166
  var redirectUri = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.redirectUri;
2144
- this.cache.delete(HAS_SESSION_CACHE_KEY);
2167
+ this.sessionStorageCache.delete(HAS_SESSION_CACHE_KEY);
2145
2168
  this._maybeClearVarnishCookie();
2146
2169
  this.emit('logout');
2147
2170
  this.window.location.href = this.logoutUrl(redirectUri);
@@ -10998,7 +11021,6 @@ var Cache = /*#__PURE__*/function () {
10998
11021
  /**
10999
11022
  * Get a value from cache (checks that the object has not expired)
11000
11023
  * @param {string} key
11001
- * @private
11002
11024
  * @returns {*} - The value if it exists, otherwise null
11003
11025
  */
11004
11026
  _createClass(Cache, [{
@@ -11034,7 +11056,6 @@ var Cache = /*#__PURE__*/function () {
11034
11056
  * @param {string} key
11035
11057
  * @param {*} value
11036
11058
  * @param {Number} expiresIn - Value in milliseconds until the entry expires
11037
- * @private
11038
11059
  * @returns {void}
11039
11060
  */
11040
11061
  }, {
@@ -11063,7 +11084,6 @@ var Cache = /*#__PURE__*/function () {
11063
11084
  /**
11064
11085
  * Delete a cache entry
11065
11086
  * @param {string} key
11066
- * @private
11067
11087
  * @returns {void}
11068
11088
  */
11069
11089
  }, {
@@ -11983,7 +12003,7 @@ __webpack_require__.r(__webpack_exports__);
11983
12003
 
11984
12004
 
11985
12005
 
11986
- var version = '5.0.0';
12006
+ var version = '5.0.1-beta.2';
11987
12007
  /* harmony default export */ __webpack_exports__["default"] = (version);
11988
12008
 
11989
12009
  /***/ }),