Repository Public
1/**2 * =============================================================================3 * SP BOUCHER | SOFTWARE & RESEARCH4 * ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project : Chat SPBoucher AI8 * File : src/lib/auth/config.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 */4243/**44 * Authentication configuration (single owner, passkeys only).45 * AUTH_RP_ID WebAuthn relying-party id — the registrable host, e.g. chat.spboucher.ai (default: host of APP_URL)46 * AUTH_ORIGINS comma-separated list of exact origins allowed to run ceremonies and to send mutations47 * (default: origin of APP_URL). Development: http://localhost:851048 * Passkeys are bound to the RP id: changing it invalidates every registered passkey.49 */50const appUrl = () => { try { return new URL(process.env.APP_URL || "https://chat.spboucher.ai"); } catch { return new URL("https://chat.spboucher.ai"); } };5152export const authConfig = {53 get rpID(): string { return (process.env.AUTH_RP_ID || appUrl().hostname).trim().toLowerCase(); },54 get origins(): string[] { const v = process.env.AUTH_ORIGINS; return (v ? v.split(",") : [appUrl().origin]).map(s => s.trim().replace(/\/$/, "")).filter(Boolean); },55 rpName: "Chat SPBoucher AI",56 get secure(): boolean { return this.origins.every(o => o.startsWith("https://")); },57 /** `__Host-` prefix: the browser only accepts it with Secure, Path=/ and no Domain (cannot be planted by a sibling host). */58 get sessionCookie(): string { return this.secure ? "__Host-csa_session" : "csa_session"; },59 get challengeCookie(): string { return this.secure ? "__Host-csa_ceremony" : "csa_ceremony"; },60 get setupCookie(): string { return this.secure ? "__Host-csa_setup" : "csa_setup"; },61 idleDays: Math.min(Number(process.env.SESSION_IDLE_DAYS ?? 30) || 30, 90), // sliding: unused for N days → expired62 absoluteDays: Math.min(Number(process.env.SESSION_ABSOLUTE_DAYS ?? 90) || 90, 180), // hard cap since the passkey login63 recentAuthSec: 10 * 60, // sensitive operations need a passkey verification this recent64 challengeTtlSec: 5 * 60,65 setupWindowSec: 10 * 60, // time to register a passkey after a bootstrap / recovery code was accepted66 bootstrapTtlMin: 15,67 /** True when the configuration can work for WebAuthn at all (rp id must be the origin host or a parent domain of it). */68 problems(): string[] {69 const out: string[] = [];70 for (const o of this.origins) {71 let u: URL; try { u = new URL(o); } catch { out.push(`AUTH_ORIGINS : « ${o} » n'est pas une origine valide`); continue; }72 const local = u.hostname === "localhost" || u.hostname === "127.0.0.1";73 if (u.protocol !== "https:" && !local) out.push(`${o} : WebAuthn exige HTTPS (sauf localhost)`);74 if (!(u.hostname === this.rpID || u.hostname.endsWith("." + this.rpID))) out.push(`${o} : l'hôte ne correspond pas à AUTH_RP_ID (${this.rpID})`);75 }76 if (!this.origins.length) out.push("aucune origine configurée (APP_URL ou AUTH_ORIGINS)");77 return out;78 },79};8081export const cookieBase = () => ({ httpOnly: true as const, secure: authConfig.secure, sameSite: "lax" as const, path: "/" });82