File tree Expand file tree Collapse file tree 4 files changed +36
-4
lines changed Expand file tree Collapse file tree 4 files changed +36
-4
lines changed Original file line number Diff line number Diff line change @@ -37,7 +37,7 @@ export class AppCheckTokenVerifier {
3737 private readonly signatureVerifier : SignatureVerifier ;
3838
3939 constructor ( private readonly app : App ) {
40- this . signatureVerifier = PublicKeySignatureVerifier . withJwksUrl ( JWKS_URL ) ;
40+ this . signatureVerifier = PublicKeySignatureVerifier . withJwksUrl ( JWKS_URL , app . options . httpAgent ) ;
4141 }
4242
4343 /**
Original file line number Diff line number Diff line change @@ -53,14 +53,15 @@ export class JwksFetcher implements KeyFetcher {
5353 private publicKeysExpireAt = 0 ;
5454 private client : jwks . JwksClient ;
5555
56- constructor ( jwksUrl : string ) {
56+ constructor ( jwksUrl : string , httpAgent ?: Agent ) {
5757 if ( ! validator . isURL ( jwksUrl ) ) {
5858 throw new Error ( 'The provided JWKS URL is not a valid URL.' ) ;
5959 }
6060
6161 this . client = jwks ( {
6262 jwksUri : jwksUrl ,
6363 cache : false , // disable jwks-rsa LRU cache as the keys are always cached for 6 hours.
64+ requestAgent : httpAgent ,
6465 } ) ;
6566 }
6667
@@ -190,8 +191,8 @@ export class PublicKeySignatureVerifier implements SignatureVerifier {
190191 return new PublicKeySignatureVerifier ( new UrlKeyFetcher ( clientCertUrl , httpAgent ) ) ;
191192 }
192193
193- public static withJwksUrl ( jwksUrl : string ) : PublicKeySignatureVerifier {
194- return new PublicKeySignatureVerifier ( new JwksFetcher ( jwksUrl ) ) ;
194+ public static withJwksUrl ( jwksUrl : string , httpAgent ?: Agent ) : PublicKeySignatureVerifier {
195+ return new PublicKeySignatureVerifier ( new JwksFetcher ( jwksUrl , httpAgent ) ) ;
195196 }
196197
197198 public verify ( token : string ) : Promise < void > {
Original file line number Diff line number Diff line change @@ -22,6 +22,7 @@ import * as chai from 'chai';
2222import * as sinon from 'sinon' ;
2323import * as mocks from '../../resources/mocks' ;
2424import * as nock from 'nock' ;
25+ import { Agent } from 'http' ;
2526
2627import { AppCheckTokenVerifier } from '../../../src/app-check/token-verifier' ;
2728import { JwtError , JwtErrorCode , PublicKeySignatureVerifier } from '../../../src/utils/jwt' ;
@@ -55,6 +56,25 @@ describe('AppCheckTokenVerifier', () => {
5556 }
5657 } ) ;
5758
59+ describe ( 'Constructor' , ( ) => {
60+ it ( 'AppOptions.httpAgent should be passed to PublicKeySignatureVerifier.withJwksUrl' , ( ) => {
61+ const mockAppWithAgent = mocks . appWithOptions ( {
62+ httpAgent : new Agent ( )
63+ } ) ;
64+ const agentForApp = mockAppWithAgent . options . httpAgent ;
65+ const verifierSpy = sinon . spy ( PublicKeySignatureVerifier , 'withJwksUrl' ) ;
66+
67+ expect ( verifierSpy . args ) . to . be . empty ;
68+
69+ new AppCheckTokenVerifier (
70+ mockAppWithAgent
71+ ) ;
72+
73+ expect ( verifierSpy . args [ 0 ] [ 1 ] ) . to . equal ( agentForApp ) ;
74+ verifierSpy . restore ( ) ;
75+ } ) ;
76+ } ) ;
77+
5878 describe ( 'verifyJWT()' , ( ) => {
5979 let mockedRequests : nock . Scope [ ] = [ ] ;
6080 let stubs : sinon . SinonStub [ ] = [ ] ;
Original file line number Diff line number Diff line change 1717'use strict' ;
1818
1919// Use untyped import syntax for Node built-ins
20+ import http = require( 'http' ) ;
2021import https = require( 'https' ) ;
2122
2223import * as _ from 'lodash' ;
@@ -380,6 +381,16 @@ describe('PublicKeySignatureVerifier', () => {
380381 expect ( verifier ) . to . be . an . instanceOf ( PublicKeySignatureVerifier ) ;
381382 expect ( ( verifier as any ) . keyFetcher ) . to . be . an . instanceOf ( JwksFetcher ) ;
382383 } ) ;
384+
385+ it ( 'should return a PublicKeySignatureVerifier instance with a JwksFetcher when a ' +
386+ 'valid jwks url and httpAgent is provided' , ( ) => {
387+ const mockHttpAgent = sinon . createStubInstance ( http . Agent ) ;
388+ const verifier = PublicKeySignatureVerifier . withJwksUrl ( 'https://www.example.com/publicKeys' , mockHttpAgent ) ;
389+ expect ( verifier ) . to . be . an . instanceOf ( PublicKeySignatureVerifier ) ;
390+ expect ( ( verifier as any ) . keyFetcher ) . to . be . an . instanceOf ( JwksFetcher ) ;
391+ expect ( ( verifier as any ) . keyFetcher . client . options . requestAgent ) . to . be . an . instanceOf ( http . Agent ) ;
392+ expect ( ( verifier as any ) . keyFetcher . client . options . requestAgent ) . to . eq ( mockHttpAgent ) ;
393+ } ) ;
383394 } ) ;
384395
385396 describe ( 'verify' , ( ) => {
You can’t perform that action at this time.
0 commit comments