import VerifiableCredentialConstants from './VerifiableCredentialConstants' ; import ValidationError from '../error_handling/ValidationError' ; import { IValidationResponse , PresentationDefinitionModel } from '../index' ; import ErrorHelpers from '../error_handling/ErrorHelpers' ; import JsonWebSignatureToken , { TokenPayload } from './JsonWebSignatureToken' ; const jp = require ( 'jsonpath' ) ; const errorCode = ( error ) => ErrorHelpers . errorCode ( 'VCSDKCLTO' , error ) ; /**
 * Enum for define the token type
 */ export enum TokenType { /**
     * Token is self issued
     */ selfIssued = 'selfIssued' , /**
     * Token is id token
     */ idToken = 'idToken' , /**
     * Token is id token hint
     */ idTokenHint = 'idTokenHint' , /**
     * Token is SIOP token issuance request
     */ siopIssuance = 'siopIssuance' , /**
     * Token is SIOP token
     */ siop = 'siop' , /**
     * Token is SIOP token presentation request with attestation presentation protocol
     */ siopPresentationAttestation = 'siopPresentationAttestation' , /**
     * Token is SIOP token presentation request with presentation exchange protocol
     */ siopPresentationExchange = 'siopPresentationExchange' , /**
     * Token is verifiable presentation
     */ verifiablePresentationJwt = 'verifiablePresentationJwt' , /**
     * Token is verifiable credential
     */ verifiableCredential = 'verifiableCredential' , /**
     * Token is verifiable credential
     */ verifiablePresentationStatus = 'verifiablePresentationStatus' } /**
 * Model for the claim token in compact format
 */ export default class ClaimToken { private _id : string = '' ; private _rawToken : string | object = '' ; private _type : TokenType ; private _decodedToken : { [ key ] : any ; } = { } ; private _tokenHeader : { [ key ] : any ; } = { } ; private _validationResponse ? : IValidationResponse ; private readonly _jsonWebToken ? : JsonWebSignatureToken ; /**
     * Token type
     */ public get type ( ) : TokenType { return this . _type ; } /**
     * Token configuration endpoint
     */ public get id ( ) : string { return this . _id ; } /**
     * Gets the raw token
     */ public get rawToken ( ) : string | object { return this . _rawToken ; } /**
     * Sets the raw token
     */ public set rawToken ( value ) { this . _rawToken = value ; } /**
     * Gets the token header
     */ public get tokenHeader ( ) : { [ key ] : any ; } { return this . _tokenHeader ; } /**
     * Gets the decoded token
     */ public get decodedToken ( ) : { [ key ] : any ; } { return this . _decodedToken ; } /**
     * Gets a IValidationResponse for the token
     */ public get validationResponse ( ) : IValidationResponse | undefined { return this . _validationResponse ; } /**
     * Sets a IValidationResponse for the token
     */ public set validationResponse ( value ) { this . _validationResponse = value ; } param typeName param token param id constructor ( typeName , token , id ? ) { const tokentypeValues = Object . values ( TokenType ) ; if ( tokentypeValues . includes ( typeName ) ) { this . _type = typeName as TokenType ; } else { throw new ValidationError ( `Type '${ typeName }' is not supported` , errorCode ( 1 ) ) ; } if ( typeof token === 'string' ) { token = new JsonWebSignatureToken ( token ) ; } if ( token instanceof JsonWebSignatureToken ) { this . _rawToken = token . token ; this . _tokenHeader = token . header ; this . _decodedToken = token . payload ; this . _jsonWebToken = token ; } else { this . _rawToken = this . _decodedToken = token ; } this . _id = id || '' ; } param token public static create ( token , id ? ) { if ( typeof token === 'string' ) { const jws = new JsonWebSignatureToken ( token ) ; const { payload } = jws ; if ( payload . iss === VerifiableCredentialConstants . TOKEN_SI_ISS ) { if ( id === VerifiableCredentialConstants . TOKEN_SI_ISS ) { return new ClaimToken ( TokenType . idTokenHint , jws , id ) ; } if ( payload . contract ) { return new ClaimToken ( TokenType . siopIssuance , jws , id ) ; } if ( payload . presentation_submission ) { return new ClaimToken ( TokenType . siopPresentationExchange , jws , id ) ; } if ( payload . attestations ) { return new ClaimToken ( TokenType . siopPresentationAttestation , jws , id ) ; } return new ClaimToken ( TokenType . siop , jws , id ) ; } if ( payload . vc ) { return new ClaimToken ( TokenType . verifiableCredential , jws , id ) ; } if ( payload . vp ) { return new ClaimToken ( TokenType . verifiablePresentationJwt , jws , id ) ; } return new ClaimToken ( TokenType . idToken , jws , id ) ; } if ( token [ '\@context' ] && token . type && token . type . length >= 1 && token . type . includes ( 'VerifiableCredential' ) ) { return new ClaimToken ( TokenType . verifiableCredential , token , id ) ; } return new ClaimToken ( TokenType . selfIssued , token , id ) ; } param attestations public static getClaimTokensFromAttestations ( attestations ) { const decodedTokens = { } ; for ( let key in attestations ) { const token = attestations [ key ] ; if ( key === VerifiableCredentialConstants . CLAIMS_SELFISSUED ) { decodedTokens [ VerifiableCredentialConstants . CLAIMS_SELFISSUED ] = new ClaimToken ( TokenType . selfIssued , token ) ; } else { for ( let tokenKey in token ) { const claimToken = ClaimToken . create ( token [ tokenKey ] , tokenKey ) ; decodedTokens [ tokenKey ] = claimToken ; } } } ; return decodedTokens ; } param payload public static getClaimTokensFromPresentationExchange ( payload ) { const decodedTokens = { } ; const descriptorMap = jp . query ( payload , `$.presentation_submission.descriptor_map.*` ) ; for ( let inx = 0 ; inx < descriptorMap . length ; inx ++ ) { const item = descriptorMap [ inx ] ; if ( item ) { if ( ! item . id ) { throw new ValidationError ( `The SIOP presentation exchange response has descriptor_map without id property` , errorCode ( 2 ) ) ; } else if ( item . path ) { const tokenFinder = jp . query ( payload , item . path ) ; if ( tokenFinder . length == 0 ) { throw new ValidationError ( `The SIOP presentation exchange response has descriptor_map with id '${ item . id }'. This path '${ item . path }' did not return a token.` , errorCode ( 3 ) ) ; } else if ( tokenFinder . length > 1 ) { throw new ValidationError ( `The SIOP presentation exchange response has descriptor_map with id '${ item . id }'. This path '${ item . path }' points to multiple credentails and should only point to one credential.` , errorCode ( 4 ) ) ; } else if ( typeof tokenFinder [ 0 ] === 'string' ) { const foundToken = tokenFinder [ 0 ] ; const claimToken = ClaimToken . create ( foundToken ) ; decodedTokens [ item . id ] = claimToken ; } else if ( tokenFinder [ 0 ] [ '\@context' ] ) { const foundToken = tokenFinder [ 0 ] ; const claimToken = ClaimToken . create ( foundToken ) ; decodedTokens [ item . id ] = claimToken ; } } else { throw new ValidationError ( `The SIOP presentation exchange response has descriptor_map with id '${ item . id }'. No path property found.` , errorCode ( 5 ) ) ; } } } return decodedTokens ; } }