Class ClaimsPrincipal

java.lang.Object
org.apache.juneau.rest.server.auth.ClaimsPrincipal
All Implemented Interfaces:
Principal

public class ClaimsPrincipal extends Object implements Principal
A Principal that exposes typed read-only access to a map of claims.

Used by token validators that produce structured assertions about the authenticated subject — e.g. JWTs (the juneau-rest-server-auth-jwt JwtTokenValidator returns a ClaimsPrincipal with the verified token's claim set) or OAuth 2.0 introspection responses.

The "principal name" returned by getName() is supplied at construction time; callers can pick whichever claim is canonical for their identity model (typically sub). The full claim set is reachable via getClaims() and per-claim typed lookup via getClaim(String, Class).

Instances are immutable — the claims map supplied to the constructor is defensively copied and exposed as an unmodifiable view. ClaimsPrincipal can be safely cached and shared across requests.

Type coercion contract for getClaim(String, Class):

The coercion table is intentionally conservative — the validator that built the claims map is the authoritative source of richer type-mapping. If a JWT validator decodes a claim as a List<String>, callers can read it back as List.class or pull individual elements via getClaims() and operate on the List directly.

See Also:
Since:
10.0.0
  • Constructor Details

    • ClaimsPrincipal

      public ClaimsPrincipal(String name, Map<String,Object> claims)
      Constructor.
      Parameters:
      name - The principal name. Must not be null or blank.
      claims - The claim map. May be null (treated as an empty map). Defensively copied.
  • Method Details

    • getName

      public String getName()
      Specified by:
      getName in interface Principal
    • getClaims

      Returns an unmodifiable view of the full claim set.
      Returns:
      The full claim map. Never null.
    • getClaim

      public <T> Optional<T> getClaim(String claimName, Class<T> type)
      Returns the value of a claim by name with the supplied target type.

      See the class-level Javadoc for the coercion contract.

      Type Parameters:
      T - The target type.
      Parameters:
      claimName - The claim name. Must not be null.
      type - The target type. Must not be null.
      Returns:
      The coerced value, or Optional.empty() if the claim is absent or cannot be coerced.
    • hasClaim

      public boolean hasClaim(String claimName)
      Returns true if a claim with the supplied name exists in the claim set.
      Parameters:
      claimName - The claim name. Must not be null.
      Returns:
      true if the claim is present, even if its value is the JSON null literal.
    • toString

      public String toString()
      Specified by:
      toString in interface Principal
      Overrides:
      toString in class Object