Skip to content

Commit

Permalink
feat: Add support for agent option. [#78] (#81)
Browse files Browse the repository at this point in the history
* feat: Add support for `agent` option. [#78]

* fix: Fix README.md.
  • Loading branch information
ShogunPanda committed Oct 1, 2021
1 parent 3d0ca14 commit 877223d
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 27 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,25 @@ npm install get-jwks
### Options

```js
const https = require('https')
const buildGetJwks = require('get-jwks')

const getJwks = buildGetJwks({
max: 100,
maxAge: 60 * 1000,
allowedDomains: ['https://example.com'],
providerDiscovery: false,
agent: new https.Agent({
keepAlive: true,
}),
})
```

- `max`: Max items to hold in cache. Defaults to 100.
- `maxAge`: Milliseconds an item will remain in cache. Defaults to 60s.
- `allowedDomains`: Array of allowed domains. By default all domains are allowed.
- `providerDiscovery`: Indicates if the Provider Configuration Information is used to automatically get the jwks_uri from the [OpenID Provider Discovery Endpoint](https://openid.net/specs/openid-connect-discovery-1_0.html#ProviderConfig). This endpoint is exposing the [Provider Metadata](https://openid.net/specs/openid-connect-discovery-1_0.html#ProviderMetadata). With this flag set to true the domain will be treated as the OpenID Issuer which is the iss property in the token. Defaults to false
- `providerDiscovery`: Indicates if the Provider Configuration Information is used to automatically get the jwks_uri from the [OpenID Provider Discovery Endpoint](https://openid.net/specs/openid-connect-discovery-1_0.html#ProviderConfig). This endpoint is exposing the [Provider Metadata](https://openid.net/specs/openid-connect-discovery-1_0.html#ProviderMetadata). With this flag set to true the domain will be treated as the OpenID Issuer which is the iss property in the token. Defaults to false.
- `agent`: The custom agent to use for requests, as specified in [node-fetch documentation](https://github.com/node-fetch/node-fetch#custom-agent). Defaults to `null`.

> `max` and `maxAge` are provided to [lru-cache](https://www.npmjs.com/package/lru-cache).
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"node": ">=10"
},
"scripts": {
"test": "tap",
"test": "tap test/*.spec.js",
"lint": "eslint ."
},
"repository": {
Expand Down
52 changes: 27 additions & 25 deletions src/get-jwks.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,42 +15,44 @@ function ensureTrailingSlash(domain) {
return domain.endsWith('/') ? domain : `${domain}/`
}

async function getJwksUri(normalizedDomain) {
const response = await fetch(
`${normalizedDomain}.well-known/openid-configuration`,
{
timeout: 5000,
}
)
const body = await response.json()

if (!response.ok) {
const error = new Error(response.statusText)
error.response = response
error.body = body
throw error
}

if (!body.jwks_uri) {
throw new Error(errors.NO_JWKS_URI)
}

return body.jwks_uri
}

function buildGetJwks(options = {}) {
const max = options.max || 100
const maxAge = options.maxAge || 60 * 1000 /* 1 minute */
const allowedDomains = (options.allowedDomains || []).map(ensureTrailingSlash)
const providerDiscovery = options.providerDiscovery || false

const agent = options.agent || null

const staleCache = new LRU({ max: max * 2, maxAge })
const cache = new LRU({
max,
maxAge,
dispose: staleCache.set.bind(staleCache),
})

async function getJwksUri(normalizedDomain) {
const response = await fetch(
`${normalizedDomain}.well-known/openid-configuration`,
{
agent,
timeout: 5000,
}
)
const body = await response.json()

if (!response.ok) {
const error = new Error(response.statusText)
error.response = response
error.body = body
throw error
}

if (!body.jwks_uri) {
throw new Error(errors.NO_JWKS_URI)
}

return body.jwks_uri
}

async function getPublicKey(signature) {
return jwkToPem(await this.getJwk(signature))
}
Expand Down Expand Up @@ -95,7 +97,7 @@ function buildGetJwks(options = {}) {
? await getJwksUri(normalizedDomain)
: `${normalizedDomain}.well-known/jwks.json`

const response = await fetch(jwksUri, { timeout: 5000 })
const response = await fetch(jwksUri, { agent, timeout: 5000 })
const body = await response.json()

if (!response.ok) {
Expand Down

0 comments on commit 877223d

Please sign in to comment.