Skip to content

feat(session): add typed session abstraction with in-memory store#156

Open
YimingIsCOLD wants to merge 4 commits into
mainfrom
feat/pkg-session
Open

feat(session): add typed session abstraction with in-memory store#156
YimingIsCOLD wants to merge 4 commits into
mainfrom
feat/pkg-session

Conversation

@YimingIsCOLD
Copy link
Copy Markdown
Contributor

@YimingIsCOLD YimingIsCOLD commented May 4, 2026

🚀 Summary

Adds a typed session package providing per-request session state (ID, CSRF token, user, data map) backed by a pluggable Store interface, plus an in-memory implementation in session/memstore for development and tests. The Store.Commit contract requires a non-nil snap, non-empty snap.ID, and positive TTL; Prepare and Drop are permissive when id is empty.

✏️ Changes

  • New server/internal/session package
    • Session type owns per-request state; not safe for concurrent use (single request goroutine).
    • Snapshot is the wire/storage form; Data map is shared with the producing/consuming Session.
    • New() returns an unauthenticated session with freshly generated ID and CSRF token (32-char base58).
    • SetUser rotates ID and CSRF token on the unauthenticated→authenticated transition (session fixation defence); auth→auth transitions leave them untouched.
    • Rotate regenerates ID and CSRF token while preserving user and data.
    • Store interface: Prepare (load), Commit (write with TTL), Drop (idempotent remove).
    • Store.Commit contract: rejects nil snap, empty snap.ID, and non-positive TTL. Prepare and Drop are permissive on empty id (return (nil, nil) / no-op).
    • The interface godoc is the authoritative contract; implementation methods use // X implements [session.Store.X] pointers.
  • New server/internal/session/memstore package
    • Store is an in-process map-backed session.Store.
    • Lazy TTL eviction on the Prepare read path.
    • WithClock option for deterministic time control in tests.
    • sync.Mutex guards the entries map; TestStore_ConcurrentAccess exercises this under -race.
    • Commit enforces the validation contract with memstore: <constraint> errors.

🧪 Test plan

  • go test -race ./server/internal/session/... — all green
  • go vet ./server/internal/session/... — clean
  • golangci-lint run ./server/internal/session/... — 0 issues

@YimingIsCOLD YimingIsCOLD self-assigned this May 5, 2026
@YimingIsCOLD YimingIsCOLD force-pushed the feat/pkg-session branch 2 times, most recently from 11ab0d6 to f9fdb18 Compare May 11, 2026 06:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant