Skip to content

Commit

Permalink
#41
Browse files Browse the repository at this point in the history
  • Loading branch information
garronej committed Nov 14, 2024
1 parent ff5447c commit d048b62
Showing 1 changed file with 26 additions and 11 deletions.
37 changes: 26 additions & 11 deletions src/oidc.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import {
UserManager as OidcClientTsUserManager,
WebStorageStateStore,
InMemoryWebStorage,
type User as OidcClientTsUser
} from "./vendor/frontend/oidc-client-ts-and-jwt-decode";
import { id } from "./vendor/frontend/tsafe";
Expand Down Expand Up @@ -204,6 +206,8 @@ export type ParamsOfCreateOidc<
getDoContinueWithImpersonation?: (params: {
parsedAccessToken: Record<string, unknown>;
}) => Promise<boolean>;

doDisableTokenPersistence?: boolean;
};

const prOidcByConfigHash = new Map<string, Promise<Oidc<any>>>();
Expand Down Expand Up @@ -320,9 +324,12 @@ export async function createOidc_nonMemoized<
autoLogoutParams = { "redirectTo": "current page" },
isAuthGloballyRequired = false,
postLoginRedirectUrl,
getDoContinueWithImpersonation
getDoContinueWithImpersonation,
doDisableTokenPersistence = false
} = params;

const store = doDisableTokenPersistence ? new InMemoryWebStorage() : window.sessionStorage;

const { issuerUri, clientId, scopes, configHash, log } = preProcessedParams;

const [getExtraQueryParams, getExtraTokenParams] = (
Expand Down Expand Up @@ -420,6 +427,7 @@ export async function createOidc_nonMemoized<
await maybeImpersonate({
configHash,
getDoContinueWithImpersonation,
store,
log
});
}
Expand Down Expand Up @@ -448,7 +456,8 @@ export async function createOidc_nonMemoized<
}).newUrl;

return redirectUri;
})()
})(),
userStore: new WebStorageStateStore({ store })
});

let lastPublicRoute: string | undefined = undefined;
Expand Down Expand Up @@ -965,9 +974,14 @@ export async function createOidc_nonMemoized<
});
}

Object.keys(sessionStorage)
.filter(key => key.startsWith(SESSION_STORAGE_PREFIX))
.forEach(key => sessionStorage.removeItem(key));
for (let i = 0; i < store.length; i++) {
const key = store.key(i);
assert(key !== null);
if (!key.startsWith(SESSION_STORAGE_PREFIX)) {
continue;
}
store.removeItem(key);
}

return undefined;
}
Expand Down Expand Up @@ -1411,8 +1425,8 @@ export async function createOidc_nonMemoized<

const assertSessionStorageNotCleared = () => {
const hasOidcSessionStorageEntry = (() => {
for (let i = 0; i < sessionStorage.length; i++) {
const key = sessionStorage.key(i);
for (let i = 0; i < store.length; i++) {
const key = store.key(i);
assert(key !== null);

if (!key.startsWith(SESSION_STORAGE_PREFIX)) {
Expand Down Expand Up @@ -1782,9 +1796,10 @@ async function maybeImpersonate(params: {
ParamsOfCreateOidc["getDoContinueWithImpersonation"],
undefined
>;
store: Storage;
log: typeof console.log | undefined;
}) {
const { configHash, getDoContinueWithImpersonation, log } = params;
const { configHash, getDoContinueWithImpersonation, store, log } = params;

const value = (() => {
const KEY = "oidc-spa_impersonate";
Expand All @@ -1800,13 +1815,13 @@ async function maybeImpersonate(params: {

window.history.replaceState({}, "", result.newUrl);

sessionStorage.setItem(KEY, result.value);
store.setItem(KEY, result.value);

return result.value;
}

from_session_storage: {
const value = sessionStorage.getItem(KEY);
const value = store.getItem(KEY);

if (value === null) {
break from_session_storage;
Expand Down Expand Up @@ -1880,7 +1895,7 @@ async function maybeImpersonate(params: {

log?.("Impersonation confirmed, storing the impersonation params in the session storage");

sessionStorage.setItem(
store.setItem(
`${SESSION_STORAGE_PREFIX}user:${issuerUri}:${clientId}`,
JSON.stringify({
"id_token": idToken,
Expand Down

0 comments on commit d048b62

Please sign in to comment.