Skip to content

Commit

Permalink
chore: UNTRACKED/k1ch/update-get-jwks/ use knex instead of PG pool
Browse files Browse the repository at this point in the history
  • Loading branch information
k1ch committed Feb 14, 2024
1 parent 920b963 commit b075e79
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 18 deletions.
20 changes: 12 additions & 8 deletions database/layer/db-keys.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
const { PGPool } = require('./pg_pool')
const pool = new PGPool()
const { usherDb } = require('./knex')
const { pgErrorHandler } = require('../utils/pgErrorHandler')

async function selectKeyWithKid (kid) {
async function selectKeyWithKid(kid) {
const sql = 'SELECT * FROM usher.keys WHERE kid = $1'
const result = await pool.query(sql, [kid])
return result.rows
}

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 () {
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]
}

async function insertKey (kid, publicKey, privateKey) {
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) {
Expand All @@ -34,7 +38,7 @@ async function insertKey (kid, publicKey, privateKey) {
}
}

async function deleteKey (kid) {
async function deleteKey(kid) {
const alreadyExistingKeys = await selectKeyWithKid(kid)
if (alreadyExistingKeys.length === 1) {
const sql = 'DELETE FROM usher.keys WHERE kid = $1'
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 b075e79

Please sign in to comment.