SPB Git

Repository Public

enroll.ts 3.5 KB 74 lines typescript

1/**2 * =============================================================================3 *                  SP BOUCHER | SOFTWARE & RESEARCH4 *             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project          : ORIX App Universe8 * File             : src/lib/auth/enroll.ts9 * Module           : lib10 * Repository       : https://git.spboucher.ai/orix-spboucher-ai11 * Project Website  : https://www.orixlab.dev12 * Created          : 2026-11-2113 * Last Modified    : 2026-11-2114 *15 * Author           : Simon-Pierre Boucher16 * Position         : Professor of Finance17 * Institution      : Université du Québec en Outaouais (UQO)18 * Organization     : SP Boucher19 * Website          : https://spboucher.ai20 * Git              : https://git.spboucher.ai21 *22 * Preferred Contact: support@spboucher.ai23 * Development      : dev@spboucher.ai24 * Support          : support@spboucher.ai25 * Security/Admin   : admin@spboucher.ai26 *27 * Copyright (c) 202628 * Simon-Pierre Boucher29 * All rights reserved where applicable. This file is governed by the30 * applicable project license, copyright notices and relevant law.31 * Existing third-party rights and notices remain unaffected.32 *33 * License          : None specified (no license file in this repository)34 *35 * Do not embed credentials, private keys, access tokens,36 * certificates or confidential personal information.37 *38 * =============================================================================39 * END OF SP BOUCHER SOURCE HEADER40 * =============================================================================41 */4243import { db, now, tx } from "../database/db";44import { audit } from "../audit";45import { randomToken, sha256 } from "../security/crypto";4647/** Enrolment tokens live apart from the WebAuthn ceremonies so that shell scripts can issue them without Next.js. */48export const OWNER = "owner";4950// ── Enrolment tokens (issued from the server's shell only) ──────────────────────────────────────────5152export type TokenPurpose = "setup" | "recovery";5354/** Issues a single-use enrolment token; only its SHA-256 is stored and the raw value is returned once. */55export function issueEnrollToken(purpose: TokenPurpose, ttlMinutes = 30): { raw: string; expiresAt: string } {56  const raw = randomToken(32), expiresAt = new Date(Date.now() + ttlMinutes * 60_000).toISOString();57  tx(() => {58    db().prepare("INSERT OR IGNORE INTO admins (id, name, created_at) VALUES (?, ?, ?)").run(OWNER, "Owner", now());59    db().prepare("UPDATE enroll_tokens SET used_at = ? WHERE used_at IS NULL").run(now()); // one live token at a time60    db().prepare("INSERT INTO enroll_tokens (token_hash, purpose, created_at, expires_at) VALUES (?, ?, ?, ?)").run(sha256(raw), purpose, now(), expiresAt);61  });62  audit(`enroll_token.issued.${purpose}`);63  return { raw, expiresAt };64}6566export type TokenRow = { token_hash: string; purpose: TokenPurpose };67export function validToken(raw: string | undefined): TokenRow | null {68  if (!raw) return null;69  return (db().prepare("SELECT token_hash, purpose FROM enroll_tokens WHERE token_hash = ? AND used_at IS NULL AND expires_at > ?").get(sha256(raw), now()) as TokenRow | undefined) ?? null;70}71export const enrollTokenInfo = (raw: string | undefined) => { const t = validToken(raw); return t ? { purpose: t.purpose } : null; };72export const credentialCount = () => (db().prepare("SELECT count(*) AS n FROM credentials").get() as { n: number }).n;7374
Files main
Menu

Theme