Skip to Content
Signet

Sessions and cookies

How Signet authenticates requests and protects browser flows.

Resolving a Session

A Session token is a 32-byte opaque value. Only its SHA-256 digest is persisted. Wrap your handlers with RequireAuth, or OptionalAuth for public pages that still want to know who the User is:

mux.Handle("/private", auth.RequireAuth(privateHandler))

func privateHandler(w http.ResponseWriter, r *http.Request) {
    user, ok := signet.UserFromContext(r.Context())
    // ...
}

Every request enforces the idle and absolute timeouts and extends the idle window on success. The defaults are 30 minutes idle and 24 hours absolute. Expired and revoked Sessions are deleted, and a Session belonging to a disabled User is rejected.

Listing and revoking

auth.ListSessions(ctx, signet.ListSessionsInput{UserID: user.ID})
auth.RevokeSession(ctx, signet.RevokeSessionInput{SessionID: id})
auth.RevokeAllSessions(ctx, signet.RevokeAllSessionsInput{UserID: user.ID})

Listing returns only safe fields — never a token digest. Each listed Session carries user_agent: the raw User-Agent header of the request that created it, truncated to 512 characters and empty when the client sent none. It is frozen at creation, so it always names the Sign-in client that started the visit. Revoke calls are successful no-ops for unknown IDs, so repeated logouts need no special handling.

Cookies and CSRF

By default the session cookie is HttpOnly, Secure, SameSite=Lax and named __Host-signet_session where the __Host- prefix is valid. State-changing requests carrying a session cookie must present a signed CSRF token in the X-CSRF-Token header, obtained from GET /csrf-token or the sign-in response. Origin and Sec-Fetch-Site are checked too.

The CSRF token is HMAC-SHA256 over a random nonce and an expiry (24-byte nonce, 12-hour TTL). It is read from X-CSRF-Token. Clearing a session cookie sets MaxAge=-1 and an expiry in the past.

The two Insecure* options exist for local development only; using them emits a warning through your logger.

Opt-in docs endpoint

signet.DocsHandler() serves the generated OpenAPI document at /openapi.json and a Scalar reference at /. It is not mounted by Client.Handler() — mount it yourself and decide whether to protect it:

mux.Handle("/docs/", http.StripPrefix("/docs", signet.DocsHandler()))

Session cache

Caching is off by default. Set Session.CacheTTL and supply a SessionCache to let a short-lived copy of a Session answer a lookup without a Store read. The TTL is capped by the Session idle timeout. A cache hit is bounded staleness: a revocation performed by another process may take up to that TTL to be observed (Revocation lag). Every revocation path flushes the cache. A cache error is logged and falls back to the Store; a Store error rejects the request.

cfg.SessionCache = myCache // implements Get, Put and Flush
cfg.Session.CacheTTL = 30 * time.Second

Leave both unset when exact, immediate revocation is required.

Fresh Sessions

Set Session.FreshAge to require recent authentication for sensitive operations. A zero window (the default) disables the check. Freshness is measured from CreatedAt — the moment the User authenticated. LastSeenAt is set when the Session is created and does not move afterwards, so it always records when the sign-in happened rather than the latest activity. Client.VerifyFreshSession returns ErrSessionNotFresh for a stale Session, and Client.RequireFreshSession is middleware that rejects stale requests with 401.

On this page