Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions apps/web/lib/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,23 @@ import 'server-only';
import { cookies } from 'next/headers';

const COOKIE = 'cp_session';
const SESSION_SECRET = process.env.SESSION_SECRET;
if (!SESSION_SECRET) {
throw new Error('SESSION_SECRET environment variable is required');
}
const enc = new TextEncoder();

// Resolve the secret lazily (at request time) rather than at module import.
// Throwing at import breaks `next build` page-data collection, which imports
// route modules in an environment without runtime secrets. This still enforces
// #20's intent: no insecure fallback, and requests fail if the secret is unset.
function getSessionSecret(): string {
const secret = process.env.SESSION_SECRET;
if (!secret) {
throw new Error('SESSION_SECRET environment variable is required');
}
return secret;
}

async function hmac(data: string): Promise<string> {
const key = await crypto.subtle.importKey(
'raw', enc.encode(SESSION_SECRET),
'raw', enc.encode(getSessionSecret()),
{ name: 'HMAC', hash: 'SHA-256' }, false, ['sign']
);
const sig = await crypto.subtle.sign('HMAC', key, enc.encode(data));
Expand Down
Loading