Davidson CI testing & AI refinement
This commit is contained in:
@@ -54,22 +54,45 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
|
||||
$ipIdentifier = 'ip:' . ($_SERVER['REMOTE_ADDR'] ?? 'unknown');
|
||||
$emailIdentifier = 'email:' . strtolower($email);
|
||||
$attemptIds = [];
|
||||
|
||||
if (
|
||||
loginAttemptCount($pdo, $ipIdentifier, LOGIN_ATTEMPT_WINDOW_SECONDS) >= MAX_LOGIN_ATTEMPTS_PER_IP
|
||||
|| loginAttemptCount($pdo, $emailIdentifier, LOGIN_ATTEMPT_WINDOW_SECONDS) >= MAX_LOGIN_ATTEMPTS_PER_EMAIL
|
||||
) {
|
||||
$errors['login'] = 'Too many login attempts. Please wait a while before trying again.';
|
||||
} else {
|
||||
// Check both limits and reserve attempt rows for both identifiers atomically, so
|
||||
// concurrent requests can't all read the same stale counts before any of them
|
||||
// commits. Reserving happens before password_verify() runs (rather than only on a
|
||||
// confirmed failure) so the check-then-record pair is a single fast transaction;
|
||||
// the reservation is released below if the login actually succeeds, so only failed
|
||||
// attempts count against the limit — matching the previous behavior.
|
||||
$pdo->exec('BEGIN IMMEDIATE');
|
||||
try {
|
||||
if (
|
||||
loginAttemptCount($pdo, $ipIdentifier, LOGIN_ATTEMPT_WINDOW_SECONDS) >= MAX_LOGIN_ATTEMPTS_PER_IP
|
||||
|| loginAttemptCount($pdo, $emailIdentifier, LOGIN_ATTEMPT_WINDOW_SECONDS) >= MAX_LOGIN_ATTEMPTS_PER_EMAIL
|
||||
) {
|
||||
$errors['login'] = 'Too many login attempts. Please wait a while before trying again.';
|
||||
} else {
|
||||
recordLoginAttempt($pdo, $ipIdentifier);
|
||||
$attemptIds[] = (int) $pdo->lastInsertId();
|
||||
recordLoginAttempt($pdo, $emailIdentifier);
|
||||
$attemptIds[] = (int) $pdo->lastInsertId();
|
||||
}
|
||||
$pdo->exec('COMMIT');
|
||||
} catch (Throwable $e) {
|
||||
$pdo->exec('ROLLBACK');
|
||||
throw $e;
|
||||
}
|
||||
|
||||
if (empty($errors)) {
|
||||
$stmt = $pdo->prepare('SELECT * FROM users WHERE email = :email');
|
||||
$stmt->execute(['email' => $email]);
|
||||
$user = $stmt->fetch(PDO::FETCH_ASSOC);
|
||||
|
||||
if (!$user || !password_verify($password, $user['password'])) {
|
||||
recordLoginAttempt($pdo, $ipIdentifier);
|
||||
recordLoginAttempt($pdo, $emailIdentifier);
|
||||
$errors['login'] = 'Invalid email or password.';
|
||||
} else {
|
||||
foreach ($attemptIds as $attemptId) {
|
||||
$pdo->prepare('DELETE FROM login_attempts WHERE id = :id')->execute(['id' => $attemptId]);
|
||||
}
|
||||
|
||||
start_app_session();
|
||||
session_regenerate_id(true);
|
||||
$_SESSION['user_id'] = $user['id'];
|
||||
|
||||
Reference in New Issue
Block a user