Skip to content

Commit

Permalink
fix(logout): should work correctly with multiple tabs (release)
Browse files Browse the repository at this point in the history
  • Loading branch information
wermanoid committed Aug 8, 2024
1 parent 23ae6ba commit dd94789
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 29 deletions.
8 changes: 5 additions & 3 deletions packages/oidc-client-service-worker/src/OidcServiceWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -367,9 +367,11 @@ const handleMessage = async (event: ExtendableMessageEvent) => {
switch (data.type) {
case 'clear':
currentDatabase.tokens = null;
currentDatabase.tabIds = [];
currentDatabase.state = {};
currentDatabase.codeVerifier = {};
currentDatabase.tabIds = currentDatabase.tabIds.filter(id => id !== tabId);
delete currentDatabase.state[tabId];
delete currentDatabase.codeVerifier[tabId];
delete currentDatabase.nonce[tabId];

currentDatabase.demonstratingProofOfPossessionNonce = null;
currentDatabase.demonstratingProofOfPossessionJwkJson = null;
currentDatabase.demonstratingProofOfPossessionConfiguration = null;
Expand Down
41 changes: 15 additions & 26 deletions packages/oidc-client/src/initWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,18 @@ export const defaultServiceWorkerUpdateRequireCallback =
location.reload();
};

const getTabId = (configurationName: string) => {
const tabId = sessionStorage.getItem(`oidc.tabId.${configurationName}`);

if (tabId) {
return tabId;
}

const newTabId = globalThis.crypto.randomUUID();
sessionStorage.setItem(`oidc.tabId.${configurationName}`, newTabId);
return newTabId;
};

const sendMessageAsync =
registration =>
(data): Promise<any> => {
Expand All @@ -72,23 +84,13 @@ const sendMessageAsync =
messageChannel.port1.close();
messageChannel.port2.close();
};
registration.active.postMessage(data, [messageChannel.port2]);
registration.active.postMessage({ ...data, tabId: getTabId(data.configurationName) }, [
messageChannel.port2,
]);
});
};

export const initWorkerAsync = async (configuration, configurationName) => {
const getTabId = () => {
const tabId = sessionStorage.getItem(`oidc.tabId.${configurationName}`);

if (tabId) {
return tabId;
}

const newTabId = globalThis.crypto.randomUUID();
sessionStorage.setItem(`oidc.tabId.${configurationName}`, newTabId);
return newTabId;
};

const serviceWorkerRelativeUrl = configuration.service_worker_relative_url;
if (
typeof window === 'undefined' ||
Expand Down Expand Up @@ -138,7 +140,6 @@ export const initWorkerAsync = async (configuration, configurationName) => {
},
},
configurationName,
tabId: getTabId(),
});

// @ts-ignore
Expand Down Expand Up @@ -183,23 +184,19 @@ export const initWorkerAsync = async (configuration, configurationName) => {
};

const setNonceAsync = nonce => {
const tabId = getTabId();
sessionStorage[`oidc.nonce.${configurationName}`] = nonce.nonce;
return sendMessageAsync(registration)({
type: 'setNonce',
data: { nonce },
configurationName,
tabId,
});
};
const getNonceAsync = async () => {
const tabId = getTabId();
// @ts-ignore
const result = await sendMessageAsync(registration)({
type: 'getNonce',
data: null,
configurationName,
tabId,
});
// @ts-ignore
let nonce = result.nonce;
Expand Down Expand Up @@ -267,12 +264,10 @@ export const initWorkerAsync = async (configuration, configurationName) => {
};

const getStateAsync = async () => {
const tabId = getTabId();
const result = await sendMessageAsync(registration)({
type: 'getState',
data: null,
configurationName,
tabId,
});
// @ts-ignore
let state = result.state;
Expand All @@ -284,23 +279,19 @@ export const initWorkerAsync = async (configuration, configurationName) => {
};

const setStateAsync = async (state: string) => {
const tabId = getTabId();
sessionStorage[`oidc.state.${configurationName}`] = state;
return sendMessageAsync(registration)({
type: 'setState',
data: { state },
configurationName,
tabId,
});
};

const getCodeVerifierAsync = async () => {
const tabId = getTabId();
const result = await sendMessageAsync(registration)({
type: 'getCodeVerifier',
data: null,
configurationName,
tabId,
});
// @ts-ignore
let codeVerifier = result.codeVerifier;
Expand All @@ -312,13 +303,11 @@ export const initWorkerAsync = async (configuration, configurationName) => {
};

const setCodeVerifierAsync = async (codeVerifier: string) => {
const tabId = getTabId();
sessionStorage[`oidc.code_verifier.${configurationName}`] = codeVerifier;
return sendMessageAsync(registration)({
type: 'setCodeVerifier',
data: { codeVerifier },
configurationName,
tabId,
});
};

Expand Down

0 comments on commit dd94789

Please sign in to comment.