Skip to content

Commit

Permalink
feat: add proxy option to jwksClient
Browse files Browse the repository at this point in the history
  • Loading branch information
Benoit Sepe committed Feb 13, 2020
1 parent 73a087d commit c7c7ba5
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 3 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ const client = jwksClient({
strictSsl: true, // Default value
jwksUri: 'https://sandrino.auth0.com/.well-known/jwks.json',
requestHeaders: {}, // Optional
requestAgentOptions: {} // Optional
requestAgentOptions: {}, // Optional
proxy: '[protocol]://[username]:[pass]@[address]:[port]', // Optional
});

const kid = 'RkI5MjI5OUY5ODc1N0Q4QzM0OUYzNkVGMTJDOUEzQkFCOTU3NjE2Rg';
Expand Down
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ declare namespace JwksRsa {
cacheMaxEntries?: number;
cacheMaxAge?: number;
jwksRequestsPerMinute?: number;
proxy?: string;
strictSsl?: boolean;
requestHeaders?: Headers;
}
Expand Down
3 changes: 2 additions & 1 deletion src/JwksClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ export class JwksClient {
uri: this.options.jwksUri,
strictSSL: this.options.strictSsl,
headers: this.options.requestHeaders,
agentOptions: this.options.requestAgentOptions
agentOptions: this.options.requestAgentOptions,
proxy: this.options.proxy,
}, (err, res) => {
if (err || res.statusCode < 200 || res.statusCode >= 300) {
this.logger('Failure:', res && res.body || err);
Expand Down
46 changes: 45 additions & 1 deletion tests/jwksClient.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { JwksClient } from "../src/JwksClient";

describe("JwksClient", () => {
const jwksHost = "http://my-authz-server";
const proxy = "my-proxy-server:2815";

beforeEach(() => {
nock.cleanAll();
Expand Down Expand Up @@ -182,8 +183,51 @@ describe("JwksClient", () => {
done();
});
});
});

it("should add a proxy", done => {
nock(jwksHost)
.get("/.well-known/jwks.json")
.reply(function(uri, requestBody) {
expect(this.req.headers).not.to.be.null;
expect(this.req.headers["user-agent"]).to.be.equal("My-bot");
expect(Object.keys(this.req.headers).length).to.be.equal(3);
return (
200,
{
keys: [
{
alg: "RS256",
kty: "RSA",
use: "sig",
x5c: ["pk1"],
kid: "ABC"
},
{
alg: "RS256",
kty: "RSA",
use: "sig",
x5c: [],
kid: "123"
}
]
}
);
});

const client = new JwksClient({
jwksUri: `${jwksHost}/.well-known/jwks.json`,
requestHeaders: {
"User-Agent": "My-bot"
},
proxy: `http://username:password@${proxy}`,
});

client.getKeys((err, keys) => {
done();
});
});
});

describe("#getSigningKeys", () => {
it("should handle errors", done => {
nock(jwksHost)
Expand Down

0 comments on commit c7c7ba5

Please sign in to comment.