import TestSetup from './TestSetup' ; import { TokenType , IExpectedSiop , ValidatorBuilder } from '../lib/index' ; import { IssuanceHelpers } from './IssuanceHelpers' ; import { DidValidation } from '../lib/input_validation/DidValidation' ; import base64url from 'base64url' ; describe ( 'DidValidation' , ( ) => { let setup ; beforeEach ( async ( ) => { setup = new TestSetup ( ) ; setup . fetchMock . reset ( ) ; } ) ; afterEach ( ( ) => { setup . fetchMock . reset ( ) ; } ) ; it ( 'should test validate' , async ( ) => { const [ request , options , siop ] = await IssuanceHelpers . createRequest ( setup , TokenType . siopIssuance , true ) ; const expected = siop . expected . filter ( ( token ) => token . type === TokenType . siopIssuance ) [ 0 ] ; const validator = new DidValidation ( options , expected ) ; let response = await validator . validate ( < string > request . rawToken ) ; expect ( response . result ) . toBeTruthy ( ) ; response = await validator . validate ( request . rawToken + 'a' ) ; expect ( response . result ) . toBeFalsy ( ) ; expect ( response . status ) . toEqual ( ValidatorBuilder . INVALID_TOKEN_STATUS_CODE ) ; expect ( response . detailedError ) . toEqual ( 'The signature on the payload in the siopIssuance is invalid' ) ; expect ( response . code ) . toEqual ( 'VCSDKVaHe27' ) ; let tokenParts = ( < string > request . rawToken ) . split ( '.' ) ; response = await validator . validate ( `.${ tokenParts [ 1 ] }.${ tokenParts [ 2 ] }` ) ; expect ( response . result ) . toBeFalsy ( ) ; expect ( response . status ) . toEqual ( ValidatorBuilder . INVALID_TOKEN_STATUS_CODE ) ; expect ( response . detailedError ) . toEqual ( 'The siopIssuance could not be deserialized' ) ; expect ( response . code ) . toEqual ( 'VCSDKVaHe01' ) ; let header = { typ : 'JWT' , alg : 'RS256' } ; tokenParts = ( < string > request . rawToken ) . split ( '.' ) ; response = await validator . validate ( `${ base64url . encode ( JSON . stringify ( header ) ) }.${ tokenParts [ 1 ] }.${ tokenParts [ 2 ] }` ) ; expect ( response . result ) . toBeFalsy ( ) ; expect ( response . status ) . toEqual ( ValidatorBuilder . INVALID_TOKEN_STATUS_CODE ) ; expect ( response . detailedError ) . toEqual ( 'The protected header in the siopIssuance does not contain the kid' ) ; expect ( response . code ) . toEqual ( 'VCSDKVaHe05' ) ; header = { typ : 'JWT' , kid : 'abc' , alg : 'RS256' } ; tokenParts = ( < string > request . rawToken ) . split ( '.' ) ; response = await validator . validate ( `${ base64url . encode ( JSON . stringify ( header ) ) }.${ tokenParts [ 1 ] }.${ tokenParts [ 2 ] }` ) ; expect ( response . result ) . toBeFalsy ( ) ; expect ( response . status ) . toEqual ( 401 ) ; expect ( response . detailedError ) . toEqual ( 'The kid in the protected header does not contain the DID. Required format for kid is <did>#kid' ) ; expect ( response . code ) . toEqual ( 'VCSDKDIDV01' ) ; options . checkTimeValidityOnTokenDelegate = ( ) => { return < any > { result : false , detailedError : 'checkTimeValidityOnTokenDelegate error' } ; } ; response = await validator . validate ( < string > request . rawToken ) ; expect ( response . result ) . toBeFalsy ( response . detailedError ) ; expect ( response . detailedError ) . toEqual ( 'checkTimeValidityOnTokenDelegate error' ) ; options . resolveDidAndGetKeysDelegate = ( ) => { return < any > { result : false , detailedError : 'resolveDidAndGetKeysDelegate error' } ; } ; response = await validator . validate ( < string > request . rawToken ) ; expect ( response . result ) . toBeFalsy ( response . detailedError ) ; expect ( response . detailedError ) . toEqual ( 'resolveDidAndGetKeysDelegate error' ) ; options . getTokenObjectDelegate = ( ) => { return < any > { result : true , didKid : '#did#kid' } ; } ; response = await validator . validate ( < string > request . rawToken ) ; expect ( response . result ) . toBeFalsy ( response . detailedError ) ; expect ( response . detailedError ) . toEqual ( 'The kid does not contain the DID' ) ; expect ( response . code ) . toEqual ( 'VCSDKDIDV02' ) ; setup . fetchMock . reset ( ) ; const resolverUrl = `${ setup . resolverUrl }/abc` ; setup . fetchMock . get ( resolverUrl , { status : 404 , response : { } } ) ; response = await options . resolveDidAndGetKeysDelegate ( < any > { did : 'abc' } ) ; expect ( response . result ) . toBeFalsy ( ) ; } ) ; } ) ;