Class JwtClaims

java.lang.Object
com.jsgalactic.axiom.security.jwt.JwtClaims

public final class JwtClaims extends Object
The claims of a token whose signature, expiry, issuer, audience and subject have been verified, read through typed accessors so that no JSON library type is exposed. Passed to a TokenCheck and to JwtAuthenticator.Builder.attributes(java.util.function.Function<com.jsgalactic.axiom.security.jwt.JwtClaims, java.util.Map<java.lang.String, java.lang.String>>).

Accessors never return a value of another type than they promise: a claim of the wrong type throws IllegalArgumentException, which the authenticator answers like any invalid token (401 invalid_token). An absent claim is empty. The claim values are untrusted in the sense that the issuer chose them; apply the same care as to any input. toString() lists the number of claims, not their values.

Immutable and thread-safe; valid after the call that received it returns.

  • Method Details

    • names

      public Set<String> names()
      Lists the names of the claims present.
      Returns:
      unmodifiable set of claim names
    • has

      public boolean has(String name)
      Reports whether a claim is present, with any value (including JSON null).
      Parameters:
      name - claim name
      Returns:
      true if present
    • string

      public Optional<String> string(String name)
      Reads a string claim.
      Parameters:
      name - claim name
      Returns:
      the value, or empty if absent or JSON null
      Throws:
      IllegalArgumentException - if the claim is not a string
    • strings

      public List<String> strings(String name)
      Reads a claim that is a string or an array of strings, as a list.
      Parameters:
      name - claim name
      Returns:
      the values; empty if absent or JSON null
      Throws:
      IllegalArgumentException - if the claim is neither a string nor an array of strings
    • number

      public OptionalLong number(String name)
      Reads a claim that is a non-negative integer of at most 15 digits.
      Parameters:
      name - claim name
      Returns:
      the value, or empty if absent or JSON null
      Throws:
      IllegalArgumentException - if the claim is not such an integer
    • bool

      public Optional<Boolean> bool(String name)
      Reads a boolean claim.
      Parameters:
      name - claim name
      Returns:
      the value, or empty if absent or JSON null
      Throws:
      IllegalArgumentException - if the claim is not a boolean
    • text

      public Optional<String> text(String name)
      Reads a scalar claim (string, number or boolean) as text.
      Parameters:
      name - claim name
      Returns:
      the text; empty if absent or JSON null
      Throws:
      IllegalArgumentException - if the claim is an array or an object
    • id

      public Optional<String> id()
      Returns the jti claim, the token's unique identifier, for replay and revocation checks.
      Returns:
      the identifier, or empty if the token has none
      Throws:
      IllegalArgumentException - if present but not a string
    • subject

      public String subject()
      Returns the sub claim, which is always present in a verified token.
      Returns:
      the subject
    • issuer

      public String issuer()
      Returns the iss claim, which is always present in a verified token.
      Returns:
      the issuer
    • expiresAt

      public Instant expiresAt()
      Returns the exp claim, which is always present in a verified token. A replay record for id() can be dropped after this instant plus the clock skew.
      Returns:
      the expiry
    • toString

      public String toString()
      Overrides:
      toString in class Object