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 {
37
37
private readonly signatureVerifier : SignatureVerifier ;
38
38
39
39
constructor ( private readonly app : App ) {
40
- this . signatureVerifier = PublicKeySignatureVerifier . withJwksUrl ( JWKS_URL ) ;
40
+ this . signatureVerifier = PublicKeySignatureVerifier . withJwksUrl ( JWKS_URL , app . options . httpAgent ) ;
41
41
}
42
42
43
43
/**
Original file line number Diff line number Diff line change @@ -53,14 +53,15 @@ export class JwksFetcher implements KeyFetcher {
53
53
private publicKeysExpireAt = 0 ;
54
54
private client : jwks . JwksClient ;
55
55
56
- constructor ( jwksUrl : string ) {
56
+ constructor ( jwksUrl : string , httpAgent ?: Agent ) {
57
57
if ( ! validator . isURL ( jwksUrl ) ) {
58
58
throw new Error ( 'The provided JWKS URL is not a valid URL.' ) ;
59
59
}
60
60
61
61
this . client = jwks ( {
62
62
jwksUri : jwksUrl ,
63
63
cache : false , // disable jwks-rsa LRU cache as the keys are always cached for 6 hours.
64
+ requestAgent : httpAgent ,
64
65
} ) ;
65
66
}
66
67
@@ -190,8 +191,8 @@ export class PublicKeySignatureVerifier implements SignatureVerifier {
190
191
return new PublicKeySignatureVerifier ( new UrlKeyFetcher ( clientCertUrl , httpAgent ) ) ;
191
192
}
192
193
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 ) ) ;
195
196
}
196
197
197
198
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';
22
22
import * as sinon from 'sinon' ;
23
23
import * as mocks from '../../resources/mocks' ;
24
24
import * as nock from 'nock' ;
25
+ import { Agent } from 'http' ;
25
26
26
27
import { AppCheckTokenVerifier } from '../../../src/app-check/token-verifier' ;
27
28
import { JwtError , JwtErrorCode , PublicKeySignatureVerifier } from '../../../src/utils/jwt' ;
@@ -55,6 +56,25 @@ describe('AppCheckTokenVerifier', () => {
55
56
}
56
57
} ) ;
57
58
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
+
58
78
describe ( 'verifyJWT()' , ( ) => {
59
79
let mockedRequests : nock . Scope [ ] = [ ] ;
60
80
let stubs : sinon . SinonStub [ ] = [ ] ;
Original file line number Diff line number Diff line change 17
17
'use strict' ;
18
18
19
19
// Use untyped import syntax for Node built-ins
20
+ import http = require( 'http' ) ;
20
21
import https = require( 'https' ) ;
21
22
22
23
import * as _ from 'lodash' ;
@@ -380,6 +381,16 @@ describe('PublicKeySignatureVerifier', () => {
380
381
expect ( verifier ) . to . be . an . instanceOf ( PublicKeySignatureVerifier ) ;
381
382
expect ( ( verifier as any ) . keyFetcher ) . to . be . an . instanceOf ( JwksFetcher ) ;
382
383
} ) ;
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
+ } ) ;
383
394
} ) ;
384
395
385
396
describe ( 'verify' , ( ) => {
You can’t perform that action at this time.
0 commit comments