feat(session): add typed session abstraction with in-memory store#156
Open
YimingIsCOLD wants to merge 4 commits into
Open
feat(session): add typed session abstraction with in-memory store#156YimingIsCOLD wants to merge 4 commits into
session): add typed session abstraction with in-memory store#156YimingIsCOLD wants to merge 4 commits into
Conversation
11ab0d6 to
f9fdb18
Compare
f9fdb18 to
e3037d2
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
🚀 Summary
Adds a typed
sessionpackage providing per-request session state (ID, CSRF token, user, data map) backed by a pluggableStoreinterface, plus an in-memory implementation insession/memstorefor development and tests. TheStore.Commitcontract requires a non-nil snap, non-emptysnap.ID, and positive TTL;PrepareandDropare permissive whenidis empty.✏️ Changes
server/internal/sessionpackageSessiontype owns per-request state; not safe for concurrent use (single request goroutine).Snapshotis the wire/storage form;Datamap is shared with the producing/consumingSession.New()returns an unauthenticated session with freshly generated ID and CSRF token (32-char base58).SetUserrotates ID and CSRF token on the unauthenticated→authenticated transition (session fixation defence); auth→auth transitions leave them untouched.Rotateregenerates ID and CSRF token while preserving user and data.Storeinterface:Prepare(load),Commit(write with TTL),Drop(idempotent remove).Store.Commitcontract: rejects nil snap, emptysnap.ID, and non-positive TTL.PrepareandDropare permissive on emptyid(return(nil, nil)/ no-op).// X implements [session.Store.X]pointers.server/internal/session/memstorepackageStoreis an in-process map-backedsession.Store.Prepareread path.WithClockoption for deterministic time control in tests.sync.Mutexguards the entries map;TestStore_ConcurrentAccessexercises this under-race.Commitenforces the validation contract withmemstore: <constraint>errors.🧪 Test plan
go test -race ./server/internal/session/...— all greengo vet ./server/internal/session/...— cleangolangci-lint run ./server/internal/session/...— 0 issues