UNPKG

oidc-client-rx

Version:

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

81 lines (80 loc) 4.92 kB
import { Injectable, inject } from "@outposts/injection-js"; import { of, throwError } from "rxjs"; import { map, switchMap, take } from "rxjs/operators"; import { CheckAuthService } from "../../auth-state/check-auth.service.js"; import { AuthWellKnownService } from "../../config/auth-well-known/auth-well-known.service.js"; import { LoggerService } from "../../logging/logger.service.js"; import { RedirectService } from "../../utils/redirect/redirect.service.js"; import { UrlService } from "../../utils/url/url.service.js"; import { PopUpService } from "../popup/popup.service.js"; import { ResponseTypeValidationService } from "../response-type-validation/response-type-validation.service.js"; import { ParService } from "./par.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 ParLoginService { loginPar(configuration, authOptions) { if (!this.responseTypeValidationService.hasConfigValidResponseType(configuration)) { this.loggerService.logError(configuration, 'Invalid response type!'); return of(void 0); } this.loggerService.logDebug(configuration, 'BEGIN Authorize OIDC Flow, no auth data'); return this.authWellKnownService.queryAndStoreAuthWellKnownEndPoints(configuration).pipe(switchMap(()=>this.parService.postParRequest(configuration, authOptions)), map((response)=>{ this.loggerService.logDebug(configuration, 'par response: ', response); const url = this.urlService.getAuthorizeParUrl(response.requestUri, configuration); this.loggerService.logDebug(configuration, 'par request url: ', url); if (!url) return void this.loggerService.logError(configuration, `Could not create URL with param ${response.requestUri}: '${url}'`); if (null == authOptions ? void 0 : authOptions.urlHandler) authOptions.urlHandler(url); else this.redirectService.redirectTo(url); })); } loginWithPopUpPar(configuration, allConfigs, authOptions, popupOptions) { const { configId } = configuration; if (!this.responseTypeValidationService.hasConfigValidResponseType(configuration)) { const errorMessage = 'Invalid response type!'; this.loggerService.logError(configuration, errorMessage); return throwError(()=>new Error(errorMessage)); } this.loggerService.logDebug(configuration, 'BEGIN Authorize OIDC Flow with popup, no auth data'); return this.authWellKnownService.queryAndStoreAuthWellKnownEndPoints(configuration).pipe(switchMap(()=>this.parService.postParRequest(configuration, authOptions)), switchMap((response)=>{ this.loggerService.logDebug(configuration, `par response: ${response}`); const url = this.urlService.getAuthorizeParUrl(response.requestUri, configuration); this.loggerService.logDebug(configuration, 'par request url: ', url); if (!url) { const errorMessage = `Could not create URL with param ${response.requestUri}: 'url'`; this.loggerService.logError(configuration, errorMessage); return throwError(()=>new Error(errorMessage)); } this.popupService.openPopUp(url, popupOptions, configuration); return this.popupService.result$.pipe(take(1), switchMap((result)=>{ const { userClosed, receivedUrl } = result; if (userClosed) return of({ isAuthenticated: false, errorMessage: 'User closed popup', userData: null, idToken: '', accessToken: '', configId }); return this.checkAuthService.checkAuth(configuration, allConfigs, receivedUrl); })); })); } constructor(){ this.loggerService = inject(LoggerService); this.responseTypeValidationService = inject(ResponseTypeValidationService); this.urlService = inject(UrlService); this.redirectService = inject(RedirectService); this.authWellKnownService = inject(AuthWellKnownService); this.popupService = inject(PopUpService); this.checkAuthService = inject(CheckAuthService); this.parService = inject(ParService); } } ParLoginService = _ts_decorate([ Injectable() ], ParLoginService); export { ParLoginService };