SPB Git

Repository Public

setup-token.ts 3.1 KB 67 lines typescript

1/**2 * =============================================================================3 *                  SP BOUCHER | SOFTWARE & RESEARCH4 *             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project          : Sheet8 * File             : scripts/setup-token.ts9 * Module           : scripts10 * Repository       : https://git.spboucher.ai/sheet11 * Project Website  : https://sheet.spboucher.ai12 * Created          : 2026-11-1713 * Last Modified    : 2026-11-1714 *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: contact@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 "./_env";44import { sql } from "../src/lib/db";45import { env } from "../src/lib/env";46import { issueSetupToken } from "../src/lib/auth";47import { audit } from "../src/lib/audit";4849/**50 * Recovery from the server's shell, when every passkey and every recovery code is lost.51 *   tsx scripts/setup-token.ts              → add a passkey (existing ones kept)52 *   tsx scripts/setup-token.ts --recovery   → replace every passkey and sign out every device53 * The link is printed on the terminal only; run it interactively, never from a job whose output is logged.54 */55async function main() {56  if (!process.stdout.isTTY && !process.argv.includes("--force-print")) throw new Error("Run this interactively: the link must not end up in a log");57  const recovery = process.argv.includes("--recovery");58  const [u] = await sql<{ id: string }[]>`SELECT id FROM users WHERE disabled_at IS NULL ORDER BY created_at LIMIT 1`;59  if (!u) throw new Error("No administrator: run scripts/bootstrap.ts first");60  const token = await issueSetupToken(u.id, recovery ? "recovery" : "setup", 30);61  await audit({ userId: u.id, actor: "system", action: "recovery.cli", meta: { recovery } });62  console.log(`${env.appUrl}/admin/setup#${token}`);63  console.log(recovery ? "Valid 30 minutes, one use. Enrolling with it REVOKES the other passkeys and signs out every device." : "Valid 30 minutes, one use.");64  await sql.end();65}66main().catch((e) => { console.error(e.message); process.exit(1); });67
Files main
Menu

Theme