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

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/identity.js CHANGED
@@ -982,7 +982,7 @@ function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.g
982
982
  * `password` (will force password confirmation, even if user is already logged in), `eid`. Those values might
983
983
  * be mixed as space-separated string. To make sure that user has authenticated with 2FA you need
984
984
  * to verify AMR (Authentication Methods References) claim in ID token.
985
- * Might also be used to ensure additional acr (sms, otp) for already logged in users.
985
+ * Might also be used to ensure additional acr (sms, otp) for already logged-in users.
986
986
  * Supported value is also 'otp-email' means one time password using email.
987
987
  * @property {string} [scope] - The OAuth scopes for the tokens. This is a list of
988
988
  * scopes, separated by space. If the list of scopes contains `openid`, the generated tokens
@@ -1016,7 +1016,7 @@ function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.g
1016
1016
  * `password` (will force password confirmation, even if user is already logged in). Those values might
1017
1017
  * be mixed as space-separated string. To make sure that user has authenticated with 2FA you need
1018
1018
  * to verify AMR (Authentication Methods References) claim in ID token.
1019
- * Might also be used to ensure additional acr (sms, otp) for already logged in users.
1019
+ * Might also be used to ensure additional acr (sms, otp) for already logged-in users.
1020
1020
  * Supported value is also 'otp-email' means one time password using email.
1021
1021
  * @property {string} [scope] - The OAuth scopes for the tokens. This is a list of
1022
1022
  * scopes, separated by space. If the list of scopes contains `openid`, the generated tokens
@@ -1090,7 +1090,7 @@ function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.g
1090
1090
 
1091
1091
  /**
1092
1092
  * @typedef {object} SimplifiedLoginData
1093
- * @property {string} identifier - Deprecated: User UUID, to be be used as `loginHint` for {@link Identity#login}
1093
+ * @property {string} identifier - Deprecated: User UUID, to be as `loginHint` for {@link Identity#login}
1094
1094
  * @property {string} display_text - Human-readable user identifier
1095
1095
  * @property {string} client_name - Client name
1096
1096
  */
@@ -1102,13 +1102,16 @@ function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.g
1102
1102
 
1103
1103
  var HAS_SESSION_CACHE_KEY = 'hasSession-cache';
1104
1104
  var SESSION_CALL_BLOCKED_CACHE_KEY = 'sessionCallBlocked-cache';
1105
- var SESSION_CALL_BLOCKED_TTL = 1000 * 60 * 5;
1105
+ var SESSION_CALL_BLOCKED_TTL = 1000 * 30;
1106
+ var TAB_ID_KEY = 'tab-id-cache';
1107
+ var TAB_ID = Math.floor(Math.random() * 100000);
1108
+ var TAB_ID_TTL = 1000 * 60 * 60 * 24 * 30;
1106
1109
  var globalWindow = function globalWindow() {
1107
1110
  return window;
1108
1111
  };
1109
1112
 
1110
1113
  /**
1111
- * Provides Identity functionalty to a web page
1114
+ * Provides Identity functionality to a web page
1112
1115
  */
1113
1116
  var Identity = /*#__PURE__*/function (_EventEmitter) {
1114
1117
  _inherits(Identity, _EventEmitter);
@@ -1144,20 +1147,20 @@ var Identity = /*#__PURE__*/function (_EventEmitter) {
1144
1147
  Object(_validate_js__WEBPACK_IMPORTED_MODULE_55__["assert"])(!redirectUri || Object(_validate_js__WEBPACK_IMPORTED_MODULE_55__["isUrl"])(redirectUri), 'redirectUri parameter is invalid');
1145
1148
  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');
1146
1149
  _spidTalk_js__WEBPACK_IMPORTED_MODULE_64__["emulate"](window);
1150
+
1151
+ // Internal hack: set as false to always refresh from hasSession
1152
+ _this._enableSessionCaching = true;
1147
1153
  _this._sessionInitiatedSent = false;
1148
1154
  _this.window = window;
1149
1155
  _this.clientId = clientId;
1150
- _this.cache = new _cache_js__WEBPACK_IMPORTED_MODULE_60__["default"](function () {
1151
- return _this.window && _this.window.sessionStorage;
1152
- });
1156
+ _this.sessionStorageCache = new _cache_js__WEBPACK_IMPORTED_MODULE_60__["default"](_this.window && _this.window.sessionStorage);
1157
+ _this.localStorageCache = new _cache_js__WEBPACK_IMPORTED_MODULE_60__["default"](_this.window && _this.window.localStorage);
1153
1158
  _this.redirectUri = redirectUri;
1154
1159
  _this.env = env;
1155
1160
  _this.log = log;
1156
1161
  _this.callbackBeforeRedirect = callbackBeforeRedirect;
1157
1162
  _this._sessionDomain = sessionDomain;
1158
-
1159
- // Internal hack: set to false to always refresh from hassession
1160
- _this._enableSessionCaching = true;
1163
+ _this._tabId = _this._getTabId();
1161
1164
 
1162
1165
  // Old session
1163
1166
  _this._session = {};
@@ -1166,50 +1169,61 @@ var Identity = /*#__PURE__*/function (_EventEmitter) {
1166
1169
  _this._setBffServerUrl(env);
1167
1170
  _this._setOauthServerUrl(env);
1168
1171
  _this._setGlobalSessionServiceUrl(env);
1169
- _this._unblockSessionCall();
1172
+ _this._unblockSessionCallByTab();
1170
1173
  return _this;
1171
1174
  }
1172
1175
 
1173
1176
  /**
1174
- * Checks if getting session is blocked
1177
+ * Read tabId from session storage if possible, otherwise save tabId to session storage and return it
1178
+ * @returns {number}
1175
1179
  * @private
1176
- *
1177
- * @returns {boolean|void}
1178
1180
  */
1179
1181
  _createClass(Identity, [{
1180
- key: "_isSessionCallBlocked",
1181
- value: function _isSessionCallBlocked() {
1182
+ key: "_getTabId",
1183
+ value: function _getTabId() {
1182
1184
  if (this._enableSessionCaching) {
1183
- return this.cache.get(SESSION_CALL_BLOCKED_CACHE_KEY);
1185
+ var tabId = this.sessionStorageCache.get(TAB_ID_KEY);
1186
+ if (!tabId) {
1187
+ this.sessionStorageCache.set(TAB_ID_KEY, TAB_ID, TAB_ID_TTL);
1188
+ return TAB_ID;
1189
+ }
1190
+ return tabId;
1184
1191
  }
1192
+ return TAB_ID;
1185
1193
  }
1186
1194
 
1187
1195
  /**
1188
- * Block calls to get session
1196
+ * Checks if calling GET session is blocked
1197
+ * @private
1198
+ * @returns {number|null}
1199
+ */
1200
+ }, {
1201
+ key: "_isSessionCallBlocked",
1202
+ value: function _isSessionCallBlocked() {
1203
+ return this.localStorageCache.get(SESSION_CALL_BLOCKED_CACHE_KEY);
1204
+ }
1205
+
1206
+ /**
1207
+ * 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
1189
1208
  * @private
1190
- *
1191
1209
  * @returns {void}
1192
1210
  */
1193
1211
  }, {
1194
1212
  key: "_blockSessionCall",
1195
1213
  value: function _blockSessionCall() {
1196
- if (this._enableSessionCaching) {
1197
- var SESSION_CALL_BLOCKED = true;
1198
- this.cache.set(SESSION_CALL_BLOCKED_CACHE_KEY, SESSION_CALL_BLOCKED, SESSION_CALL_BLOCKED_TTL);
1199
- }
1214
+ this.localStorageCache.set(SESSION_CALL_BLOCKED_CACHE_KEY, this._tabId, SESSION_CALL_BLOCKED_TTL);
1200
1215
  }
1201
1216
 
1202
1217
  /**
1203
- * Unblocks calls to get session
1218
+ * Unblocks calls to get session if the lock was put by the same tab
1204
1219
  * @private
1205
- *
1206
1220
  * @returns {void}
1207
1221
  */
1208
1222
  }, {
1209
- key: "_unblockSessionCall",
1210
- value: function _unblockSessionCall() {
1211
- if (this._enableSessionCaching) {
1212
- this.cache.delete(SESSION_CALL_BLOCKED_CACHE_KEY);
1223
+ key: "_unblockSessionCallByTab",
1224
+ value: function _unblockSessionCallByTab() {
1225
+ if (this._isSessionCallBlocked() === this._tabId) {
1226
+ this.localStorageCache.delete(SESSION_CALL_BLOCKED_CACHE_KEY);
1213
1227
  }
1214
1228
  }
1215
1229
 
@@ -1317,7 +1331,7 @@ var Identity = /*#__PURE__*/function (_EventEmitter) {
1317
1331
  }
1318
1332
 
1319
1333
  /**
1320
- * Emits the relevant events based on the previous and new reply from hassession
1334
+ * Emits the relevant events based on the previous and new reply from {@link Identity#hasSession}
1321
1335
  * @private
1322
1336
  * @param {object} previous
1323
1337
  * @param {object} current
@@ -1401,7 +1415,7 @@ var Identity = /*#__PURE__*/function (_EventEmitter) {
1401
1415
  }
1402
1416
 
1403
1417
  /**
1404
- * Set the Varnish cookie (`SP_ID`) when hasSession() is called. Note that most browsers require
1418
+ * Set the Varnish cookie (`SP_ID`) when {@link Identity#hasSession} is called. Note that most browsers require
1405
1419
  * that you are on a "real domain" for this to work — so, **not** `localhost`
1406
1420
  * @param {object} [options]
1407
1421
  * @param {number} [options.expiresIn] Override this to set number of seconds before the varnish
@@ -1554,7 +1568,7 @@ var Identity = /*#__PURE__*/function (_EventEmitter) {
1554
1568
  break;
1555
1569
  }
1556
1570
  // Try to resolve from cache (it has a TTL)
1557
- cachedSession = _this2.cache.get(HAS_SESSION_CACHE_KEY);
1571
+ cachedSession = _this2.sessionStorageCache.get(HAS_SESSION_CACHE_KEY);
1558
1572
  if (!cachedSession) {
1559
1573
  _context.next = 4;
1560
1574
  break;
@@ -1564,7 +1578,9 @@ var Identity = /*#__PURE__*/function (_EventEmitter) {
1564
1578
  sessionData = null;
1565
1579
  _context.prev = 5;
1566
1580
  _context.next = 8;
1567
- return _this2._sessionService.get('/v2/session');
1581
+ return _this2._sessionService.get('/v2/session', {
1582
+ tabId: _this2._tabId
1583
+ });
1568
1584
  case 8:
1569
1585
  sessionData = _context.sent;
1570
1586
  _context.next = 15;
@@ -1574,7 +1590,7 @@ var Identity = /*#__PURE__*/function (_EventEmitter) {
1574
1590
  _context.t0 = _context["catch"](5);
1575
1591
  if (_context.t0 && _context.t0.code === 400 && _this2._enableSessionCaching) {
1576
1592
  expiresIn = 1000 * (_context.t0.expiresIn || 300);
1577
- _this2.cache.set(HAS_SESSION_CACHE_KEY, {
1593
+ _this2.sessionStorageCache.set(HAS_SESSION_CACHE_KEY, {
1578
1594
  error: _context.t0
1579
1595
  }, expiresIn);
1580
1596
  }
@@ -1592,11 +1608,13 @@ var Identity = /*#__PURE__*/function (_EventEmitter) {
1592
1608
  _context.next = 20;
1593
1609
  return _this2.callbackBeforeRedirect();
1594
1610
  case 20:
1595
- return _context.abrupt("return", _this2._sessionService.makeUrl(sessionData.redirectURL));
1611
+ return _context.abrupt("return", _this2._sessionService.makeUrl(sessionData.redirectURL, {
1612
+ tabId: _this2._getTabId()
1613
+ }));
1596
1614
  case 21:
1597
1615
  if (_this2._enableSessionCaching) {
1598
1616
  _expiresIn = 1000 * (sessionData.expiresIn || 300);
1599
- _this2.cache.set(HAS_SESSION_CACHE_KEY, sessionData, _expiresIn);
1617
+ _this2.sessionStorageCache.set(HAS_SESSION_CACHE_KEY, sessionData, _expiresIn);
1600
1618
  }
1601
1619
  case 22:
1602
1620
  return _context.abrupt("return", _postProcess(sessionData));
@@ -1667,7 +1685,7 @@ var Identity = /*#__PURE__*/function (_EventEmitter) {
1667
1685
  }, {
1668
1686
  key: "clearCachedUserSession",
1669
1687
  value: function clearCachedUserSession() {
1670
- this.cache.delete(HAS_SESSION_CACHE_KEY);
1688
+ this.sessionStorageCache.delete(HAS_SESSION_CACHE_KEY);
1671
1689
  }
1672
1690
 
1673
1691
  /**
@@ -1761,7 +1779,7 @@ var Identity = /*#__PURE__*/function (_EventEmitter) {
1761
1779
  * @description This function calls {@link Identity#hasSession} internally and thus has the side
1762
1780
  * effect that it might perform an auto-login on the user
1763
1781
  * @throws {SDKError} If the user isn't connected to the merchant
1764
- * @return {Promise<string>} The `userId` field (not to be confused with the `uuid`)
1782
+ * @return {number} The `userId` field (not to be confused with the `uuid`)
1765
1783
  */
1766
1784
  }, {
1767
1785
  key: "getUserId",
@@ -2062,7 +2080,7 @@ var Identity = /*#__PURE__*/function (_EventEmitter) {
2062
2080
  _ref4$prompt = _ref4.prompt,
2063
2081
  prompt = _ref4$prompt === void 0 ? 'select_account' : _ref4$prompt;
2064
2082
  this._closePopup();
2065
- this.cache.delete(HAS_SESSION_CACHE_KEY);
2083
+ this.sessionStorageCache.delete(HAS_SESSION_CACHE_KEY);
2066
2084
  var url = this.loginUrl({
2067
2085
  state: state,
2068
2086
  acrValues: acrValues,
@@ -2135,7 +2153,7 @@ var Identity = /*#__PURE__*/function (_EventEmitter) {
2135
2153
  key: "logout",
2136
2154
  value: function logout() {
2137
2155
  var redirectUri = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.redirectUri;
2138
- this.cache.delete(HAS_SESSION_CACHE_KEY);
2156
+ this.sessionStorageCache.delete(HAS_SESSION_CACHE_KEY);
2139
2157
  this._maybeClearVarnishCookie();
2140
2158
  this.emit('logout');
2141
2159
  this.window.location.href = this.logoutUrl(redirectUri);
@@ -10992,7 +11010,6 @@ var Cache = /*#__PURE__*/function () {
10992
11010
  /**
10993
11011
  * Get a value from cache (checks that the object has not expired)
10994
11012
  * @param {string} key
10995
- * @private
10996
11013
  * @returns {*} - The value if it exists, otherwise null
10997
11014
  */
10998
11015
  _createClass(Cache, [{
@@ -11028,7 +11045,6 @@ var Cache = /*#__PURE__*/function () {
11028
11045
  * @param {string} key
11029
11046
  * @param {*} value
11030
11047
  * @param {Number} expiresIn - Value in milliseconds until the entry expires
11031
- * @private
11032
11048
  * @returns {void}
11033
11049
  */
11034
11050
  }, {
@@ -11057,7 +11073,6 @@ var Cache = /*#__PURE__*/function () {
11057
11073
  /**
11058
11074
  * Delete a cache entry
11059
11075
  * @param {string} key
11060
- * @private
11061
11076
  * @returns {void}
11062
11077
  */
11063
11078
  }, {
@@ -11977,7 +11992,7 @@ __webpack_require__.r(__webpack_exports__);
11977
11992
 
11978
11993
 
11979
11994
 
11980
- var version = '5.0.0';
11995
+ var version = '5.0.1-beta';
11981
11996
  /* harmony default export */ __webpack_exports__["default"] = (version);
11982
11997
 
11983
11998
  /***/ })