Files
FWS-DiscordBot/src/web/setup-state.js
T
nocodeandClaude Opus 5 e03b184d68 feat(web): add the HTTP layer and Discord-backed sessions
Visitors sign in with Discord and inherit the very same access levels as the
slash commands, so there is no second permission model to keep in sync.

Writes require a header a cross-site form cannot forge, on top of the
`SameSite=Lax` cookie, and a bootstrap token opens the configuration wizard
alone for the case where no Discord application exists yet.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-23 20:09:13 +02:00

33 lines
995 B
JavaScript

import { randomBytes, timingSafeEqual } from 'node:crypto';
import { env } from '../config/env.js';
/**
* Bootstrap access to the configuration wizard (M16-T09).
*
* Before `.env` exists there is no Discord application to log in with, so the
* wizard accepts a single shared token: either `WEB_SETUP_TOKEN`, or one
* generated for the lifetime of `npm run setup` and printed in the console.
*/
let runtimeToken = null;
export function generateRuntimeSetupToken() {
runtimeToken = randomBytes(24).toString('base64url');
return runtimeToken;
}
export function hasSetupToken() {
return Boolean(env.web.setupToken || runtimeToken);
}
export function isSetupTokenValid(candidate) {
const value = String(candidate ?? '');
if (value.length === 0) return false;
return [env.web.setupToken, runtimeToken].filter(Boolean).some((expected) => {
const a = Buffer.from(expected);
const b = Buffer.from(value);
return a.length === b.length && timingSafeEqual(a, b);
});
}