import ErrorHelpers from '../error_handling/ErrorHelpers' ; import { ClaimToken , IExpectedVerifiablePresentation , ITokenValidator , TokenType , VerifiableCredentialValidation } from '../index' ; import { IValidationResponse } from '../input_validation/IValidationResponse' ; import ValidationQueue from '../input_validation/ValidationQueue' ; import ValidationQueueItem from '../input_validation/ValidationQueueItem' ; import { VerifiablePresentationValidation } from '../input_validation/VerifiablePresentationValidation' ; import IValidatorOptions from '../options/IValidatorOptions' ; import ValidationOptions from '../options/ValidationOptions' ; const errorCode = ( error ) => ErrorHelpers . errorCode ( 'VCSDKVPTV' , error ) ; /**
 * Class to validate a token
 */ export default class VerifiablePresentationTokenValidator implements ITokenValidator { param validatorOption param expected constructor ( private validatorOption , private expected ) { } param queue param queueItem param siopDid public async validate ( queue , queueItem , siopDid ) { const options = new ValidationOptions ( this . validatorOption , TokenType . verifiablePresentationJwt ) ; const validator = new VerifiablePresentationValidation ( options , this . expected , siopDid , queueItem . id ) ; let validationResult = await validator . validate ( < string > queueItem . tokenToValidate . rawToken ) ; if ( validationResult . result ) { validationResult = this . getTokens ( validationResult , queue ) ; } return validationResult as IValidationResponse ; } param validationResponse param queue public getTokens ( validationResponse , queue ) { if ( ! validationResponse . payloadObject . vp || ! validationResponse . payloadObject . vp . verifiableCredential ) { return { result : false , status : this . validatorOption . invalidTokenError , code : errorCode ( 1 ) , detailedError : 'No verifiable credential' } ; } const vc = validationResponse . payloadObject . vp . verifiableCredential ; validationResponse . tokensToValidate = { } ; if ( vc ) { const validatorSafeguards = this . validatorOption . validationSafeguards ; if ( vc . length > validatorSafeguards . maxNumberOfVCTokensInPresentation ) { return { result : false , status : this . validatorOption . invalidTokenError , code : errorCode ( 2 ) , detailedError : `The number of VCs ${ vc . length } in presentation exceeds the maximum ${ validatorSafeguards . maxNumberOfVCTokensInPresentation }.` } ; } let oversized = vc . filter ( ( token ) => token . length > validatorSafeguards . maxSizeOfVCTokensInPresentation ) ; if ( oversized . length !== 0 ) { return { result : false , status : this . validatorOption . invalidTokenError , code : errorCode ( 3 ) , detailedError : `The presentation has an oversized VC tokens. Size ${ oversized [ 0 ] . length }. Maximum size: ${ validatorSafeguards . maxSizeOfVCTokensInPresentation }.` } ; } for ( let token in vc ) { const claimToken = ClaimToken . create ( vc [ token ] ) ; const vcType = VerifiableCredentialValidation . getVerifiableCredentialType ( claimToken . decodedToken . vc || claimToken . decodedToken ) ; validationResponse . tokensToValidate [ vcType ] = claimToken ; queue . enqueueToken ( vcType , claimToken ) ; } } return validationResponse ; } /**
     * Gets the type of token to validate
     */ public get isType ( ) : TokenType { return TokenType . verifiablePresentationJwt ; } }