Repository Public
1/**2 * =============================================================================3 * SP BOUCHER | SOFTWARE & RESEARCH4 * ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project : Chat SPBoucher AI8 * File : src/lib/auth/audit.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 { sql } from "../db";4445export interface AuditCtx { ip?: string | null; userAgent?: string | null; sessionPublicId?: string | null }4647/**48 * Minimal security audit trail: table auth_events + one structured log line.49 * Never pass assertions, challenges, key material, codes or conversation content in `detail`.50 */51export async function audit(event: string, ctx: AuditCtx = {}, detail?: Record<string, string | number | boolean | null>): Promise<void> {52 const ua = ctx.userAgent?.slice(0, 200) ?? null;53 console.log(JSON.stringify({ level: /fail|ratelimit|denied|burned/.test(event) ? "warn" : "info", evt: `auth.${event}`, ip: ctx.ip ?? null, session: ctx.sessionPublicId ?? null, ...(detail ?? {}) }));54 try { await sql`INSERT INTO auth_events (event, ip, user_agent, session_public_id, detail) VALUES (${event}, ${ctx.ip ?? null}, ${ua}, ${ctx.sessionPublicId ?? null}, ${detail ? sql.json(detail as never) : null})`; }55 catch (e) { console.error(JSON.stringify({ level: "error", evt: "auth.audit_write_failed", message: (e as Error).message })); }56}5758export { deviceLabel } from "./device";59