import { KoaContextWithOIDC , Provider } from 'oidc-provider' ; import { ArgumentsHost , Injectable } from '@nestjs/common' ; import { FcException , FcExceptionFilter } from '@fc/exceptions' ; import { ErrorCode , OidcProviderEvents } from '../enums' ; import { OidcProviderRuntimeException } from '../exceptions' ; import { OidcCtx } from '../interfaces' ; @ Injectable ( ) export class OidcProviderErrorService { private errorEvents = [ OidcProviderEvents . AUTHORIZATION_ERROR , OidcProviderEvents . BACKCHANNEL_ERROR , OidcProviderEvents . JWKS_ERROR , OidcProviderEvents . CHECK_SESSION_ORIGIN_ERROR , OidcProviderEvents . CHECK_SESSION_ERROR , OidcProviderEvents . DISCOVERY_ERROR , OidcProviderEvents . END_SESSION_ERROR , OidcProviderEvents . GRANT_ERROR , OidcProviderEvents . INTROSPECTION_ERROR , OidcProviderEvents . PUSHED_AUTHORIZATION_REQUEST_ERROR , OidcProviderEvents . REGISTRATION_CREATE_ERROR , OidcProviderEvents . REGISTRATION_DELETE_ERROR , OidcProviderEvents . REGISTRATION_READ_ERROR , OidcProviderEvents . REGISTRATION_UPDATE_ERROR , OidcProviderEvents . REVOCATION_ERROR , OidcProviderEvents . SERVER_ERROR , OidcProviderEvents . USERINFO_ERROR , ] ; constructor ( private readonly exceptionFilter ) { } param { Provider } provider catchErrorEvents ( provider ) { this . errorEvents . forEach ( ( eventName ) => { provider . on ( eventName , this . triggerError . bind ( this , eventName ) ) ; } ) ; } param { KoaContextWithOIDC } ctx param { string } out param { any } error see https renderError ( ctx , _out , error ) { const exception = new OidcProviderRuntimeException ( error ) ; this . throwError ( ctx , exception ) ; } triggerError ( eventName , ctx , error ) { let wrappedError ; if ( error instanceof FcException ) { wrappedError = error ; } else { wrappedError = new OidcProviderRuntimeException ( error , ErrorCode [ eventName . toUpperCase ( ) ] ) ; } /**
         * Flag the request as invalid
         * to inform async tratment (event listeners)
         */ ctx . oidc [ 'isError' ] = true ; if ( wrappedError . redirect === true ) { this . throwError ( ctx , wrappedError ) ; } } param ctx param exception throwError ( ctx , exception ) { const host = FcExceptionFilter . ArgumentHostAdapter ( ctx ) as ArgumentsHost ; this . exceptionFilter . catch ( exception , host ) ; } }