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

Topic/k1ch/ internal caching for GET:/jwks.json #101

Closed
wants to merge 1 commit into from
Closed
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
16 changes: 14 additions & 2 deletions server/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion server/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@dmgt-tech/the-usher-server",
"version": "2.0.0",
"version": "2.0.1",
"description": "The Usher Authorization Server",
"engines": {
"node": ">=18"
Expand Down Expand Up @@ -32,6 +32,7 @@
"js-yaml": "4.1.0",
"jwks-rsa": "3.1.0",
"moment": "2.30.1",
"node-cache": "5.1.2",
"oas-tools": "2.2.2",
"pem-jwk": "2.0.0",
"serverless-http": "3.1.0",
Expand Down
26 changes: 19 additions & 7 deletions server/src/api_endpoints/endpoint_jwksjson.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,28 @@
const keystore = require('database/layer/db-keys')
const { pem2jwk } = require('pem-jwk')
const createError = require('http-errors')
const NodeCache = require('node-cache')

const myCache = new NodeCache({ stdTTL: 60, checkperiod: 30 })
const getJwks = async (req, res) => {
try {
const keyPairs = await keystore.selectAllKeys()
const publicKeys = keyPairs?.map(keyPair => {
const item = pem2jwk(keyPair.public_key)
item.kid = keyPair.kid
return item
})
res.status(200).send({ keys: publicKeys })
const cacheKeyName = 'usher-jwks'
let response = myCache.get(cacheKeyName)

if (response) {
res.set('x-cache', 'Hit from Usher')
} else {
const keyPairs = await keystore.selectAllKeys()
const publicKeys = keyPairs?.map(keyPair => {
const item = pem2jwk(keyPair.public_key)
item.kid = keyPair.kid
return item
})
response = { keys: publicKeys }
myCache.set(cacheKeyName, response)
}

res.status(200).send(response)
} catch ({ httpStatusCode = 500, message }) {
return next(createError(httpStatusCode, { message }))
}
Expand Down
13 changes: 10 additions & 3 deletions server/test/endpoint_jwks.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,19 @@ const { getServerUrl } = require('./lib/urls')

describe('Get JWKS', () => {
const url = `${getServerUrl()}/.well-known/jwks.json`
const fetchJWKS = () => fetch(url, { method: 'GET', headers: { 'Content-Type': 'application/json' } });

it('should return a JWKS containing a list of keys', async function () {
const response = await fetch(url, { method: 'GET', headers: { 'Content-Type': 'application/json' } })
assert.strictEqual(response.status, 200)
it('should return a JWKS containing a list of keys', async () => {
const response = await fetchJWKS()
assert.equal(response.status, 200)
const jwks = await response.json()
assert('keys' in jwks, 'There should be a keys element in the object.')
assert(jwks.keys.length > 0, 'There should be at least one key in the keys array')
})

it('should return a JWKS from internal cache', async () => {
const response = await fetchJWKS()
assert.equal(response.status, 200)
assert.equal(response.headers.get('x-cache'), 'Hit from Usher')
})
})