Fixdd additonal issues identified by Davidson CI
This commit is contained in:
@@ -8,12 +8,26 @@ if (isset($_SESSION['username'])) {
|
||||
exit;
|
||||
}
|
||||
|
||||
const LOGIN_ATTEMPT_WINDOW_SECONDS = 900;
|
||||
const MAX_LOGIN_ATTEMPTS_PER_IP = 20;
|
||||
const MAX_LOGIN_ATTEMPTS_PER_EMAIL = 8;
|
||||
|
||||
function loginAttemptCount(PDO $pdo, string $identifier, int $windowSeconds): int
|
||||
{
|
||||
$stmt = $pdo->prepare("SELECT COUNT(*) FROM login_attempts WHERE identifier = :identifier AND created_at > datetime('now', :window)");
|
||||
$stmt->execute(['identifier' => $identifier, 'window' => '-' . $windowSeconds . ' seconds']);
|
||||
return (int) $stmt->fetchColumn();
|
||||
}
|
||||
|
||||
function recordLoginAttempt(PDO $pdo, string $identifier): void
|
||||
{
|
||||
$pdo->prepare('INSERT INTO login_attempts (identifier) VALUES (:identifier)')->execute(['identifier' => $identifier]);
|
||||
}
|
||||
|
||||
$errors = [];
|
||||
$email = '';
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
sleep(4);
|
||||
|
||||
$email = trim($_POST['email'] ?? '');
|
||||
$password = $_POST['password'] ?? '';
|
||||
|
||||
@@ -27,20 +41,39 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
|
||||
if (empty($errors)) {
|
||||
$pdo = getDbConnection();
|
||||
$stmt = $pdo->prepare('SELECT * FROM users WHERE email = :email');
|
||||
$stmt->execute(['email' => $email]);
|
||||
$user = $stmt->fetch(PDO::FETCH_ASSOC);
|
||||
$pdo->exec('CREATE TABLE IF NOT EXISTS login_attempts (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
identifier TEXT NOT NULL,
|
||||
created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP
|
||||
)');
|
||||
$pdo->exec('CREATE INDEX IF NOT EXISTS idx_login_attempts_identifier ON login_attempts (identifier)');
|
||||
|
||||
if (!$user || !password_verify($password, $user['password'])) {
|
||||
$errors['login'] = 'Invalid email or password.';
|
||||
$ipIdentifier = 'ip:' . ($_SERVER['REMOTE_ADDR'] ?? 'unknown');
|
||||
$emailIdentifier = 'email:' . strtolower($email);
|
||||
|
||||
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 {
|
||||
start_app_session();
|
||||
session_regenerate_id(true);
|
||||
$_SESSION['user_id'] = $user['id'];
|
||||
$_SESSION['username'] = $user['username'];
|
||||
$stmt = $pdo->prepare('SELECT * FROM users WHERE email = :email');
|
||||
$stmt->execute(['email' => $email]);
|
||||
$user = $stmt->fetch(PDO::FETCH_ASSOC);
|
||||
|
||||
header('Location: /');
|
||||
exit;
|
||||
if (!$user || !password_verify($password, $user['password'])) {
|
||||
recordLoginAttempt($pdo, $ipIdentifier);
|
||||
recordLoginAttempt($pdo, $emailIdentifier);
|
||||
$errors['login'] = 'Invalid email or password.';
|
||||
} else {
|
||||
start_app_session();
|
||||
session_regenerate_id(true);
|
||||
$_SESSION['user_id'] = $user['id'];
|
||||
$_SESSION['username'] = $user['username'];
|
||||
|
||||
header('Location: /');
|
||||
exit;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user