From 7959be19eca3d4213a5e3ead9f8d19ffcb39c5d7 Mon Sep 17 00:00:00 2001 From: niculescu-bogdan-constantin Date: Fri, 12 Apr 2024 09:33:58 +0200 Subject: [PATCH] move session call lock to local storage --- __tests__/identity.js | 2 +- src/identity.js | 37 ++++++++++++++++--------------------- 2 files changed, 17 insertions(+), 22 deletions(-) diff --git a/__tests__/identity.js b/__tests__/identity.js index cea08b0..ace0db2 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.cache.cache.get('hasSession-cache')).expiresOn; + const getExpiresOn = () => JSON.parse(identity.sessionStorageCache.cache.get('hasSession-cache')).expiresOn; await identity.hasSession(); diff --git a/src/identity.js b/src/identity.js index 7107df3..b9db8d7 100644 --- a/src/identity.js +++ b/src/identity.js @@ -185,7 +185,8 @@ export class Identity extends EventEmitter { this._sessionInitiatedSent = false; this.window = window; this.clientId = clientId; - this.cache = new Cache(() => this.window && this.window.sessionStorage); + this.sessionStorageCache = new Cache(() => this.window && this.window.sessionStorage); + this.localStorageCache = new Cache(() => this.window && this.window.localStorage); this.redirectUri = redirectUri; this.env = env; this.log = log; @@ -214,9 +215,7 @@ export class Identity extends EventEmitter { * @returns {boolean|void} */ _isSessionCallBlocked(){ - if (this._enableSessionCaching) { - return this.cache.get(SESSION_CALL_BLOCKED_CACHE_KEY); - } + return this.localStorageCache.get(SESSION_CALL_BLOCKED_CACHE_KEY); } /** @@ -226,15 +225,13 @@ export class Identity extends EventEmitter { * @returns {void} */ _blockSessionCall(){ - if (this._enableSessionCaching) { - const SESSION_CALL_BLOCKED = true; + const SESSION_CALL_BLOCKED = true; - this.cache.set( - SESSION_CALL_BLOCKED_CACHE_KEY, - SESSION_CALL_BLOCKED, - SESSION_CALL_BLOCKED_TTL - ); - } + this.localStorageCache.set( + SESSION_CALL_BLOCKED_CACHE_KEY, + SESSION_CALL_BLOCKED, + SESSION_CALL_BLOCKED_TTL + ); } /** @@ -244,9 +241,7 @@ export class Identity extends EventEmitter { * @returns {void} */ _unblockSessionCall(){ - if (this._enableSessionCaching) { - this.cache.delete(SESSION_CALL_BLOCKED_CACHE_KEY); - } + this.localStorageCache.delete(SESSION_CALL_BLOCKED_CACHE_KEY); } /** @@ -566,7 +561,7 @@ export class Identity extends EventEmitter { const _getSession = async () => { if (this._enableSessionCaching) { // Try to resolve from cache (it has a TTL) - let cachedSession = this.cache.get(HAS_SESSION_CACHE_KEY); + let cachedSession = this.sessionStorageCache.get(HAS_SESSION_CACHE_KEY); if (cachedSession) { return _postProcess(cachedSession); } @@ -577,7 +572,7 @@ export class Identity extends EventEmitter { } catch (err) { if (err && err.code === 400 && this._enableSessionCaching) { const expiresIn = 1000 * (err.expiresIn || 300); - this.cache.set(HAS_SESSION_CACHE_KEY, { error: err }, expiresIn); + this.sessionStorageCache.set(HAS_SESSION_CACHE_KEY, { error: err }, expiresIn); } throw err; } @@ -594,7 +589,7 @@ export class Identity extends EventEmitter { if (this._enableSessionCaching) { const expiresIn = 1000 * (sessionData.expiresIn || 300); - this.cache.set(HAS_SESSION_CACHE_KEY, sessionData, expiresIn); + this.sessionStorageCache.set(HAS_SESSION_CACHE_KEY, sessionData, expiresIn); } } @@ -642,7 +637,7 @@ export class Identity extends EventEmitter { * @returns {void} */ clearCachedUserSession() { - this.cache.delete(HAS_SESSION_CACHE_KEY); + this.sessionStorageCache.delete(HAS_SESSION_CACHE_KEY); } /** @@ -853,7 +848,7 @@ export class Identity extends EventEmitter { prompt = 'select_account' }) { this._closePopup(); - this.cache.delete(HAS_SESSION_CACHE_KEY); + this.sessionStorageCache.delete(HAS_SESSION_CACHE_KEY); const url = this.loginUrl({ state, acrValues, @@ -902,7 +897,7 @@ export class Identity extends EventEmitter { * @return {void} */ logout(redirectUri = this.redirectUri) { - this.cache.delete(HAS_SESSION_CACHE_KEY); + this.sessionStorageCache.delete(HAS_SESSION_CACHE_KEY); this._maybeClearVarnishCookie(); this.emit('logout'); this.window.location.href = this.logoutUrl(redirectUri);