Skip to content

Commit 2fb4a27

Browse files
fix: support httpAgent in JwksFetcher (#2689)
Co-authored-by: Lahiru Maramba <[email protected]>
1 parent 3e06bab commit 2fb4a27

File tree

4 files changed

+36
-4
lines changed

4 files changed

+36
-4
lines changed

src/app-check/token-verifier.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff 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
/**

src/utils/jwt.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff 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> {

test/unit/app-check/token-verifier.spec.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import * as chai from 'chai';
2222
import * as sinon from 'sinon';
2323
import * as mocks from '../../resources/mocks';
2424
import * as nock from 'nock';
25+
import { Agent } from 'http';
2526

2627
import { AppCheckTokenVerifier } from '../../../src/app-check/token-verifier';
2728
import { 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[] = [];

test/unit/utils/jwt.spec.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
'use strict';
1818

1919
// Use untyped import syntax for Node built-ins
20+
import http = require('http');
2021
import https = require('https');
2122

2223
import * 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', () => {

0 commit comments

Comments
 (0)