mirror of
https://gh-proxy.org/https://github.com/klinsmaya/multica.git
synced 2026-11-10 02:16:45 +08:00
* feat(auth): slide UI session expiry instead of fixing it at login A session token's exp was stamped once at login and never moved, so every continuously active user was logged out 30 days later, always mid-task. The session now re-issues itself once it drops below half its TTL, turning that scheduled logout into an idle bound. Renewal is stateless: it copies the claims of a token we signed and moves exp, so there is no session table and no DB read on the hot path. - Browsers are renewed inline by middleware.Auth on safe requests, keeping the session HttpOnly — no client change and no multi-tab coordination. - Clients holding the session as a string (Desktop, mobile) get POST /api/auth/refresh, which only ever accepts a UI JWT. PATs, agent task tokens and cloud-node PATs are refused: a machine credential must not become an interactive session. An expired JWT cannot be revived. - CSRF tokens are now bound to a new sid claim, stable across renewals, so re-issuing the auth cookie no longer invalidates a CSRF token another tab already read. The pre-existing binding stays valid so sessions that predate this keep working until they renew. - CloudFront cookies are re-signed alongside a renewal, so the CDN policy cannot outlive its session. The idle bound is one-sided and the comments say so: a session last used while it still had over half its TTL left is not extended, so the guaranteed survivable gap is TTL/2, not TTL. Refs MUL-7436 Co-authored-by: multica-agent <[email protected]> * feat(auth): renew the session on Desktop, mobile, and legacy token-mode web Completes the client half of sliding sessions. Browsers on cookie auth need nothing — the server re-issues their cookie inline — so this covers the clients that carry the session as a string and have to persist a new one. Renewal follows use, not a clock. Checks run at launch, on returning to the foreground, and on interaction; there is deliberately no interval timer, because a timer would keep an abandoned but still-open window (or a backgrounded phone) signed in forever. The server decides whether a session is renewable and tells the client when to ask again, derived from the deployment's configured TTL — so no client reads exp, hardcodes a cadence, or has to reason about clock skew. Renewal is not a session event, and each step protects that: - Concurrent triggers collapse into one request, so two valid tokens never race to be the one stored. On mobile the single-flight flag is set before the first await, since reading the Keychain is itself async. - A response that lands after logout or an account switch is discarded by comparing against the session the attempt started from; writing it back would resurrect a session the app has already torn down. - Offline, 5xx and an unreadable body all keep the current session, which is still valid for at least another half TTL. Only a real 401 ends a session, and that path is untouched — nothing here can clear tabs or drafts. - WebSocket clients now read the token at connect time instead of pinning the one captured when they were built, so a reconnect after a renewal authenticates with the current credential. Web also retries a rejected CSRF token once. Session-bound CSRF makes that unreachable in the steady state; it covers the single transition where a session predating that binding is renewed for the first time. Refs MUL-7436 Co-authored-by: multica-agent <[email protected]> * fix(auth): address review blockers on sliding session renewal Six fixes from review, each with a regression test that fails without it. An expired session was answered 403, not 401. The CSRF gate runs before the JWT is validated, and it read `sid` through the expiry-enforcing parser — so an expired cookie lost its sid, its perfectly good CSRF token failed to verify, and the user's first action after expiry was a rejected write instead of a return to the login page. Reading the session id and deciding access are now different questions with different parsers: the first only needs the signature, the second still enforces expiry. Rolling back was not survivable. A previous release can verify only the token-bound CSRF binding, so once a session was issued or renewed by this one, a rollback left every signed-in user able to read and unable to write, with no client retry able to help. Both bindings are now issued: clients echo both, the server accepts either, and the token-bound cookie is re-minted on every renewal so a rollback taken at any moment still finds a usable pair. Mobile could resurrect a session it had just ended. Keychain deletes are async, so a renewal that sampled storage after logout began but before the delete landed would write its replacement back. A session epoch, incremented synchronously at the start of logout, a 401 teardown and a new sign-in, closes the window comparing tokens cannot — including the case where logout lands during the renewal's own write, which is now undone rather than left behind. Mobile only checked at launch and on foreground, so an app held in the foreground and used continuously would never check again and could expire under the user's hands. Every touch now passes through a capture-phase responder that declines the gesture it observes. CloudFront cookies were re-signed by a middleware not every renewing route mounts — the plugin bridge renews without it, and would have slid the session forward while leaving the CDN policy behind. Re-signing moved next to the renewal itself, so the two cannot diverge by route. The renewal check interval could exceed the window it has to land in: a flat five-minute floor meant any TTL under ten minutes produced a cadence longer than the renewal window, logging out clients that followed it exactly. The cadence is now capped at half the threshold, which outranks both clamps. Refs MUL-7436 Co-authored-by: multica-agent <[email protected]> * fix(auth): close the CORS, credential-write and short-TTL gaps Three blockers from the second review round. The two CSRF values now travel in one header instead of two. A second header name would have to be in the server's CORS allowlist, and that list is version-specific: a browser holding a cookie this release issued, talking to a rolled-back server, would send a header that server never allowlisted. The preflight fails, the request never reaches a handler, and no retry helps — precisely the half-logged-in state the second cookie exists to prevent. With one header name the server just tries both bindings, and the client switches to the token-bound value when a server rejects the session-bound one. It switches back as soon as the cookie changes, keyed on the rejected value rather than a flag, so it cannot oscillate once per renewal. A test now pins the CSRF header to the allowlist so a future second header fails here rather than in production. Mobile credential writes are serialized, and a stale renewal is refused rather than undone. Compensating after the fact meant deleting whatever was stored — which, after a logout and a fresh sign-in, is the new account's token: the app would be logged in in memory and signed out on the next launch. The check and the write now happen inside one serialized unit, so a logout or a sign-in cannot land between them and a refusal writes nothing at all. AUTH_TOKEN_TTL has a documented one-minute minimum. Clients are told when to check again in whole seconds, so a shorter TTL asks the wire to carry a sub-second cadence: it truncates, and below about four seconds it serialises to 0. Values under the minimum are clamped up with a startup warning rather than honoured. Mobile's floors were never actually lowered in the previous commit — 1h/1m, not the 5s/5m claimed — so both clients now floor at 5s, below anything the server can send, and fall back to 30s, which fits the smallest supported window. The cadence tests assert the serialised integer a client actually receives, not the internal time.Duration. Refs MUL-7436 Co-authored-by: multica-agent <[email protected]> * fix(auth): keep a failed renewal cheap and a renewed token shared Two blockers from the full-PR review, plus the documentation the config change owes. A failed check no longer consumes the normal cadence. The two were one number, so a single 503 spent whatever the server had last suggested — with the 30-day default that is three days. A client told to check again in three days on day 12, returning on day 29 with one day of session left and hitting one 503, would not have been allowed to retry until day 32: two days after the session it was trying to save had expired. Failures now buy a short backing-off delay of their own, reset by the next success, so recovery follows the network rather than the cadence. The bearer token is read from shared storage per request instead of cached per client. Desktop runs one ApiClient per window over one localStorage, so a session renewed in one window left the others sending a credential on its way out until it expired and took the whole session down. A 401 is also now matched against the credential that produced it: a request that went out just before a renewal can be answered after it, and treating that as expiry would tear down a session that is demonstrably alive, clearing tabs and drafts with it. Documentation catches up with both the behaviour and the new minimum, in all four languages: sessions are an idle bound rather than a countdown from sign-in, the bound is one-sided, and AUTH_TOKEN_TTL clamps up at 60s. The rationale for that minimum is corrected too — it is the 5s floor every client applies to the derived cadence, not whole-second serialisation, which only binds far below it. Also removes two claims that were not true: that a failed renewal leaves at least half a TTL (it happens inside the window, so less), and that an open untouched window never renews (cookie-mode sessions slide on any authenticated request, background polling included). Refs MUL-7436 Co-authored-by: multica-agent <[email protected]> * fix(auth): back off on a renewal response that carries no cadence A 200 whose body fails schema validation does not throw — parseWithFallback returns zeroes — so both clients reset the failure count without moving the next-check deadline, and every subsequent interaction fired another request at a server already answering badly. A missing cadence is now treated as a failure and takes the same short backoff; a token that did arrive is still applied. Also settles the documentation wording: the 30 days is not an idle timer. Renewal only happens past the halfway point, so an unused session can end sooner than 30 days after its last use, and saying otherwise promised precision the design does not have. Refs MUL-7436 Co-authored-by: multica-agent <[email protected]> --------- Co-authored-by: J <[email protected]> Co-authored-by: multica-agent <[email protected]>
420 lines
15 KiB
Go
420 lines
15 KiB
Go
package auth
|
|
|
|
import (
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/golang-jwt/jwt/v5"
|
|
)
|
|
|
|
// resetAuthTokenTTLForTest makes AuthTokenTTL() re-read the environment.
|
|
// The production path caches it behind a sync.Once on purpose — the TTL is
|
|
// deployment configuration, not per-request state — so a test that wants a
|
|
// specific TTL has to clear the cache on both sides of itself.
|
|
func resetAuthTokenTTLForTest(t *testing.T) {
|
|
t.Helper()
|
|
clear := func() {
|
|
authTokenTTLOnce = sync.Once{}
|
|
authTokenTTLCached = 0
|
|
}
|
|
clear()
|
|
t.Cleanup(clear)
|
|
}
|
|
|
|
func sessionClaims(t *testing.T, exp time.Time, sid string) jwt.MapClaims {
|
|
t.Helper()
|
|
// float64, not int64: every claims map the production code sees comes out
|
|
// of jwt.Parse, which JSON-decodes numeric dates into float64. An int64
|
|
// here would make MapClaims.GetExpirationTime fail in a way it never does
|
|
// at runtime, and the test would be asserting against a shape that does
|
|
// not exist.
|
|
claims := jwt.MapClaims{
|
|
"sub": "user-1",
|
|
"email": "[email protected]",
|
|
"name": "User One",
|
|
"exp": float64(exp.Unix()),
|
|
"iat": float64(time.Now().Add(-time.Hour).Unix()),
|
|
}
|
|
if sid != "" {
|
|
claims["sid"] = sid
|
|
}
|
|
return claims
|
|
}
|
|
|
|
func signSession(t *testing.T, claims jwt.MapClaims) string {
|
|
t.Helper()
|
|
signed, err := jwt.NewWithClaims(jwt.SigningMethodHS256, claims).SignedString(JWTSecret())
|
|
if err != nil {
|
|
t.Fatalf("sign session token: %v", err)
|
|
}
|
|
return signed
|
|
}
|
|
|
|
// The renewal window is the defining behaviour of a sliding session: too
|
|
// eager and every response carries a Set-Cookie, too lazy and an active user
|
|
// still gets logged out. Half the TTL is the contract Bohan approved on
|
|
// MUL-7436, so pin both sides of the boundary.
|
|
func TestShouldRenewSession_HalfTTLBoundary(t *testing.T) {
|
|
t.Setenv("AUTH_TOKEN_TTL", "720h") // 30 days
|
|
resetAuthTokenTTLForTest(t)
|
|
|
|
now := time.Now()
|
|
ttl := AuthTokenTTL()
|
|
|
|
cases := []struct {
|
|
name string
|
|
remaining time.Duration
|
|
want bool
|
|
}{
|
|
{"freshly issued", ttl, false},
|
|
{"just above half", ttl/2 + time.Minute, false},
|
|
{"exactly half", ttl / 2, true},
|
|
{"just below half", ttl/2 - time.Minute, true},
|
|
{"nearly expired", time.Minute, true},
|
|
// An expired session is dead, not stale. Renewal extends a live
|
|
// session; it must never be a way to resurrect one.
|
|
{"expired a second ago", -time.Second, false},
|
|
{"long expired", -30 * 24 * time.Hour, false},
|
|
}
|
|
for _, tc := range cases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
if got := ShouldRenewSession(now, now.Add(tc.remaining)); got != tc.want {
|
|
t.Errorf("ShouldRenewSession(remaining=%s) = %v, want %v", tc.remaining, got, tc.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestShouldRenewSession_NoExpiryIsNotRenewable(t *testing.T) {
|
|
if ShouldRenewSession(time.Now(), time.Time{}) {
|
|
t.Error("a token with no exp must not be renewable")
|
|
}
|
|
}
|
|
|
|
// Clients must never hardcode a cadence: a deployment that shortens the TTL
|
|
// has to get proportionally more frequent checks, or its sessions die between
|
|
// two of them.
|
|
func TestSessionRenewCheckInterval_TracksTTL(t *testing.T) {
|
|
cases := []struct {
|
|
ttl string
|
|
want time.Duration
|
|
why string
|
|
}{
|
|
{"720h", 72 * time.Hour, "30d default → TTL/10 = 3d, same as the daemon's PAT cadence"},
|
|
{"24h", 144 * time.Minute, "short TTL → proportionally short cadence"},
|
|
{"30m", 3 * time.Minute, "TTL/10, comfortably inside the 15m window"},
|
|
{"10m", time.Minute, "the TTL the PR's manual test plan uses"},
|
|
{"1m", 6 * time.Second, "the shortest supported TTL; 6s is expressible in whole seconds"},
|
|
{"20s", 6 * time.Second, "below MinAuthTokenTTL, so the TTL itself is clamped up to 1m first"},
|
|
{"87600h", 7 * 24 * time.Hour, "10y → clamped at the ceiling"},
|
|
}
|
|
for _, tc := range cases {
|
|
t.Run(tc.ttl, func(t *testing.T) {
|
|
t.Setenv("AUTH_TOKEN_TTL", tc.ttl)
|
|
resetAuthTokenTTLForTest(t)
|
|
if got := SessionRenewCheckInterval(); got != tc.want {
|
|
t.Errorf("SessionRenewCheckInterval() = %s, want %s (TTL=%s: %s)", got, tc.want, tc.ttl, tc.why)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// The cadence has exactly one property that must hold for every configurable
|
|
// TTL: a client that follows it gets at least two chances inside the renewal
|
|
// window. Without that a short TTL produces an interval LONGER than the window
|
|
// — and a client doing everything right is logged out while actively using the
|
|
// app. A flat five-minute floor did that for any TTL under ten minutes.
|
|
//
|
|
// The assertion is made against the value that actually reaches a client —
|
|
// whole seconds, as it is serialised — not the internal time.Duration. Those
|
|
// differ, and only the serialised one governs client behaviour.
|
|
func TestSessionRenewCheckInterval_WireValueFitsInsideTheRenewalWindow(t *testing.T) {
|
|
for _, ttl := range []string{"1s", "10s", "1m", "5m", "10m", "30m", "1h", "24h", "720h", "8760h", "87600h"} {
|
|
t.Run(ttl, func(t *testing.T) {
|
|
t.Setenv("AUTH_TOKEN_TTL", ttl)
|
|
resetAuthTokenTTLForTest(t)
|
|
|
|
// Exactly what handler.RefreshSession puts on the wire.
|
|
wireSeconds := int(SessionRenewCheckInterval().Seconds())
|
|
if wireSeconds < 1 {
|
|
t.Fatalf("cadence serialises to %ds — a client is told to check immediately, forever (TTL=%s)", wireSeconds, ttl)
|
|
}
|
|
|
|
asClientSeesIt := time.Duration(wireSeconds) * time.Second
|
|
threshold := AuthRenewThreshold()
|
|
if asClientSeesIt > threshold/2 {
|
|
t.Errorf("client-visible cadence %s leaves fewer than two attempts inside a %s window (TTL=%s)",
|
|
asClientSeesIt, threshold, ttl)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// Clients floor the server's cadence to protect themselves from a nonsense
|
|
// value. That floor must never be the thing that decides the cadence, so the
|
|
// server's own smallest possible value has to sit above it.
|
|
func TestSessionRenewCheckInterval_StaysAboveTheClientFloors(t *testing.T) {
|
|
// packages/core/platform/session-renewal.ts and
|
|
// apps/mobile/data/session-renewal.ts both floor at 5 seconds.
|
|
const clientFloor = 5 * time.Second
|
|
|
|
for _, ttl := range []string{"1s", "30s", "1m", "10m", "720h"} {
|
|
t.Run(ttl, func(t *testing.T) {
|
|
t.Setenv("AUTH_TOKEN_TTL", ttl)
|
|
resetAuthTokenTTLForTest(t)
|
|
|
|
asClientSeesIt := time.Duration(int(SessionRenewCheckInterval().Seconds())) * time.Second
|
|
if asClientSeesIt < clientFloor {
|
|
t.Errorf("server cadence %s is below the %s client floor, so clients would silently override it (TTL=%s)",
|
|
asClientSeesIt, clientFloor, ttl)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// A TTL too short to serve correctly is clamped up rather than honoured or
|
|
// replaced by the default. Honouring it would mean telling clients to check
|
|
// on a cadence the wire cannot carry; falling back to 30 days would be a far
|
|
// bigger surprise than the one-minute floor.
|
|
func TestAuthTokenTTL_ClampsBelowTheSupportedMinimum(t *testing.T) {
|
|
cases := map[string]time.Duration{
|
|
"1s": MinAuthTokenTTL,
|
|
"30s": MinAuthTokenTTL,
|
|
"59s": MinAuthTokenTTL,
|
|
"1m": time.Minute,
|
|
"10m": 10 * time.Minute,
|
|
}
|
|
for raw, want := range cases {
|
|
t.Run(raw, func(t *testing.T) {
|
|
t.Setenv("AUTH_TOKEN_TTL", raw)
|
|
resetAuthTokenTTLForTest(t)
|
|
if got := AuthTokenTTL(); got != want {
|
|
t.Errorf("AuthTokenTTL() = %s, want %s (AUTH_TOKEN_TTL=%s)", got, want, raw)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// Renewal reads no database, so the claims it copies forward ARE the identity
|
|
// the next request authenticates as. Dropping one would silently downgrade
|
|
// the session.
|
|
func TestRenewSessionToken_CarriesIdentityForward(t *testing.T) {
|
|
t.Setenv("AUTH_TOKEN_TTL", "720h")
|
|
resetAuthTokenTTLForTest(t)
|
|
|
|
old := sessionClaims(t, time.Now().Add(24*time.Hour), "session-abc")
|
|
token, expiresAt, err := RenewSessionToken(old)
|
|
if err != nil {
|
|
t.Fatalf("RenewSessionToken: %v", err)
|
|
}
|
|
|
|
next, err := ParseSessionToken(token)
|
|
if err != nil {
|
|
t.Fatalf("renewed token does not parse: %v", err)
|
|
}
|
|
for _, claim := range []string{"sub", "email", "name", "sid"} {
|
|
if next[claim] != old[claim] {
|
|
t.Errorf("claim %q = %v, want %v", claim, next[claim], old[claim])
|
|
}
|
|
}
|
|
|
|
// exp moved to a full TTL from now, not from the old expiry.
|
|
want := time.Now().Add(AuthTokenTTL())
|
|
if delta := expiresAt.Sub(want); delta > time.Minute || delta < -time.Minute {
|
|
t.Errorf("new expiry %s is not ~now+TTL (%s)", expiresAt, want)
|
|
}
|
|
if got := SessionExpiry(next); got.Unix() != expiresAt.Unix() {
|
|
t.Errorf("exp claim %s does not match returned expiry %s", got, expiresAt)
|
|
}
|
|
}
|
|
|
|
// Sessions that predate MUL-7436 carry no `sid`. They must still renew — this
|
|
// is the migration path for everyone logged in when this ships — and the
|
|
// renewed token has to acquire one so its CSRF binding becomes stable.
|
|
func TestRenewSessionToken_MintsSessionIDForLegacyToken(t *testing.T) {
|
|
legacy := sessionClaims(t, time.Now().Add(24*time.Hour), "")
|
|
|
|
token, _, err := RenewSessionToken(legacy)
|
|
if err != nil {
|
|
t.Fatalf("RenewSessionToken: %v", err)
|
|
}
|
|
if sid := SessionIDFromToken(token); sid == "" {
|
|
t.Error("renewed legacy token must carry a session id")
|
|
}
|
|
}
|
|
|
|
// Renewal is the one place a live session's lifetime is decided after login,
|
|
// so the disabled-user check has to live there too — otherwise disabling an
|
|
// abusive account leaves their session renewing itself forever.
|
|
func TestRenewSessionToken_RejectsDisabledUser(t *testing.T) {
|
|
// One of the ids on the standing emergency denylist.
|
|
var disabledID string
|
|
for id := range temporarilyDisabledUserIDs {
|
|
disabledID = id
|
|
break
|
|
}
|
|
claims := sessionClaims(t, time.Now().Add(time.Hour), "sid-1")
|
|
claims["sub"] = disabledID
|
|
|
|
_, _, err := RenewSessionToken(claims)
|
|
if err != ErrTemporarilyDisabledUser {
|
|
t.Fatalf("err = %v, want ErrTemporarilyDisabledUser", err)
|
|
}
|
|
}
|
|
|
|
func TestRenewSessionToken_RejectsClaimsWithoutSubject(t *testing.T) {
|
|
_, _, err := RenewSessionToken(jwt.MapClaims{"email": "[email protected]"})
|
|
if err != ErrNotSessionToken {
|
|
t.Fatalf("err = %v, want ErrNotSessionToken", err)
|
|
}
|
|
}
|
|
|
|
// Every non-JWT credential the auth middleware accepts must be unable to
|
|
// become an interactive session. These all authenticate fine; none of them is
|
|
// a session.
|
|
func TestParseSessionToken_RejectsNonSessionCredentials(t *testing.T) {
|
|
cases := map[string]string{
|
|
"personal access token": "mul_0123456789abcdef0123456789abcdef01234567",
|
|
"agent task token": "mat_0123456789abcdef0123456789abcdef01234567",
|
|
"cloud node pat": "mcn_0123456789abcdef0123456789abcdef01234567",
|
|
"empty": "",
|
|
"garbage": "not-a-token",
|
|
"wrong secret": func() string {
|
|
s, _ := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
|
|
"sub": "user-1", "exp": time.Now().Add(time.Hour).Unix(),
|
|
}).SignedString([]byte("some-other-secret"))
|
|
return s
|
|
}(),
|
|
}
|
|
for name, token := range cases {
|
|
t.Run(name, func(t *testing.T) {
|
|
if _, err := ParseSessionToken(token); err != ErrNotSessionToken {
|
|
t.Errorf("err = %v, want ErrNotSessionToken", err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// An expired JWT must not parse, which is what keeps the refresh endpoint
|
|
// from being a resurrection mechanism even before ShouldRenewSession looks at
|
|
// the clock.
|
|
func TestParseSessionToken_RejectsExpiredToken(t *testing.T) {
|
|
expired := signSession(t, sessionClaims(t, time.Now().Add(-time.Minute), "sid-1"))
|
|
if _, err := ParseSessionToken(expired); err != ErrNotSessionToken {
|
|
t.Errorf("err = %v, want ErrNotSessionToken", err)
|
|
}
|
|
}
|
|
|
|
func TestSessionIDFromToken(t *testing.T) {
|
|
withSID := signSession(t, sessionClaims(t, time.Now().Add(time.Hour), "sid-xyz"))
|
|
if got := SessionIDFromToken(withSID); got != "sid-xyz" {
|
|
t.Errorf("SessionIDFromToken() = %q, want %q", got, "sid-xyz")
|
|
}
|
|
|
|
legacy := signSession(t, sessionClaims(t, time.Now().Add(time.Hour), ""))
|
|
if got := SessionIDFromToken(legacy); got != "" {
|
|
t.Errorf("legacy token session id = %q, want empty", got)
|
|
}
|
|
|
|
if got := SessionIDFromToken("mul_deadbeef"); got != "" {
|
|
t.Errorf("PAT session id = %q, want empty", got)
|
|
}
|
|
}
|
|
|
|
// A session that is used at least once inside every renewal window never
|
|
// expires. This is the behaviour the issue asks for, stated as a simulation
|
|
// rather than as a property of one call.
|
|
func TestSlidingSession_ContinuousUseOutlivesTTL(t *testing.T) {
|
|
t.Setenv("AUTH_TOKEN_TTL", "720h") // 30 days
|
|
resetAuthTokenTTLForTest(t)
|
|
|
|
ttl := AuthTokenTTL()
|
|
claims := sessionClaims(t, time.Now().Add(ttl), "sid-live")
|
|
|
|
// Use the app every 10 days for 120 days — past both the old 30-day wall
|
|
// and the 90-day absolute cap that was considered and dropped.
|
|
now := time.Now()
|
|
for day := 10; day <= 120; day += 10 {
|
|
now = now.Add(10 * 24 * time.Hour)
|
|
expiresAt := SessionExpiry(claims)
|
|
if !expiresAt.After(now) {
|
|
t.Fatalf("session expired on day %d — an actively used session must not", day)
|
|
}
|
|
if !ShouldRenewSession(now, expiresAt) {
|
|
continue
|
|
}
|
|
token, _, err := RenewSessionToken(claims)
|
|
if err != nil {
|
|
t.Fatalf("day %d: RenewSessionToken: %v", day, err)
|
|
}
|
|
renewed, err := ParseSessionToken(token)
|
|
if err != nil {
|
|
t.Fatalf("day %d: renewed token does not parse: %v", day, err)
|
|
}
|
|
// RenewSessionToken always dates from the real clock, so re-stamp the
|
|
// simulated expiry to keep the loop honest about the TTL it grants.
|
|
renewed["exp"] = float64(now.Add(ttl).Unix())
|
|
claims = renewed
|
|
}
|
|
|
|
if sid, _ := claims["sid"].(string); sid != "sid-live" {
|
|
t.Errorf("session id changed across renewals: %q — CSRF binding would break", sid)
|
|
}
|
|
}
|
|
|
|
// The flip side: stop using the app and the session does die. Renewal removes
|
|
// the scheduled logout, not expiry itself.
|
|
func TestSlidingSession_IdleSessionStillExpires(t *testing.T) {
|
|
t.Setenv("AUTH_TOKEN_TTL", "720h")
|
|
resetAuthTokenTTLForTest(t)
|
|
|
|
ttl := AuthTokenTTL()
|
|
lastUsed := time.Now()
|
|
expiresAt := lastUsed.Add(ttl)
|
|
|
|
// Last request came in while the token still had more than half its life
|
|
// left, so nothing was renewed...
|
|
if ShouldRenewSession(lastUsed, expiresAt) {
|
|
t.Fatal("a freshly issued session must not be renewable")
|
|
}
|
|
// ...and the session is gone one TTL after it was issued.
|
|
if ShouldRenewSession(lastUsed.Add(ttl+time.Second), expiresAt) {
|
|
t.Error("an expired idle session must not be renewable")
|
|
}
|
|
}
|
|
|
|
// SessionIDFromToken must answer "which session is this" for a token whose
|
|
// signature is ours, even once it has expired. It feeds CSRF validation, which
|
|
// runs before authentication — returning "" for an expired token made a valid
|
|
// CSRF value fail and turned session expiry into a 403 on the user's next
|
|
// write instead of the 401 that logs them out.
|
|
func TestSessionIDFromToken_ReadsExpiredTokens(t *testing.T) {
|
|
expired := signSession(t, sessionClaims(t, time.Now().Add(-24*time.Hour), "sid-expired"))
|
|
if got := SessionIDFromToken(expired); got != "sid-expired" {
|
|
t.Errorf("SessionIDFromToken(expired) = %q, want sid-expired", got)
|
|
}
|
|
|
|
// Still only OUR tokens: a forged one has no session to name.
|
|
forged, _ := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
|
|
"sub": "user-1", "sid": "forged", "exp": float64(time.Now().Add(-time.Hour).Unix()),
|
|
}).SignedString([]byte("some-other-secret"))
|
|
if got := SessionIDFromToken(forged); got != "" {
|
|
t.Errorf("SessionIDFromToken(forged) = %q, want empty", got)
|
|
}
|
|
}
|
|
|
|
// The authentication parser keeps enforcing expiry, which is what stops the
|
|
// relaxed read above from leaking into anything that decides access. These two
|
|
// answers must stay different for the same token.
|
|
func TestParseSessionToken_StillEnforcesExpiryAfterTheSplit(t *testing.T) {
|
|
expired := signSession(t, sessionClaims(t, time.Now().Add(-time.Minute), "sid-x"))
|
|
|
|
if _, err := ParseSessionToken(expired); err != ErrNotSessionToken {
|
|
t.Errorf("ParseSessionToken(expired) err = %v, want ErrNotSessionToken", err)
|
|
}
|
|
if SessionIDFromToken(expired) == "" {
|
|
t.Error("SessionIDFromToken must still read the same token")
|
|
}
|
|
}
|