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

View file

@ -41,6 +41,29 @@ const notificationsApiKey = process.env.NOTIFICATIONS_API_KEY?.trim() || null;
// INVITE_LINK : lien publié par /invitation quand la commande ne reçoit pas de lien.
const inviteLink = process.env.INVITE_LINK?.trim() || null;
// Connexion avec Discord sur la page web (facultative, active si les trois sont définies) :
// DISCORD_CLIENT_SECRET (portail développeur, OAuth2), WEB_PUBLIC_URL (adresse publique du
// site, sans / final) et SESSION_SECRET (32 caractères au moins, signe les sessions).
const discordClientSecret = process.env.DISCORD_CLIENT_SECRET?.trim() || null;
const webPublicUrl = process.env.WEB_PUBLIC_URL?.trim().replace(/\/+$/, "") || null;
const sessionSecret = process.env.SESSION_SECRET?.trim() || null;
const discordOAuthEnabled = Boolean(discordClientSecret && webPublicUrl && sessionSecret);
if (discordOAuthEnabled) {
let publicUrl;
try {
publicUrl = new URL(webPublicUrl);
} catch {
throw new Error("WEB_PUBLIC_URL doit être une adresse complète (https://…)");
}
if (publicUrl.protocol !== "https:" && publicUrl.hostname !== "localhost") {
throw new Error("WEB_PUBLIC_URL doit être en https");
}
if (sessionSecret.length < 32) {
throw new Error("SESSION_SECRET doit contenir au moins 32 caractères");
}
}
if (notificationsApiKey && notificationsApiKey === process.env.WEB_API_KEY) {
throw new Error("NOTIFICATIONS_API_KEY doit être différente de WEB_API_KEY");
}
@ -54,6 +77,10 @@ const config = Object.freeze({
webTrustedProxies,
notificationsApiKey,
inviteLink,
discordOAuthEnabled,
discordClientSecret,
webPublicUrl,
sessionSecret,
applicationId: process.env.DISCORD_APPLICATION_ID,
token: process.env.DISCORD_TOKEN,
voiceChannelId: process.env.VOICE_CHANNEL_ID,