Class Security

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

public final class Security extends Object
Authentication and authorization policies as Middleware, bound to one Authenticator.

 var security = Security.of(authenticator);
 app.use(security.authenticate());                      // identity when credentials are sent
 app.get("/me", ctx -> ctx.identity().orElseThrow().principal(), security.authenticated());
 app.group("/admin", admin -> {
     admin.use(security.hasRole("admin"));             // 401 without credentials, 403 without the role
     admin.delete("/notes/:id", deleteNote, security.hasPermission("notes:delete"));
 });
 

Outcomes. Every policy resolves the identity first: it uses the one already attached to the context, or runs the authenticator and attaches what it returns. Without an identity the policy throws UnauthorizedException (401, code unauthorized) with the authenticator's challenge; invalid credentials fail with whatever 401 the authenticator throws. An identity that lacks the required role or permission gets ForbiddenException (403, code forbidden); the response does not say which grant was missing. Both are ordinary application/problem+json responses and can be mapped with error handlers.

Placement. Policies are middleware, so they protect exactly the scope they are registered on (global, group or route) and run before the handler. Stacked policies must all pass. Use authenticate() globally to make the identity available to handlers of public routes as well; it never rejects a request without credentials.

Lifecycle and thread safety. Immutable and thread-safe. The middleware it returns hold the authenticator and are owned by the application they are registered with. An identity is attached at most once per request (Context.identity(SecurityIdentity)); when several Security instances guard one route, the first identity attached wins and later policies check it instead of authenticating again.

  • Method Details

    • of

      public static Security of(Authenticator authenticator)
      Creates policies that authenticate with the authenticator.
      Parameters:
      authenticator - thread-safe authenticator
      Returns:
      policies bound to it
      Throws:
      IllegalArgumentException - if the authenticator's challenge is not a valid header value
    • authenticate

      public Middleware authenticate()
      Returns middleware that attaches the identity when the request carries valid credentials and continues anonymously when it carries none. Invalid credentials still fail with 401: a client that sends a broken token learns so instead of being served as anonymous.
      Returns:
      optional authentication middleware
    • authenticated

      public Middleware authenticated()
      Returns middleware that requires an identity (401 otherwise).
      Returns:
      policy middleware
    • hasRole

      public Middleware hasRole(String role)
      Returns middleware that requires an identity with the role (401 without an identity, 403 without the role).
      Parameters:
      role - exact role name
      Returns:
      policy middleware
    • hasAnyRole

      public Middleware hasAnyRole(String... roles)
      Returns middleware that requires an identity with at least one of the roles (401 without an identity, 403 with none of them).
      Parameters:
      roles - exact role names; at least one
      Returns:
      policy middleware
    • hasPermission

      public Middleware hasPermission(String permission)
      Returns middleware that requires an identity with the permission (401 without an identity, 403 without the permission).
      Parameters:
      permission - exact permission name
      Returns:
      policy middleware