@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/global.js CHANGED
@@ -1865,7 +1865,7 @@ function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.g
1865
1865
  * `password` (will force password confirmation, even if user is already logged in), `eid`. Those values might
1866
1866
  * be mixed as space-separated string. To make sure that user has authenticated with 2FA you need
1867
1867
  * to verify AMR (Authentication Methods References) claim in ID token.
1868
- * Might also be used to ensure additional acr (sms, otp) for already logged in users.
1868
+ * Might also be used to ensure additional acr (sms, otp) for already logged-in users.
1869
1869
  * Supported value is also 'otp-email' means one time password using email.
1870
1870
  * @property {string} [scope] - The OAuth scopes for the tokens. This is a list of
1871
1871
  * scopes, separated by space. If the list of scopes contains `openid`, the generated tokens
@@ -1899,7 +1899,7 @@ function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.g
1899
1899
  * `password` (will force password confirmation, even if user is already logged in). Those values might
1900
1900
  * be mixed as space-separated string. To make sure that user has authenticated with 2FA you need
1901
1901
  * to verify AMR (Authentication Methods References) claim in ID token.
1902
- * Might also be used to ensure additional acr (sms, otp) for already logged in users.
1902
+ * Might also be used to ensure additional acr (sms, otp) for already logged-in users.
1903
1903
  * Supported value is also 'otp-email' means one time password using email.
1904
1904
  * @property {string} [scope] - The OAuth scopes for the tokens. This is a list of
1905
1905
  * scopes, separated by space. If the list of scopes contains `openid`, the generated tokens
@@ -1973,7 +1973,7 @@ function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.g
1973
1973
 
1974
1974
  /**
1975
1975
  * @typedef {object} SimplifiedLoginData
1976
- * @property {string} identifier - Deprecated: User UUID, to be be used as `loginHint` for {@link Identity#login}
1976
+ * @property {string} identifier - Deprecated: User UUID, to be as `loginHint` for {@link Identity#login}
1977
1977
  * @property {string} display_text - Human-readable user identifier
1978
1978
  * @property {string} client_name - Client name
1979
1979
  */
@@ -1985,13 +1985,16 @@ function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.g
1985
1985
 
1986
1986
  var HAS_SESSION_CACHE_KEY = 'hasSession-cache';
1987
1987
  var SESSION_CALL_BLOCKED_CACHE_KEY = 'sessionCallBlocked-cache';
1988
- var SESSION_CALL_BLOCKED_TTL = 1000 * 60 * 5;
1988
+ var SESSION_CALL_BLOCKED_TTL = 1000 * 30;
1989
+ var TAB_ID_KEY = 'tab-id-cache';
1990
+ var TAB_ID = Math.floor(Math.random() * 100000);
1991
+ var TAB_ID_TTL = 1000 * 60 * 60 * 24 * 30;
1989
1992
  var globalWindow = function globalWindow() {
1990
1993
  return window;
1991
1994
  };
1992
1995
 
1993
1996
  /**
1994
- * Provides Identity functionalty to a web page
1997
+ * Provides Identity functionality to a web page
1995
1998
  */
1996
1999
  var Identity = /*#__PURE__*/function (_EventEmitter) {
1997
2000
  _inherits(Identity, _EventEmitter);
@@ -2027,20 +2030,20 @@ var Identity = /*#__PURE__*/function (_EventEmitter) {
2027
2030
  Object(_validate_js__WEBPACK_IMPORTED_MODULE_55__["assert"])(!redirectUri || Object(_validate_js__WEBPACK_IMPORTED_MODULE_55__["isUrl"])(redirectUri), 'redirectUri parameter is invalid');
2028
2031
  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');
2029
2032
  _spidTalk_js__WEBPACK_IMPORTED_MODULE_64__["emulate"](window);
2033
+
2034
+ // Internal hack: set as false to always refresh from hasSession
2035
+ _this._enableSessionCaching = true;
2030
2036
  _this._sessionInitiatedSent = false;
2031
2037
  _this.window = window;
2032
2038
  _this.clientId = clientId;
2033
- _this.cache = new _cache_js__WEBPACK_IMPORTED_MODULE_60__["default"](function () {
2034
- return _this.window && _this.window.sessionStorage;
2035
- });
2039
+ _this.sessionStorageCache = new _cache_js__WEBPACK_IMPORTED_MODULE_60__["default"](_this.window && _this.window.sessionStorage);
2040
+ _this.localStorageCache = new _cache_js__WEBPACK_IMPORTED_MODULE_60__["default"](_this.window && _this.window.localStorage);
2036
2041
  _this.redirectUri = redirectUri;
2037
2042
  _this.env = env;
2038
2043
  _this.log = log;
2039
2044
  _this.callbackBeforeRedirect = callbackBeforeRedirect;
2040
2045
  _this._sessionDomain = sessionDomain;
2041
-
2042
- // Internal hack: set to false to always refresh from hassession
2043
- _this._enableSessionCaching = true;
2046
+ _this._tabId = _this._getTabId();
2044
2047
 
2045
2048
  // Old session
2046
2049
  _this._session = {};
@@ -2049,50 +2052,61 @@ var Identity = /*#__PURE__*/function (_EventEmitter) {
2049
2052
  _this._setBffServerUrl(env);
2050
2053
  _this._setOauthServerUrl(env);
2051
2054
  _this._setGlobalSessionServiceUrl(env);
2052
- _this._unblockSessionCall();
2055
+ _this._unblockSessionCallByTab();
2053
2056
  return _this;
2054
2057
  }
2055
2058
 
2056
2059
  /**
2057
- * Checks if getting session is blocked
2060
+ * Read tabId from session storage if possible, otherwise save tabId to session storage and return it
2061
+ * @returns {number}
2058
2062
  * @private
2059
- *
2060
- * @returns {boolean|void}
2061
2063
  */
2062
2064
  _createClass(Identity, [{
2063
- key: "_isSessionCallBlocked",
2064
- value: function _isSessionCallBlocked() {
2065
+ key: "_getTabId",
2066
+ value: function _getTabId() {
2065
2067
  if (this._enableSessionCaching) {
2066
- return this.cache.get(SESSION_CALL_BLOCKED_CACHE_KEY);
2068
+ var tabId = this.sessionStorageCache.get(TAB_ID_KEY);
2069
+ if (!tabId) {
2070
+ this.sessionStorageCache.set(TAB_ID_KEY, TAB_ID, TAB_ID_TTL);
2071
+ return TAB_ID;
2072
+ }
2073
+ return tabId;
2067
2074
  }
2075
+ return TAB_ID;
2068
2076
  }
2069
2077
 
2070
2078
  /**
2071
- * Block calls to get session
2079
+ * Checks if calling GET session is blocked
2080
+ * @private
2081
+ * @returns {number|null}
2082
+ */
2083
+ }, {
2084
+ key: "_isSessionCallBlocked",
2085
+ value: function _isSessionCallBlocked() {
2086
+ return this.localStorageCache.get(SESSION_CALL_BLOCKED_CACHE_KEY);
2087
+ }
2088
+
2089
+ /**
2090
+ * 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
2072
2091
  * @private
2073
- *
2074
2092
  * @returns {void}
2075
2093
  */
2076
2094
  }, {
2077
2095
  key: "_blockSessionCall",
2078
2096
  value: function _blockSessionCall() {
2079
- if (this._enableSessionCaching) {
2080
- var SESSION_CALL_BLOCKED = true;
2081
- this.cache.set(SESSION_CALL_BLOCKED_CACHE_KEY, SESSION_CALL_BLOCKED, SESSION_CALL_BLOCKED_TTL);
2082
- }
2097
+ this.localStorageCache.set(SESSION_CALL_BLOCKED_CACHE_KEY, this._tabId, SESSION_CALL_BLOCKED_TTL);
2083
2098
  }
2084
2099
 
2085
2100
  /**
2086
- * Unblocks calls to get session
2101
+ * Unblocks calls to get session if the lock was put by the same tab
2087
2102
  * @private
2088
- *
2089
2103
  * @returns {void}
2090
2104
  */
2091
2105
  }, {
2092
- key: "_unblockSessionCall",
2093
- value: function _unblockSessionCall() {
2094
- if (this._enableSessionCaching) {
2095
- this.cache.delete(SESSION_CALL_BLOCKED_CACHE_KEY);
2106
+ key: "_unblockSessionCallByTab",
2107
+ value: function _unblockSessionCallByTab() {
2108
+ if (this._isSessionCallBlocked() === this._tabId) {
2109
+ this.localStorageCache.delete(SESSION_CALL_BLOCKED_CACHE_KEY);
2096
2110
  }
2097
2111
  }
2098
2112
 
@@ -2200,7 +2214,7 @@ var Identity = /*#__PURE__*/function (_EventEmitter) {
2200
2214
  }
2201
2215
 
2202
2216
  /**
2203
- * Emits the relevant events based on the previous and new reply from hassession
2217
+ * Emits the relevant events based on the previous and new reply from {@link Identity#hasSession}
2204
2218
  * @private
2205
2219
  * @param {object} previous
2206
2220
  * @param {object} current
@@ -2284,7 +2298,7 @@ var Identity = /*#__PURE__*/function (_EventEmitter) {
2284
2298
  }
2285
2299
 
2286
2300
  /**
2287
- * Set the Varnish cookie (`SP_ID`) when hasSession() is called. Note that most browsers require
2301
+ * Set the Varnish cookie (`SP_ID`) when {@link Identity#hasSession} is called. Note that most browsers require
2288
2302
  * that you are on a "real domain" for this to work — so, **not** `localhost`
2289
2303
  * @param {object} [options]
2290
2304
  * @param {number} [options.expiresIn] Override this to set number of seconds before the varnish
@@ -2437,7 +2451,7 @@ var Identity = /*#__PURE__*/function (_EventEmitter) {
2437
2451
  break;
2438
2452
  }
2439
2453
  // Try to resolve from cache (it has a TTL)
2440
- cachedSession = _this2.cache.get(HAS_SESSION_CACHE_KEY);
2454
+ cachedSession = _this2.sessionStorageCache.get(HAS_SESSION_CACHE_KEY);
2441
2455
  if (!cachedSession) {
2442
2456
  _context.next = 4;
2443
2457
  break;
@@ -2447,7 +2461,9 @@ var Identity = /*#__PURE__*/function (_EventEmitter) {
2447
2461
  sessionData = null;
2448
2462
  _context.prev = 5;
2449
2463
  _context.next = 8;
2450
- return _this2._sessionService.get('/v2/session');
2464
+ return _this2._sessionService.get('/v2/session', {
2465
+ tabId: _this2._tabId
2466
+ });
2451
2467
  case 8:
2452
2468
  sessionData = _context.sent;
2453
2469
  _context.next = 15;
@@ -2457,7 +2473,7 @@ var Identity = /*#__PURE__*/function (_EventEmitter) {
2457
2473
  _context.t0 = _context["catch"](5);
2458
2474
  if (_context.t0 && _context.t0.code === 400 && _this2._enableSessionCaching) {
2459
2475
  expiresIn = 1000 * (_context.t0.expiresIn || 300);
2460
- _this2.cache.set(HAS_SESSION_CACHE_KEY, {
2476
+ _this2.sessionStorageCache.set(HAS_SESSION_CACHE_KEY, {
2461
2477
  error: _context.t0
2462
2478
  }, expiresIn);
2463
2479
  }
@@ -2475,11 +2491,13 @@ var Identity = /*#__PURE__*/function (_EventEmitter) {
2475
2491
  _context.next = 20;
2476
2492
  return _this2.callbackBeforeRedirect();
2477
2493
  case 20:
2478
- return _context.abrupt("return", _this2._sessionService.makeUrl(sessionData.redirectURL));
2494
+ return _context.abrupt("return", _this2._sessionService.makeUrl(sessionData.redirectURL, {
2495
+ tabId: _this2._getTabId()
2496
+ }));
2479
2497
  case 21:
2480
2498
  if (_this2._enableSessionCaching) {
2481
2499
  _expiresIn = 1000 * (sessionData.expiresIn || 300);
2482
- _this2.cache.set(HAS_SESSION_CACHE_KEY, sessionData, _expiresIn);
2500
+ _this2.sessionStorageCache.set(HAS_SESSION_CACHE_KEY, sessionData, _expiresIn);
2483
2501
  }
2484
2502
  case 22:
2485
2503
  return _context.abrupt("return", _postProcess(sessionData));
@@ -2550,7 +2568,7 @@ var Identity = /*#__PURE__*/function (_EventEmitter) {
2550
2568
  }, {
2551
2569
  key: "clearCachedUserSession",
2552
2570
  value: function clearCachedUserSession() {
2553
- this.cache.delete(HAS_SESSION_CACHE_KEY);
2571
+ this.sessionStorageCache.delete(HAS_SESSION_CACHE_KEY);
2554
2572
  }
2555
2573
 
2556
2574
  /**
@@ -2644,7 +2662,7 @@ var Identity = /*#__PURE__*/function (_EventEmitter) {
2644
2662
  * @description This function calls {@link Identity#hasSession} internally and thus has the side
2645
2663
  * effect that it might perform an auto-login on the user
2646
2664
  * @throws {SDKError} If the user isn't connected to the merchant
2647
- * @return {Promise<string>} The `userId` field (not to be confused with the `uuid`)
2665
+ * @return {number} The `userId` field (not to be confused with the `uuid`)
2648
2666
  */
2649
2667
  }, {
2650
2668
  key: "getUserId",
@@ -2945,7 +2963,7 @@ var Identity = /*#__PURE__*/function (_EventEmitter) {
2945
2963
  _ref4$prompt = _ref4.prompt,
2946
2964
  prompt = _ref4$prompt === void 0 ? 'select_account' : _ref4$prompt;
2947
2965
  this._closePopup();
2948
- this.cache.delete(HAS_SESSION_CACHE_KEY);
2966
+ this.sessionStorageCache.delete(HAS_SESSION_CACHE_KEY);
2949
2967
  var url = this.loginUrl({
2950
2968
  state: state,
2951
2969
  acrValues: acrValues,
@@ -3018,7 +3036,7 @@ var Identity = /*#__PURE__*/function (_EventEmitter) {
3018
3036
  key: "logout",
3019
3037
  value: function logout() {
3020
3038
  var redirectUri = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.redirectUri;
3021
- this.cache.delete(HAS_SESSION_CACHE_KEY);
3039
+ this.sessionStorageCache.delete(HAS_SESSION_CACHE_KEY);
3022
3040
  this._maybeClearVarnishCookie();
3023
3041
  this.emit('logout');
3024
3042
  this.window.location.href = this.logoutUrl(redirectUri);
@@ -11026,7 +11044,6 @@ var Cache = /*#__PURE__*/function () {
11026
11044
  /**
11027
11045
  * Get a value from cache (checks that the object has not expired)
11028
11046
  * @param {string} key
11029
- * @private
11030
11047
  * @returns {*} - The value if it exists, otherwise null
11031
11048
  */
11032
11049
  _createClass(Cache, [{
@@ -11062,7 +11079,6 @@ var Cache = /*#__PURE__*/function () {
11062
11079
  * @param {string} key
11063
11080
  * @param {*} value
11064
11081
  * @param {Number} expiresIn - Value in milliseconds until the entry expires
11065
- * @private
11066
11082
  * @returns {void}
11067
11083
  */
11068
11084
  }, {
@@ -11091,7 +11107,6 @@ var Cache = /*#__PURE__*/function () {
11091
11107
  /**
11092
11108
  * Delete a cache entry
11093
11109
  * @param {string} key
11094
- * @private
11095
11110
  * @returns {void}
11096
11111
  */
11097
11112
  }, {
@@ -12011,7 +12026,7 @@ __webpack_require__.r(__webpack_exports__);
12011
12026
 
12012
12027
 
12013
12028
 
12014
- var version = '5.0.0';
12029
+ var version = '5.0.1-beta';
12015
12030
  /* harmony default export */ __webpack_exports__["default"] = (version);
12016
12031
 
12017
12032
  /***/ }),