Class JwtClaimsSet

  • All Implemented Interfaces:
    Payload

    public class JwtClaimsSet
    extends JWObject
    implements Payload
    An implementation that holds a JWT's Claims Set.

    Provides methods to set claims for all the reserved claim names as well as custom claims.

    Since:
    2.0.0
    • Constructor Detail

      • JwtClaimsSet

        public JwtClaimsSet()
        Constructs a new, empty JwtClaimsSet.
      • JwtClaimsSet

        public JwtClaimsSet​(Map<String,​Object> claims)
        Constructs a new JwtClaimsSet, with its claims set to the contents of the given Map.
        Parameters:
        claims - A Map containing the claims to be set in the Claims Set.
    • Method Detail

      • setType

        public void setType​(String type)
        Gets the type of the contents of the Claims Set.

        The values used for this claim SHOULD come from the same value space as the JWT header parameter "typ", with the same rules applying.

        Parameters:
        type - The Claims Set content type.
      • getType

        public String getType()
        Gets the type of the contents of the Claims Set.

        The values used for this claim SHOULD come from the same value space as the JWT header parameter "typ", with the same rules applying.

        Returns:
        The Claims Set content type.
      • setJwtId

        public void setJwtId​(String jwtId)
        Sets the unique ID of the JWT.
        Parameters:
        jwtId - The JWT's ID.
      • getJwtId

        public String getJwtId()
        Gets the unique ID of the JWT.
        Returns:
        The JWT's ID or null if claim not present.
      • setIssuer

        public void setIssuer​(String issuer)
        Sets the issuer this JWT was issued by.

        The given issuer can be any arbitrary string without any ":" characters, if the string does contain a ":" character then it must be a valid URI.

        Parameters:
        issuer - The JWT's issuer.
      • setIssuer

        public void setIssuer​(URI issuer)
        Sets the issuer this JWT was issued by.
        Parameters:
        issuer - The JWT's issuer.
      • getIssuer

        public String getIssuer()
        Gets the issuer this JWT was issued by.
        Returns:
        The JWT's issuer or null if claim not present.
      • setSubject

        public void setSubject​(String subject)
        Sets the subject this JWT is issued to.

        The given subject can be any arbitrary string without any ":" characters, if the string does contain a ":" character then it must be a valid URI.

        Parameters:
        subject - The JWT's principal.
        See Also:
        setSubject(java.net.URI)
      • setSubject

        public void setSubject​(URI subject)
        Sets the subject this JWT is issued to.
        Parameters:
        subject - The JWT's principal.
        See Also:
        setSubject(String)
      • getSubject

        public String getSubject()
        Gets the subject this JWT is issued to.
        Returns:
        The JWT's principal or null if claim not present.
      • addAudience

        public void addAudience​(String audience)
        Adds an entry to the JWT's intended audience list, in the Claims Set.

        The given audience can be any arbitrary string without any ":" characters, if the string does contain a ":" character then it must be a valid URI.

        Parameters:
        audience - The JWT's intended audience.
        See Also:
        addAudience(java.net.URI)
      • addAudience

        public void addAudience​(URI audience)
        Adds an entry to the JWT's intended audience list, in the Claims Set.
        Parameters:
        audience - The JWT's intended audience.
        See Also:
        addAudience(String)
      • getAudience

        public List<String> getAudience()
        Gets the intended audience for the JWT from the Claims Set.
        Returns:
        The JWT's intended audience or null if claim not present.
      • setIssuedAtTime

        public void setIssuedAtTime​(Date issuedAtTime)
        Sets the time the JWT was issued at, in the Claims Set.

        The given date will be converted into an IntDate to be stored in the JWT Claims Set.

        Parameters:
        issuedAtTime - The JWT's issued at time.
      • getIssuedAtTime

        public Date getIssuedAtTime()
        Gets the time the JWT was issued at, from the Claims Set.
        Returns:
        The JWT's issued at time or null if claim not present.
      • setNotBeforeTime

        public void setNotBeforeTime​(Date notBeforeTime)
        Sets the time the JWT is not allowed to be processed before, in the Claims Set.

        The given date will be converted into an IntDate to be stored in the JWT Claims Set.

        Parameters:
        notBeforeTime - The JWT's not before time.
      • getNotBeforeTime

        public Date getNotBeforeTime()
        Gets the time the JWT is not allowed to be processed before, from the Claims Set.
        Returns:
        The JWT's not before time or null if claim not present.
      • setExpirationTime

        public void setExpirationTime​(Date expirationTime)
        Sets the expiration time of the JWT in the Claims Set.

        The given date will be converted into an IntDate to be stored in the JWT Claims Set.

        Parameters:
        expirationTime - The JWT's expiration time.
      • getExpirationTime

        public Date getExpirationTime()
        Gets the expiration time of the JWT from the Claims Set.
        Returns:
        The JWT's expiration time or null if claim not present.
      • setClaim

        public void setClaim​(String key,
                             Object value)
        Sets a claim with the specified name and value.

        If the key matches one of the reserved claim names, then the relevant set method is called to set that claim with the specified name and value.

        Parameters:
        key - The claim name.
        value - The claim value.
      • setClaims

        public void setClaims​(Map<String,​Object> claims)
        Sets claims using the values contained in the specified map.
        Parameters:
        claims - The Map to use to set the claims.
      • getClaim

        public Object getClaim​(String key)
        Gets a claim value for the specified key.

        If the key matches one of the reserved claim names, then the relevant get method is called to get that claim value.

        Parameters:
        key - The claim name.
        Returns:
        The value stored against the claim name.
      • getClaim

        public <T> T getClaim​(String key,
                              Class<T> clazz)
        Gets a claim value for the specified claim name and then casts it to the specified type.
        Type Parameters:
        T - The required type for the claim value.
        Parameters:
        key - The claim name.
        clazz - The class of the required type.
        Returns:
        The value stored against the claim name.
        See Also:
        getClaim(String)
      • copy

        public JwtClaimsSet copy()
        Description copied from interface: Payload
        Create a copy of the current payload.
        Specified by:
        copy in interface Payload
        Returns:
        a copy of the payload.