import { Injectable } from '@nestjs/common' ; import { ApacheIgniteService } from '@fc/apache-ignite' ; import { ConfigService } from '@fc/config' ; import { CryptographyService } from '@fc/cryptography' ; import { EidasNameIdFormats , EidasPartialRequest , EidasRequest , EidasResponse , EidasSpTypes , } from '@fc/eidas' ; import { EidasCountries } from '@fc/eidas-country' ; import { LightRequestService , LightResponseService , } from '@fc/eidas-light-protocol' ; import { LoggerService } from '@fc/logger' ; import { EidasClientConfig } from './dto' ; import { ReadLightResponseFromCacheException , WriteLightRequestInCacheException , } from './exceptions' ; @ Injectable ( ) export class EidasClientService { private connectorRequestCache ; private connectorResponseCache ; constructor ( private readonly config , private readonly logger , private readonly apacheIgnite , private readonly crypto , private readonly lightRequest , private readonly lightResponse ) { this . logger . setContext ( this . constructor . name ) ; } /**
     * Retrieve the ignite caches for the connector:
     * connectorRequestCache -> write the FranceConnect request to the FR Node
     * connectorResponseCache -> read the provider country response from the FR Node
     */ onModuleInit ( ) { const { connectorRequestCache , connectorResponseCache } = this . config . get < EidasClientConfig > ( 'EidasClient' ) ; this . logger . debug ( `Accessing caches ${ connectorRequestCache } and ${ connectorResponseCache }...` ) ; /**
         * Cache can be prepared even if not connected to apache-ignite
         */ this . connectorRequestCache = this . apacheIgnite . getCache ( connectorRequestCache ) ; this . connectorResponseCache = this . apacheIgnite . getCache ( connectorResponseCache ) ; } completeEidasRequest ( eidasPartialRequest , citizenCountryCode ) { const { levelOfAssurance , requestedAttributes } = eidasPartialRequest ; const eidasRequest = { id : this . crypto . genRandomString ( 64 ) , citizenCountryCode , issuer : 'EIDASBridge Connector' , levelOfAssurance , nameIdFormat : EidasNameIdFormats . UNSPECIFIED , providerName : 'FranceConnect' , spType : EidasSpTypes . PUBLIC , relayState : this . crypto . genRandomString ( 32 ) , requestedAttributes , } ; return eidasRequest ; } param request returns prepareLightRequest ( requested ) { const { connectorRequestIssuer } = this . config . get < EidasClientConfig > ( 'EidasClient' ) ; const token = this . lightRequest . generateToken ( requested . id , connectorRequestIssuer ) ; const lightRequest = this . lightRequest . formatRequest ( requested ) ; return { token , lightRequest , } ; } param id param lightRequest returns async writeLightRequestInCache ( id , lightRequest ) { try { await this . connectorRequestCache . put ( id , lightRequest ) ; } catch ( e ) { throw new WriteLightRequestInCacheException ( ) ; } } param token returns async readLightResponseFromCache ( token ) { const { id } = this . lightResponse . parseToken ( token ) ; try { return await this . connectorResponseCache . get ( id ) ; } catch ( e ) { throw new ReadLightResponseFromCacheException ( ) ; } } param lightResponse returns parseLightResponse ( lightResponse ) { return this . lightResponse . parseResponse ( lightResponse ) ; } }