Skip to content

Commit

Permalink
UNTRACKED/k1ch/update-get-jwks/ use knex instead of PG pool (#92)
Browse files Browse the repository at this point in the history
* chore: UNTRACKED/k1ch/update-get-jwks/ use knex instead of PG pool

* chore: UNTRACKED/k1ch/update-get-jwks/ remove pg pool from db-keys.js
  • Loading branch information
k1ch authored Feb 15, 2024
1 parent 920b963 commit e5eec27
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 42 deletions.
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 }

0 comments on commit e5eec27

Please sign in to comment.