feat(auth): throttle logins, close sessions on password change, password policy
All checks were successful
Deploy Gameservers Control / check (push) Successful in 6s
Deploy Gameservers Control / deploy (push) Has been skipped

- limit login attempts per IP (5 per 15 min by default, counted before the
  password check so parallel requests cannot exceed it), 429 + Retry-After,
  failed logins logged with the client IP
- TRUSTED_PROXIES env to honour X-Forwarded-For from the reverse proxy only
- changing or regenerating a group password closes that group's other
  sessions; deleting a group closes all of its sessions
- new group passwords: 12 to 256 characters and not shared with another
  group (login opens the first matching group)
- PATCH /groups/:id/password no longer echoes the password
- purge expired sessions every 10 minutes

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Shinuwa 2026-12-02 12:34:11 +01:00
parent 55be6bf034
commit 7d89a4e646
10 changed files with 280 additions and 16 deletions

View file

@ -1,8 +1,10 @@
import express from "express";
import { config } from "../config.js";
import { serializeSession } from "../services/permissions.js";
import { readGroups } from "../services/storage.js";
import { createSession, destroySession } from "../services/sessions.js";
import { verifyPassword } from "../services/crypto.js";
import { clearAttempts, getRetryAfterMs, recordAttempt } from "../services/loginThrottle.js";
import { requireAuth } from "../middlewares/auth.js";
const router = express.Router();
@ -12,23 +14,38 @@ router.get("/session", requireAuth, (req, res) => {
});
router.post("/login", async (req, res) => {
const clientKey = req.ip || "unknown";
const retryAfterMs = getRetryAfterMs(clientKey);
if (retryAfterMs > 0) {
res.set("Retry-After", String(Math.ceil(retryAfterMs / 1000)));
return res
.status(429)
.json({ error: "Trop de tentatives de connexion, reessayez dans quelques minutes" });
}
const password = String(req.body?.password || "");
if (!password) {
return res.status(400).json({ error: "Mot de passe requis" });
}
const groups = readGroups();
for (const group of groups) {
if (await verifyPassword(password, group.passwordHash)) {
const session = createSession(group);
return res.json({
token: session.token,
expiresAt: new Date(session.expiresAt).toISOString(),
session: serializeSession(group),
});
recordAttempt(clientKey);
if (password.length <= config.passwordMaxLength) {
const groups = readGroups();
for (const group of groups) {
if (await verifyPassword(password, group.passwordHash)) {
clearAttempts(clientKey);
const session = createSession(group);
return res.json({
token: session.token,
expiresAt: new Date(session.expiresAt).toISOString(),
session: serializeSession(group),
});
}
}
}
console.warn(`[auth] Failed login from ${clientKey}`);
return res.status(401).json({ error: "Mot de passe invalide" });
});