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

@ -30,6 +30,25 @@ function createBearerAuth(getExpectedKey) {
const authMiddleware = createBearerAuth(() => config.webApiKey);
// API du jukebox : la clé (en-tête Authorization) ou une session Discord (cookie, voir
// sessionMiddleware). Avec le cookie, une requête qui modifie quelque chose doit venir du site
// lui-même (en-tête Origin), en plus de la protection SameSite du cookie.
function createJukeboxAuth({ publicUrl } = {}) {
const allowedOrigin = publicUrl ? new URL(publicUrl).origin : null;
return function jukeboxAuth(req, res, next) {
if (req.headers.authorization || !req.user) {
return authMiddleware(req, res, next);
}
const safeMethod = req.method === "GET" || req.method === "HEAD";
const origin = req.headers.origin;
if (!safeMethod && origin && origin !== allowedOrigin) {
return res.status(403).json({ error: "Origine refusée" });
}
return next();
};
}
const LOOPBACK_ADDRESSES = new Set(["127.0.0.1", "::1", "::ffff:127.0.0.1"]);
function isLoopbackAddress(address) {
@ -45,4 +64,4 @@ function localOnly(req, res, next) {
return res.status(403).json({ error: "Route réservée aux applications locales", code: "LOCAL_ONLY" });
}
export { authMiddleware, createBearerAuth, isLoopbackAddress, localOnly };
export { authMiddleware, createBearerAuth, createJukeboxAuth, isLoopbackAddress, localOnly };