From 32df257f34e50149b6f949ecd4335dbe11928ef9 Mon Sep 17 00:00:00 2001 From: Bob van de Vijver Date: Tue, 9 Jul 2024 16:15:44 +0200 Subject: [PATCH 1/2] Support multiple authorisations to be stored and restored --- public/init-swagger-ui.js | 42 +++++++++++++++++++++++++++++++-------- 1 file changed, 34 insertions(+), 8 deletions(-) diff --git a/public/init-swagger-ui.js b/public/init-swagger-ui.js index bb06f3f26..60c466752 100644 --- a/public/init-swagger-ui.js +++ b/public/init-swagger-ui.js @@ -25,26 +25,52 @@ function loadSwaggerUI(userOptions = {}) { const storageKey = 'nelmio_api_auth'; - // if we have auth in storage use it - if (sessionStorage.getItem(storageKey)) { - try { - ui.authActions.authorize(JSON.parse(sessionStorage.getItem(storageKey))); - } catch (ignored) { - // catch any errors here so it does not stop script execution + function getAuthorizationsFromStorage(){ + if (sessionStorage.getItem(storageKey)) { + try { + return JSON.parse(sessionStorage.getItem(storageKey)) + } catch (ignored) { + // catch any errors here so it does not stop script execution + } } + + return {}; + } + + // if we have auth in storage use it + try { + const currentAuthorizations = getAuthorizationsFromStorage(); + Object.keys(currentAuthorizations).forEach(k => ui.authActions.authorize({[k]: currentAuthorizations[k]})); + } catch (ignored) { + // catch any errors here so it does not stop script execution } // hook into authorize to store the auth in local storage when user performs authorization const currentAuthorize = ui.authActions.authorize; ui.authActions.authorize = function (payload) { - sessionStorage.setItem(storageKey, JSON.stringify(payload)); + try { + sessionStorage.setItem(storageKey, JSON.stringify(Object.assign( + getAuthorizationsFromStorage(), + payload + ))); + } catch (ignored) { + // catch any errors here so it does not stop script execution + } + return currentAuthorize(payload); }; // hook into logout to clear auth from storage if user logs out const currentLogout = ui.authActions.logout; ui.authActions.logout = function (payload) { - sessionStorage.removeItem(storageKey); + try { + let currentAuth = getAuthorizationsFromStorage(); + payload.forEach(k => delete currentAuth[k]); + sessionStorage.setItem(storageKey, JSON.stringify(currentAuth)); + } catch (ignored) { + // catch any errors here so it does not stop script execution + } + return currentLogout(payload); }; From c74cfc4d2b791e915a06bda42d3022a4aecf58d1 Mon Sep 17 00:00:00 2001 From: Bob van de Vijver Date: Tue, 9 Jul 2024 16:25:01 +0200 Subject: [PATCH 2/2] Apply suggestions from code review --- public/init-swagger-ui.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/public/init-swagger-ui.js b/public/init-swagger-ui.js index 60c466752..8f4ee3c8a 100644 --- a/public/init-swagger-ui.js +++ b/public/init-swagger-ui.js @@ -25,10 +25,10 @@ function loadSwaggerUI(userOptions = {}) { const storageKey = 'nelmio_api_auth'; - function getAuthorizationsFromStorage(){ + function getAuthorizationsFromStorage() { if (sessionStorage.getItem(storageKey)) { try { - return JSON.parse(sessionStorage.getItem(storageKey)) + return JSON.parse(sessionStorage.getItem(storageKey)); } catch (ignored) { // catch any errors here so it does not stop script execution }