import "server-only"; import { query, queryOne } from "@/lib/db"; const STATE_TTL_MS = 10 * 60 * 1000; export async function storeState( state: string, nonce: string, codeVerifier: string ): Promise { // Kein Cron nötig: abgelaufene Zeilen bei jedem neuen State gleich // mit aufräumen (wie bei webauthn_challenges). await query("delete from oidc_states where expires_at < now()"); await query( "insert into oidc_states (state, nonce, code_verifier, expires_at) values ($1, $2, $3, $4)", [state, nonce, codeVerifier, new Date(Date.now() + STATE_TTL_MS)] ); } /** Einmalig verwendbar: die Zeile wird beim Abruf sofort gelöscht. */ export async function consumeState( state: string ): Promise<{ nonce: string; codeVerifier: string } | null> { const row = await queryOne<{ nonce: string; code_verifier: string }>( `delete from oidc_states where state = $1 and expires_at > now() returning nonce, code_verifier`, [state] ); return row ? { nonce: row.nonce, codeVerifier: row.code_verifier } : null; }