Repository Public
1/**2 * =============================================================================3 * SP BOUCHER | SOFTWARE & RESEARCH4 * ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project : Chat SPBoucher AI8 * File : src/lib/auth/current.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, headers } from "next/headers";45import { authConfig, cookieBase } from "./config";46import { readSession, sessionCookieOptions, type SessionRow } from "./session";4748/** Request-scoped helpers for route handlers and server components (the proxy and the CLI use session.ts directly). */49export async function currentSession(): Promise<SessionRow | null> {50 return readSession((await cookies()).get(authConfig.sessionCookie)?.value);51}52export async function sessionToken(): Promise<string | undefined> { return (await cookies()).get(authConfig.sessionCookie)?.value; }53export async function setSessionCookie(token: string): Promise<void> { (await cookies()).set(authConfig.sessionCookie, token, sessionCookieOptions()); }54export async function clearSessionCookie(): Promise<void> { (await cookies()).set(authConfig.sessionCookie, "", { ...cookieBase(), maxAge: 0 }); }5556export async function requestMeta(): Promise<{ ip: string; userAgent: string; origin: string | null }> {57 const h = await headers();58 return { ip: (h.get("x-forwarded-for") ?? "").split(",")[0].trim() || h.get("x-real-ip") || "local", userAgent: (h.get("user-agent") ?? "").slice(0, 300), origin: h.get("origin") };59}6061/**62 * CSRF defence for every state-changing request: the Origin header must be one of the configured origins.63 * Browsers always send Origin on cross-site POST/PUT/PATCH/DELETE; when it is absent we accept only an explicit64 * `Sec-Fetch-Site: same-origin`. (The session cookie is also SameSite=Lax and `__Host-` prefixed.)65 */66export async function assertSameOrigin(): Promise<void> {67 const h = await headers();68 const origin = h.get("origin");69 if (origin) { if (!authConfig.origins.includes(origin.replace(/\/$/, ""))) throw Object.assign(new Error("Origine refusée"), { status: 403, code: "bad_origin" }); return; }70 if (h.get("sec-fetch-site") === "same-origin") return;71 throw Object.assign(new Error("Origine absente"), { status: 403, code: "bad_origin" });72}73