fix(api): count failed sign-ins from zero again after the 15-minute window [T-2538]
What was wrong
Without Redis (the default: api/src/redis/redis.module.ts:15-17 returns no client when REDIS_HOST or REDIS_PORT is unset), the failed sign-in count is the loginAttempts column of the user (api/src/services/user.service.ts:379-386).
user.service.ts:353-358: once 15 minutes have passed sincelastLogin,authenticatesets the localloginAttemptsvariable to 0, but the stored count stays as it was.user.service.ts:423-431(incrementLoginAttempts) then adds 1 to the stored count, and moveslastLoginto now.- The stored count only goes back to 0 after a successful password sign-in (
resetLoginAttempts, line 433). The default maximum is 3 (api/src/config.ts:162).
So two wrong passwords yesterday leave loginAttempts = 2; one wrong password today stores 3 and moves lastLogin to now, and the next attempt gets 429 for 15 minutes, even with the right password. Users who sign in through Ever ID never clear the count.
What changed
UserService.authenticate: when the window has passed and Redis is not used, it also clears the stored count, with a conditional update:loginAttempts = 0for this user only whilelastLoginis still older than the window or null. A failed attempt made meanwhile has already movedlastLoginto now, so its count is not undone.incrementLoginAttemptskeeps its atomic increment, so parallel failed attempts all still count. The Redis path is unchanged.docs/changelog.md: a line undernext.
Tests (fail before, pass after)
- New
api/src/services/user.service.spec.ts: an in-memory User repository and a login-attempts storage whosegetRedisClient()returns null; the user starts withloginAttempts = 5andlastLogin20 minutes ago.- A wrong password gives 401 and leaves the stored count at 1. On develop: 6.
- The right password then returns the user. On develop:
TooManyRequestsException. - A failed attempt stored after the user was read keeps counting (2 + 1 = 3). It fails (1) if the clear is made unconditional.
- Within the window the account still locks (429, count 4).
- On develop: 2 failed, 2 passed. With the fix: 4 passed.
- New case in
api/test/auth.e2e-spec.ts: 2 wrong passwords,lastLoginset 20 minutes back through the User repository, 1 wrong password, then the right one. On develop:expected 200 "OK", got 429 "Too Many Requests". With the fix: 200, and the auth e2e file passes 18/18.
Checks run
- e2e suite as CI runs it (
yarn jest --config ./test/jest-e2e.json --runInBand --forceExitinapi, SQLite): 21/21 suites, 189/189 tests passed - Unit suite (
jest --maxWorkers=2inapi): 171/172 passed. The one failure,src/formatters/properties.spec.ts("should export properties files"), happens only on Windows and is not related to this change: thepropertieslibrary ends lines with CRLF whenprocess.platform === "win32", and the fixture uses LF. CircleCI runs on Linux yarn --cwd api lint(tslint): passedyarn --cwd api build(tsc): passedyarn check-fmt(prettier --check "**/*.ts"at the root, as CircleCI runs it): passed
Fleet task T-2538; authored by the lead lane while the fleet is paused by its daily cost ceiling.
š¤ Generated with Claude Code
Summary by cubic
Fixes failed sign-in counting without Redis so the counter restarts from zero once 15 minutes have passed since the last attempt, instead of keeping the old stored count until a successful password sign-in. Without this, one wrong password days after a couple of earlier failures could reach the maximum of 3 and lock the account for 15 minutes, even with the right password.
When the window has passed and Redis is not configured, authenticate now also clears the stored loginAttempts count on the user, using a conditional update so the reset only applies while lastLogin is still older than the window. That way a concurrent failed attempt that already moved lastLogin keeps its count. The Redis path is unchanged.
- Adds a unit spec with an in-memory user repository and a no-Redis storage, plus an e2e case covering two wrong passwords,
lastLoginmoved back 20 minutes, one more wrong password, and the right password succeeding.