Skip to content

Commit

Permalink
fix(oidc-client): tokens refresh conflict multiple tabs
Browse files Browse the repository at this point in the history
  • Loading branch information
guillaume-chervet committed Nov 17, 2023
1 parent 216adc7 commit c7c10a2
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 15 deletions.
2 changes: 1 addition & 1 deletion packages/oidc-client/src/initSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export const initSession = (configurationName, storage = sessionStorage) => {
return { nonce: storage[`oidc.nonce.${configurationName}`] };
};

const setDemonstratingProofOfPossessionNonce = (dpopNonce:string) => {
const setDemonstratingProofOfPossessionNonce = async (dpopNonce:string) => {
storage[`oidc.dpop_nonce.${configurationName}`] = dpopNonce;
};

Expand Down
6 changes: 3 additions & 3 deletions packages/oidc-client/src/initWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,16 +157,16 @@ export const initWorkerAsync = async(configuration, configurationName) => {
return getLoginParamsCache[configurationName];
};

const setDemonstratingProofOfPossessionNonce = (demonstratingProofOfPossessionNonce: string) => {
sendMessageAsync(registration)({ type: 'setDemonstratingProofOfPossessionNonce', data: { demonstratingProofOfPossessionNonce }, configurationName });
const setDemonstratingProofOfPossessionNonce = async (demonstratingProofOfPossessionNonce: string) => {
await sendMessageAsync(registration)({ type: 'setDemonstratingProofOfPossessionNonce', data: { demonstratingProofOfPossessionNonce }, configurationName });
};

const getDemonstratingProofOfPossessionNonce = async () => {
const result = await sendMessageAsync(registration)({type: 'getDemonstratingProofOfPossessionNonce', data: null, configurationName});
return result.demonstratingProofOfPossessionNonce;
};

const setDemonstratingProofOfPossessionJwkAsync = (demonstratingProofOfPossessionJwk:string) => {
const setDemonstratingProofOfPossessionJwkAsync = async (demonstratingProofOfPossessionJwk:string) => {
const demonstratingProofOfPossessionJwkJson = JSON.stringify(demonstratingProofOfPossessionJwk);
sendMessageAsync(registration)({ type: 'setDemonstratingProofOfPossessionJwk', data: { demonstratingProofOfPossessionJwkJson }, configurationName });
};
Expand Down
35 changes: 24 additions & 11 deletions packages/oidc-client/src/renewTokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,33 @@ import timer from './timer.js';
import { StringMap } from './types.js';

export async function renewTokensAndStartTimerAsync(oidc, refreshToken, forceRefresh = false, extras:StringMap = null) {
const updateTokens = (tokens) => { oidc.tokens = tokens; };
const { tokens, status } = await oidc.synchroniseTokensAsync(refreshToken, 0, forceRefresh, extras, updateTokens);

const serviceWorker = await initWorkerAsync(oidc.configuration, oidc.configurationName);
if (!serviceWorker) {
const session = initSession(oidc.configurationName, oidc.configuration.storage);
await session.setTokens(oidc.tokens);
}
const configuration = oidc.configuration;
const lockResourcesName = `${configuration.client_id}_${oidc.configurationName}_${configuration.authority}`;
console.log(`lockResourcesName: ${lockResourcesName}`);
const tokens = await navigator.locks.request(lockResourcesName, async (lock) => {
console.log(`locked: ${lockResourcesName}`);
const updateTokens = (tokens) => {
oidc.tokens = tokens;
};
const {tokens, status} = await oidc.synchroniseTokensAsync(refreshToken, 0, forceRefresh, extras, updateTokens);

if (!oidc.tokens) {
await oidc.destroyAsync(status);
return;
}
const serviceWorker = await initWorkerAsync(oidc.configuration, oidc.configurationName);
if (!serviceWorker) {
const session = initSession(oidc.configurationName, oidc.configuration.storage);
await session.setTokens(oidc.tokens);
}

if (!oidc.tokens) {
await oidc.destroyAsync(status);
return null;
}
return tokens
});
console.log(`zndloc: ${lockResourcesName}`);
if(!tokens){
return null;
}
if (oidc.timeoutId) {
oidc.timeoutId = autoRenewTokens(oidc, tokens.refreshToken, oidc.tokens.expiresAt, extras);
}
Expand Down

0 comments on commit c7c10a2

Please sign in to comment.