oidc-client-rx
Version:
ReactiveX enhanced OIDC and OAuth2 protocol support for browser-based JavaScript applications
57 lines (56 loc) • 2.23 kB
JavaScript
import { inject } from "injection-js";
import { BrowserStorageService } from "./browser-storage.service.js";
class StoragePersistenceService {
read(key, config) {
const storedConfig = this.browserStorageService.read(key, config) || {};
return storedConfig[key];
}
write(key, value, config) {
const storedConfig = this.browserStorageService.read(key, config) || {};
storedConfig[key] = value;
return this.browserStorageService.write(storedConfig, config);
}
remove(key, config) {
const storedConfig = this.browserStorageService.read(key, config) || {};
delete storedConfig[key];
this.browserStorageService.write(storedConfig, config);
}
clear(config) {
this.browserStorageService.clear(config);
}
resetStorageFlowData(config) {
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) {
this.remove("authzData", config);
this.remove("reusable_refresh_token", config);
this.remove("authnResult", config);
}
getAccessToken(config) {
return this.read("authzData", config);
}
getIdToken(config) {
return this.read("authnResult", config)?.id_token;
}
getRefreshToken(config) {
const refreshToken = this.read("authnResult", config)?.refresh_token;
if (!refreshToken && config.allowUnsafeReuseRefreshToken) return this.read("reusable_refresh_token", config);
return refreshToken;
}
getAuthenticationResult(config) {
return this.read("authnResult", config);
}
constructor(){
this.browserStorageService = inject(BrowserStorageService);
}
}
export { StoragePersistenceService };