Repository Public
1/**2 * =============================================================================3 * SP BOUCHER | SOFTWARE & RESEARCH4 * ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project : Chat SPBoucher AI8 * File : src/lib/auth/http.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 "server-only";44import { cookies } from "next/headers";45import { NextResponse } from "next/server";46import { authConfig, cookieBase } from "./config";47import { hitAll } from "./ratelimit";48import { audit } from "./audit";49import { requestMeta } from "./current";5051/** Shared plumbing of the /api/auth/* handlers. */52export async function setCeremony(id: string) { (await cookies()).set(authConfig.challengeCookie, id, { ...cookieBase(), sameSite: "strict", maxAge: authConfig.challengeTtlSec }); }53export async function takeCeremony(): Promise<string | undefined> { const c = await cookies(); const v = c.get(authConfig.challengeCookie)?.value; c.set(authConfig.challengeCookie, "", { ...cookieBase(), sameSite: "strict", maxAge: 0 }); return v; }54export async function setSetupCookie(token: string) { (await cookies()).set(authConfig.setupCookie, token, { ...cookieBase(), sameSite: "strict", maxAge: authConfig.setupWindowSec }); }55export async function getSetupCookie() { return (await cookies()).get(authConfig.setupCookie)?.value; }56export async function clearSetupCookie() { (await cookies()).set(authConfig.setupCookie, "", { ...cookieBase(), sameSite: "strict", maxAge: 0 }); }5758/** Per-IP and global limits for unauthenticated auth endpoints. Returns a 429 response when exceeded. */59export async function throttle(kind: "ceremony" | "code"): Promise<NextResponse | null> {60 const { ip, userAgent } = await requestMeta();61 const rules = kind === "code"62 ? [{ bucket: `code:ip:${ip}`, limit: 5, windowSec: 900 }, { bucket: "code:all", limit: 30, windowSec: 3600 }]63 : [{ bucket: `auth:ip:${ip}`, limit: 20, windowSec: 60 }, { bucket: "auth:all", limit: 120, windowSec: 60 }];64 const r = await hitAll(rules);65 if (!r.limited) return null;66 await audit("ratelimit", { ip, userAgent }, { kind });67 return NextResponse.json({ error: "Trop de tentatives. Réessaie plus tard.", code: "rate_limited" }, { status: 429, headers: { "Retry-After": String(r.retryAfterSec) } });68}6970/** Same body and status for every failed verification: no hint about what exists or what went wrong. */71export const neutralFailure = () => NextResponse.json({ error: "Vérification refusée.", code: "auth_failed" }, { status: 401 });72