feat(identity): PostgresOtpStore — atomic consume closes #89 (#120 slice 1, ADR-011) #122

Merged
amiryahaya merged 1 commit from feat/120-slice1-postgres-otp-store into main 2026-11-08 14:23:55 +01:00
Owner

#120 slice 1 / ADR-011 / #89 re-scoped: the OTP store moves into the database, and the #89 production race ceases to exist rather than being patched.

The atomicity design (why this beats the planned INCR/Lua hardening): every mutation is a single-statement atomic operation against the new otp_codes table:

  • success consume = one conditional DELETE … WHERE Key/Code/Attempts < max/ExpiresAt > now — delete IS the consume; two concurrent correct codes can never both pass
  • attempt bump = one conditional UPDATE … SET attempts = attempts + 1 WHERE Attempts < max — the counter can never walk past the ceiling and can never lose a bump between read and write
  • lock burn = one conditional DELETE (Locked is terminal); expired rows swept on read

The SQL is provider-neutral — the SQLite test harness runs the identical statements production Postgres will (ADR-011's no-dual-store-test-surface principle).

Changes: OtpCode entity (string PK = store key, Data.Entities) + AddOtpCodes migration; IOtpStore now always PostgresOtpStore (scoped — the redis-conditional DI branch is gone); RedisOtpStore deleted; InMemoryOtpStore kept but unregistered (barrier-race harness for AuthConsistencyTests).

Tests: 7 new (PostgresOtpStoreTests): single-use consume, bump-to-max-then-lock-and-burn with the correct code verified dead after burn, attempts-never-exceed-max over 12 wrong codes, expired rejection regardless of code, regenerate-overwrite resets attempts, GetAsync expiry visibility, and an end-to-end OtpService verify through DI asserting the Postgres store is what's wired. The full existing OTP/registration suite doubles as the regression net for the swap.

Suite 939/0/39 (Release+Debug), coverage gate 87.57%. First slice of #120; closes #89.

**#120 slice 1 / ADR-011 / #89 re-scoped:** the OTP store moves into the database, and the #89 production race ceases to exist rather than being patched. **The atomicity design** (why this beats the planned INCR/Lua hardening): every mutation is a single-statement atomic operation against the new `otp_codes` table: - success consume = one conditional `DELETE … WHERE Key/Code/Attempts < max/ExpiresAt > now` — delete IS the consume; two concurrent correct codes can never both pass - attempt bump = one conditional `UPDATE … SET attempts = attempts + 1 WHERE Attempts < max` — the counter can never walk past the ceiling and can never lose a bump between read and write - lock burn = one conditional DELETE (Locked is terminal); expired rows swept on read The SQL is provider-neutral — the SQLite test harness runs the **identical statements** production Postgres will (ADR-011's no-dual-store-test-surface principle). **Changes:** OtpCode entity (string PK = store key, Data.Entities) + `AddOtpCodes` migration; `IOtpStore` now always `PostgresOtpStore` (scoped — the redis-conditional DI branch is gone); `RedisOtpStore` deleted; `InMemoryOtpStore` kept but unregistered (barrier-race harness for AuthConsistencyTests). **Tests:** 7 new (PostgresOtpStoreTests): single-use consume, bump-to-max-then-lock-and-burn with the correct code verified dead after burn, attempts-never-exceed-max over 12 wrong codes, expired rejection regardless of code, regenerate-overwrite resets attempts, GetAsync expiry visibility, and an end-to-end OtpService verify through DI asserting the Postgres store is what's wired. The full existing OTP/registration suite doubles as the regression net for the swap. Suite 939/0/39 (Release+Debug), coverage gate 87.57%. First slice of #120; closes #89.
feat(identity): PostgresOtpStore — atomic consume closes #89 (#120 slice 1, ADR-011)
All checks were successful
Security CI / security-scan (pull_request) Successful in 3m2s
API CI / build-and-test (pull_request) Successful in 4m30s
5ea7b4310c
Every OTP state mutation is a single-statement atomic operation against
the new otp_codes table:
- success consume = one conditional DELETE (delete IS the consume) — two
  concurrent correct codes can never both pass
- attempt bump = one conditional UPDATE ... WHERE Attempts < max — the
  counter can never walk past the ceiling and can never lose a bump
- lock burn = one conditional DELETE; expired rows swept on read

The SQL is provider-neutral: the SQLite test harness runs the identical
statements production Postgres will — no dual-store test surface.

- OtpCode entity (string PK = store key) + AddOtpCodes migration
- DI: IOtpStore is now always PostgresOtpStore (scoped); the
  redis-conditional branch is gone, RedisOtpStore deleted
- InMemoryOtpStore kept but unregistered — it is the barrier-race harness
  in AuthConsistencyTests (in-process gate, no DB)
- 7 new tests: single-use consume, bump-to-max-then-lock-and-burn,
  attempts-never-exceed-max, expired rejection, overwrite-resets-attempts,
  GetAsync expiry visibility, end-to-end OtpService verify through DI

Suite 939/0/39 Release+Debug, coverage 87.57%.
Sign in to join this conversation.
No description provided.