oidc-client-rx
Version:
ReactiveX enhanced OIDC and OAuth2 protocol support for browser-based JavaScript applications
110 lines (109 loc) • 5.5 kB
JavaScript
import { Injectable, inject } from "injection-js";
import { Subject, of, throwError } from "rxjs";
import { catchError, map } from "rxjs/operators";
import { AuthStateService } from "../auth-state/auth-state.service.js";
import { ImplicitFlowCallbackService } from "../callback/implicit-flow-callback.service.js";
import { IntervalService } from "../callback/interval.service.js";
import { FlowsService } from "../flows/flows.service.js";
import { FlowsDataService } from "../flows/flows-data.service.js";
import { ResetAuthDataService } from "../flows/reset-auth-data.service.js";
import { HttpParams } from "../http/index.js";
import { LoggerService } from "../logging/logger.service.js";
import { FlowHelper } from "../utils/flowHelper/flow-helper.service.js";
import { ValidationResult } from "../validation/validation-result.js";
import { IFrameService } from "./existing-iframe.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;
}
const IFRAME_FOR_SILENT_RENEW_IDENTIFIER = "myiFrameForSilentRenew";
class SilentRenewService {
get refreshSessionWithIFrameCompleted$() {
return this.refreshSessionWithIFrameCompletedInternal$.asObservable();
}
getOrCreateIframe(config) {
const existingIframe = this.getExistingIframe();
if (!existingIframe) return this.iFrameService.addIFrameToWindowBody(IFRAME_FOR_SILENT_RENEW_IDENTIFIER, config);
return existingIframe;
}
isSilentRenewConfigured(configuration) {
const { useRefreshToken, silentRenew } = configuration;
return !useRefreshToken && Boolean(silentRenew);
}
codeFlowCallbackSilentRenewIframe(urlParts, config, allConfigs) {
const params = new HttpParams({
fromString: urlParts[1]
});
const errorParam = params.get("error");
if (errorParam) {
this.authStateService.updateAndPublishAuthState({
isAuthenticated: false,
validationResult: ValidationResult.LoginRequired,
isRenewProcess: true
});
this.resetAuthDataService.resetAuthorizationData(config, allConfigs);
this.flowsDataService.setNonce("", config);
this.intervalService.stopPeriodicTokenCheck();
return throwError(()=>new Error(errorParam));
}
const code = params.get("code") ?? "";
const state = params.get("state") ?? "";
const sessionState = params.get("session_state");
const callbackContext = {
code,
refreshToken: "",
state,
sessionState,
authResult: null,
isRenewProcess: true,
jwtKeys: null,
validationResult: null,
existingIdToken: null
};
return this.flowsService.processSilentRenewCodeFlowCallback(callbackContext, config, allConfigs).pipe(catchError((error)=>{
this.intervalService.stopPeriodicTokenCheck();
this.resetAuthDataService.resetAuthorizationData(config, allConfigs);
return throwError(()=>new Error(error));
}));
}
silentRenewEventHandler(e, config, allConfigs) {
this.loggerService.logDebug(config, "silentRenewEventHandler");
if (!e.detail) return of(void 0);
let callback$;
const isCodeFlow = this.flowHelper.isCurrentFlowCodeFlow(config);
if (isCodeFlow) {
const urlParts = e.detail.toString().split("?");
callback$ = this.codeFlowCallbackSilentRenewIframe(urlParts, config, allConfigs);
} else callback$ = this.implicitFlowCallbackService.authenticatedImplicitFlowCallback(config, allConfigs, e.detail);
return callback$.pipe(map((callbackContext)=>{
this.refreshSessionWithIFrameCompletedInternal$.next(callbackContext);
this.flowsDataService.resetSilentRenewRunning(config);
}), catchError((err)=>{
this.loggerService.logError(config, `Error: ${err}`);
this.refreshSessionWithIFrameCompletedInternal$.next(null);
this.flowsDataService.resetSilentRenewRunning(config);
return of(void 0);
}));
}
getExistingIframe() {
return this.iFrameService.getExistingIFrame(IFRAME_FOR_SILENT_RENEW_IDENTIFIER);
}
constructor(){
this.refreshSessionWithIFrameCompletedInternal$ = new Subject();
this.loggerService = inject(LoggerService);
this.iFrameService = inject(IFrameService);
this.flowsService = inject(FlowsService);
this.resetAuthDataService = inject(ResetAuthDataService);
this.flowsDataService = inject(FlowsDataService);
this.authStateService = inject(AuthStateService);
this.flowHelper = inject(FlowHelper);
this.implicitFlowCallbackService = inject(ImplicitFlowCallbackService);
this.intervalService = inject(IntervalService);
}
}
SilentRenewService = _ts_decorate([
Injectable()
], SilentRenewService);
export { SilentRenewService };