feat: init and fork some code from angular-auth-oidc-client
Some checks are pending
Build, Lint & Test Lib / Built, Lint and Test Library (push) Waiting to run
Build, Lint & Test Lib / Angular latest (push) Blocked by required conditions
Build, Lint & Test Lib / Angular latest & Schematics Job (push) Blocked by required conditions
Build, Lint & Test Lib / Angular latest Standalone & Schematics Job (push) Blocked by required conditions
Build, Lint & Test Lib / Angular 16 & RxJs 6 (push) Blocked by required conditions
Build, Lint & Test Lib / Angular V16 (push) Blocked by required conditions
Docs / Build and Deploy Docs Job (push) Waiting to run
Docs / Close Pull Request Job (push) Waiting to run
Some checks are pending
Build, Lint & Test Lib / Built, Lint and Test Library (push) Waiting to run
Build, Lint & Test Lib / Angular latest (push) Blocked by required conditions
Build, Lint & Test Lib / Angular latest & Schematics Job (push) Blocked by required conditions
Build, Lint & Test Lib / Angular latest Standalone & Schematics Job (push) Blocked by required conditions
Build, Lint & Test Lib / Angular 16 & RxJs 6 (push) Blocked by required conditions
Build, Lint & Test Lib / Angular V16 (push) Blocked by required conditions
Docs / Build and Deploy Docs Job (push) Waiting to run
Docs / Close Pull Request Job (push) Waiting to run
This commit is contained in:
97
src/storage/storage-persistence.service.ts
Normal file
97
src/storage/storage-persistence.service.ts
Normal file
@@ -0,0 +1,97 @@
|
||||
import { inject, Injectable } from 'injection-js';
|
||||
import { AuthResult } from '../flows/callback-context';
|
||||
import { OpenIdConfiguration } from '../config/openid-configuration';
|
||||
import { BrowserStorageService } from './browser-storage.service';
|
||||
|
||||
export type StorageKeys =
|
||||
| 'authnResult'
|
||||
| 'authzData'
|
||||
| 'access_token_expires_at'
|
||||
| 'authWellKnownEndPoints'
|
||||
| 'userData'
|
||||
| 'authNonce'
|
||||
| 'codeVerifier'
|
||||
| 'authStateControl'
|
||||
| 'reusable_refresh_token'
|
||||
| 'session_state'
|
||||
| 'storageSilentRenewRunning'
|
||||
| 'storageCodeFlowInProgress'
|
||||
| 'storageCustomParamsAuthRequest'
|
||||
| 'storageCustomParamsRefresh'
|
||||
| 'storageCustomParamsEndSession'
|
||||
| 'redirect'
|
||||
| 'configIds'
|
||||
| 'jwtKeys'
|
||||
| 'popupauth';
|
||||
|
||||
|
||||
export class StoragePersistenceService {
|
||||
private readonly browserStorageService = inject(BrowserStorageService);
|
||||
|
||||
read(key: StorageKeys, config: OpenIdConfiguration): any {
|
||||
const storedConfig = this.browserStorageService.read(key, config) || {};
|
||||
|
||||
return storedConfig[key];
|
||||
}
|
||||
|
||||
write(key: StorageKeys, value: any, config: OpenIdConfiguration): boolean {
|
||||
const storedConfig = this.browserStorageService.read(key, config) || {};
|
||||
|
||||
storedConfig[key] = value;
|
||||
|
||||
return this.browserStorageService.write(storedConfig, config);
|
||||
}
|
||||
|
||||
remove(key: StorageKeys, config: OpenIdConfiguration): void {
|
||||
const storedConfig = this.browserStorageService.read(key, config) || {};
|
||||
|
||||
delete storedConfig[key];
|
||||
|
||||
this.browserStorageService.write(storedConfig, config);
|
||||
}
|
||||
|
||||
clear(config: OpenIdConfiguration): void {
|
||||
this.browserStorageService.clear(config);
|
||||
}
|
||||
|
||||
resetStorageFlowData(config: OpenIdConfiguration): void {
|
||||
this.remove('session_state', config);
|
||||
this.remove('storageSilentRenewRunning', config);
|
||||
this.remove('storageCodeFlowInProgress', config);
|
||||
this.remove('codeVerifier', config);
|
||||
this.remove('userData', config);
|
||||
this.remove('storageCustomParamsAuthRequest', config);
|
||||
this.remove('access_token_expires_at', config);
|
||||
this.remove('storageCustomParamsRefresh', config);
|
||||
this.remove('storageCustomParamsEndSession', config);
|
||||
this.remove('reusable_refresh_token', config);
|
||||
}
|
||||
|
||||
resetAuthStateInStorage(config: OpenIdConfiguration): void {
|
||||
this.remove('authzData', config);
|
||||
this.remove('reusable_refresh_token', config);
|
||||
this.remove('authnResult', config);
|
||||
}
|
||||
|
||||
getAccessToken(config: OpenIdConfiguration): string {
|
||||
return this.read('authzData', config);
|
||||
}
|
||||
|
||||
getIdToken(config: OpenIdConfiguration): string {
|
||||
return this.read('authnResult', config)?.id_token;
|
||||
}
|
||||
|
||||
getRefreshToken(config: OpenIdConfiguration): string {
|
||||
const refreshToken = this.read('authnResult', config)?.refresh_token;
|
||||
|
||||
if (!refreshToken && config.allowUnsafeReuseRefreshToken) {
|
||||
return this.read('reusable_refresh_token', config);
|
||||
}
|
||||
|
||||
return refreshToken;
|
||||
}
|
||||
|
||||
getAuthenticationResult(config: OpenIdConfiguration): AuthResult {
|
||||
return this.read('authnResult', config);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user