import ErrorHelpers from '../error_handling/ErrorHelpers' ; import { ClaimToken , createJwkThumbprint , IExpectedSiop } from '../index' ; import { IValidationOptions } from '../options/IValidationOptions' ; import { DidValidation } from './DidValidation' ; import { ISiopValidation , ISiopValidationResponse } from './SiopValidationResponse' ; const errorCode = ( error ) => ErrorHelpers . errorCode ( 'VCSDKSIVa' , error ) ; /**
 * Class for siop validation
 */ export class SiopValidation implements ISiopValidation { param options param expected constructor ( protected options , protected expected ) { this . _didValidation = new DidValidation ( this . options , this . expected ) ; } private _didValidation : DidValidation ; public get didValidation ( ) : DidValidation { return this . _didValidation ; } public set didValidation ( validator ) { this . _didValidation = validator ; } param siop returns public async validate ( siop ) { if ( siop . validationResponse ) { return siop . validationResponse ; } let validationResponse = await this . didValidation . validate ( siop . rawToken ) ; if ( ! validationResponse . result ) { return siop . validationResponse = validationResponse ; } validationResponse = this . options . checkScopeValidityOnSiopTokenDelegate ( validationResponse , this . expected ) ; if ( ! validationResponse . result ) { return siop . validationResponse = validationResponse ; } if ( ! validationResponse . tokenId ) { return siop . validationResponse = { result : false , code : errorCode ( 1 ) , detailedError : `The SIOP token identifier (jti/id) is missing` , status : 400 } ; } if ( this . options . validatorOptions . performFullSiopValidation ) { validationResponse = this . validateSelfIssuedClaims ( validationResponse ) ; } return siop . validationResponse = validationResponse ; } param validationResponse returns private validateSelfIssuedClaims ( validationResponse ) { if ( ! validationResponse . payloadObject . did || validationResponse . payloadObject . did !== validationResponse . did ) { return { result : false , code : errorCode ( 2 ) , detailedError : 'The did claim is invalid' , status : this . options . validatorOptions . invalidTokenError , } ; } const jwk = validationResponse . didSigningPublicKey ; const sub_jwk = validationResponse . payloadObject . sub_jwk ; if ( ! sub_jwk ) { return { result : false , code : errorCode ( 3 ) , detailedError : 'The sub_jwk claim is missing' , status : this . options . validatorOptions . invalidTokenError , } ; } const sub = validationResponse . payloadObject . sub ; if ( ! sub ) { return { result : false , code : errorCode ( 4 ) , detailedError : 'The sub claim is missing' , status : this . options . validatorOptions . invalidTokenError , } ; } const didJwkThumbprint = createJwkThumbprint ( jwk ) ; const subJwkThumbprint = createJwkThumbprint ( sub_jwk ) ; if ( didJwkThumbprint !== subJwkThumbprint || subJwkThumbprint !== sub ) { return { result : false , code : errorCode ( 5 ) , detailedError : 'The sub claim is invalid' , status : this . options . validatorOptions . invalidTokenError , } ; } return validationResponse ; } }