Class Session

java.lang.Object
com.jsgalactic.axiom.security.Session

public final class Session extends Object
One request's view of its session, from 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 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

      public Optional<Instant> createdAt()
      Returns when the stored session was created.
      Returns:
      the creation time, empty for a new session
    • identity

      public Optional<SecurityIdentity> identity()
      Returns the signed-in identity, including a change made earlier in this request.
      Returns:
      the identity, empty when anonymous or invalidated
    • attribute

      public Optional<String> attribute(String name)
      Returns an attribute, including changes made earlier in this request.
      Parameters:
      name - attribute name
      Returns:
      the value, if set
    • attributes

      public Map<String,String> attributes()
      Returns a snapshot of the attributes without the reserved ones.
      Returns:
      an unmodifiable copy
    • attribute

      public Session attribute(String name, String value)
      Sets an attribute; applied when the request completes.
      Parameters:
      name - [A-Za-z0-9_.:-]{1,128}, not starting with axiom.
      value - up to 4,096 characters
      Returns:
      this session
      Throws:
      IllegalArgumentException - for an invalid or reserved name, a long value, or more than 64 attributes
      IllegalStateException - if the session was invalidated
    • removeAttribute

      public Session removeAttribute(String name)
      Removes an attribute; applied when the request completes.
      Parameters:
      name - attribute name
      Returns:
      this session
    • authenticate

      public Session authenticate(SecurityIdentity identity)
      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

      public Session rotate()
      Rotates the identifier when the request completes, keeping the data.
      Returns:
      this session
      Throws:
      IllegalStateException - if the session was invalidated
    • invalidate

      public Session invalidate()
      Deletes the session when the request completes and expires the cookie (logout).
      Returns:
      this session
    • toString

      public String toString()
      Overrides:
      toString in class Object