import { Injectable } from '@nestjs/common' ; import { EidasAttributes , EidasRequest , EidasResponse , EidasResponseAttributes , } from '@fc/eidas' ; import { LoggerService } from '@fc/logger' ; import { IOidcIdentity , OidcError } from '@fc/oidc' ; import { AttributesToClaimsMap , LevelOfAssurancesToAcrValueMap , RequestedAttributesToScopesMap , } from '../mappers' ; @ Injectable ( ) export class EidasToOidcService { constructor ( private readonly logger ) { logger . setContext ( this . constructor . name ) ; } param requestedAttributes param levelOfAssurance return mapPartialRequest ( { levelOfAssurance , requestedAttributes , } ) { const scopeSet = this . mapRequestedAttributesToScopes ( requestedAttributes ) ; const scope = Array . from ( scopeSet ) ; this . logger . trace ( { requestedAttributes , scope } ) ; return { acr_values : LevelOfAssurancesToAcrValueMap [ levelOfAssurance ] , scope , } ; } mapPartialResponseSuccess ( { attributes , levelOfAssurance } ) { return { acr : LevelOfAssurancesToAcrValueMap [ levelOfAssurance ] , userinfos : this . mapAttributesToClaims ( attributes ) , } ; } mapPartialResponseFailure ( { status } ) { const errorDescription = `StatusCode: ${ status . statusCode }\nSubStatusCode: ${ status . subStatusCode }\nStatusMessage: ${ status . statusMessage }` ; return { error : 'eidas_node_error' , error_description : errorDescription , } ; } param requestedAttributes return private mapRequestedAttributesToScopes ( requestedAttributes ) { const scopesSet = new Set < string > ( ) ; see https scopesSet . add ( 'openid' ) ; return requestedAttributes . reduce ( this . requestedAttributesToScopesReducer , scopesSet ) ; } param scopeSet param attribute return private requestedAttributesToScopesReducer ( scopeSet , attribute ) { RequestedAttributesToScopesMap [ attribute ] ?. forEach ( ( elem ) => { scopeSet . add ( elem ) ; } ) ; return scopeSet ; } private mapAttributesToClaims ( attributes ) { const attributesKeys = Object . keys ( attributes ) ; return attributesKeys . reduce ( this . getClaimsBoundedAttributesToClaimsReducer ( attributes ) , { } as IOidcIdentity ) ; } param attributes param claims param currentAttribute return private getClaimsBoundedAttributesToClaimsReducer ( attributes ) { return this . attributesToClaimsReducer . bind ( EidasToOidcService , attributes ) ; } private attributesToClaimsReducer ( attributes , claims , currentAttribute ) { if ( AttributesToClaimsMap [ currentAttribute ] ) { Object . assign ( claims , AttributesToClaimsMap [ currentAttribute ] ( attributes ) ) ; } return claims ; } }