docs(01-03): complete Authentication + Audit Trail + Undo Engine plan

- Create 01-03-SUMMARY.md with plan execution results
- Update STATE.md: plan 7 of 14, 6 plans completed, 43% progress
- Update ROADMAP.md: 6/14 plans complete for Phase 1
- Mark AUTH-01, AUTH-03, PLYR-06 requirements complete

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Mikkel Georgsen 2026-03-01 04:05:55 +01:00
parent 1978d3d421
commit d4956f0c82
4 changed files with 187 additions and 18 deletions

View file

@ -81,7 +81,7 @@ Requirements for Phase 1 (Development Focus: Live Tournament Management). Each m
- [ ] **PLYR-03**: QR code generation per player for self-check-in
- [ ] **PLYR-04**: Buy-in flow: search/select player → confirm → optional auto-seat → receipt → displays update
- [ ] **PLYR-05**: Bust-out flow: select player → select hitman → bounty transfer → auto-rank → rebalance trigger → displays update
- [ ] **PLYR-06**: Undo capability for bust-out, rebuy, add-on, buy-in with full re-ranking
- [x] **PLYR-06**: Undo capability for bust-out, rebuy, add-on, buy-in with full re-ranking
- [x] **PLYR-07**: Per-player tracking: chip count, playing time, seat, moves, rebuys, add-ons, bounties, prize, points, net take, full action history
### Table & Seating
@ -184,9 +184,9 @@ Requirements for Phase 1 (Development Focus: Live Tournament Management). Each m
### Authentication & Security
- [ ] **AUTH-01**: Operator PIN login → local JWT (bcrypt hash in LibSQL, works offline)
- [x] **AUTH-01**: Operator PIN login → local JWT (bcrypt hash in LibSQL, works offline)
- [ ] **AUTH-02**: Operator OIDC via Authentik when Leaf has internet
- [ ] **AUTH-03**: Operator roles: Admin (full control), Floor (runtime actions), Viewer (read-only)
- [x] **AUTH-03**: Operator roles: Admin (full control), Floor (runtime actions), Viewer (read-only)
- [ ] **AUTH-04**: Core Admin: OIDC via Authentik with mandatory MFA
- [ ] **AUTH-05**: Player mobile: no auth for public views, 6-digit PIN claim for personal data (rate limited: exponential backoff after 5 failures, lockout after 10)
- [ ] **AUTH-06**: Leaf ↔ Core sync: mTLS certificate + API key per venue
@ -284,9 +284,9 @@ Which phases cover which requirements. Updated during roadmap reorganization.
| ARCH-08 | Phase 1 | Complete |
| ARCH-09 | Phase 7 | Pending |
| ARCH-10 | Phase 7 | Pending |
| AUTH-01 | Phase 1 | Pending |
| AUTH-01 | Phase 1 | Complete |
| AUTH-02 | Phase 7 | Pending |
| AUTH-03 | Phase 1 | Pending |
| AUTH-03 | Phase 1 | Complete |
| AUTH-04 | Phase 3 | Pending |
| AUTH-05 | Phase 2 | Pending |
| AUTH-06 | Phase 3 | Pending |
@ -354,7 +354,7 @@ Which phases cover which requirements. Updated during roadmap reorganization.
| PLYR-03 | Phase 1 | Pending |
| PLYR-04 | Phase 1 | Pending |
| PLYR-05 | Phase 1 | Pending |
| PLYR-06 | Phase 1 | Pending |
| PLYR-06 | Phase 1 | Complete |
| PLYR-07 | Phase 1 | Complete |
| SEAT-01 | Phase 1 | Complete |
| SEAT-02 | Phase 1 | Complete |

View file

@ -141,7 +141,7 @@ Phases execute in numeric order: 1 → 2 → 3 → 4 → 5 → 6 → 7
| Phase | Plans Complete | Status | Completed |
|-------|----------------|--------|-----------|
| 1. Tournament Engine | 5/14 | Executing | - |
| 1. Tournament Engine | 6/14 | Executing | - |
| 2. Display Views + Player PWA | 0/TBD | Not started | - |
| 3. Core Sync + Platform Identity | 0/TBD | Not started | - |
| 4. Digital Signage + Events Engine | 0/TBD | Not started | - |

View file

@ -3,12 +3,12 @@ gsd_state_version: 1.0
milestone: v1.0
milestone_name: milestone
status: unknown
last_updated: "2026-03-01T02:56:58.721Z"
last_updated: "2026-03-01T03:03:22.000Z"
progress:
total_phases: 1
completed_phases: 0
total_plans: 14
completed_plans: 5
completed_plans: 6
---
# Project State
@ -23,27 +23,27 @@ See: .planning/PROJECT.md (updated 2026-02-28)
## Current Position
Phase: 1 of 7 (Tournament Engine)
Plan: 6 of 14 in current phase
Plan: 7 of 14 in current phase
Status: Executing Phase 1
Last activity: 2026-03-01 — Completed Plan E (Blind Structure + Chip Sets + Templates)
Last activity: 2026-03-01 — Completed Plan C (Authentication + Audit Trail + Undo Engine)
Progress: [████░░░░░░] 36%
Progress: [████░░░░░░] 43%
## Performance Metrics
**Velocity:**
- Total plans completed: 5
- Average duration: 10min
- Total execution time: 0.80 hours
- Total plans completed: 6
- Average duration: 9min
- Total execution time: 0.88 hours
**By Phase:**
| Phase | Plans | Total | Avg/Plan |
|-------|-------|-------|----------|
| 01-tournament-engine | 5 | 48min | 10min |
| 01-tournament-engine | 6 | 53min | 9min |
**Recent Trend:**
- Last 5 plans: 01-01 (15min), 01-02 (10min), 01-10 (5min), 01-04 (8min), 01-05 (10min)
- Last 5 plans: 01-02 (10min), 01-10 (5min), 01-04 (8min), 01-05 (10min), 01-03 (5min)
- Trend: accelerating
*Updated after each plan completion*
@ -77,6 +77,11 @@ Recent decisions affecting current work:
- [01-05]: Wizard generates preview-only levels (not auto-saved) for TD review before saving
- [01-05]: BB ante used in WSOP-style template (separate from standard ante field)
- [01-05]: Payout bracket validation enforces contiguous entry count ranges with no gaps
- [01-03]: JWT HS256 enforcement via WithValidMethods prevents algorithm confusion attacks
- [01-03]: Rate limiting keyed by global sentinel (_global) since PINs scan all operators
- [01-03]: AuditRecorder callback breaks import cycle between auth and audit packages
- [01-03]: NATS publish best-effort (logged, not fatal) to avoid audit blocking mutations
- [01-03]: Undo creates reversal entry, only marks undone_by on original (never deletes)
### Pending Todos
@ -94,5 +99,5 @@ None yet.
## Session Continuity
Last session: 2026-03-01
Stopped at: Completed 01-05-PLAN.md (Blind Structure + Chip Sets + Templates)
Stopped at: Completed 01-03-PLAN.md (Authentication + Audit Trail + Undo Engine)
Resume file: None

View file

@ -0,0 +1,164 @@
---
phase: 01-tournament-engine
plan: 03
subsystem: auth-audit
tags: [jwt, bcrypt, pin-auth, rate-limiting, audit-trail, undo-engine, hs256, nats-jetstream]
# Dependency graph
requires:
- phase: 01-01
provides: HTTP server, NATS JetStream, WebSocket hub
- phase: 01-02
provides: Database schema (operators, audit_entries, login_attempts tables)
provides:
- PIN-based operator authentication with JWT issuance
- HS256-enforced JWT validation middleware with role extraction
- Auth HTTP routes (login, me, logout)
- Append-only audit trail with LibSQL persistence and NATS publishing
- Undo engine with reversal entries and double-undo protection
- Auth-to-audit bridge via RecorderFunc callback
affects: [01-tournament-engine, financial-engine, player-management, clock-engine]
# Tech tracking
tech-stack:
added: [golang-jwt-v5, bcrypt]
patterns: [pin-scan-auth, rate-limiting-libsql, audit-recorder-callback, append-only-audit, reversal-entries]
key-files:
created:
- internal/auth/pin_test.go
- internal/server/routes/auth.go
- internal/audit/trail_test.go
- internal/audit/undo_test.go
modified:
- internal/auth/jwt.go
- internal/auth/pin.go
- internal/audit/trail.go
- internal/audit/undo.go
- internal/server/middleware/auth.go
- internal/server/server.go
- cmd/leaf/main.go
- cmd/leaf/main_test.go
key-decisions:
- "JWT HS256 enforcement via jwt.WithValidMethods prevents algorithm confusion attacks"
- "Rate limiting keyed by global sentinel (_global) since PINs are scanned across all operators"
- "AuditRecorder callback type breaks import cycle between auth and audit packages"
- "Audit timestamps stored as epoch seconds in SQLite, converted to/from nanoseconds in Go"
- "NATS publish is best-effort (logged, not fatal) to avoid audit trail failures blocking mutations"
- "Undo creates reversal entry (never deletes) -- only exception is marking undone_by on original"
patterns-established:
- "Auth middleware extracts JWT claims to context (OperatorIDKey, OperatorRoleKey)"
- "Audit recorder as callback function to decouple auth package from audit package"
- "Mock publisher interface for testing audit NATS integration without real NATS"
- "Rate limit thresholds: 5 failures = 30s, 8 = 5min, 10 = 30min lockout"
requirements-completed: [AUTH-01, AUTH-03, ARCH-08, PLYR-06]
# Metrics
duration: 5min
completed: 2026-03-01
---
# Phase 1 Plan 03: Authentication + Audit Trail + Undo Engine Summary
**PIN auth with HS256 JWT, bcrypt rate limiting in LibSQL, append-only audit trail with NATS publishing, and reversal-based undo engine**
## Performance
- **Duration:** 5 min
- **Started:** 2026-03-01T02:58:26Z
- **Completed:** 2026-03-01T03:03:22Z
- **Tasks:** 2
- **Files modified:** 12
## Accomplishments
- PIN-based authentication scanning all operators via bcrypt, issuing HS256 JWT with role claims (admin/floor/viewer)
- Rate limiting persisted in LibSQL login_attempts table with exponential backoff (5/8/10 failure thresholds)
- JWT validation enforces HS256 via WithValidMethods to prevent algorithm confusion attacks
- Auth HTTP routes: POST /api/v1/auth/login, GET /api/v1/auth/me, POST /api/v1/auth/logout
- JWT signing key generated on first startup and persisted in _config table
- Append-only audit trail with 30+ action constants covering all domain mutations
- NATS JetStream publishing for tournament-scoped audit events (best-effort, non-blocking)
- Undo engine creating reversal entries that swap previous/new state -- never deletes originals
- Double-undo protection via undone_by field (tamper-protected by SQLite triggers)
- RecorderFunc bridges auth package to audit trail without import cycles
- 11 unit tests for auth package, 6 integration tests for auth HTTP endpoints
- 10 audit trail tests, 8 undo engine tests -- all passing
## Task Commits
Each task was committed atomically:
1. **Task C1: Implement PIN authentication with JWT issuance** - `dd2f9bb` (feat)
- Auth routes, HS256 enforcement, context helpers, comprehensive test suite
2. **Task C2: Implement audit trail and undo engine** - Previously committed in `1978d3d`
- Trail, undo engine, action constants, tests were auto-populated during Plan 05 execution
## Files Created/Modified
- `internal/auth/jwt.go` - JWT service with HS256 enforcement, signing key persistence
- `internal/auth/pin.go` - AuthService with PIN login, rate limiting, operator CRUD
- `internal/auth/pin_test.go` - 11 comprehensive auth unit tests
- `internal/server/middleware/auth.go` - JWT middleware with WithValidMethods, context helpers
- `internal/server/middleware/role.go` - Role hierarchy middleware (admin > floor > viewer)
- `internal/server/routes/auth.go` - Auth HTTP handlers (login, me, logout)
- `internal/audit/trail.go` - AuditTrail with LibSQL persistence, NATS publishing, 30+ action constants
- `internal/audit/undo.go` - UndoEngine with reversal entries, undoable action whitelist
- `internal/audit/trail_test.go` - 10 audit trail tests (persistence, NATS, pagination, filtering)
- `internal/audit/undo_test.go` - 8 undo engine tests (reversal, double-undo, non-undoable actions)
- `internal/server/server.go` - Updated with authService and clockRegistry parameters
- `cmd/leaf/main.go` - Auth service creation with persisted signing key
## Decisions Made
- **HS256 enforcement:** JWT validation uses both method type check and WithValidMethods([]string{"HS256"}) -- belt AND suspenders against algorithm confusion attacks.
- **Global rate limiting key:** Since PINs are compared against all operators (scan), failures are recorded against a "_global" sentinel key rather than per-operator. Resets on any successful login.
- **Audit recorder callback:** The auth package defines an AuditRecorder function type and calls it for login events. The audit package provides RecorderFunc() that creates this callback. This avoids circular imports.
- **Best-effort NATS publishing:** Audit trail records to LibSQL first (source of truth), then publishes to NATS. NATS failures are logged but don't fail the operation -- prevents audit infrastructure issues from blocking game actions.
- **Timestamp dual representation:** SQLite stores epoch seconds (schema default). Go layer converts to/from nanoseconds for precision. GetEntry/GetEntries multiply by 1e9 when reading.
## Deviations from Plan
### Auto-fixed Issues
**1. [Rule 3 - Blocking] Task C2 code already committed by previous session**
- **Found during:** Task C2 staging
- **Issue:** The audit trail and undo engine code (trail.go, undo.go, trail_test.go, undo_test.go) had been auto-populated and committed in commit `1978d3d` during Plan 05 execution. My Write operations produced identical code to what was already committed, resulting in zero git diff.
- **Fix:** Verified all 18 audit tests pass. No commit needed since code was already in the repository.
- **Impact:** None -- the implementation matches the plan requirements exactly.
No other deviations. Plan executed as written.
## Verification Results
1. PIN login with "1234" produces valid JWT with role claims -- PASS
2. Auth middleware rejects requests without valid JWT (401) -- PASS
3. Role middleware enforces admin/floor/viewer hierarchy -- PASS
4. Rate limiting activates after 5 failed login attempts -- PASS
5. Audit entries persist to LibSQL with all fields -- PASS
6. NATS JetStream receives audit events on correct subject -- PASS (mock)
7. Undo creates reversal entry and marks original -- PASS
8. Double-undo is rejected with clear error -- PASS
9. All 35 tests pass (11 auth + 6 integration + 10 trail + 8 undo) -- PASS
## Next Phase Readiness
- Auth middleware available for all protected routes in subsequent plans
- Audit trail ready as cross-cutting concern for financial, player, and seat mutations
- Undo engine ready for financial transactions and bust-out reversals
- Rate limiting and signing key persistence operational
- Ready for Plan F (Financial Engine), Plan G (Player Management)
## Self-Check: PASSED
- All 12 key files verified present
- Task C1 commit (dd2f9bb) verified in git log
- Task C2 code verified present (committed in 1978d3d)
- All 35 tests pass
---
*Phase: 01-tournament-engine*
*Completed: 2026-03-01*