Skip to content

Commit

Permalink
Update allowedDomains to issuersWhitelist (#229)
Browse files Browse the repository at this point in the history
* Update allowedDomains to issuersWhitelist

* update language to refer to issuers instead of domains

* support backwards compatibility

* add deprecation details for allowedDomains
  • Loading branch information
eugenio-oddone authored Aug 16, 2023
1 parent 056df88 commit 0c59210
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 22 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const getJwks = buildGetJwks({
max: 100,
ttl: 60 * 1000,
timeout: 5000,
allowedDomains: ['https://example.com'],
issuersWhitelist: ['https://example.com'],
checkIssuer: (issuer) => {
return issuer === 'https://example.com'
},
Expand All @@ -36,8 +36,9 @@ const getJwks = buildGetJwks({
- `max`: Max items to hold in cache. Defaults to 100.
- `ttl`: Milliseconds an item will remain in cache. Defaults to 60s.
- `timeout`: Specifies how long it should wait to retrieve a JWK before it fails. The time is set in milliseconds. Defaults to 5s.
- `allowedDomains`: Array of allowed domains. By default all domains are allowed.
- `checkIssuer`: Optional user defined function to validate a token's domain
- `issuersWhitelist`: Array of allowed issuers. By default all issuers are allowed.
- `allowedDomains`: This has been **deprecated** and replaced with `issuersWhitelist`.
- `checkIssuer`: Optional user defined function to validate a token's domain.
- `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. Ignored if jwksPath is specified.
- `jwksPath`: Specify a relative path to the jwks_uri. Example `/otherdir/jwks.json`. Takes precedence over providerDiscovery. Optional.
- `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`.
Expand Down Expand Up @@ -132,7 +133,7 @@ const buildGetJwks = require('get-jwks')
// often encoded as the `iss` property of the token payload
const domain = 'https://...'

const getJwks = buildGetJwks({ allowedDomains: [...]})
const getJwks = buildGetJwks({ issuersWhitelist: [...]})

// create a verifier function with key as a function
const verifyWithPromise = createVerifier({
Expand Down
2 changes: 1 addition & 1 deletion src/get-jwks.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type GetPublicKeyOptions = {
type GetJwksOptions = {
max?: number
ttl?: number
allowedDomains?: string[]
issuersWhitelist?: string[]
providerDiscovery?: boolean
jwksPath?: string
agent?: Agent
Expand Down
4 changes: 2 additions & 2 deletions src/get-jwks.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function buildGetJwks(options = {}) {
const max = options.max || 100
const ttl = options.ttl || 60 * 1000 /* 1 minute */
const timeout = options.timeout || 5 * 1000 /* 5 seconds */
const allowedDomains = (options.allowedDomains || []).map(ensureTrailingSlash)
const issuersWhitelist = (options.issuersWhitelist || options.allowedDomains || []).map(ensureTrailingSlash)
const checkIssuer = options.checkIssuer
const providerDiscovery = options.providerDiscovery || false
const jwksPath = options.jwksPath
Expand Down Expand Up @@ -65,7 +65,7 @@ function buildGetJwks(options = {}) {

const normalizedDomain = ensureTrailingSlash(domain)

if (allowedDomains.length && !allowedDomains.includes(normalizedDomain)) {
if (issuersWhitelist.length && !issuersWhitelist.includes(normalizedDomain)) {
const error = new GetJwksError(errorCode.DOMAIN_NOT_ALLOWED)
return Promise.reject(error)
}
Expand Down
6 changes: 3 additions & 3 deletions test/getJwk.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ t.test('allowed domains', async t => {
nock(allowedDomain).get('/.well-known/jwks.json').reply(200, jwks)

const getJwks = buildGetJwks({
allowedDomains: [allowedDomain],
issuersWhitelist: [allowedDomain],
})

const [{ alg, kid }] = jwks.keys
Expand All @@ -252,7 +252,7 @@ t.test('allowed domains', async t => {
nock(domain1).get('/.well-known/jwks.json').reply(200, jwks)
nock(domain2).get('/.well-known/jwks.json').reply(200, jwks)

const getJwks = buildGetJwks({ allowedDomains: [domain1, domain2] })
const getJwks = buildGetJwks({ issuersWhitelist: [domain1, domain2] })

const [{ alg, kid }] = jwks.keys

Expand Down Expand Up @@ -297,7 +297,7 @@ t.test('allowed domains', async t => {

t.test('forbids domain outside of the allow list', async t => {
const getJwks = buildGetJwks({
allowedDomains: ['https://example.com/'],
issuersWhitelist: ['https://example.com/'],
})

const [{ alg, kid }] = jwks.keys
Expand Down
24 changes: 12 additions & 12 deletions test/getJwkDiscovery.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,23 +244,23 @@ t.test('allowed domains for discovery', async t => {
['https://example.com/', 'https://example.com'],
]

allowedCombinations.forEach(([allowedDomain, domainFromToken]) => {
allowedCombinations.forEach(([allowedIssuer, domainFromToken]) => {
t.test(
`allows domain ${allowedDomain} requested with ${domainFromToken} for discovery`,
`allows domain ${allowedIssuer} requested with ${domainFromToken} for discovery`,
async t => {
const allowedDomainSlash = allowedDomain.endsWith('/')
? allowedDomain
: `${allowedDomain}/`
nock(allowedDomain)
const allowedIssuerSlash = allowedIssuer.endsWith('/')
? allowedIssuer
: `${allowedIssuer}/`
nock(allowedIssuer)
.get('/.well-known/openid-configuration')
.reply(200, {
issuer: allowedDomain,
jwks_uri: `${allowedDomainSlash}.well-known/certs`,
issuer: allowedIssuer,
jwks_uri: `${allowedIssuerSlash}.well-known/certs`,
})
nock(allowedDomain).get('/.well-known/certs').reply(200, jwks)
nock(allowedIssuer).get('/.well-known/certs').reply(200, jwks)
const getJwks = buildGetJwks({
providerDiscovery: true,
allowedDomains: [allowedDomain],
issuersWhitelist: [allowedIssuer],
})

const [{ alg, kid }] = jwks.keys
Expand Down Expand Up @@ -290,7 +290,7 @@ t.test('allowed domains for discovery', async t => {

const getJwks = buildGetJwks({
providerDiscovery: true,
allowedDomains: [domain1, domain2],
issuersWhitelist: [domain1, domain2],
})

const [{ alg, kid }] = jwks.keys
Expand All @@ -302,7 +302,7 @@ t.test('allowed domains for discovery', async t => {
t.test('forbids domain outside of the allow list', async t => {
const getJwks = buildGetJwks({
providerDiscovery: true,
allowedDomains: ['https://example.com/'],
issuersWhitelist: ['https://example.com/'],
})

const [{ alg, kid }] = jwks.keys
Expand Down

0 comments on commit 0c59210

Please sign in to comment.