Class JwtAuthenticator.Builder

java.lang.Object
com.jsgalactic.axiom.security.jwt.JwtAuthenticator.Builder
Enclosing class:
JwtAuthenticator

public static final class JwtAuthenticator.Builder extends Object
Configures a JwtAuthenticator. Not thread-safe; build once at startup.
  • Method Details

    • hmacKey

      public JwtAuthenticator.Builder hmacKey(JwsAlgorithm algorithm, byte[] secret)
      Registers an HMAC secret used for tokens without a kid.
      Parameters:
      algorithm - HS256, HS384 or HS512
      secret - at least 32, 48 or 64 bytes respectively; copied
      Returns:
      this builder
      Throws:
      IllegalArgumentException - for a non-HMAC algorithm, a short secret, or a second key without key ID for the algorithm
    • hmacKey

      public JwtAuthenticator.Builder hmacKey(String keyId, JwsAlgorithm algorithm, byte[] secret)
      Registers an HMAC secret for tokens whose kid is the key ID.
      Parameters:
      keyId - key ID, unique within this builder
      algorithm - HS256, HS384 or HS512
      secret - at least 32, 48 or 64 bytes respectively; copied
      Returns:
      this builder
      Throws:
      IllegalArgumentException - for a non-HMAC algorithm, a short secret or a duplicate key ID
    • publicKey

      public JwtAuthenticator.Builder publicKey(JwsAlgorithm algorithm, PublicKey key)
      Registers a public key used for tokens without a kid.
      Parameters:
      algorithm - an RS, PS, ES or EdDSA algorithm matching the key
      key - RSA key of at least 2048 bits (RS and PS), EC key on the algorithm's curve (ES) or Ed25519 key (EdDSA)
      Returns:
      this builder
      Throws:
      IllegalArgumentException - if the key does not fit the algorithm, or for a second key without key ID for the algorithm
    • publicKey

      public JwtAuthenticator.Builder publicKey(String keyId, JwsAlgorithm algorithm, PublicKey key)
      Registers a public key for tokens whose kid is the key ID.
      Parameters:
      keyId - key ID, unique within this builder
      algorithm - an RS, PS, ES or EdDSA algorithm matching the key
      key - RSA key of at least 2048 bits (RS and PS), EC key on the algorithm's curve (ES) or Ed25519 key (EdDSA)
      Returns:
      this builder
      Throws:
      IllegalArgumentException - if the key does not fit the algorithm or for a duplicate key ID
    • jwks

      public JwtAuthenticator.Builder jwks(JwksSource source)
      Takes further verification keys from the issuer's JSON Web Key Set, with default options. See jwks(JwksSource, JwksOptions).
      Parameters:
      source - where the key set comes from, for example JwksSource.url(java.net.URI)
      Returns:
      this builder
      Throws:
      IllegalStateException - if a key set source was already configured
    • jwks

      public JwtAuthenticator.Builder jwks(JwksSource source, JwksOptions options)
      Takes further verification keys from the issuer's JSON Web Key Set, which the application configures and the token cannot influence. The set is fetched on first use, refreshed when older than JwksOptions.refreshInterval(java.time.Duration), and refetched when a token names an unknown kid, at most once per JwksOptions.minRefreshInterval(java.time.Duration), so key rotation needs no restart and unknown key IDs cannot make the server fetch in a loop. A failed fetch keeps the last good set until JwksOptions.maxStale(java.time.Duration) has passed.

      Keys of the set are bound to their declared alg exactly like statically registered keys and pass the same size and curve checks. Statically registered keys with the same key ID win over keys of the set. Tokens without a kid are verified only with statically registered keys.

      Parameters:
      source - where the key set comes from
      options - bounds and timings
      Returns:
      this builder
      Throws:
      IllegalStateException - if a key set source was already configured
      IllegalArgumentException - if the options contradict each other
    • issuer

      public JwtAuthenticator.Builder issuer(String issuer)
      Accepts tokens whose iss equals this value. Call again to accept several issuers.
      Parameters:
      issuer - exact issuer
      Returns:
      this builder
    • audience

      public JwtAuthenticator.Builder audience(String audience)
      Accepts tokens whose aud contains this value. Call again to accept several.
      Parameters:
      audience - exact audience, usually this API's identifier
      Returns:
      this builder
    • clockSkew

      public JwtAuthenticator.Builder clockSkew(Duration skew)
      Sets the tolerated clock difference for exp, nbf and iat.
      Parameters:
      skew - zero to five minutes, whole seconds; 30 seconds by default
      Returns:
      this builder
    • maxTokenLength

      public JwtAuthenticator.Builder maxTokenLength(int characters)
      Sets the longest accepted token, checked before decoding.
      Parameters:
      characters - 256 to 65,536; 8,192 by default
      Returns:
      this builder
    • rolesClaim

      public JwtAuthenticator.Builder rolesClaim(String claim)
      Names the claim holding roles (roles by default).
      Parameters:
      claim - claim name
      Returns:
      this builder
    • permissionsClaim

      public JwtAuthenticator.Builder permissionsClaim(String claim)
      Names the claim holding permissions (scope by default).
      Parameters:
      claim - claim name
      Returns:
      this builder
    • exposeClaims

      public JwtAuthenticator.Builder exposeClaims(String... names)
      Exposes verified claims as identity attributes under the same names. Only string, number and boolean claims are exposed, as text; a claim that is absent or JSON null is skipped, and one that is an array or object makes the token invalid. Values over 2,048 characters make the token invalid too. Only claims listed here are exposed; every other claim stays unavailable to handlers.
      Parameters:
      names - claim names, at most 64 in total with the other attribute settings
      Returns:
      this builder
    • attributes

      Maps verified claims to identity attributes with application code, for example to flatten a nested or array claim. Runs after the token checks accepted the token; its entries are added to those of exposeClaims(java.lang.String...) and replace same-named ones. An IllegalArgumentException from the function, or an attribute that SecurityIdentity refuses, makes the token invalid; other exceptions propagate.
      Parameters:
      mapper - thread-safe function from verified claims to attribute names and values
      Returns:
      this builder
    • tokenCheck

      public JwtAuthenticator.Builder tokenCheck(TokenCheck check)
      Adds a check that runs after signature, time, issuer, audience and subject verification, for revocation or jti replay detection. See TokenCheck for the outcomes. Checks run in registration order.
      Parameters:
      check - thread-safe check
      Returns:
      this builder
    • realm

      public JwtAuthenticator.Builder realm(String realm)
      Sets the realm of the WWW-Authenticate challenge (api by default).
      Parameters:
      realm - 1 to 64 letters, digits, spaces and . _ : / -
      Returns:
      this builder
    • clock

      public JwtAuthenticator.Builder clock(Clock clock)
      Sets the clock for time-based claims, for tests; the system UTC clock by default.
      Parameters:
      clock - clock
      Returns:
      this builder
    • build

      public JwtAuthenticator build()
      Builds the immutable authenticator.
      Returns:
      the authenticator
      Throws:
      IllegalStateException - without a key, an issuer or an audience