Add server-side session records with expiry and rotation for SEP-10 auth - #834
Open
gadst12 wants to merge 1 commit into
Open
Add server-side session records with expiry and rotation for SEP-10 auth#834gadst12 wants to merge 1 commit into
gadst12 wants to merge 1 commit into
Conversation
The session cookie previously held the wallet public key directly with no maxAge, so a completed SEP-10 verification produced a session valid forever with no server-side way to revoke it short of the browser deleting its own cookie. - lib/sessionStore.ts: file-backed session records (id, publicKey, createdAt, expiresAt, revoked) with createSession/getValidSession/ revokeSession/renewSession - sep10 route now issues an opaque session id (not the raw public key) with a bounded maxAge (SESSION_MAX_AGE_SECONDS, default 24h), and its DELETE handler revokes the record server-side in addition to clearing the cookie - /api/auth/session GET now rejects expired/revoked sessions instead of trusting any cookie value, and a new PUT handler rotates an active session to a fresh id/expiry so long sessions can renew without dropping the user - referral routes now resolve the wallet address via the session record instead of assuming the cookie value is the address
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
Closes #660
app/api/auth/sep10/route.tsset the session cookie (response.cookies.set('session', publicKey, ...)) with nomaxAge/expires, so once a wallet completed SEP-10 verification the resulting session was valid indefinitely until the browser hit theDELETEhandler. There was also no server-side session record at all — the cookie's raw value was the identity, with nothing to revoke server-side beyond deleting that exact cookie — and/api/auth/sessionGET trusted any present cookie value with no expiry check.Changes
lib/sessionStore.ts(new): a lightweight file-backed session store (mirrors the existinglib/referralStore.tspattern) holding{ id, publicKey, createdAt, expiresAt, revoked }records, withcreateSession,getValidSession,revokeSession, andrenewSession(rotation: issues a fresh id/expiry and revokes the old one).app/api/auth/sep10/route.ts:POSTnow creates a server-side session record on successful verification and sets thesessioncookie to an opaque session id (not the raw public key) with a boundedmaxAge(SESSION_MAX_AGE_SECONDSenv var, default 24h).DELETEnow revokes the session record server-side in addition to clearing the cookie.app/api/auth/session/route.ts:GETnow resolves the cookie throughgetValidSessionand rejects expired/revoked sessions with 401, instead of trusting any present cookie value.PUThandler rotates an active session to a fresh id/expiry (renewal), so long-lived users aren't abruptly logged out — returns 401 and clears the cookie if the session can no longer be renewed.generate,redeem,count): previously read the raw cookie value and used it directly as the wallet address. Since the cookie is now an opaque session id, these routes resolve the actual public key viagetValidSessionfirst..env.example: documents the newSESSION_MAX_AGE_SECONDSvar (required by the repo'svalidate-env.jscheck).__tests__/api/auth/sep10/route.test.tsand__tests__/api/auth/session/route.test.tsto cover session creation/cookie shape, revocation, expiry, and rotation; added__tests__/lib/sessionStore.test.tscovering the store's expiry/revocation/rotation semantics directly.Test plan
npx jest __tests__/api/auth __tests__/lib/sessionStore.test.ts— all passingnpx eslinton all changed files — cleannpx prettier --checkon all changed files — cleannode scripts/validate-env.js— passes with the new env var declaredtsc --noEmiterrors introduced by this diff (pre-existing unrelated errors onmainuntouched)