Class Session
Sessions.session(com.jsgalactic.axiom.context.Context).
var session = sessions.session(ctx);
session.attribute("cart", "42"); // creates the session on first write
session.authenticate(new SecurityIdentity("ada", Set.of("user"), Set.of())); // login: new identifier
session.invalidate(); // logout
Lazy and transactional. Reading never creates a session, so anonymous traffic costs the store nothing. Writes are recorded and applied to the store atomically, once, when the request completes normally: if the handler throws, none of them is applied (and no cookie is issued). Concurrent requests of one session therefore do not overwrite each other's attributes; two requests that set the same attribute or identity are ordered by completion.
Identifier rotation. authenticate(com.jsgalactic.axiom.context.SecurityIdentity) and rotate give the session a new
secret identifier when the request completes; the old identifier stops working immediately. Call
authenticate at every sign-in and whenever the identity's privileges change, so an
identifier an attacker planted or observed before the change is useless afterwards (session
fixation). A client-supplied identifier that the store does not know is never adopted. The
identifier is deliberately not exposed: it appears only in the cookie.
Attributes whose names start with axiom. are reserved for the library (the CSRF
token); authenticate deletes them so tokens do not survive a change of identity.
Methods are synchronized, but a session belongs to one request and is released when it ends.
toString() shows no identifier or data.
-
Method Summary
Modifier and TypeMethodDescriptionReturns an attribute, including changes made earlier in this request.Sets an attribute; applied when the request completes.Returns a snapshot of the attributes without the reserved ones.authenticate(SecurityIdentity identity) Signs the identity in and rotates the identifier when the request completes.Returns when the stored session was created.identity()Returns the signed-in identity, including a change made earlier in this request.Deletes the session when the request completes and expires the cookie (logout).booleanisNew()Tells whether this session is not stored yet (no valid cookie came with the request).removeAttribute(String name) Removes an attribute; applied when the request completes.rotate()Rotates the identifier when the request completes, keeping the data.toString()
-
Method Details
-
isNew
public boolean isNew()Tells whether this session is not stored yet (no valid cookie came with the request).- Returns:
- true for a session that exists only if this request writes to it
-
createdAt
Returns when the stored session was created.- Returns:
- the creation time, empty for a new session
-
identity
Returns the signed-in identity, including a change made earlier in this request.- Returns:
- the identity, empty when anonymous or invalidated
-
attribute
Returns an attribute, including changes made earlier in this request.- Parameters:
name- attribute name- Returns:
- the value, if set
-
attributes
Returns a snapshot of the attributes without the reserved ones.- Returns:
- an unmodifiable copy
-
attribute
Sets an attribute; applied when the request completes.- Parameters:
name-[A-Za-z0-9_.:-]{1,128}, not starting withaxiom.value- up to 4,096 characters- Returns:
- this session
- Throws:
IllegalArgumentException- for an invalid or reserved name, a long value, or more than 64 attributesIllegalStateException- if the session was invalidated
-
removeAttribute
Removes an attribute; applied when the request completes.- Parameters:
name- attribute name- Returns:
- this session
-
authenticate
Signs the identity in and rotates the identifier when the request completes. Attributes are kept except the reserved ones; the previous identity, if any, is replaced.- Parameters:
identity- the verified identity- Returns:
- this session
- Throws:
IllegalStateException- if the session was invalidated
-
rotate
Rotates the identifier when the request completes, keeping the data.- Returns:
- this session
- Throws:
IllegalStateException- if the session was invalidated
-
invalidate
Deletes the session when the request completes and expires the cookie (logout).- Returns:
- this session
-
toString
-