Class Sessions

java.lang.Object
com.jsgalactic.axiom.security.Sessions
All Implemented Interfaces:
Middleware

public final class Sessions extends Object implements Middleware
Server-side sessions as middleware: a cookie carries a random secret identifier, a SessionStore holds everything else.

 var sessions = Sessions.builder(InMemorySessionStore.builder().build()).build();
 var security = Security.of(sessions.authenticator());
 app.use(sessions);                               // loads the session, issues the cookie
 app.use(security.authenticate());                // identity from the session
 app.post("/login", ctx -> {
     sessions.session(ctx).authenticate(verifiedIdentity);   // new identifier at login
     return ctx.noContent();
 });
 app.post("/logout", ctx -> { sessions.session(ctx).invalidate(); return ctx.noContent(); });
 

Identifiers. 256 random bits from a SecureRandom, base64url without padding (43 characters). The cookie value must have exactly that shape and name a live session in the store, otherwise it is ignored: the server never adopts an identifier the client chose, which defeats session fixation. Identifiers rotate at Session.authenticate(com.jsgalactic.axiom.context.SecurityIdentity) and Session.rotate. They are never logged, exposed through the API, or placed in problem responses.

Cookie. Secure, HttpOnly, SameSite=Lax, Path=/, host-only, and named __Host-sid by default (see SetCookie); the cookie is a browser-session cookie unless Sessions.Builder.maxAge is set. The name defaults to __Secure-sid when a path or domain is configured and to sid when Secure is turned off for local development. Request cookies are parsed with Cookies: a malformed or oversized Cookie header, or the session cookie sent twice, means "no session".

Lifecycle. The middleware looks the session up once per request, makes it available through session(Context) and SessionAuthenticator, and after the handler returns applies the recorded writes and sets the cookie when the identifier is new or changed (or expires it after Session.invalidate()). Nothing is stored for requests that do not write. If the handler throws, no writes are applied. A response carries one Set-Cookie: when the session needs the cookie and the response already has one, the request fails with IllegalStateException (before the store is modified) rather than losing the login silently.

Place it before the Security policies that use its authenticator. Thread-safe and shared by all requests; the store owns the session data.

  • Method Details

    • builder

      public static Sessions.Builder builder(SessionStore store)
      Starts a configuration with the secure defaults.
      Parameters:
      store - where session data lives
      Returns:
      a builder
    • session

      public Session session(Context context)
      Returns the session of the request the context belongs to.
      Parameters:
      context - the context passed to a handler or middleware that runs inside this middleware
      Returns:
      the request's session; it is created in the store on its first write
      Throws:
      IllegalStateException - if this middleware is not active for the request
    • authenticator

      public SessionAuthenticator authenticator()
      Returns an authenticator that yields the identity stored in the session, for Security.of(com.jsgalactic.axiom.security.Authenticator).
      Returns:
      the authenticator, with the default challenge
    • authenticator

      public SessionAuthenticator authenticator(String challenge)
      Returns an authenticator with another WWW-Authenticate challenge for 401 answers.
      Parameters:
      challenge - a valid header value, for example Cookie realm="app"
      Returns:
      the authenticator
    • handle

      public Response handle(Context context, Middleware.Next next) throws Exception
      Description copied from interface: Middleware
      Handles one request, usually by calling next.run().
      Specified by:
      handle in interface Middleware
      Parameters:
      context - the request-scoped context shared with the handler; status settings made here apply to the handler's result mapping
      next - the rest of the chain
      Returns:
      the response to use; never null
      Throws:
      Exception - to fail the request as a handler exception would
    • toString

      public String toString()
      Overrides:
      toString in class Object