feat(auth): throttle logins, close sessions on password change, password policy
- 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:
parent
55be6bf034
commit
7d89a4e646
10 changed files with 280 additions and 16 deletions
46
src/services/loginThrottle.js
Normal file
46
src/services/loginThrottle.js
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
import { config } from "../config.js";
|
||||
|
||||
// Tentatives de connexion par adresse IP, sur une fenetre fixe ouverte a la premiere tentative.
|
||||
// Chaque tentative est comptee AVANT la verification du mot de passe, pour que des requetes
|
||||
// envoyees en parallele ne puissent pas depasser la limite; une connexion reussie remet a zero.
|
||||
const attempts = new Map();
|
||||
|
||||
function getEntry(key, now) {
|
||||
const entry = attempts.get(key);
|
||||
if (!entry) return null;
|
||||
if (entry.resetAt <= now) {
|
||||
attempts.delete(key);
|
||||
return null;
|
||||
}
|
||||
return entry;
|
||||
}
|
||||
|
||||
function getRetryAfterMs(key, now = Date.now()) {
|
||||
const entry = getEntry(key, now);
|
||||
if (!entry || entry.count < config.loginMaxAttempts) return 0;
|
||||
return entry.resetAt - now;
|
||||
}
|
||||
|
||||
function recordAttempt(key, now = Date.now()) {
|
||||
const entry = getEntry(key, now) || { count: 0, resetAt: now + config.loginWindowMs };
|
||||
entry.count += 1;
|
||||
attempts.set(key, entry);
|
||||
}
|
||||
|
||||
function clearAttempts(key) {
|
||||
attempts.delete(key);
|
||||
}
|
||||
|
||||
function pruneAttempts(now = Date.now()) {
|
||||
for (const [key, entry] of attempts) {
|
||||
if (entry.resetAt <= now) attempts.delete(key);
|
||||
}
|
||||
}
|
||||
|
||||
function resetLoginThrottle() {
|
||||
attempts.clear();
|
||||
}
|
||||
|
||||
setInterval(pruneAttempts, 10 * 60 * 1000).unref();
|
||||
|
||||
export { clearAttempts, getRetryAfterMs, recordAttempt, resetLoginThrottle };
|
||||
Loading…
Add table
Add a link
Reference in a new issue