Interface Authenticator

All Known Implementing Classes:
JwtAuthenticator, SessionAuthenticator

public interface Authenticator
Turns the credentials a request carries into a verified SecurityIdentity. Implemented once per credential scheme, for example a bearer token or an API key, and used through Security.

Outcomes:

  • The request carries no credentials for this scheme: return empty. The request continues anonymously; a policy that needs an identity then answers 401 with challenge().
  • The credentials are valid: return the identity.
  • The credentials are present but malformed, expired, forged or otherwise invalid: throw an UnauthorizedException carrying a challenge, which is answered 401 as application/problem+json with WWW-Authenticate. Never put the credential, or anything derived from it, into the exception's code or challenge; attach diagnostic detail as a cause with initCause, which reaches logs only.
Any other exception fails the request like a handler exception (500 over HTTP).

Thread safety and lifecycle. One instance serves every request concurrently, on the request's thread, under its deadline; implementations must be thread-safe and should not block for long. The application owns the instance through the middleware it is registered with.

  • Method Summary

    Modifier and Type
    Method
    Description
    Verifies the request's credentials.
    Returns the WWW-Authenticate challenge sent when a policy requires an identity and the request carries no credentials, for example Bearer realm="api".
  • Method Details

    • authenticate

      Optional<SecurityIdentity> authenticate(Request request)
      Verifies the request's credentials.
      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
      Throws:
      UnauthorizedException - when credentials are present but invalid
    • challenge

      String challenge()
      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.
      Returns:
      challenge: an auth scheme, optionally followed by a space and parameters, in visible ASCII