From 008e1f96fdfe5f49b1fb15d9e44999a3b9265475 Mon Sep 17 00:00:00 2001 From: niculescu-bogdan-constantin Date: Mon, 15 Apr 2024 13:28:48 +0200 Subject: [PATCH 1/4] Revert "move session call lock to local storage" This reverts commit 3e62be3d2b7eae34e8860b2e0452d4797a04f392. --- __tests__/identity.js | 2 +- src/identity.js | 37 +++++++++++++++++++++---------------- 2 files changed, 22 insertions(+), 17 deletions(-) diff --git a/__tests__/identity.js b/__tests__/identity.js index 24d6d1a..35ad789 100644 --- a/__tests__/identity.js +++ b/__tests__/identity.js @@ -563,7 +563,7 @@ describe('Identity', () => { jest.spyOn(Date, 'now') .mockReturnValue(new Date("2019-11-09T10:00:00").getTime()); - const getExpiresOn = () => JSON.parse(identity.sessionStorageCache.cache.get('hasSession-cache')).expiresOn; + const getExpiresOn = () => JSON.parse(identity.cache.cache.get('hasSession-cache')).expiresOn; await identity.hasSession(); diff --git a/src/identity.js b/src/identity.js index 2c7a7ed..32f20d2 100644 --- a/src/identity.js +++ b/src/identity.js @@ -189,8 +189,7 @@ export class Identity extends EventEmitter { this._sessionInitiatedSent = false; this.window = window; this.clientId = clientId; - this.sessionStorageCache = new Cache(() => this.window && this.window.sessionStorage); - this.localStorageCache = new Cache(() => this.window && this.window.localStorage); + this.cache = new Cache(() => this.window && this.window.sessionStorage); this.redirectUri = redirectUri; this.env = env; this.log = log; @@ -236,7 +235,9 @@ export class Identity extends EventEmitter { * @returns {boolean|void} */ _isSessionCallBlocked(){ - return this.localStorageCache.get(SESSION_CALL_BLOCKED_CACHE_KEY); + if (this._enableSessionCaching) { + return this.cache.get(SESSION_CALL_BLOCKED_CACHE_KEY); + } } /** @@ -246,13 +247,15 @@ export class Identity extends EventEmitter { * @returns {void} */ _blockSessionCall(){ - const SESSION_CALL_BLOCKED = true; + if (this._enableSessionCaching) { + const SESSION_CALL_BLOCKED = true; - this.localStorageCache.set( - SESSION_CALL_BLOCKED_CACHE_KEY, - SESSION_CALL_BLOCKED, - SESSION_CALL_BLOCKED_TTL - ); + this.cache.set( + SESSION_CALL_BLOCKED_CACHE_KEY, + SESSION_CALL_BLOCKED, + SESSION_CALL_BLOCKED_TTL + ); + } } /** @@ -262,7 +265,9 @@ export class Identity extends EventEmitter { * @returns {void} */ _unblockSessionCall(){ - this.localStorageCache.delete(SESSION_CALL_BLOCKED_CACHE_KEY); + if (this._enableSessionCaching) { + this.cache.delete(SESSION_CALL_BLOCKED_CACHE_KEY); + } } /** @@ -582,7 +587,7 @@ export class Identity extends EventEmitter { const _getSession = async () => { if (this._enableSessionCaching) { // Try to resolve from cache (it has a TTL) - let cachedSession = this.sessionStorageCache.get(HAS_SESSION_CACHE_KEY); + let cachedSession = this.cache.get(HAS_SESSION_CACHE_KEY); if (cachedSession) { return _postProcess(cachedSession); } @@ -593,7 +598,7 @@ export class Identity extends EventEmitter { } catch (err) { if (err && err.code === 400 && this._enableSessionCaching) { const expiresIn = 1000 * (err.expiresIn || 300); - this.sessionStorageCache.set(HAS_SESSION_CACHE_KEY, { error: err }, expiresIn); + this.cache.set(HAS_SESSION_CACHE_KEY, { error: err }, expiresIn); } throw err; } @@ -610,7 +615,7 @@ export class Identity extends EventEmitter { if (this._enableSessionCaching) { const expiresIn = 1000 * (sessionData.expiresIn || 300); - this.sessionStorageCache.set(HAS_SESSION_CACHE_KEY, sessionData, expiresIn); + this.cache.set(HAS_SESSION_CACHE_KEY, sessionData, expiresIn); } } @@ -658,7 +663,7 @@ export class Identity extends EventEmitter { * @returns {void} */ clearCachedUserSession() { - this.sessionStorageCache.delete(HAS_SESSION_CACHE_KEY); + this.cache.delete(HAS_SESSION_CACHE_KEY); } /** @@ -869,7 +874,7 @@ export class Identity extends EventEmitter { prompt = 'select_account' }) { this._closePopup(); - this.sessionStorageCache.delete(HAS_SESSION_CACHE_KEY); + this.cache.delete(HAS_SESSION_CACHE_KEY); const url = this.loginUrl({ state, acrValues, @@ -918,7 +923,7 @@ export class Identity extends EventEmitter { * @return {void} */ logout(redirectUri = this.redirectUri) { - this.sessionStorageCache.delete(HAS_SESSION_CACHE_KEY); + this.cache.delete(HAS_SESSION_CACHE_KEY); this._maybeClearVarnishCookie(); this.emit('logout'); this.window.location.href = this.logoutUrl(redirectUri); From 4a661b7e7e90cc8096beed7a4e26399ca632e8ba Mon Sep 17 00:00:00 2001 From: niculescu-bogdan-constantin Date: Mon, 15 Apr 2024 13:39:31 +0200 Subject: [PATCH 2/4] Revert "fix: name change for cache" This reverts commit 255ece94c0133e8bdda6469e2bf4c0ff774b4c69. --- src/identity.d.ts | 3 +-- src/identity.js | 6 +++--- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/identity.d.ts b/src/identity.d.ts index e75406c..49c10d7 100644 --- a/src/identity.d.ts +++ b/src/identity.d.ts @@ -28,8 +28,7 @@ export class Identity extends TinyEmitter { _sessionInitiatedSent: boolean; window: any; clientId: string; - sessionStorageCache: any; - localStorageCache: any; + cache: any; redirectUri: string; env: string; log: Function; diff --git a/src/identity.js b/src/identity.js index 32f20d2..ddfce1b 100644 --- a/src/identity.js +++ b/src/identity.js @@ -218,9 +218,9 @@ export class Identity extends EventEmitter { */ _getTabId() { if (this._enableSessionCaching) { - const tabId = this.sessionStorageCache.get(TAB_ID_KEY); + const tabId = this.cache.get(TAB_ID_KEY); if (!tabId) { - this.sessionStorageCache.set(TAB_ID_KEY, TAB_ID, TAB_ID_TTL); + this.cache.set(TAB_ID_KEY, TAB_ID, TAB_ID_TTL); return TAB_ID; } @@ -594,7 +594,7 @@ export class Identity extends EventEmitter { } let sessionData = null; try { - sessionData = await this._sessionService.get('/v2/session', {tabId: this._getTabId()}); + sessionData = await this._sessionService.get('/v2/session'); } catch (err) { if (err && err.code === 400 && this._enableSessionCaching) { const expiresIn = 1000 * (err.expiresIn || 300); From 8ade01a7ac3193b60532bfd92cb8a577e8e3e542 Mon Sep 17 00:00:00 2001 From: niculescu-bogdan-constantin Date: Mon, 15 Apr 2024 13:55:11 +0200 Subject: [PATCH 3/4] fix: add tabid to session call --- src/identity.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/identity.js b/src/identity.js index ddfce1b..98d1a7f 100644 --- a/src/identity.js +++ b/src/identity.js @@ -594,7 +594,7 @@ export class Identity extends EventEmitter { } let sessionData = null; try { - sessionData = await this._sessionService.get('/v2/session'); + sessionData = await this._sessionService.get('/v2/session', {tabId: this._getTabId()}); } catch (err) { if (err && err.code === 400 && this._enableSessionCaching) { const expiresIn = 1000 * (err.expiresIn || 300); From 9f2e54881ee9b36445c28011311cc30863177535 Mon Sep 17 00:00:00 2001 From: bogdan-niculescu-sch <104439589+bogdan-niculescu-sch@users.noreply.github.com> Date: Mon, 15 Apr 2024 14:08:45 +0200 Subject: [PATCH 4/4] chore: release 5.0.1 (#281) --- package-lock.json | 4 ++-- package.json | 2 +- src/version.js | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/package-lock.json b/package-lock.json index f5bfdf9..8a3d319 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@schibsted/account-sdk-browser", - "version": "5.0.0", + "version": "5.0.1", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@schibsted/account-sdk-browser", - "version": "5.0.0", + "version": "5.0.1", "license": "MIT", "dependencies": { "tiny-emitter": "^2.1.0" diff --git a/package.json b/package.json index 01e289e..79b47b3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@schibsted/account-sdk-browser", - "version": "5.0.0", + "version": "5.0.1", "description": "Schibsted account SDK for browsers", "main": "index.js", "type": "module", diff --git a/src/version.js b/src/version.js index e818658..11862d5 100644 --- a/src/version.js +++ b/src/version.js @@ -1,5 +1,5 @@ // Automatically generated in 'npm version' by scripts/genversion.js 'use strict' -const version = '5.0.0'; +const version = '5.0.1'; export default version;