Class Sessions
- All Implemented Interfaces:
Middleware
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.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classBuildsSessions; used during configuration, not thread-safe.Nested classes/interfaces inherited from interface com.jsgalactic.axiom.context.Middleware
Middleware.Next -
Method Summary
Modifier and TypeMethodDescriptionReturns an authenticator that yields the identity stored in the session, forSecurity.of(com.jsgalactic.axiom.security.Authenticator).authenticator(String challenge) Returns an authenticator with anotherWWW-Authenticatechallenge for 401 answers.static Sessions.Builderbuilder(SessionStore store) Starts a configuration with the secure defaults.handle(Context context, Middleware.Next next) Handles one request, usually by callingnext.run().Returns the session of the request the context belongs to.toString()Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface com.jsgalactic.axiom.context.Middleware
afterError
-
Method Details
-
builder
Starts a configuration with the secure defaults.- Parameters:
store- where session data lives- Returns:
- a builder
-
session
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
Returns an authenticator that yields the identity stored in the session, forSecurity.of(com.jsgalactic.axiom.security.Authenticator).- Returns:
- the authenticator, with the default challenge
-
authenticator
Returns an authenticator with anotherWWW-Authenticatechallenge for 401 answers.- Parameters:
challenge- a valid header value, for exampleCookie realm="app"- Returns:
- the authenticator
-
handle
Description copied from interface:MiddlewareHandles one request, usually by callingnext.run().- Specified by:
handlein interfaceMiddleware- Parameters:
context- the request-scoped context shared with the handler; status settings made here apply to the handler's result mappingnext- the rest of the chain- Returns:
- the response to use; never null
- Throws:
Exception- to fail the request as a handler exception would
-
toString
-