Class SessionAuthenticator

java.lang.Object
com.jsgalactic.axiom.security.SessionAuthenticator
All Implemented Interfaces:
Authenticator

public final class SessionAuthenticator extends Object implements Authenticator
An Authenticator that reads the identity from the request's session, so the policies of Security work for cookie-authenticated browsers exactly as for bearer tokens.

 var security = Security.of(sessions.authenticator());
 app.use(sessions);
 app.get("/account", handler, security.authenticated());   // 401 without a signed-in session
 

A request without a valid session is anonymous (empty), never a 401 by itself: an unknown, expired, forged or malformed cookie is indistinguishable from none. Only the policy that requires an identity answers 401, with this authenticator's challenge. Cookies carry no standard authentication scheme, so the challenge is informational; set one with Sessions.authenticator(String). Inside Sessions the session already loaded for the request is used; outside it the store is consulted read-only (the lookup counts as an access).

Thread-safe; created by Sessions.

  • Method Details

    • authenticate

      public Optional<SecurityIdentity> authenticate(Request request)
      Description copied from interface: Authenticator
      Verifies the request's credentials.
      Specified by:
      authenticate in interface Authenticator
      Parameters:
      request - the immutable request; headers are untrusted client input
      Returns:
      the verified identity, or empty when the request carries no credentials for this scheme
    • challenge

      public String challenge()
      Description copied from interface: Authenticator
      Returns the WWW-Authenticate challenge sent when a policy requires an identity and the request carries no credentials, for example Bearer realm="api". A constant chosen by the application, never a value taken from a request.
      Specified by:
      challenge in interface Authenticator
      Returns:
      challenge: an auth scheme, optionally followed by a space and parameters, in visible ASCII