UNPKG

oidc-client-rx

Version:

ReactiveX enhanced OIDC and OAuth2 protocol support for browser-based JavaScript applications

72 lines (71 loc) 4.15 kB
import { Injectable, inject } from "@outposts/injection-js"; import { of, throwError } from "rxjs"; import { catchError, switchMap } from "rxjs/operators"; import { AuthStateService } from "../../auth-state/auth-state.service.js"; import { LoggerService } from "../../logging/logger.service.js"; import { UserService } from "../../user-data/user.service.js"; import { FlowsDataService } from "../flows-data.service.js"; import { ResetAuthDataService } from "../reset-auth-data.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 UserCallbackHandlerService { callbackUser(callbackContext, configuration, allConfigs) { const { isRenewProcess, validationResult, authResult, refreshToken } = callbackContext; const { autoUserInfo, renewUserInfoAfterTokenRenew } = configuration; if (!autoUserInfo) { if (!isRenewProcess || renewUserInfoAfterTokenRenew) { if (null == validationResult ? void 0 : validationResult.decodedIdToken) this.userService.setUserDataToStore(validationResult.decodedIdToken, configuration, allConfigs); } if (!isRenewProcess && !refreshToken) this.flowsDataService.setSessionState(null == authResult ? void 0 : authResult.session_state, configuration); this.publishAuthState(validationResult, isRenewProcess); return of(callbackContext); } return this.userService.getAndPersistUserDataInStore(configuration, allConfigs, isRenewProcess, null == validationResult ? void 0 : validationResult.idToken, null == validationResult ? void 0 : validationResult.decodedIdToken).pipe(switchMap((userData)=>{ if (userData) { if (!refreshToken) this.flowsDataService.setSessionState(null == authResult ? void 0 : authResult.session_state, configuration); this.publishAuthState(validationResult, isRenewProcess); return of(callbackContext); } this.resetAuthDataService.resetAuthorizationData(configuration, allConfigs); this.publishUnauthenticatedState(validationResult, isRenewProcess); const errorMessage = `Called for userData but they were ${userData}`; this.loggerService.logWarning(configuration, errorMessage); return throwError(()=>new Error(errorMessage)); }), catchError((err)=>{ const errorMessage = `Failed to retrieve user info with error: ${err}`; this.loggerService.logWarning(configuration, errorMessage); return throwError(()=>new Error(errorMessage)); })); } publishAuthState(stateValidationResult, isRenewProcess) { if (!stateValidationResult) return; this.authStateService.updateAndPublishAuthState({ isAuthenticated: true, validationResult: stateValidationResult.state, isRenewProcess }); } publishUnauthenticatedState(stateValidationResult, isRenewProcess) { if (!stateValidationResult) return; this.authStateService.updateAndPublishAuthState({ isAuthenticated: false, validationResult: stateValidationResult.state, isRenewProcess }); } constructor(){ this.loggerService = inject(LoggerService); this.authStateService = inject(AuthStateService); this.flowsDataService = inject(FlowsDataService); this.userService = inject(UserService); this.resetAuthDataService = inject(ResetAuthDataService); } } UserCallbackHandlerService = _ts_decorate([ Injectable() ], UserCallbackHandlerService); export { UserCallbackHandlerService };