- 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>
57 lines
1.9 KiB
JavaScript
57 lines
1.9 KiB
JavaScript
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();
|
|
|
|
router.get("/session", requireAuth, (req, res) => {
|
|
return res.json({ session: serializeSession(req.group) });
|
|
});
|
|
|
|
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" });
|
|
}
|
|
|
|
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" });
|
|
});
|
|
|
|
router.post("/logout", requireAuth, (req, res) => {
|
|
destroySession(req.authToken);
|
|
return res.json({ ok: true });
|
|
});
|
|
|
|
export { router as authRouter };
|