feat(identity): PostgresSessionStore — sessions + challenges in the database (#120 slice 2, ADR-011) #124

Merged
amiryahaya merged 2 commits from feat/120-slice2-postgres-session-store into main 2026-11-08 16:33:11 +01:00
Owner

#120 slice 2 / ADR-011: ISessionStore resolves to a Drive-side PostgresSessionStore — the SDK's Redis session store is no longer wired anywhere, and the redis-conditional DI branch is gone entirely.

Design:

  • sessions table keyed by SHA-256 token hash — the raw bearer token NEVER touches the DB (same discipline as ApiKey.SecretHash). Sliding 60-min TTL (LastAccessedAt touched on access — semantics identical to the SDK stores), per-session device fingerprint (IDA-06), MaxSessions cap, InvalidateSessionsForAccount, mfa-pending sessions are ordinary sessions with an mfa: value.
  • session_challenges table — consume-once, keyed did:purpose, ChallengeTtl — replacing the SDK stores' challenge maps.
  • Read paths are AsNoTracking with set-based writes: GetSession runs once per authenticated request and must not pay change-tracking costs. (This also fixed a real test-harness lesson: a tracked entity from the inserting context shadowed an aged DB row via identity resolution — the set-based shape removes that class of bug in prod too.)
  • SessionCleanupService (1-min sweep) replaces the SDK GC timer; expiry is also enforced at read time, so the sweep is hygiene, not correctness.
  • The SDK InMemorySessionStore remains ONLY as ISseNotificationBus backing the frozen wallet pairing flow (single-instance; ADR-011 revisit triggers cover scaling it) + hosted service for its SSE GC.

Scope note: RedisSessionStoreIntegrationTests (the testcontainers class — source of the CI 60s-container hangs) is removed in this slice rather than slice 4, since the SDK Redis store is no longer referenced anywhere; noted on #120. RedisHealthCheck.cs file + compose service + package ref cleanup remain slice 4.

Tests: 14 new (PostgresSessionStoreTests): round-trip, value-not-token, mfa-value semantics, sliding-touch, expiry sweep, device fingerprint, invalidate-for-account with bystander, hashed-at-rest invariant, active counts, challenge consume-once/expiry/purpose-independence, DI wiring assertion, CreateSessionDirect seed path. The full auth suite (bootstrap, OIDC flows, admin MFA gate, logout, device revocation) doubles as the end-to-end regression for the swap.

Suite 938/0/39 (Release+Debug; net −6 removed Redis-container tests), coverage gate 87.39%. Second slice of #120.

**#120 slice 2 / ADR-011:** ISessionStore resolves to a Drive-side PostgresSessionStore — the SDK's Redis session store is no longer wired anywhere, and the redis-conditional DI branch is gone entirely. **Design:** - **sessions table** keyed by SHA-256 token hash — the raw bearer token NEVER touches the DB (same discipline as ApiKey.SecretHash). Sliding 60-min TTL (LastAccessedAt touched on access — semantics identical to the SDK stores), per-session device fingerprint (IDA-06), MaxSessions cap, InvalidateSessionsForAccount, mfa-pending sessions are ordinary sessions with an mfa: value. - **session_challenges table** — consume-once, keyed did:purpose, ChallengeTtl — replacing the SDK stores' challenge maps. - **Read paths are AsNoTracking with set-based writes**: GetSession runs once per authenticated request and must not pay change-tracking costs. (This also fixed a real test-harness lesson: a tracked entity from the inserting context shadowed an aged DB row via identity resolution — the set-based shape removes that class of bug in prod too.) - **SessionCleanupService** (1-min sweep) replaces the SDK GC timer; expiry is also enforced at read time, so the sweep is hygiene, not correctness. - The SDK InMemorySessionStore remains ONLY as ISseNotificationBus backing the frozen wallet pairing flow (single-instance; ADR-011 revisit triggers cover scaling it) + hosted service for its SSE GC. **Scope note:** RedisSessionStoreIntegrationTests (the testcontainers class — source of the CI 60s-container hangs) is removed in this slice rather than slice 4, since the SDK Redis store is no longer referenced anywhere; noted on #120. RedisHealthCheck.cs file + compose service + package ref cleanup remain slice 4. **Tests:** 14 new (PostgresSessionStoreTests): round-trip, value-not-token, mfa-value semantics, sliding-touch, expiry sweep, device fingerprint, invalidate-for-account with bystander, hashed-at-rest invariant, active counts, challenge consume-once/expiry/purpose-independence, DI wiring assertion, CreateSessionDirect seed path. The full auth suite (bootstrap, OIDC flows, admin MFA gate, logout, device revocation) doubles as the end-to-end regression for the swap. Suite 938/0/39 (Release+Debug; net −6 removed Redis-container tests), coverage gate 87.39%. Second slice of #120.
docs: ADR-012 step-up trusts IdP AMR/acr; ADR-003 client-surface mechanics
All checks were successful
Security CI / security-scan (pull_request) Successful in 2m35s
3ee403ba05
Passkey-pivot documentation from the 2026-11-08 discussion:
- ADR-012 (new): IdP-asserted MFA factors (amr/acr/auth_time in the
  validated id_token) satisfy admin step-up (IDA-05); the current
  TotpEnabled-only gate would wrongly demand TOTP setup from an admin who
  just completed a realm-enforced passkey ceremony. Drive-side TOTP
  survives only as legacy wallet-admin gate, bootstrap break-glass (#91),
  trustee-approval step-up (KEY-17). Code impact listed (OidcTokenValidator
  amr parsing, OidcVerify/Authenticate gating order, FakeIdpServer amr
  tokens) — lands with task 2.2.
- ADR-003 refinement: per-platform client mechanics — web native, mobile
  browser-sheet with daily biometric unlock, desktop system-browser only
  (never WebAuthn in the embedded webview: WKWebView lacks prompts,
  WebView2 inconsistent); passkeys never touch the key hierarchy.
- SRS IDA-05: acceptance criteria extended for IdP-asserted factors.
- Dev plan task 2.2: ADR-012 leg added.
feat(identity): PostgresSessionStore — sessions + challenges in the database (#120 slice 2, ADR-011)
All checks were successful
Security CI / security-scan (pull_request) Successful in 3m9s
API CI / build-and-test (pull_request) Successful in 5m6s
cf82ede945
The SDK's ISessionStore now resolves to PostgresSessionStore: a sessions
table (SHA-256 token hash only — the DB never holds bearer material, same
discipline as ApiKey.SecretHash) and a session_challenges table. Semantics
match the SDK stores they replace: sliding 60-min TTL touched on access,
per-session device fingerprint (IDA-06), consume-once challenges keyed
did:purpose, MaxSessions cap, InvalidateSessionsForAccount. Read paths are
AsNoTracking with set-based writes (GetSession is the hot path — once per
authenticated request — and must not pay change-tracking costs).

- UserSession + SessionChallenge entities, AddSessions migration
- SessionCleanupService (1-min sweep) replaces the SDK stores' GC timer —
  row expiry is also enforced at read time, so the sweep is hygiene
- DI: the redis-conditional branch is gone entirely (ConnectionMultiplexer,
  AddStackExchangeRedisCache, RedisSessionStore, RedisHealthCheck wiring);
  the SDK InMemorySessionStore remains ONLY as ISseNotificationBus backing
  the frozen wallet pairing flow (single-instance; ADR-011 revisit triggers
  cover scaling it) + hosted service for its SSE GC
- RedisSessionStoreIntegrationTests (testcontainers) removed — the SDK
  Redis store is no longer wired anywhere (slice-4 fixture cleanup pulled
  forward; noted on #120)
- AdminOidcFlowTests seed helper learned the new store
- 14 new tests incl. hashed-at-rest invariant, sliding-touch, expiry sweep,
  device fingerprint, invalidate-for-account, mfa-value semantics

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