import { ClientMetadata , KoaContextWithOIDC } from 'oidc-provider' ; import { Inject , Injectable } from '@nestjs/common' ; import { ConfigService } from '@fc/config' ; import { LoggerService } from '@fc/logger' ; import { IServiceProviderAdapter , OidcSession } from '@fc/oidc' ; import { SERVICE_PROVIDER_SERVICE_TOKEN } from '@fc/oidc/tokens' ; import { OidcProviderConfig } from '@fc/oidc-provider' ; import { ISessionBoundContext , SessionService } from '@fc/session' ; import { OidcProviderRedisAdapter } from '../adapters' ; import { OidcProviderService } from '../oidc-provider.service' ; import { OidcProviderErrorService } from './oidc-provider-error.service' ; @ Injectable ( ) export class OidcProviderConfigService { constructor ( private readonly logger , private readonly config , private readonly sessionService , private readonly errorService , todo @ Inject ( SERVICE_PROVIDER_SERVICE_TOKEN ) private readonly serviceProvider ) { this . logger . setContext ( this . constructor . name ) ; } param { boolean } refresh returns { Promise < OidcProviderConfig > } async getConfig ( oidcProviderService ) { see https see https see https const adapter = OidcProviderRedisAdapter . getConstructorWithDI ( oidcProviderService , this . serviceProvider ) ; /**
         * Get data from config file
         */ const { prefix , issuer , configuration , forcedPrompt , knownAcrValues , defaultAcrValue , } = this . config . get < OidcProviderConfig > ( 'OidcProvider' ) ; /**
         * Bind callbacks to this class before passing them to oidc-provider
         * so we can use the NestJS dependencies injection
         */ const logoutSource = this . logoutSource . bind ( this ) ; const postLogoutSuccessSource = this . postLogoutSuccessSource . bind ( this ) ; const findAccount = this . findAccount . bind ( this ) ; const pairwiseIdentifier = this . pairwiseIdentifier . bind ( this ) ; const renderError = this . errorService . renderError . bind ( this . errorService ) ; const clientBasedCORS = this . clientBasedCORS . bind ( this ) ; const url = this . url . bind ( this , prefix ) ; /**
         * Build final configuration object
         */ const oidcProviderConfig = { forcedPrompt , prefix , issuer , knownAcrValues , defaultAcrValue , configuration : { ... configuration , features : { ... configuration . features , rpInitiatedLogout : { ... configuration . features . rpInitiatedLogout , logoutSource , postLogoutSuccessSource , } , } , adapter , findAccount , pairwiseIdentifier , renderError , clientBasedCORS , interactions : { url } , pkce : { methods : [ 'S256' ] , required : ( ) => false , } , } , } ; this . logger . trace ( { oidcProviderConfig } ) ; return oidcProviderConfig ; } see https private async findAccount ( ctx , interactionId ) { this . logger . debug ( 'OidcProviderConfigService.findAccount' ) ; try { const sessionId = await this . sessionService . getAlias ( interactionId ) ; const boundSessionContext = { sessionId , moduleName : 'OidcClient' , } ; const { spIdentity } = await this . sessionService . get ( boundSessionContext ) ; const account = { accountId : interactionId , async claims ( ) { return spIdentity ; } , } ; this . logger . trace ( { account } ) ; return account ; } catch ( error ) { this . errorService . throwError ( ctx , error ) ; } } see https private pairwiseIdentifier ( _ctx , accountId ) { return accountId ; } see https TODO see https private async logoutSource ( ctx , form ) { ctx . body = `<!DOCTYPE html>
      <head>
        <title>Déconnexion</title>
      </head>
      <body>
        ${ form }
        <script>
          var form = document.forms[0];
          var input = document.createElement('input');
          input.type = 'hidden';
          input.name = 'logout';
          input.value = 'yes';
          form.appendChild(input);
          form.submit();
        </script>
      </body>
      </html>` ; } see https TODO see https private async postLogoutSuccessSource ( ctx ) { ctx . body = `<!DOCTYPE html>
      <head>
        <title>Déconnexion</title>
      </head>
      <body>
        <p>Vous êtes bien déconnecté, vous pouvez fermer votre navigateur.</p>
      </body>
      </html>` ; } private clientBasedCORS ( _ctx , _origin , _client ) { return false ; } see https private async url ( prefix , _ctx , interaction ) { return `${ prefix }/interaction/${ interaction . uid }` ; } }