import { Crypto , IDidResolver , IdTokenTokenValidator , ITokenValidator , IValidatorOptions , ManagedHttpResolver , Requestor , SelfIssuedTokenValidator , SiopTokenValidator , TokenType , ValidationSafeguards , Validator , VerifiableCredentialTokenValidator , VerifiablePresentationTokenValidator } from '../index' ; import { IExpectedIdToken , IExpectedSelfIssued , IExpectedVerifiableCredential , IExpectedVerifiablePresentation , IssuerMap } from '../options/IExpected' ; import FetchRequest from '../tracing/FetchRequest' ; import IFetchRequest from '../tracing/IFetchRequest' ; import VerifiableCredentialConstants from '../verifiable_credential/VerifiableCredentialConstants' ; /**
 * Class to build a token validator
 */ export default class ValidatorBuilder { /**
     * The default behavior when we encounter an invalid status code
     */ public static readonly INVALID_TOKEN_STATUS_CODE = 401 ; private _tokenValidators : ( { [ type ] : ITokenValidator ; } ) | undefined ; private _trustedIssuersForVerifiableCredentials : { [ credentialType ] : string [ ] ; } | undefined ; private _trustedIssuerConfigurationsForIdTokens : IssuerMap | undefined ; private _audienceUrl : string | undefined ; private _requestor : Requestor | undefined ; private _state : string | undefined ; private _nonce : string | undefined ; private _fetchRequest : IFetchRequest = new FetchRequest ( ) ; private _resolver : IDidResolver = new ManagedHttpResolver ( VerifiableCredentialConstants . UNIVERSAL_RESOLVER_URL , this . _fetchRequest ) ; private _validationSafeguards : ValidationSafeguards = new ValidationSafeguards ( ) ; private _invalidTokenError : number ; private _useFullSiopValidation : boolean ; param _crypto constructor ( private _crypto ) { this . _invalidTokenError = ValidatorBuilder . INVALID_TOKEN_STATUS_CODE ; this . _useFullSiopValidation = false ; } /**
     * Gets the crypto object
     */ public get crypto ( ) { return this . _crypto ; } /**
     * Build the validator
     */ public build ( ) { return new Validator ( this ) ; } /**
     * Gets the validation options
     */ public get validationOptions ( ) : IValidatorOptions { return { resolver : this . resolver , fetchRequest : this . fetchRequest , validationSafeguards : this . _validationSafeguards , crypto : this . crypto , invalidTokenError : this . _invalidTokenError , performFullSiopValidation : this . _useFullSiopValidation , } ; } param state returns public useState ( state ) { this . _state = state ; return this ; } /**
     * Get the state for the response
     */ public get state ( ) { return this . _state ; } param nonce returns public useNonce ( nonce ) { this . _nonce = nonce ; return this ; } /**
     * Get the nonce for the response
     */ public get nonce ( ) { return this . _nonce ; } param requestor public useRequestor ( requestor ) { this . _requestor = requestor ; this . _audienceUrl = requestor . audienceUrl ( ) ; this . _trustedIssuersForVerifiableCredentials = requestor . trustedIssuersForVerifiableCredentials ( ) ; if ( ! this . _state && requestor . builder . state ) { this . _state = requestor . builder . state ; } if ( ! this . _nonce && requestor . builder . nonce ) { this . _nonce = requestor . builder . nonce ; } return this ; } /**
     * Gets the requestor
     */ public get requestor ( ) { return this . _requestor ; } param validator returns public useValidators ( validators ) { const validatorArray = validators as ITokenValidator [ ] ; this . _tokenValidators = { } ; if ( validatorArray . length ) { for ( let inx = 0 ; inx < validatorArray . length ; inx ++ ) { this . _tokenValidators [ validatorArray [ inx ] . isType ] = validatorArray [ inx ] ; } } else { this . _tokenValidators [ ( validators as ITokenValidator ) . isType ] = ( validators as ITokenValidator ) ; } return this ; } /**
     * Gets the token validators
     */ public get tokenValidators ( ) : { [ type ] : ITokenValidator ; } { if ( ! this . _tokenValidators ) { const validatorOptions = this . validationOptions ; this . _tokenValidators = { selfIssued : new SelfIssuedTokenValidator ( validatorOptions , < IExpectedSelfIssued > { type : TokenType . selfIssued } ) , idToken : new IdTokenTokenValidator ( validatorOptions , < IExpectedIdToken > { type : TokenType . idToken , configuration : this . _trustedIssuerConfigurationsForIdTokens } ) , verifiableCredential : new VerifiableCredentialTokenValidator ( validatorOptions , < IExpectedVerifiableCredential > { type : TokenType . verifiableCredential , contractIssuers : this . _trustedIssuersForVerifiableCredentials } ) , verifiablePresentationJwt : new VerifiablePresentationTokenValidator ( validatorOptions , < IExpectedVerifiablePresentation > { type : TokenType . verifiablePresentationJwt , didAudience : this . crypto . builder . did } ) , siopPresentationAttestation : new SiopTokenValidator ( validatorOptions , { type : TokenType . siopPresentationAttestation , audience : this . _audienceUrl , } ) , siop : new SiopTokenValidator ( validatorOptions , { type : TokenType . siop , audience : this . _audienceUrl , } ) , siopPresentationExchange : new SiopTokenValidator ( validatorOptions , { type : TokenType . siopPresentationExchange , audience : this . _audienceUrl , } ) , siopIssuance : new SiopTokenValidator ( validatorOptions , { type : TokenType . siopIssuance , audience : this . _audienceUrl , } ) } ; } return this . _tokenValidators ; } param fetchRequest public useFetchRequest ( fetchRequest ) { ( < any > this . resolver ) . fetchRequest = this . _fetchRequest = fetchRequest ; return this ; } /**
     * Gets the fetch client
     */ public get fetchRequest ( ) : IFetchRequest { return this . _fetchRequest ; } param resolver public useResolver ( resolver ) { this . _resolver = resolver ; return this ; } /**
     * Gets the resolver
     */ public get resolver ( ) : IDidResolver { return this . _resolver ; } returns public performFullSiopValidation ( ) { this . _useFullSiopValidation = true ; return this ; } /**
     * Gets a flag indicating whether or not to perform full siop token validation
     * some scenarios in the sdk use SiopValidation for non-siop tokens
     */ public get useFullSiopValidation ( ) : boolean { return this . _useFullSiopValidation ; } param issuers public useTrustedIssuersForVerifiableCredentials ( issuers ) { this . _trustedIssuersForVerifiableCredentials = issuers ; if ( this . _tokenValidators ) { const vcValidator = this . _tokenValidators [ TokenType . verifiableCredential ] ; if ( vcValidator ) { const expected = { type : TokenType . verifiableCredential , contractIssuers : issuers } ; this . _tokenValidators [ TokenType . verifiableCredential ] = new VerifiableCredentialTokenValidator ( this . validationOptions , expected ) ; } } return this ; } param issuers public get trustedIssuersForVerifiableCredentials ( ) : { [ credentialType ] : string [ ] ; } | undefined { return this . _trustedIssuersForVerifiableCredentials ; } param issuers public useTrustedIssuerConfigurationsForIdTokens ( issuers ) { this . _trustedIssuerConfigurationsForIdTokens = issuers ; if ( this . _tokenValidators ) { const idtokenValidator = this . _tokenValidators [ TokenType . idToken ] ; if ( idtokenValidator ) { const expected = { type : TokenType . idToken , configuration : issuers } ; this . _tokenValidators [ TokenType . idToken ] = new IdTokenTokenValidator ( this . validationOptions , expected ) ; } } return this ; } param issuers public get trustedIssuerConfigurationsForIdTokens ( ) : IssuerMap | undefined { return this . _trustedIssuerConfigurationsForIdTokens ; } param audience public useAudienceUrl ( audience ) { this . _audienceUrl = audience ; return this ; } param issuers public get audienceUrl ( ) : string | undefined { return this . _audienceUrl ; } /**
     * Gets the maximum number of VC tokens in a SIOP presentation
     */ public get maxNumberOfVCTokensInPresentation ( ) { return this . _validationSafeguards . maxNumberOfVCTokensInPresentation ; } /**
     * Sets the maximum number of VC tokens in a SIOP presentation
     */ public useMaxNumberOfVCTokensInPresentation ( value ) { this . _validationSafeguards . maxNumberOfVCTokensInPresentation = value ; return this ; } /**
     * Gets the maximum size of VP tokens in a SIOP
     */ public get maxSizeOfVPTokensInSiop ( ) { return this . _validationSafeguards . maxSizeOfVPTokensInSiop ; } /**
     * Sets the maximum size of VP tokens in a SIOP
     */ public useMaxSizeOfVPTokensInSiop ( value ) { this . _validationSafeguards . maxSizeOfVPTokensInSiop = value ; return this ; } /**
     * Gets the maximum size of VC tokens in a presentation
     */ public get maxSizeOfVCTokensInPresentation ( ) { return this . _validationSafeguards . maxSizeOfVCTokensInPresentation ; } /**
     * Sets the maximum size of VC tokens in a presentation
     */ public useMaxSizeOfVCTokensInPresentation ( value ) { this . _validationSafeguards . maxSizeOfVCTokensInPresentation = value ; return this ; } /**
     * Gets the maximum size of ID tokens
     */ public get maxSizeOfIdToken ( ) { return this . _validationSafeguards . maxSizeOfIdToken ; } /**
     * Sets the maximum size of ID tokens
     */ public useMaxSizeOfIdToken ( value ) { this . _validationSafeguards . maxSizeOfIdToken = value ; return this ; } param value returns public invalidTokensFailWith ( value ) { this . _invalidTokenError = value ; return this ; } private _enableVerifiedCredentialsStatusCheck = true ; public get featureVerifiedCredentialsStatusCheckEnabled ( ) : boolean { return this . _enableVerifiedCredentialsStatusCheck ; } public enableFeatureVerifiedCredentialsStatusCheck ( enable ) { this . _enableVerifiedCredentialsStatusCheck = enable ; return this ; } }