oidc-client-rx
Version:
ReactiveX enhanced OIDC and OAuth2 protocol support for browser-based JavaScript applications
132 lines (131 loc) • 6.18 kB
JavaScript
import { Injectable, inject } from "injection-js";
import { forkJoin, of } from "rxjs";
import { concatMap, map } from "rxjs/operators";
import { injectAbstractType } from "../injection/inject.js";
import { LoggerService } from "../logging/logger.service.js";
import { EventTypes } from "../public-events/event-types.js";
import { PublicEventsService } from "../public-events/public-events.service.js";
import { StoragePersistenceService } from "../storage/storage-persistence.service.js";
import { PlatformProvider } from "../utils/platform-provider/platform.provider.js";
import { AuthWellKnownService } from "./auth-well-known/auth-well-known.service.js";
import { DEFAULT_CONFIG } from "./default-config.js";
import { StsConfigLoader } from "./loader/config-loader.js";
import { ConfigValidationService } from "./validation/config-validation.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 ConfigurationService {
hasManyConfigs() {
return Object.keys(this.configsInternal).length > 1;
}
getAllConfigurations() {
return Object.values(this.configsInternal);
}
getOpenIDConfiguration(configId) {
if (this.configsAlreadySaved()) return of(this.getConfig(configId));
return this.getOpenIDConfigurations(configId).pipe(map((result)=>result.currentConfig));
}
getOpenIDConfigurations(configId) {
return this.loadConfigs().pipe(concatMap((allConfigs)=>this.prepareAndSaveConfigs(allConfigs)), map((allPreparedConfigs)=>({
allConfigs: allPreparedConfigs,
currentConfig: this.getConfig(configId)
})));
}
hasAtLeastOneConfig() {
return Object.keys(this.configsInternal).length > 0;
}
saveConfig(readyConfig) {
const { configId } = readyConfig;
this.configsInternal[configId] = readyConfig;
}
loadConfigs() {
return this.loader.loadConfigs();
}
configsAlreadySaved() {
return this.hasAtLeastOneConfig();
}
getConfig(configId) {
if (configId) {
const config = this.configsInternal[configId];
if (!config) console.warn(`[oidc-client-rx] No configuration found for config id '${configId}'.`);
return config || null;
}
const [, value] = Object.entries(this.configsInternal)[0] || [
[
null,
null
]
];
return value || null;
}
prepareAndSaveConfigs(passedConfigs) {
if (!this.configValidationService.validateConfigs(passedConfigs)) return of([]);
this.createUniqueIds(passedConfigs);
const allHandleConfigs$ = passedConfigs.map((x)=>this.handleConfig(x));
const as = forkJoin(allHandleConfigs$).pipe(map((config)=>config.filter((conf)=>Boolean(conf))), map((c)=>c));
return as;
}
createUniqueIds(passedConfigs) {
passedConfigs.forEach((config, index)=>{
if (!config.configId) config.configId = `${index}-${config.clientId}`;
});
}
handleConfig(passedConfig) {
if (!this.configValidationService.validateConfig(passedConfig)) {
this.loggerService.logError(passedConfig, "Validation of config rejected with errors. Config is NOT set.");
return of(null);
}
if (!passedConfig.authWellknownEndpointUrl) passedConfig.authWellknownEndpointUrl = passedConfig.authority;
const usedConfig = this.prepareConfig(passedConfig);
this.saveConfig(usedConfig);
const configWithAuthWellKnown = this.enhanceConfigWithWellKnownEndpoint(usedConfig);
this.publicEventsService.fireEvent(EventTypes.ConfigLoaded, configWithAuthWellKnown);
return of(usedConfig);
}
enhanceConfigWithWellKnownEndpoint(configuration) {
const alreadyExistingAuthWellKnownEndpoints = this.storagePersistenceService.read("authWellKnownEndPoints", configuration);
if (alreadyExistingAuthWellKnownEndpoints) {
configuration.authWellknownEndpoints = alreadyExistingAuthWellKnownEndpoints;
return configuration;
}
const passedAuthWellKnownEndpoints = configuration.authWellknownEndpoints;
if (passedAuthWellKnownEndpoints) {
this.authWellKnownService.storeWellKnownEndpoints(configuration, passedAuthWellKnownEndpoints);
configuration.authWellknownEndpoints = passedAuthWellKnownEndpoints;
}
return configuration;
}
prepareConfig(configuration) {
const openIdConfigurationInternal = {
...DEFAULT_CONFIG,
...configuration
};
this.setSpecialCases(openIdConfigurationInternal);
return openIdConfigurationInternal;
}
setSpecialCases(currentConfig) {
if (!this.platformProvider.isBrowser()) {
currentConfig.startCheckSession = false;
currentConfig.silentRenew = false;
currentConfig.useRefreshToken = false;
currentConfig.usePushedAuthorisationRequests = false;
}
}
constructor(){
this.loggerService = inject(LoggerService);
this.publicEventsService = inject(PublicEventsService);
this.storagePersistenceService = inject(StoragePersistenceService);
this.platformProvider = inject(PlatformProvider);
this.authWellKnownService = inject(AuthWellKnownService);
this.loader = injectAbstractType(StsConfigLoader);
this.configValidationService = inject(ConfigValidationService);
this.configsInternal = {};
}
}
ConfigurationService = _ts_decorate([
Injectable()
], ConfigurationService);
export { ConfigurationService };