@schibsted/account-sdk-browser 5.0.1-beta.3 → 5.0.1-beta.4

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@schibsted/account-sdk-browser",
3
- "version": "5.0.1-beta.3",
3
+ "version": "5.0.1-beta.4",
4
4
  "description": "Schibsted account SDK for browsers",
5
5
  "main": "index.js",
6
6
  "type": "module",
package/src/identity.d.ts CHANGED
@@ -45,9 +45,10 @@ export class Identity extends TinyEmitter {
45
45
  */
46
46
  private _getTabId;
47
47
  /**
48
- * Checks if calling get session is blocked
48
+ * Checks if calling GET session is blocked by a cache storage
49
49
  * @private
50
- * @returns {boolean|void}
50
+ * @param {Cache} cache - cache to check
51
+ * @returns {string|null}
51
52
  */
52
53
  private _isSessionCallBlocked;
53
54
  /**
@@ -61,7 +62,7 @@ export class Identity extends TinyEmitter {
61
62
  * @private
62
63
  * @returns {void}
63
64
  */
64
- private _unblockSessionCall;
65
+ private _unblockSessionCallByTab;
65
66
  /**
66
67
  * Set SPiD server URL
67
68
  * @private
package/src/identity.js CHANGED
@@ -180,7 +180,7 @@ export class Identity extends EventEmitter {
180
180
  env = 'PRE',
181
181
  log,
182
182
  window = globalWindow(),
183
- callbackBeforeRedirect = ()=>{}
183
+ callbackBeforeRedirect = () => {}
184
184
  }) {
185
185
  super();
186
186
  assert(isNonEmptyString(clientId), 'clientId parameter is required');
@@ -197,7 +197,7 @@ export class Identity extends EventEmitter {
197
197
  this.window = window;
198
198
  this.clientId = clientId;
199
199
  this.sessionStorageCache = new Cache(() => this.window && this.window.sessionStorage);
200
- this.localStorageCache = new Cache(() =>this.window && this.window.localStorage);
200
+ this.localStorageCache = new Cache(() => this.window && this.window.localStorage);
201
201
  this.redirectUri = redirectUri;
202
202
  this.env = env;
203
203
  this.log = log;
@@ -237,12 +237,13 @@ export class Identity extends EventEmitter {
237
237
  }
238
238
 
239
239
  /**
240
- * Checks if calling GET session is blocked
240
+ * Checks if calling GET session is blocked by a cache storage
241
241
  * @private
242
- * @returns {number|null}
242
+ * @param {Cache} cache - cache to check
243
+ * @returns {string|null}
243
244
  */
244
- _isSessionCallBlocked(){
245
- return this.localStorageCache.get(SESSION_CALL_BLOCKED_CACHE_KEY);
245
+ _isSessionCallBlocked(cache) {
246
+ return cache.get(SESSION_CALL_BLOCKED_CACHE_KEY);
246
247
  }
247
248
 
248
249
  /**
@@ -250,7 +251,15 @@ export class Identity extends EventEmitter {
250
251
  * @private
251
252
  * @returns {void}
252
253
  */
253
- _blockSessionCall(){
254
+ _blockSessionCall() {
255
+ // session storage block protects against single tab, multiple identity instance concurrency
256
+ this.sessionStorageCache.set(
257
+ SESSION_CALL_BLOCKED_CACHE_KEY,
258
+ this._tabId,
259
+ SESSION_CALL_BLOCKED_TTL
260
+ );
261
+
262
+ // local storage block protects against cross-tab concurrency
254
263
  this.localStorageCache.set(
255
264
  SESSION_CALL_BLOCKED_CACHE_KEY,
256
265
  this._tabId,
@@ -264,9 +273,11 @@ export class Identity extends EventEmitter {
264
273
  * @returns {void}
265
274
  */
266
275
  _unblockSessionCallByTab() {
267
- if (this._isSessionCallBlocked() === this._tabId) {
276
+ if (this._isSessionCallBlocked(this.localStorageCache) === this._tabId) {
268
277
  this.localStorageCache.delete(SESSION_CALL_BLOCKED_CACHE_KEY);
269
278
  }
279
+
280
+ this.sessionStorageCache.delete(SESSION_CALL_BLOCKED_CACHE_KEY);
270
281
  }
271
282
 
272
283
  /**
@@ -342,7 +353,7 @@ export class Identity extends EventEmitter {
342
353
  this._globalSessionService = new RESTClient({
343
354
  serverUrl: urlMapper(url, ENDPOINTS.SESSION_SERVICE),
344
355
  log: this.log,
345
- defaultParams: { client_sdrn, sdk_version: version },
356
+ defaultParams: {client_sdrn, sdk_version: version},
346
357
  });
347
358
  }
348
359
 
@@ -506,7 +517,7 @@ export class Identity extends EventEmitter {
506
517
  * @returns {void}
507
518
  */
508
519
  _clearVarnishCookie() {
509
- const baseDomain = this._session && typeof this._session.baseDomain === 'string'
520
+ const baseDomain = this._session && typeof this._session.baseDomain === 'string'
510
521
  ? this._session.baseDomain
511
522
  : document.domain;
512
523
 
@@ -568,10 +579,11 @@ export class Identity extends EventEmitter {
568
579
  this._maybeSetVarnishCookie(sessionData);
569
580
  this._emitSessionEvent(this._session, sessionData);
570
581
  this._session = sessionData;
582
+
571
583
  return sessionData;
572
584
  };
573
585
 
574
- const _checkRedirectionNeed = (sessionData= {}) => {
586
+ const _checkRedirectionNeed = (sessionData = {}) => {
575
587
  const sessionDataKeys = Object.keys(sessionData);
576
588
 
577
589
  return sessionDataKeys.length === 1 &&
@@ -581,8 +593,8 @@ export class Identity extends EventEmitter {
581
593
  const _getSession = async () => {
582
594
  const callSessionEndpoint = async () => {
583
595
  try {
584
- // Blocking future calls to session-service. This lock is removed after the response is processed
585
- // to account for redirection that can happen towards session-service too
596
+ /* Blocking future calls to session-service. This lock is removed after the response is processed
597
+ to account for redirection that can happen towards session-service too */
586
598
  this._blockSessionCall();
587
599
 
588
600
  return await this._sessionService.get('/v2/session', {tabId: this._tabId});
@@ -611,7 +623,7 @@ export class Identity extends EventEmitter {
611
623
  this.sessionStorageCache.set(HAS_SESSION_CACHE_KEY, sessionData, expiresIn);
612
624
  }
613
625
 
614
- return _postProcess(sessionData)
626
+ return _postProcess(sessionData);
615
627
  }
616
628
  };
617
629
 
@@ -624,11 +636,12 @@ export class Identity extends EventEmitter {
624
636
  }
625
637
  }
626
638
 
627
- if (this._isSessionCallBlocked()) {
639
+ if (this._isSessionCallBlocked(this.sessionStorageCache) || this._isSessionCallBlocked(this.localStorageCache)) {
628
640
  if (this._session && this._session.userId) {
629
641
  return _postProcess(this._session);
630
642
  }
631
643
 
644
+ // If blockedAction is defined, do that and return the result, otherwise return null
632
645
  if (blockedAction) {
633
646
  const blockedResult = await blockedAction();
634
647
 
@@ -637,30 +650,32 @@ export class Identity extends EventEmitter {
637
650
 
638
651
  return null;
639
652
  }
653
+
654
+ // If session service calls are not blocked, call it
640
655
  const sessionData = await callSessionEndpoint();
641
656
 
642
657
  return await useSessionResponseIfValid(sessionData);
643
658
  };
644
659
 
645
- return await checkIfSessionCallIsNeededAndSafe(async ()=> {
660
+ return await checkIfSessionCallIsNeededAndSafe(async () => {
646
661
  let retryCount = 0;
647
662
 
648
663
  // Try to call session-service MAX_SESSION_CALL_RETRIES times, waiting up to 1 second each time
649
664
  while (retryCount < MAX_SESSION_CALL_RETRIES) {
650
665
  retryCount++;
666
+
651
667
  const randomWaitingStep = Math.floor(Math.random() * 9); // ignoring waiting times that are too small to matter
652
668
  const randomWaitTime = MIN_SESSION_CALL_WAIT_TIME + (randomWaitingStep * 100);
653
- await new Promise( resolve => { setTimeout(() =>{
654
- return resolve();
655
- }, randomWaitTime)});
669
+ await new Promise(resolve => setTimeout(resolve, randomWaitTime));
656
670
 
671
+ // attempt to call session service, but don't take any action if call is blocked and don't use the result
657
672
  const result = await checkIfSessionCallIsNeededAndSafe(null);
658
673
  if (result) {
659
674
  return result;
660
675
  }
661
676
  }
662
677
 
663
- // exceeded number of attempts, returning old session info
678
+ // Exceeded number of attempts, returning old session info
664
679
  if (this._session && this._session.userId) {
665
680
  return this._session;
666
681
  }
@@ -726,8 +741,8 @@ export class Identity extends EventEmitter {
726
741
  async isConnected() {
727
742
  try {
728
743
  const data = await this.hasSession();
729
- // if data is not an object, the promise will fail.
730
- // if the result is present, it's boolean. But if it's not, it should be assumed false.
744
+ /* If data is not an object, the promise will fail.
745
+ If the result is present, it's boolean. But if it's not, it should be assumed false. */
731
746
  return !!data.result;
732
747
  } catch (_) {
733
748
  return false;
@@ -802,13 +817,13 @@ export class Identity extends EventEmitter {
802
817
  if (!pairId)
803
818
  throw new SDKError('pairId missing in user session!');
804
819
 
805
- if(!externalParty || externalParty.length === 0) {
820
+ if (!externalParty || externalParty.length === 0) {
806
821
  throw new SDKError('externalParty cannot be empty');
807
822
  }
808
- const _toHexDigest = (hashBuffer) =>{
809
- // convert buffer to byte array
823
+ const _toHexDigest = (hashBuffer) => {
824
+ // Convert buffer to byte array
810
825
  const hashArray = Array.from(new Uint8Array(hashBuffer));
811
- // convert bytes to hex string
826
+ // Convert bytes to hex string
812
827
  return hashArray.map(b => b.toString(16).padStart(2, '0')).join('');
813
828
  }
814
829
 
@@ -884,6 +899,7 @@ export class Identity extends EventEmitter {
884
899
  return null;
885
900
  }
886
901
  }
902
+
887
903
  /**
888
904
  * If a popup is desired, this function needs to be called in response to a user event (like
889
905
  * click or tap) in order to work correctly. Otherwise the popup will be blocked by the
@@ -1006,7 +1022,7 @@ export class Identity extends EventEmitter {
1006
1022
  prompt = 'select_account',
1007
1023
  }) {
1008
1024
  if (typeof arguments[0] !== 'object') {
1009
- // backward compatibility
1025
+ // Backward compatibility
1010
1026
  state = arguments[0];
1011
1027
  acrValues = arguments[1];
1012
1028
  scope = arguments[2] || scope;
@@ -1134,7 +1150,10 @@ export class Identity extends EventEmitter {
1134
1150
  };
1135
1151
 
1136
1152
  const loginNotYouHandler = async () => {
1137
- this.login(Object.assign(await prepareLoginParams(loginParams), {loginHint: userData.identifier, prompt: 'login'}));
1153
+ this.login(Object.assign(await prepareLoginParams(loginParams), {
1154
+ loginHint: userData.identifier,
1155
+ prompt: 'login'
1156
+ }));
1138
1157
  };
1139
1158
 
1140
1159
  const initHandler = () => {
package/src/version.js CHANGED
@@ -1,5 +1,5 @@
1
1
  // Automatically generated in 'npm version' by scripts/genversion.js
2
2
 
3
3
  'use strict'
4
- const version = '5.0.1-beta.3';
4
+ const version = '5.0.1-beta.4';
5
5
  export default version;