Repository Public
1/**2 * =============================================================================3 * SP BOUCHER | SOFTWARE & RESEARCH4 * ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project : Chat SPBoucher AI8 * File : src/lib/auth/transition.ts9 * Module : lib10 * Repository : https://git.spboucher.ai/chat-spboucher-ai11 * Project Website : https://chat.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 { createHmac, timingSafeEqual } from "node:crypto";44import { sql, getSetting, setSetting } from "../db";4546/**47 * TRANSITION 0.2 → 0.3 ONLY. Delete this file, LEGACY_COOKIE handling in src/proxy.ts and the ACCESS_* variables once the48 * first passkey is registered (docs/OPERATIONS.md « Migration vers les passkeys »).49 *50 * During an explicitly opened, short window the old access cookie (`csa_access` = HMAC(ACCESS_KEY)) is accepted for ONE51 * thing: registering the first passkey from a device the owner was already using. It never opens the app.52 * The window is open only when all of these hold:53 * - AUTH_TRANSITION_UNTIL is set to a future instant (ISO 8601) in the server environment,54 * - migration 003 was applied less than 7 days ago (the window cannot be reopened later by editing the variable),55 * - the transition was not closed (it closes itself as soon as one passkey has been registered through it),56 * - no passkey exists yet.57 */58export const LEGACY_COOKIE = "csa_access";59const MAX_WINDOW_DAYS = 7;6061export async function transitionOpen(): Promise<boolean> {62 const until = Date.parse(process.env.AUTH_TRANSITION_UNTIL ?? "");63 const key = process.env.ACCESS_KEY ?? "";64 if (!Number.isFinite(until) || until <= Date.now() || key.length < 32) return false;65 const st = await getSetting<{ transitionClosedAt?: string }>("auth", {});66 if (st.transitionClosedAt) return false;67 const r = await sql<{ applied_at: Date; n: number }[]>`SELECT (SELECT applied_at FROM schema_migrations WHERE name = '003_passkeys_sessions.sql') AS applied_at, (SELECT count(*)::int FROM webauthn_credentials) AS n`;68 if (!r[0]?.applied_at || r[0].n > 0) return false;69 return Date.now() - r[0].applied_at.getTime() < MAX_WINDOW_DAYS * 86_400_000;70}7172export function legacyCookieValid(value: string | undefined | null): boolean {73 const key = process.env.ACCESS_KEY ?? "";74 if (!value || key.length < 32) return false;75 const want = Buffer.from(createHmac("sha256", key).update("csa-access-v1").digest("base64url")); const got = Buffer.from(value);76 return want.length === got.length && timingSafeEqual(want, got);77}7879export async function closeTransition(reason: string): Promise<void> {80 const st = await getSetting<Record<string, unknown>>("auth", {});81 if (!st.transitionClosedAt) await setSetting("auth", { ...st, transitionClosedAt: new Date().toISOString(), transitionClosedBy: reason });82}83