Browse Source

update: enhance MemoryTokenStorageService to support session storage for specific keys

pull/25701/head
sumeyye 3 months ago
parent
commit
441d2b52a1
  1. 73
      npm/ng-packs/packages/oauth/src/lib/services/memory-token-storage.service.ts

73
npm/ng-packs/packages/oauth/src/lib/services/memory-token-storage.service.ts

@ -22,6 +22,8 @@ export class MemoryTokenStorageService implements OAuthStorage {
'granted_scopes', 'granted_scopes',
]; ];
private keysShouldStoreInSession = ['nonce', 'PKCE_verifier', 'session_state'];
private worker?: any; private worker?: any;
private port?: MessagePort; private port?: MessagePort;
private cache = new Map<string, string>(); private cache = new Map<string, string>();
@ -89,38 +91,55 @@ export class MemoryTokenStorageService implements OAuthStorage {
} }
getItem(key: string): string | null { getItem(key: string): string | null {
if (!this.keysShouldStoreInMemory.includes(key)) { if (this.keysShouldStoreInMemory.includes(key)) {
return this.localStorageService.getItem(key); return this.cache.get(key) || null;
}
if (this.keysShouldStoreInSession.includes(key)) {
return this.getSessionItem(key);
} }
return this.cache.get(key) || null;
return this.localStorageService.getItem(key);
} }
setItem(key: string, value: string): void { setItem(key: string, value: string): void {
if (!this.keysShouldStoreInMemory.includes(key)) { if (this.keysShouldStoreInMemory.includes(key)) {
this.localStorageService.setItem(key, value); if (this.useSharedWorker && this.port) {
this.cache.set(key, value);
this.port.postMessage({ action: 'set', key, value });
} else {
this.cache.set(key, value);
}
return; return;
} }
if (this.useSharedWorker && this.port) { if (this.keysShouldStoreInSession.includes(key)) {
this.cache.set(key, value); this.setSessionItem(key, value);
this.port.postMessage({ action: 'set', key, value }); return;
} else {
this.cache.set(key, value);
} }
this.localStorageService.setItem(key, value);
} }
removeItem(key: string): void { removeItem(key: string): void {
if (!this.keysShouldStoreInMemory.includes(key)) { if (this.keysShouldStoreInMemory.includes(key)) {
this.localStorageService.removeItem(key); if (this.useSharedWorker && this.port) {
this.cache.delete(key);
this.port.postMessage({ action: 'remove', key });
} else {
this.cache.delete(key);
}
return; return;
} }
if (this.useSharedWorker && this.port) { if (this.keysShouldStoreInSession.includes(key)) {
this.cache.delete(key); this.removeSessionItem(key);
this.port.postMessage({ action: 'remove', key }); return;
} else {
this.cache.delete(key);
} }
this.localStorageService.removeItem(key);
} }
clear(): void { clear(): void {
@ -165,6 +184,26 @@ export class MemoryTokenStorageService implements OAuthStorage {
}, 100); }, 100);
} }
private getSessionStorage(): Storage | null {
try {
return this._document.defaultView?.sessionStorage ?? null;
} catch {
return null;
}
}
private getSessionItem(key: string): string | null {
return this.getSessionStorage()?.getItem(key) ?? null;
}
private setSessionItem(key: string, value: string): void {
this.getSessionStorage()?.setItem(key, value);
}
private removeSessionItem(key: string): void {
this.getSessionStorage()?.removeItem(key);
}
private createWorkerDataUrl(): string { private createWorkerDataUrl(): string {
const workerScript = `const tokenStore = new Map(); const workerScript = `const tokenStore = new Map();
const ports = new Set(); const ports = new Set();

Loading…
Cancel
Save