Skip to content

Commit

Permalink
chore: constants extraction and logic change (#245)
Browse files Browse the repository at this point in the history
* chore: small improvements

* chore: constants

* chore: removed useless async
  • Loading branch information
ilteoood authored Oct 11, 2023
1 parent 42737e3 commit 5961f1c
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 16 deletions.
15 changes: 9 additions & 6 deletions src/get-jwks.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,21 @@ const jwkToPem = require('jwk-to-pem')

const { errorCode, GetJwksError } = require('./error')

const ONE_MINUTE = 60 * 1000
const FIVE_SECONDS = 5 * 1000

function ensureTrailingSlash(domain) {
return domain.endsWith('/') ? domain : `${domain}/`
return domain[domain.length - 1] === '/' ? domain : `${domain}/`
}

function ensureNoLeadingSlash(path) {
return path.startsWith('/') ? path.substring(1) : path
return path[0] === '/' ? path.substring(1) : path
}

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 ttl = options.ttl || ONE_MINUTE
const timeout = options.timeout || FIVE_SECONDS
const issuersWhitelist = (options.issuersWhitelist || options.allowedDomains || []).map(ensureTrailingSlash)
const checkIssuer = options.checkIssuer
const providerDiscovery = options.providerDiscovery || false
Expand Down Expand Up @@ -83,7 +86,7 @@ function buildGetJwks(options = {}) {
}

const jwkPromise = retrieveJwk(normalizedDomain, alg, kid).catch(
async err => {
err => {
const stale = staleCache.get(cacheKey)

cache.delete(cacheKey)
Expand Down Expand Up @@ -118,7 +121,7 @@ function buildGetJwks(options = {}) {
})
}

if (!body.keys || body.keys.length === 0) {
if (!body.keys?.length) {
throw new GetJwksError(errorCode.NO_JWKS)
}

Expand Down
4 changes: 4 additions & 0 deletions test/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,17 @@ const jwks = {
}
],
}

const privateKey = readFileSync(path.join(__dirname, 'private.pem'), 'utf8')

const jwk = jwks.keys[1]

const token = jwt.sign({ name: 'Jane Doe' }, privateKey, {
algorithm: jwk.alg,
issuer: domain,
keyid: jwk.kid,
})

const oidcConfig = {
issuer: 'https://localhost/',
jwks_uri: 'https://localhost/.well-known/certs',
Expand Down
4 changes: 2 additions & 2 deletions test/fast-jwt-integration.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ const { createVerifier } = require('fast-jwt')
const { jwks, token } = require('./constants')
const buildGetJwks = require('../src/get-jwks')

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

t.afterEach(async () => {
t.afterEach(() => {
nock.cleanAll()
nock.enableNetConnect()
})
Expand Down
4 changes: 2 additions & 2 deletions test/fastify-jwt-integrations.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ const fjwt = require('@fastify/jwt')
const { oidcConfig, jwks, token, domain } = require('./constants')
const buildGetJwks = require('../src/get-jwks')

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

t.afterEach(async () => {
t.afterEach(() => {
nock.cleanAll()
nock.enableNetConnect()
})
Expand Down
4 changes: 2 additions & 2 deletions test/getJwk.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ const { jwks, domain } = require('./constants')
const buildGetJwks = require('../src/get-jwks')
const { GetJwksError, errorCode } = require('../src/error')

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

t.afterEach(async () => {
t.afterEach(() => {
nock.cleanAll()
nock.enableNetConnect()
})
Expand Down
4 changes: 2 additions & 2 deletions test/getJwkDiscovery.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ const { oidcConfig, jwks, domain } = require('./constants')
const buildGetJwks = require('../src/get-jwks')
const { errorCode, GetJwksError } = require('../src/error')

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

t.afterEach(async () => {
t.afterEach(() => {
nock.cleanAll()
nock.enableNetConnect()
})
Expand Down
4 changes: 2 additions & 2 deletions test/getJwksUri.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ const { domain } = require('./constants')
const buildGetJwks = require('../src/get-jwks')
const { GetJwksError, errorCode } = require('../src/error')

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

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

0 comments on commit 5961f1c

Please sign in to comment.