diff --git a/src/OidcClientSettings.ts b/src/OidcClientSettings.ts index 3574ff32c..f33f072bf 100644 --- a/src/OidcClientSettings.ts +++ b/src/OidcClientSettings.ts @@ -4,6 +4,7 @@ import { WebStorageStateStore } from "./WebStorageStateStore"; import type { OidcMetadata } from "./OidcMetadata"; import type { StateStore } from "./StateStore"; +import { InMemoryWebStorage } from "./InMemoryWebStorage"; const DefaultResponseType = "code"; const DefaultScope = "openid"; @@ -87,7 +88,7 @@ export interface OidcClientSettings { mergeClaims?: boolean; /** - * Storage object used to persist interaction state (default: local storage). + * Storage object used to persist interaction state (default: window.localStorage, InMemoryWebStorage iff no window). * E.g. `stateStore: new WebStorageStateStore({ store: window.localStorage })` */ stateStore?: StateStore; @@ -164,7 +165,7 @@ export class OidcClientSettingsStore { userInfoJwtIssuer = "OP", mergeClaims = false, // other behavior - stateStore = new WebStorageStateStore(), + stateStore, // extra query params extraQueryParams = {}, extraTokenParams = {}, @@ -199,7 +200,13 @@ export class OidcClientSettingsStore { this.userInfoJwtIssuer = userInfoJwtIssuer; this.mergeClaims = !!mergeClaims; - this.stateStore = stateStore; + if (stateStore) { + this.stateStore = stateStore; + } + else { + const store = typeof window !== "undefined" ? window.localStorage : new InMemoryWebStorage(); + this.stateStore = new WebStorageStateStore({ store }); + } this.extraQueryParams = extraQueryParams; this.extraTokenParams = extraTokenParams; diff --git a/src/UserManagerSettings.ts b/src/UserManagerSettings.ts index caaeb86ba..468a66862 100644 --- a/src/UserManagerSettings.ts +++ b/src/UserManagerSettings.ts @@ -4,6 +4,7 @@ import { OidcClientSettings, OidcClientSettingsStore } from "./OidcClientSettings"; import type { PopupWindowFeatures } from "./utils"; import { WebStorageStateStore } from "./WebStorageStateStore"; +import { InMemoryWebStorage } from "./InMemoryWebStorage"; const DefaultAccessTokenExpiringNotificationTimeInSeconds = 60; const DefaultCheckSessionIntervalInSeconds = 2; @@ -53,7 +54,7 @@ export interface UserManagerSettings extends OidcClientSettings { accessTokenExpiringNotificationTimeInSeconds?: number; /** - * Storage object used to persist User for currently authenticated user (default: session storage). + * Storage object used to persist User for currently authenticated user (default: window.sessionStorage, InMemoryWebStorage iff no window). * E.g. `userStore: new WebStorageStateStore({ store: window.localStorage })` */ userStore?: WebStorageStateStore; @@ -112,7 +113,7 @@ export class UserManagerSettingsStore extends OidcClientSettingsStore { revokeAccessTokenOnSignout = false, accessTokenExpiringNotificationTimeInSeconds = DefaultAccessTokenExpiringNotificationTimeInSeconds, - userStore = new WebStorageStateStore({ store: sessionStorage }), + userStore, } = args; super(args); @@ -143,6 +144,12 @@ export class UserManagerSettingsStore extends OidcClientSettingsStore { this.revokeAccessTokenOnSignout = revokeAccessTokenOnSignout; this.accessTokenExpiringNotificationTimeInSeconds = accessTokenExpiringNotificationTimeInSeconds; - this.userStore = userStore; + if (userStore) { + this.userStore = userStore; + } + else { + const store = typeof window !== "undefined" ? window.sessionStorage : new InMemoryWebStorage(); + this.userStore = new WebStorageStateStore({ store }); + } } } diff --git a/src/utils/CryptoUtils.ts b/src/utils/CryptoUtils.ts index ff03d52ef..777d37685 100644 --- a/src/utils/CryptoUtils.ts +++ b/src/utils/CryptoUtils.ts @@ -27,7 +27,8 @@ export class CryptoUtils { * Generates RFC4122 version 4 guid */ public static generateUUIDv4(): string { - const hasRandomValues = window.crypto && Object.prototype.hasOwnProperty.call(window.crypto, "getRandomValues"); + const hasRandomValues = typeof window !== "undefined" && window.crypto && + Object.prototype.hasOwnProperty.call(window.crypto, "getRandomValues"); const uuid = hasRandomValues ? CryptoUtils._cryptoUUIDv4() : CryptoUtils._UUIDv4(); return uuid.replace(/-/g, ""); } diff --git a/src/utils/Timer.ts b/src/utils/Timer.ts index 3f43fd575..07a9903d4 100644 --- a/src/utils/Timer.ts +++ b/src/utils/Timer.ts @@ -18,10 +18,10 @@ export type IntervalTimer = { */ export const g_timer: IntervalTimer = { setInterval: function (cb: () => void, duration?: number): number { - return window.setInterval(cb, duration); + return setInterval(cb, duration); }, clearInterval: function (handle: number): void { - return window.clearInterval(handle); + return clearInterval(handle); }, };