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
- Throttle failed logins per account: an account with 5 or more failed password attempts in the last 15 minutes is locked. While it is locked, `LoginHandler::handle` answers `LoginResult::locked(retry_after)` without checking the password; the HTTP layer (unchanged) turns that into 429 with a `Retry-After` header.
- "The last 15 minutes" is a half-open window of 900 seconds on the injected clock: at instant `now`, a failure recorded at instant `t` counts while `now - t < 900 s`. A failure exactly 900 seconds old no longer counts. Instants are whole seconds of `std::chrono::steady_clock` (production floors `steady_clock::now()` to seconds); the clock is injected so tests can set it.
- When an account is locked, `retry_after` is the whole number of seconds until it unlocks, from 1 to 900: an attempt made exactly `retry_after` seconds later is no longer refused because of those failures. The login page counts down from it and re-enables the Sign in button at 0, and the mobile app retries once `Retry-After` has passed.
- Only a wrong password for an existing account counts as a failure. A refused (locked) attempt is not counted and does not extend the lock, and a successful login clears the account's failures. Unknown emails answer `invalid_credentials` as before and are not throttled.
- Unchanged: `AccountStore`, `SessionStore`, `LoginResult` and the HTTP layer. Ship unit tests for the throttle using the repo's `tests/check.hpp` helper.
Files touched
- src/auth/login_handler.hpp
- src/auth/login_handler.cpp
- src/auth/login_throttle.hpp
- src/auth/login_throttle.cpp
- tests/login_throttle_test.cpp