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 15eab05
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 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
2 changes: 1 addition & 1 deletion server/src/api_endpoints/endpoint_jwksjson.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const pem2jwk = require('pem-jwk').pem2jwk

async function getJwks (req, res) {
const keyPairs = await keystore.selectAllKeys()
const publicKeys = keyPairs.map(keyPair => {
const publicKeys = keyPairs?.map(keyPair => {
const item = pem2jwk(keyPair.public_key)
item.kid = keyPair.kid
return item
Expand Down

0 comments on commit 15eab05

Please sign in to comment.