Skip to content

Commit

Permalink
BREAKING CHANGE: use fetch (#322)
Browse files Browse the repository at this point in the history
* feat: migrate

* BREAKING CHANGE: remove node-fetch in favour of fetch
  • Loading branch information
ilteoood authored Nov 21, 2024
1 parent 3cae365 commit 59f8c1b
Show file tree
Hide file tree
Showing 13 changed files with 177 additions and 192 deletions.
1 change: 0 additions & 1 deletion .taprc

This file was deleted.

11 changes: 4 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"directory": "./test/types"
},
"scripts": {
"test": "tap test/*.spec.js && tsd",
"test": "node --test test/*.spec.js && tsd",
"lint": "eslint ."
},
"repository": {
Expand All @@ -33,22 +33,19 @@
"homepage": "https://github.com/nearform/get-jwks#readme",
"dependencies": {
"jwk-to-pem": "^2.0.4",
"lru-cache": "^11.0.0",
"node-fetch": "^2.6.1"
"lru-cache": "^11.0.0"
},
"devDependencies": {
"@fastify/jwt": "^8.0.0",
"@types/node": "^22.0.0",
"@types/node-fetch": "^2.6.2",
"eslint": "^8.6.0",
"fast-jwt": "^4.0.0",
"fastify": "^4.0.3",
"jsonwebtoken": "^9.0.0",
"nock": "^13.0.7",
"nock": "^v14.0.0-beta.16",
"prettier": "^3.0.0",
"sinon": "^19.0.2",
"tap": "^16.0.0",
"tsd": "^0.31.0",
"typescript": "^5.0.2"
}
}
}
2 changes: 0 additions & 2 deletions src/error.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import type { Response } from 'node-fetch'

export enum errorCode {
OPENID_CONFIGURATION_REQUEST_FAILED = 'OPENID_CONFIGURATION_REQUEST_FAILED',
JWKS_REQUEST_FAILED = 'JWKS_REQUEST_FAILED',
Expand Down
4 changes: 1 addition & 3 deletions src/get-jwks.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { LRUCache } from 'lru-cache'
import type { Agent } from 'https'

type GetPublicKeyOptions = {
domain?: string
Expand All @@ -24,8 +23,7 @@ type GetJwksOptions = {
issuersWhitelist?: string[]
providerDiscovery?: boolean
jwksPath?: string
agent?: Agent
timeout?: number
fetchOptions?: RequestInit
}

declare namespace buildGetJwks {
Expand Down
11 changes: 3 additions & 8 deletions src/get-jwks.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict'

const fetch = require('node-fetch')
const { LRUCache } = require('lru-cache')
const jwkToPem = require('jwk-to-pem')

Expand All @@ -20,14 +19,13 @@ function ensureNoLeadingSlash(path) {
function buildGetJwks(options = {}) {
const max = options.max || 100
const ttl = options.ttl || ONE_MINUTE
const timeout = options.timeout || FIVE_SECONDS
const issuersWhitelist = (options.issuersWhitelist || []).map(ensureTrailingSlash)
const checkIssuer = options.checkIssuer
const providerDiscovery = options.providerDiscovery || false
const jwksPath = options.jwksPath
? ensureNoLeadingSlash(options.jwksPath)
: false
const agent = options.agent || null
const fetchOptions = { timeout: FIVE_SECONDS, ...options.fetchOptions }
const staleCache = new LRUCache({ max: max * 2, ttl })
const cache = new LRUCache({
max,
Expand All @@ -38,10 +36,7 @@ function buildGetJwks(options = {}) {
async function getJwksUri(normalizedDomain) {
const response = await fetch(
`${normalizedDomain}.well-known/openid-configuration`,
{
agent,
timeout,
}
fetchOptions,
)
const body = await response.json()

Expand Down Expand Up @@ -111,7 +106,7 @@ function buildGetJwks(options = {}) {
? await getJwksUri(normalizedDomain)
: `${normalizedDomain}.well-known/jwks.json`

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

if (!response.ok) {
Expand Down
24 changes: 12 additions & 12 deletions test/cache.spec.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
'use strict'

const t = require('tap')
const {test} = require('node:test')
const nock = require('nock')
const jwkToPem = require('jwk-to-pem')

const { jwks, domain } = require('./constants')

const buildGetJwks = require('../src/get-jwks')

t.test(
test(
'if there is already a key in cache, it should not make a http request',
async t => {
const getJwks = buildGetJwks()
Expand All @@ -20,14 +20,14 @@ t.test(

const publicKey = await getJwks.getPublicKey({ domain, alg, kid })
const jwk = await getJwks.getJwk({ domain, alg, kid })
t.ok(publicKey)
t.ok(jwk)
t.equal(publicKey, jwkToPem(jwk))
t.same(jwk, localKey)
t.assert.ok(publicKey)
t.assert.ok(jwk)
t.assert.equal(publicKey, jwkToPem(jwk))
t.assert.equal(jwk, localKey)
}
)

t.test(
test(
'if initialized without any cache settings it should use default values',
async t => {
nock('https://localhost/').get('/.well-known/jwks.json').reply(200, jwks)
Expand All @@ -37,10 +37,10 @@ t.test(
const publicKey = await getJwks.getPublicKey({ domain, alg, kid })
const jwk = await getJwks.getJwk({ domain, alg, kid })

t.ok(publicKey)
t.ok(jwk)
t.ok(getJwks.cache)
t.equal(cache.max, 100)
t.equal(cache.ttl, 60000)
t.assert.ok(publicKey)
t.assert.ok(jwk)
t.assert.ok(getJwks.cache)
t.assert.equal(cache.max, 100)
t.assert.equal(cache.ttl, 60000)
}
)
10 changes: 5 additions & 5 deletions test/fast-jwt-integration.spec.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
'use strict'

const t = require('tap')
const {beforeEach, afterEach, test} = require('node:test')
const nock = require('nock')
const { createVerifier } = require('fast-jwt')

const { jwks, token } = require('./constants')
const buildGetJwks = require('../src/get-jwks')

t.beforeEach(() => {
beforeEach(() => {
nock.disableNetConnect()
})

t.afterEach(() => {
afterEach(() => {
nock.cleanAll()
nock.enableNetConnect()
})

t.test('fast-jwt integration tests', async t => {
test('fast-jwt integration tests', async t => {
const domain = 'https://localhost/'
nock(domain).get('/.well-known/jwks.json').reply(200, jwks)

Expand All @@ -33,5 +33,5 @@ t.test('fast-jwt integration tests', async t => {
})
const payload = await verifyWithPromise(token)

t.equal(payload.name, 'Jane Doe')
t.assert.equal(payload.name, 'Jane Doe')
})
18 changes: 9 additions & 9 deletions test/fastify-jwt-integrations.spec.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
'use strict'

const t = require('tap')
const { beforeEach, afterEach, test } = require('node:test')
const nock = require('nock')
const Fastify = require('fastify')
const fjwt = require('@fastify/jwt')

const { oidcConfig, jwks, token, domain } = require('./constants')
const buildGetJwks = require('../src/get-jwks')

t.beforeEach(() => {
beforeEach(() => {
nock.disableNetConnect()
})

t.afterEach(() => {
afterEach(() => {
nock.cleanAll()
nock.enableNetConnect()
})

t.test('@fastify/jwt integration tests', async t => {
test('@fastify/jwt integration tests', async t => {
nock(domain).get('/.well-known/jwks.json').reply(200, jwks)

const fastify = Fastify()
Expand Down Expand Up @@ -52,11 +52,11 @@ t.test('@fastify/jwt integration tests', async t => {
},
})

t.equal(response.statusCode, 200)
t.equal(response.body, 'Jane Doe')
t.assert.equal(response.statusCode, 200)
t.assert.equal(response.body, 'Jane Doe')
})

t.test('@fastify/jwt integration tests with providerDiscovery', async t => {
test('@fastify/jwt integration tests with providerDiscovery', async t => {
nock(domain)
.get('/.well-known/openid-configuration')
.once()
Expand Down Expand Up @@ -94,6 +94,6 @@ t.test('@fastify/jwt integration tests with providerDiscovery', async t => {
},
})

t.equal(response.statusCode, 200)
t.equal(response.body, 'Jane Doe')
t.assert.equal(response.statusCode, 200)
t.assert.equal(response.body, 'Jane Doe')
})
Loading

0 comments on commit 59f8c1b

Please sign in to comment.