Lock accounts after repeated failed logins

C++ · C++ · intermediate · modification

Nothing stops a script from guessing passwords against one account as fast as the login endpoint answers. This PR adds `auth::LoginThrottle` (`src/auth/login_throttle.hpp`, `src/auth/login_throttle.cpp`, both new): per account it keeps the instants of recent failed attempts in a `std::deque`, drops the ones that have left the 15-minute window, and locks the account at 5, reporting how long until the lock lifts. The clock is a `std::function` returning whole seconds of `steady_clock`, so tests can move time by hand. `LoginHandler` (`src/auth/login_handler.hpp`, `src/auth/login_handler.cpp`) owns a throttle: after looking up the account it asks the throttle first and answers `locked` with the wait, records a failure on a wrong password and clears the account's failures on success. The handler's constructor gains a clock parameter defaulting to `LoginThrottle::steady_now`, so `main` is unchanged. `tests/login_throttle_test.cpp` (new) adds unit tests that drive `LoginThrottle` with a fake clock.

C++20, standard library only, built with CMake (unchanged: it builds `src/auth/*.cpp` into the server and every `tests/*_test.cpp` together with `tests/check_main.cpp` into the test runner) and compiled in CI by both Apple clang 17 and GCC 13, with `-Wall -Wextra -Wconversion -Werror`. The auth service is one process that handles one login request at a time on its main thread (password verification included), so `LoginHandler` and its throttle are used only on that thread and need no locking; throttle state lives in memory and resets on restart (accepted). The injected clock never goes backwards. Existing and unchanged: `auth::AccountStore` (`src/auth/accounts.hpp`) with `find_by_email` (returns `std::optional<Account>`) and `verify_password`; `AccountId` is a `std::uint64_t` and there are at most 50,000 accounts, so the throttle's map is bounded by the account count. `auth::SessionStore::create(AccountId)` returns a session token. `auth::LoginResult` (`src/auth/login_result.hpp`) already has the `locked` status and `retry_after` field, and the HTTP layer already maps it to 429 with `Retry-After: <retry_after>`; nothing returned `locked` before this PR. Accepted by product: anyone can lock an account for up to 15 minutes by sending 5 wrong passwords, and a lock reveals that an email has an account (signup reveals it too); unknown and wrong-password logins already take the same time. `tests/check.hpp` is the repo's tiny test helper: `TEST(name) { ... }` registers a test, `CHECK(expr)` and `CHECK_EQ(a, b)` record a failure with file and line, and the `main` in `tests/check_main.cpp` runs every registered test and exits non-zero if any check failed.

Requirements

Files touched

Review this PR

C++ practice