import { Injectable } from '@nestjs/common' ; import { ApacheIgniteService } from '@fc/apache-ignite' ; import { ConfigService } from '@fc/config' ; import { CryptographyService } from '@fc/cryptography' ; import { EidasNameIdFormats , EidasRequest , EidasResponse } from '@fc/eidas' ; import { LightRequestService , LightResponseService , } from '@fc/eidas-light-protocol' ; import { LoggerService } from '@fc/logger' ; import { EidasProviderConfig } from './dto' ; import { ReadLightRequestFromCacheException , WriteLightResponseInCacheException , } from './exceptions' ; @ Injectable ( ) export class EidasProviderService { private proxyServiceRequestCache ; private proxyServiceResponseCache ; constructor ( private readonly config , private readonly logger , private readonly crypto , private readonly apacheIgnite , private readonly lightRequest , private readonly lightResponse ) { this . logger . setContext ( this . constructor . name ) ; } /**
     * Retrieve the ignite caches for the proxy service:
     * proxyServiceRequestCache -> get the client country request from the FR Node
     * proxyServiceResponseCache -> write the FranceConnect response to the FR Node
     */ onModuleInit ( ) { const { proxyServiceRequestCache , proxyServiceResponseCache } = this . config . get < EidasProviderConfig > ( 'EidasProvider' ) ; this . logger . debug ( `Accessing caches ${ proxyServiceRequestCache } and ${ proxyServiceResponseCache }...` ) ; this . proxyServiceRequestCache = this . apacheIgnite . getCache ( proxyServiceRequestCache ) ; this . proxyServiceResponseCache = this . apacheIgnite . getCache ( proxyServiceResponseCache ) ; } param token returns async readLightRequestFromCache ( token ) { const { id } = this . lightRequest . parseToken ( token ) ; try { /**
             * We can't just return the promise as it will prevent any exception thrown by
             * "proxyServiceRequestCache.get" to be caught
             */ const lightRequest = await this . proxyServiceRequestCache . get ( id ) ; return lightRequest ; } catch ( e ) { throw new ReadLightRequestFromCacheException ( ) ; } } param lightRequest returns parseLightRequest ( lightRequest ) { return this . lightRequest . parseRequest ( lightRequest ) ; } param partialEidasResponse return completeFcSuccessResponse ( { subject , attributes , levelOfAssurance , status } , eidasRequest ) { const response = { id : this . crypto . genRandomString ( 64 ) , issuer : 'FR EidasBridge - ProxyService' , inResponseToId : eidasRequest . id , relayState : eidasRequest . relayState , subject , subjectNameIdFormat : EidasNameIdFormats . PERSISTENT , levelOfAssurance , status , attributes , } ; return response ; } param partialEidasResponse return completeFcFailureResponse ( { status } , eidasRequest ) { const response = { id : this . crypto . genRandomString ( 64 ) , issuer : 'FR EidasBridge - ProxyService' , inResponseToId : eidasRequest . id , relayState : eidasRequest . relayState , status , } ; return response ; } param response returns prepareLightResponse ( response ) { const { proxyServiceResponseIssuer } = this . config . get < EidasProviderConfig > ( 'EidasProvider' ) ; const token = this . lightResponse . generateToken ( response . id , proxyServiceResponseIssuer ) ; const lightResponse = this . lightResponse . formatResponse ( response ) ; return { token , lightResponse , } ; } param id param lightResponse returns async writeLightResponseInCache ( id , lightResponse ) { try { await this . proxyServiceResponseCache . put ( id , lightResponse ) ; } catch ( e ) { throw new WriteLightResponseInCacheException ( ) ; } } }