summaryrefslogtreecommitdiff
path: root/api/db/_getAuthenticator.ts
diff options
context:
space:
mode:
authorJoe Carstairs <jcarstairs@scottlogic.com>2024-01-22 16:27:58 +0000
committerJoe Carstairs <jcarstairs@scottlogic.com>2024-01-29 10:51:49 +0000
commiteac3fb00ef11c08a277dd8f9e2684b79f15193c2 (patch)
tree1560e57bb5872d698565dc502aa7659d89c830a4 /api/db/_getAuthenticator.ts
parentaf44cac699b0591eba908db3835c4e5792303095 (diff)
Authenticator db functions use Uint8Array type for id, public_key fields
Diffstat (limited to 'api/db/_getAuthenticator.ts')
-rw-r--r--api/db/_getAuthenticator.ts25
1 files changed, 18 insertions, 7 deletions
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<Pick<Authenticator, T>> {
+>(id: Authenticator['id'], fields: T[]): Promise<Pick<Authenticator, T>> {
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<Pick<Authenticator, T>> = {};
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<Authenticator, T>;
} 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}`);
}
}