Let guild members log in to the jukebox with Discord

- Optional Discord OAuth2 login (identify scope), enabled by
  DISCORD_CLIENT_SECRET, WEB_PUBLIC_URL and SESSION_SECRET; restricted to
  members of the configured guild, checked at login.
- Stateless 30-day session in a signed HttpOnly SameSite=Lax cookie; the
  OAuth state is checked against its own short-lived cookie.
- The jukebox API accepts the key or the session; with the session,
  changes must come from the site's own origin. The member's server name
  becomes the uploader name.
- GET /auth/me tells the page whether the login is enabled and who is
  logged in.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Shinuwa 2026-11-29 12:17:48 +01:00
parent 9fb54798dd
commit 399e851178
11 changed files with 489 additions and 13 deletions

78
src/web/auth/session.js Normal file
View file

@ -0,0 +1,78 @@
import { createHmac, timingSafeEqual } from "node:crypto";
const SESSION_COOKIE = "palico_session";
const STATE_COOKIE = "palico_oauth_state";
const SESSION_DURATION = 30 * 24 * 60 * 60 * 1000;
function sign(payload, secret) {
return createHmac("sha256", secret).update(payload).digest("base64url");
}
// Jeton de session sans stockage côté serveur : « données.signature », signé par HMAC.
// Changer SESSION_SECRET déconnecte tout le monde.
function createSessionToken({ id, name }, secret, now = Date.now()) {
const payload = Buffer.from(
JSON.stringify({ sub: id, name, exp: now + SESSION_DURATION })
).toString("base64url");
return `${payload}.${sign(payload, secret)}`;
}
function readSessionToken(token, secret, now = Date.now()) {
if (typeof token !== "string" || !secret) return null;
const [payload, signature, extra] = token.split(".");
if (!payload || !signature || extra !== undefined) return null;
const expected = Buffer.from(sign(payload, secret));
const received = Buffer.from(signature);
if (expected.length !== received.length || !timingSafeEqual(expected, received)) {
return null;
}
try {
const data = JSON.parse(Buffer.from(payload, "base64url").toString("utf8"));
if (typeof data.sub !== "string" || typeof data.exp !== "number" || data.exp < now) {
return null;
}
return { id: data.sub, name: String(data.name || "").slice(0, 80) };
} catch {
return null;
}
}
function parseCookies(header = "") {
const cookies = {};
for (const part of header.split(";")) {
const index = part.indexOf("=");
if (index === -1) continue;
const name = part.slice(0, index).trim();
try {
cookies[name] = decodeURIComponent(part.slice(index + 1).trim());
} catch {
// Cookie mal formé : ignoré.
}
}
return cookies;
}
function serializeCookie(name, value, { maxAge, path = "/", secure = true } = {}) {
return [
`${name}=${encodeURIComponent(value)}`,
`Path=${path}`,
"HttpOnly",
"SameSite=Lax",
secure ? "Secure" : null,
maxAge !== undefined ? `Max-Age=${Math.floor(maxAge / 1000)}` : null,
]
.filter(Boolean)
.join("; ");
}
export {
SESSION_COOKIE,
SESSION_DURATION,
STATE_COOKIE,
createSessionToken,
parseCookies,
readSessionToken,
serializeCookie,
};