login.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. require_once __DIR__ . '/auth.php';
  3. if (ADMIN_LOGGED_IN) {
  4. header('Location: index.php');
  5. exit;
  6. }
  7. // Brute-force throttling.
  8. //
  9. // A hard lock is applied per client IP only. Locking the *username* instead
  10. // would let anyone shut the admin out of their own panel by guessing at it,
  11. // so repeated failures against a username buy a growing delay rather than a
  12. // closed door -- enough to make distributed guessing impractical.
  13. const LOGIN_MAX_PER_IP = 10; // failures per IP within the window
  14. const LOGIN_WINDOW_MIN = 15; // rolling window, minutes
  15. const LOGIN_MAX_DELAY = 5; // seconds, cap on the per-username slowdown
  16. function login_failures(PDO $pdo, string $column, string $value): int {
  17. $col = $column === 'ip' ? 'ip' : 'username'; // never interpolate raw input
  18. $stmt = $pdo->prepare(
  19. "SELECT COUNT(*) FROM login_attempts
  20. WHERE $col = ? AND attempted_at > (NOW() - INTERVAL " . LOGIN_WINDOW_MIN . " MINUTE)");
  21. $stmt->execute([$value]);
  22. return (int)$stmt->fetchColumn();
  23. }
  24. function record_login_failure(PDO $pdo, string $ip, string $user): void {
  25. $pdo->prepare("INSERT INTO login_attempts (ip, username) VALUES (?,?)")
  26. ->execute([$ip, substr($user, 0, 50)]);
  27. // Opportunistic cleanup so the table cannot grow without bound.
  28. $pdo->query("DELETE FROM login_attempts WHERE attempted_at < (NOW() - INTERVAL 1 DAY)");
  29. }
  30. $error = '';
  31. $ip = substr($_SERVER['REMOTE_ADDR'] ?? 'unknown', 0, 45);
  32. if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  33. require_csrf();
  34. $user = trim($_POST['username'] ?? '');
  35. $pass = $_POST['password'] ?? '';
  36. $pdo = get_pdo();
  37. if (login_failures($pdo, 'ip', $ip) >= LOGIN_MAX_PER_IP) {
  38. $error = 'Too many failed attempts. Try again in ' . LOGIN_WINDOW_MIN . ' minutes.';
  39. } elseif ($user === '' || $pass === '') {
  40. $error = 'Please enter username and password.';
  41. } else {
  42. $stmt = $pdo->prepare("SELECT password_hash FROM admin_users WHERE username = ?");
  43. $stmt->execute([$user]);
  44. $row = $stmt->fetch();
  45. if ($row && password_verify($pass, $row['password_hash'])) {
  46. session_regenerate_id(true);
  47. $_SESSION['admin_logged_in'] = true;
  48. $_SESSION['admin_user'] = $user;
  49. // A fresh token for the new session id.
  50. unset($_SESSION['csrf_token']);
  51. // Clear this client's failure history.
  52. $pdo->prepare("DELETE FROM login_attempts WHERE ip = ?")->execute([$ip]);
  53. header('Location: index.php');
  54. exit;
  55. }
  56. record_login_failure($pdo, $ip, $user);
  57. $error = 'Invalid username or password.';
  58. // Slow the next guess down in proportion to how many came before it.
  59. sleep(min(LOGIN_MAX_DELAY, 1 + login_failures($pdo, 'username', $user)));
  60. }
  61. }
  62. ?>
  63. <!DOCTYPE html>
  64. <html lang="en">
  65. <head>
  66. <meta charset="UTF-8">
  67. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  68. <title>PackIt Admin – Login</title>
  69. <link rel="preconnect" href="https://fonts.googleapis.com">
  70. <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
  71. <link href="https://fonts.googleapis.com/css2?family=Bebas+Neue&family=DM+Sans:wght@300;400;500;600&display=swap" rel="stylesheet">
  72. <style>
  73. :root{--bg:#0f1117;--surface:#181c27;--border:#2a2f3f;--accent:#e8c547;--accent2:#4fd1c5;--text:#e8eaf0;--muted:#7a8099;--danger:#e05555;--radius:10px;}
  74. *,*::before,*::after{box-sizing:border-box;margin:0;padding:0;}
  75. body{background:var(--bg);color:var(--text);font-family:'DM Sans',sans-serif;min-height:100vh;display:flex;align-items:center;justify-content:center;}
  76. body::before{content:'';position:fixed;inset:0;background:radial-gradient(ellipse 60% 60% at 50% 40%, rgba(232,197,71,.05) 0%, transparent 70%);pointer-events:none;}
  77. .login-box{background:var(--surface);border:1px solid var(--border);border-radius:var(--radius);padding:2.5rem 2rem;width:100%;max-width:360px;position:relative;}
  78. .login-logo{font-family:'Bebas Neue',sans-serif;font-size:2.8rem;color:var(--accent);letter-spacing:.05em;margin-bottom:.25rem;}
  79. .login-sub{font-size:.78rem;color:var(--muted);text-transform:uppercase;letter-spacing:.1em;margin-bottom:2rem;}
  80. label{display:block;font-size:.75rem;font-weight:600;color:var(--muted);text-transform:uppercase;letter-spacing:.08em;margin-bottom:.3rem;}
  81. input{display:block;width:100%;background:var(--bg);border:1.5px solid var(--border);color:var(--text);padding:.6rem .85rem;border-radius:var(--radius);font-family:'DM Sans',sans-serif;font-size:.9rem;outline:none;margin-bottom:1rem;transition:border-color .15s;}
  82. input:focus{border-color:var(--accent2);}
  83. .btn-login{width:100%;background:var(--accent);color:#0f1117;border:none;padding:.75rem;border-radius:var(--radius);font-family:'DM Sans',sans-serif;font-size:.95rem;font-weight:600;cursor:pointer;margin-top:.5rem;transition:background .15s;}
  84. .btn-login:hover{background:#f0d260;}
  85. .error-msg{background:rgba(224,85,85,.1);border:1px solid rgba(224,85,85,.3);color:var(--danger);padding:.65rem .9rem;border-radius:var(--radius);font-size:.85rem;margin-bottom:1rem;}
  86. </style>
  87. </head>
  88. <body>
  89. <div class="login-box">
  90. <div class="login-logo">PackIt</div>
  91. <div class="login-sub">Admin Access</div>
  92. <?php if ($error): ?>
  93. <div class="error-msg"><?= htmlspecialchars($error) ?></div>
  94. <?php endif; ?>
  95. <form method="POST">
  96. <?= csrf_field() ?>
  97. <label for="username">Username</label>
  98. <input type="text" id="username" name="username"
  99. value="<?= htmlspecialchars($_POST['username'] ?? '') ?>"
  100. autocomplete="username" required>
  101. <label for="password">Password</label>
  102. <input type="password" id="password" name="password"
  103. autocomplete="current-password" required>
  104. <button class="btn-login" type="submit">Sign in</button>
  105. </form>
  106. </div>
  107. </body>
  108. </html>