oidc-client-rx
Version:
ReactiveX enhanced OIDC and OAuth2 protocol support for browser-based JavaScript applications
106 lines (105 loc) • 5.22 kB
JavaScript
import { Injectable, inject } from "injection-js";
import { LoggerService } from "../logging/logger.service.js";
import { StoragePersistenceService } from "../storage/storage-persistence.service.js";
import { RandomService } from "./random/random.service.js";
function _ts_decorate(decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : null === desc ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if ("object" == typeof Reflect && "function" == typeof Reflect.decorate) r = Reflect.decorate(decorators, target, key, desc);
else for(var i = decorators.length - 1; i >= 0; i--)if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
}
class FlowsDataService {
createNonce(configuration) {
const nonce = this.randomService.createRandom(40, configuration);
this.loggerService.logDebug(configuration, `Nonce created. nonce:${nonce}`);
this.setNonce(nonce, configuration);
return nonce;
}
setNonce(nonce, configuration) {
this.storagePersistenceService.write("authNonce", nonce, configuration);
}
getAuthStateControl(configuration) {
if (!configuration) return "";
return this.storagePersistenceService.read("authStateControl", configuration);
}
setAuthStateControl(authStateControl, configuration) {
if (!configuration) return false;
return this.storagePersistenceService.write("authStateControl", authStateControl, configuration);
}
getExistingOrCreateAuthStateControl(configuration) {
let state = this.storagePersistenceService.read("authStateControl", configuration);
if (!state) {
state = this.randomService.createRandom(40, configuration);
this.storagePersistenceService.write("authStateControl", state, configuration);
}
return state;
}
setSessionState(sessionState, configuration) {
this.storagePersistenceService.write("session_state", sessionState, configuration);
}
resetStorageFlowData(configuration) {
this.storagePersistenceService.resetStorageFlowData(configuration);
}
getCodeVerifier(configuration) {
return this.storagePersistenceService.read("codeVerifier", configuration);
}
createCodeVerifier(configuration) {
const codeVerifier = this.randomService.createRandom(67, configuration);
this.storagePersistenceService.write("codeVerifier", codeVerifier, configuration);
return codeVerifier;
}
isCodeFlowInProgress(configuration) {
return !!this.storagePersistenceService.read("storageCodeFlowInProgress", configuration);
}
setCodeFlowInProgress(configuration) {
this.storagePersistenceService.write("storageCodeFlowInProgress", true, configuration);
}
resetCodeFlowInProgress(configuration) {
this.storagePersistenceService.write("storageCodeFlowInProgress", false, configuration);
}
isSilentRenewRunning(configuration) {
const { configId, silentRenewTimeoutInSeconds } = configuration;
const storageObject = this.getSilentRenewRunningStorageEntry(configuration);
if (!storageObject) return false;
if ("not-running" === storageObject.state) return false;
const timeOutInMilliseconds = (silentRenewTimeoutInSeconds ?? 0) * 1000;
const dateOfLaunchedProcessUtc = Date.parse(storageObject.dateOfLaunchedProcessUtc);
const currentDateUtc = Date.parse(new Date().toISOString());
const elapsedTimeInMilliseconds = Math.abs(currentDateUtc - dateOfLaunchedProcessUtc);
const isProbablyStuck = elapsedTimeInMilliseconds > timeOutInMilliseconds;
if (isProbablyStuck) {
this.loggerService.logDebug(configuration, "silent renew process is probably stuck, state will be reset.", configId);
this.resetSilentRenewRunning(configuration);
return false;
}
return "running" === storageObject.state;
}
setSilentRenewRunning(configuration) {
const storageObject = {
state: "running",
dateOfLaunchedProcessUtc: new Date().toISOString()
};
this.storagePersistenceService.write("storageSilentRenewRunning", JSON.stringify(storageObject), configuration);
}
resetSilentRenewRunning(configuration) {
if (!configuration) return;
this.storagePersistenceService.write("storageSilentRenewRunning", "", configuration);
}
getSilentRenewRunningStorageEntry(configuration) {
const storageEntry = this.storagePersistenceService.read("storageSilentRenewRunning", configuration);
if (!storageEntry) return {
dateOfLaunchedProcessUtc: "",
state: "not-running"
};
return JSON.parse(storageEntry);
}
constructor(){
this.loggerService = inject(LoggerService);
this.storagePersistenceService = inject(StoragePersistenceService);
this.randomService = inject(RandomService);
}
}
FlowsDataService = _ts_decorate([
Injectable()
], FlowsDataService);
export { FlowsDataService };