import * as _ from 'lodash' ; import { Injectable } from '@nestjs/common' ; import { ConfigService } from '@fc/config' ; import { EidasRequest } from '@fc/eidas' ; import { EidasLightProtocolConfig } from '../dto' ; import { EidasJsonToXmlException } from '../exceptions' ; import { IParsedToken } from '../interfaces' ; import { LightProtocolCommonsService } from './light-protocol-commons.service' ; import { LightProtocolXmlService } from './light-protocol-xml.service' ; @ Injectable ( ) export class LightRequestService { constructor ( private readonly config , private readonly lightCommons , private readonly lightXml ) { } param { EidasResponse } data formatRequest ( eidasRequest ) { let pathsObject = this . lightXml . jsonToPathsObject ( eidasRequest ) ; pathsObject = this . lightXml . replaceInPaths ( pathsObject , /^/ , 'lightRequest.' ) ; pathsObject = this . lightXml . addDeclarationFields ( pathsObject ) ; pathsObject = this . lightXml . replaceInPaths ( pathsObject , /requestedAttributes\.([0-9]+)/ , 'requestedAttributes.attribute.$1.definition' ) ; pathsObject = this . lightXml . upperCaseFirstCharForProps ( pathsObject , [ 'requestedAttributes' , ] ) ; pathsObject = this . lightXml . prefixProps ( pathsObject , [ 'requestedAttributes' ] , 'http://eidas.europa.eu/attributes/naturalperson/' ) ; pathsObject = this . lightXml . prefixProps ( pathsObject , [ 'nameIdFormat' ] , 'urn:oasis:names:tc:SAML:1.1:nameid-format:' ) ; pathsObject = this . lightXml . prefixProps ( pathsObject , [ 'levelOfAssurance' ] , 'http://eidas.europa.eu/LoA/' ) ; pathsObject = this . lightXml . replaceInPaths ( pathsObject , /^lightRequest\.(.*)$/ , 'lightRequest.$1._text' ) ; const inflatedJson = this . lightXml . pathsObjectToJson ( pathsObject ) ; try { return this . lightXml . jsonToXml ( inflatedJson ) ; } catch ( error ) { throw new EidasJsonToXmlException ( error ) ; } } generateToken ( id , issuer , date ? ) { const { lightRequestConnectorSecret } = this . config . get < EidasLightProtocolConfig > ( 'EidasLightProtocol' ) ; return this . lightCommons . generateToken ( id , issuer , lightRequestConnectorSecret , date ) ; } param { string } xml parseRequest ( xml ) { const json = this . lightXml . xmlToJson ( xml ) ; let pathsObject = this . lightXml . jsonToPathsObject ( json ) ; pathsObject = this . lightXml . removeDeclarationFields ( pathsObject ) ; pathsObject = this . lightXml . replaceInPaths ( pathsObject , 'value._text' , 'value.0._text' ) ; pathsObject = this . lightXml . replaceInPaths ( pathsObject , 'lightRequest.' , '' ) ; pathsObject = this . lightXml . replaceInPaths ( pathsObject , '._text' , '' ) ; pathsObject = this . lightXml . replaceInPaths ( pathsObject , '.definition' , '' ) ; pathsObject = this . lightXml . replaceInPaths ( pathsObject , '.attribute' , '' ) ; pathsObject = this . lightXml . stripUrlAndUrnForProps ( pathsObject , [ 'levelOfAssurance' , 'nameIdFormat' , 'requestedAttributes' , ] ) ; pathsObject = this . lightXml . lowerCaseFirstCharForProps ( pathsObject , [ 'requestedAttributes' , ] ) ; return this . lightXml . pathsObjectToJson ( pathsObject ) as unknown as EidasRequest ; } parseToken ( token ) { const { lightRequestProxyServiceSecret } = this . config . get < EidasLightProtocolConfig > ( 'EidasLightProtocol' ) ; return this . lightCommons . parseToken ( token , lightRequestProxyServiceSecret ) ; } }