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

@ -5,10 +5,11 @@ import multer from "multer";
import path from "path";
import { config } from "../config.js";
import {
authMiddleware,
createBearerAuth,
createJukeboxAuth,
localOnly,
} from "./middlewares/auth.js";
import { authRouter, sessionMiddleware } from "./routes/auth.js";
import { eventsRouter } from "./routes/events.js";
import { libraryRouter } from "./routes/library.js";
import { notificationsRouter } from "./routes/notifications.js";
@ -43,15 +44,44 @@ function authFailureLimit() {
});
}
const UNKNOWN_MEMBER = 10007;
// Membre du serveur Discord configuré, ou null s'il n'en fait pas partie.
function guildMemberFinder(client) {
return async (userId) => {
const guild =
client.guilds?.cache.get(config.discordGuildId) ??
(await client.guilds.fetch(config.discordGuildId));
try {
return await guild.members.fetch(userId);
} catch (error) {
if (error?.code === UNKNOWN_MEMBER) return null;
throw error;
}
};
}
function createApp(
dependencies,
{ notificationsApiKey = config.notificationsApiKey } = {}
{
notificationsApiKey = config.notificationsApiKey,
oauth = {
enabled: config.discordOAuthEnabled,
clientId: config.applicationId,
clientSecret: config.discordClientSecret,
publicUrl: config.webPublicUrl,
sessionSecret: config.sessionSecret,
},
findMember = guildMemberFinder(dependencies.client),
fetchImpl = fetch,
} = {}
) {
const app = express();
app.disable("x-powered-by");
app.set("trust proxy", config.webTrustedProxies);
app.use(helmet());
app.use(express.json({ limit: "32kb" }));
app.use(sessionMiddleware(oauth));
// /healthz : le processus répond (sonde de vie). /readyz : le bot est aussi connecté à
// Discord, sinon 503 (à utiliser pour la supervision).
@ -75,10 +105,17 @@ function createApp(
notificationsRouter(dependencies)
);
app.use(
"/auth",
// /auth/me est appelée à chaque chargement de page : seule la connexion est limitée.
apiRateLimit({ windowMs: 15 * 60_000, limit: 60, skip: (req) => req.path === "/me" }),
authRouter({ oauth, findMember, fetchImpl })
);
const apiRouter = express.Router();
apiRouter.use(apiRateLimit({ windowMs: 60_000, limit: 120 }));
apiRouter.use(authFailureLimit());
apiRouter.use(authMiddleware);
apiRouter.use(createJukeboxAuth({ publicUrl: oauth.publicUrl }));
apiRouter.use(
"/uploads",
apiRateLimit({ windowMs: 15 * 60_000, limit: 10 }),