Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handle optional request agentOptions #84

Merged
merged 2 commits into from
Jul 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ const jwksClient = require('jwks-rsa');
const client = jwksClient({
strictSsl: true, // Default value
jwksUri: 'https://sandrino.auth0.com/.well-known/jwks.json',
requestHeaders: {} // Optional
requestHeaders: {}, // Optional
requestAgentOptions: {} // Optional
});

const kid = 'RkI5MjI5OUY5ODc1N0Q4QzM0OUYzNkVGMTJDOUEzQkFCOTU3NjE2Rg';
Expand Down Expand Up @@ -80,6 +81,27 @@ client.getSigningKey(kid, (err, key) => {
});
```

### Using AgentOptions for TLS/SSL Configuration

The `requestAgentOptions` property can be used to configure SSL/TLS options. An
example use case is providing a trusted private (i.e. enterprise/corporate) root
certificate authority to establish TLS communication with the `jwks_uri`.

```js
const jwksClient = require("jwks-rsa");
const client = jwksClient({
strictSsl: true, // Default value
jwksUri: 'https://my-enterprise-id-provider/.well-known/jwks.json',
requestHeaders: {}, // Optional
requestAgentOptions: {
ca: fs.readFileSync(caFile)
}
});
```

For more information, see [the NodeJS request library `agentOptions`
documentation](https://github.com/request/request#using-optionsagentoptions).

## Running Tests

```
Expand Down
17 changes: 17 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,23 @@ declare namespace JwksRsa {
publicKey: string;
}

interface AgentOptions {
[key: string]: string;
}

interface Options {
jwksUri: string;
rateLimit?: boolean;
cache?: boolean;
cacheMaxEntries?: number;
cacheMaxAge?: number;
jwksRequestsPerMinute?: number;
strictSsl?: boolean;
requestHeaders?: Headers;
requestAgentOptions?: AgentOptions;
handleSigningKeyError?(err: Error, cb: (err: Error) => void): any;
}

interface RsaSigningKey {
kid: string;
nbf: string;
Expand Down
3 changes: 2 additions & 1 deletion src/JwksClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ export class JwksClient {
json: true,
uri: this.options.jwksUri,
strictSSL: this.options.strictSsl,
headers: this.options.requestHeaders
headers: this.options.requestHeaders,
agentOptions: this.options.requestAgentOptions
}, (err, res) => {
if (err || res.statusCode < 200 || res.statusCode >= 300) {
this.logger('Failure:', res && res.body || err);
Expand Down
38 changes: 38 additions & 0 deletions tests/jwksClient.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,44 @@ describe("JwksClient", () => {
});
});

it("should set request agentOptions when provided", done => {
nock(jwksHost)
.get("./well-known/jwks.json")
.reply(function() {
expect(this.req.agentOptions).not.to.be.null;
expect(this.req.agentOptions["ca"]).to.be.equal("loadCA()");
return 200;
});

const client = new JwksClient({
jwksUri: `${jwksHost}/.well-known/jwks.json`,
requestAgentOptions: {
ca: "loadCA()"
}
});

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

it("should not set request agentOptions by default", done => {
nock(jwksHost)
.get("/.well-known/jwks.json")
.reply(function() {
expect(this.req).to.not.have.property("agentOptions");
return 200;
});

const client = new JwksClient({
jwksUri: `${jwksHost}/.well-known/jwks.json`
});

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

it("should send extra header", done => {
nock(jwksHost)
.get("/.well-known/jwks.json")
Expand Down