From eac3fb00ef11c08a277dd8f9e2684b79f15193c2 Mon Sep 17 00:00:00 2001 From: Joe Carstairs Date: Mon, 22 Jan 2024 16:27:58 +0000 Subject: Authenticator db functions use Uint8Array type for id, public_key fields --- api/db/_getAuthenticator.ts | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) (limited to 'api/db/_getAuthenticator.ts') diff --git a/api/db/_getAuthenticator.ts b/api/db/_getAuthenticator.ts index e93023b..70c577c 100644 --- a/api/db/_getAuthenticator.ts +++ b/api/db/_getAuthenticator.ts @@ -1,14 +1,15 @@ +import type Authenticator from '../types/Authenticator'; + import { sql } from '@vercel/postgres'; import { AUTHENTICATORS } from './_tables'; -import Authenticator from '../types/Authenticator'; export default async function getAuthenticator< T extends keyof Authenticator ->(id: string, fields: T[]): Promise> { +>(id: Authenticator['id'], fields: T[]): Promise> { const query = ` SELECT ${fields.join(', ')} FROM ${AUTHENTICATORS.name} - WHERE ${AUTHENTICATORS.fields.id} = '${id}'; + WHERE ${AUTHENTICATORS.fields.id} = '{ ${id.toString()} }'; `; try { @@ -16,16 +17,26 @@ export default async function getAuthenticator< const row = result.rows.at(0); if (!row) { - throw new Error(`No authenticator with ID ${id} found in database.`); + throw new Error(`No authenticator with ID [${id.toString()}] found in database.`); } const authenticator: Partial> = {}; for (const field in fields) { - // Assume type conversion is already done by node-postgres - authenticator[field] = row[AUTHENTICATORS.fields[field]]; + switch (field) { + case 'id': + case 'public_key': + // Convert from number[] to Uint8Array + // We know that we'll get Postgres integer[] as JavaScript number[]: + // https://github.com/brianc/node-pg-types/blob/master/lib/textParsers.js + authenticator[field] = new Uint8Array(row[AUTHENTICATORS.fields[field]]); + break; + default: + // Assume adequate type conversion is already done by node-postgres + authenticator[field] = row[AUTHENTICATORS.fields[field]]; + } } return authenticator as Pick; } catch (err) { - throw new Error(`Failed to get authenticator with ID ${id} from database. Query was ${query.replace(/\s+/g, ' ')}. ${err}`); + throw new Error(`Failed to get authenticator with ID [${id.toString()}] from database. Query was ${query.replace(/\s+/g, ' ')}. ${err}`); } } -- cgit v1.2.3