import { IPayloadProtectionSigning } from 'verifiablecredentials-crypto-sdk-typescript' ; import { AuthenticationErrorCode } from '../error_handling/AuthenticationErrorCode' ; import ErrorHelpers from '../error_handling/ErrorHelpers' ; import { IExpectedBase } from '../index' ; import { IValidationOptions } from '../options/IValidationOptions' ; import { IDidValidation , IDidValidationResponse } from './DidValidationResponse' ; const errorCode = ( error ) => ErrorHelpers . errorCode ( 'VCSDKDIDV' , error ) ; /**
 * Class for input validation of a token signed with DID key
 */ export class DidValidation implements IDidValidation { param options param expectedSchema constructor ( private options , private expected ) { } param token returns public async validate ( token ) { let validationResponse = { result : true , status : 200 , } ; validationResponse = await this . options . getTokenObjectDelegate ( validationResponse , token ) ; if ( ! validationResponse . result ) { return validationResponse ; } const parts = validationResponse . didKid ! . split ( '#' ) ; if ( parts . length <= 1 ) { return { result : false , code : errorCode ( 1 ) , detailedError : `The kid in the protected header does not contain the DID. Required format for kid is <did>#kid` , status : this . options . validatorOptions . invalidTokenError , wwwAuthenticateError : AuthenticationErrorCode . invalidRequest , } ; } validationResponse . did = parts [ 0 ] ; if ( ! validationResponse . did ) { return validationResponse = { result : false , code : errorCode ( 2 ) , detailedError : 'The kid does not contain the DID' , status : this . options . validatorOptions . invalidTokenError , wwwAuthenticateError : AuthenticationErrorCode . invalidRequest , } ; } validationResponse = await this . options . resolveDidAndGetKeysDelegate ( validationResponse ) ; if ( ! validationResponse . result ) { return validationResponse ; } validationResponse = await this . options . validateDidSignatureDelegate ( validationResponse , validationResponse . didSignature as IPayloadProtectionSigning ) ; if ( ! validationResponse . result ) { return validationResponse ; } validationResponse = await this . options . checkTimeValidityOnTokenDelegate ( validationResponse ) ; if ( ! validationResponse . result ) { return validationResponse ; } validationResponse . tokenId = validationResponse . payloadObject . jti || validationResponse . payloadObject . id ; return validationResponse ; } }