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

UNTRACKED/k1ch/update-get-jwks/ use knex instead of PG pool #92

Merged
merged 2 commits into from
Feb 15, 2024
Merged
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
62 changes: 30 additions & 32 deletions database/layer/db-keys.js
Original file line number Diff line number Diff line change
@@ -1,46 +1,44 @@
const { PGPool } = require('./pg_pool')
const pool = new PGPool()
const { usherDb } = require('./knex')
const { pgErrorHandler } = require('../utils/pgErrorHandler')

async function selectKeyWithKid (kid) {
const sql = 'SELECT * FROM usher.keys WHERE kid = $1'
const result = await pool.query(sql, [kid])
return result.rows
const selectKeyWithKid = async (kid) => {
try {
return await usherDb('keys').where('kid', kid)
} catch (err) {
throw pgErrorHandler(err)
}
}

async function selectAllKeys () {
const sql = 'SELECT * FROM usher.keys ORDER BY key DESC'
const result = await pool.query(sql)
return result.rows
const selectAllKeys = async () => {
try {
return await usherDb('keys').select('*').orderBy('key', 'desc')
} catch (err) {
throw pgErrorHandler(err)
}
}

async function selectLatestKey () {
const sql = 'SELECT * FROM usher.keys ORDER BY key DESC LIMIT 1'
const result = await pool.query(sql)
return result.rows[0]
const selectLatestKey = async () => {
try {
return await usherDb('keys').select('*').orderBy('key', 'desc').first()
} catch (err) {
throw pgErrorHandler(err)
}
}

async function insertKey (kid, publicKey, privateKey) {
// TODO: Security Review: Should keys be encrypted prior to storing in DB?
const alreadyExistingKeys = await selectKeyWithKid(kid)
if (alreadyExistingKeys.length > 0) {
throw new Error('Insert failed. Key with kid ' + kid + ' already exists in the keystore.')
}
const sql = 'INSERT INTO usher.keys (kid, public_key, private_key) VALUES ($1, $2, $3)'
const insertKey = async (kid, publicKey, privateKey) => {
try {
await pool.query(sql, [kid, publicKey, privateKey])
return 'Insert successful'
} catch (error) {
return `Insert failed: ${error.message}`
const [insertedKey] = await usherDb('keys').insert({ kid, public_key: publicKey, private_key: privateKey }).returning('*')
return insertedKey
} catch (err) {
throw pgErrorHandler(err)
}
}

async function deleteKey (kid) {
const alreadyExistingKeys = await selectKeyWithKid(kid)
if (alreadyExistingKeys.length === 1) {
const sql = 'DELETE FROM usher.keys WHERE kid = $1'
await pool.query(sql, [kid])
} else {
throw new Error('Delete failed. Key with kid ' + kid + ' not found.')
const deleteKey = async (kid) => {
try {
return await usherDb('keys').where('kid', kid).del()
} catch (err) {
throw pgErrorHandler(err)
}
}

Expand Down
24 changes: 14 additions & 10 deletions server/src/api_endpoints/endpoint_jwksjson.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
const keystore = require('database/layer/db-keys')
const pem2jwk = require('pem-jwk').pem2jwk
const { pem2jwk } = require('pem-jwk')
const createError = require('http-errors')

async function getJwks (req, res) {
const keyPairs = await keystore.selectAllKeys()
const publicKeys = keyPairs.map(keyPair => {
const item = pem2jwk(keyPair.public_key)
item.kid = keyPair.kid
return item
})
const result = { keys: publicKeys }
res.status(200).send(result)
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 })
} catch ({ httpStatusCode = 500, message }) {
return next(createError(httpStatusCode, { message }))
}
}

module.exports = { getJwks }
Loading