Package com.nimbusds.jwt
Class JWTClaimsSet
- java.lang.Object
-
- com.nimbusds.jwt.JWTClaimsSet
-
- All Implemented Interfaces:
java.io.Serializable
@Immutable public final class JWTClaimsSet extends java.lang.Object implements java.io.SerializableJSON Web Token (JWT) claims set. This class is immutable.Supports all
getRegisteredNames()registered claims} of the JWT specification:- iss - Issuer
- sub - Subject
- aud - Audience
- exp - Expiration Time
- nbf - Not Before
- iat - Issued At
- jti - JWT ID
The set may also contain custom claims; these will be serialised and parsed along the registered ones.
Example JWT claims set:
{ "sub" : "joe", "exp" : 1300819380, "http://example.com/is_root" : true }Example usage:
JWTClaimsSet claimsSet = new JWTClaimsSet.Builder() .subject("joe") .expirationTime(new Date(1300819380 * 1000l) .claim("http://example.com/is_root", true) .build();- Version:
- 2021-02-22
- Author:
- Vladimir Dzhuvinov, Justin Richer
- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classJWTClaimsSet.BuilderBuilder for constructing JSON Web Token (JWT) claims sets.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanequals(java.lang.Object o)java.util.List<java.lang.String>getAudience()Gets the audience (aud) claim.java.lang.BooleangetBooleanClaim(java.lang.String name)Gets the specified claim (registered or custom) asBoolean.java.lang.ObjectgetClaim(java.lang.String name)Gets the specified claim (registered or custom).java.util.Map<java.lang.String,java.lang.Object>getClaims()Gets the claims (registered and custom).java.util.DategetDateClaim(java.lang.String name)Gets the specified claim (registered or custom) asDate.java.lang.DoublegetDoubleClaim(java.lang.String name)Gets the specified claim (registered or custom) asDouble.java.util.DategetExpirationTime()Gets the expiration time (exp) claim.java.lang.FloatgetFloatClaim(java.lang.String name)Gets the specified claim (registered or custom) asFloat.java.lang.IntegergetIntegerClaim(java.lang.String name)Gets the specified claim (registered or custom) asInteger.java.lang.StringgetIssuer()Gets the issuer (iss) claim.java.util.DategetIssueTime()Gets the issued-at (iat) claim.java.util.Map<java.lang.String,java.lang.Object>getJSONObjectClaim(java.lang.String name)Gets the specified claim (registered or custom) as a JSON object.java.lang.StringgetJWTID()Gets the JWT ID (jti) claim.java.lang.LonggetLongClaim(java.lang.String name)Gets the specified claim (registered or custom) asLong.java.util.DategetNotBeforeTime()Gets the not-before (nbf) claim.static java.util.Set<java.lang.String>getRegisteredNames()Gets the registered JWT claim names.java.lang.String[]getStringArrayClaim(java.lang.String name)Gets the specified claims (registered or custom) as aStringarray.java.lang.StringgetStringClaim(java.lang.String name)Gets the specified claim (registered or custom) asString.java.util.List<java.lang.String>getStringListClaim(java.lang.String name)Gets the specified claims (registered or custom) as aListlist of strings.java.lang.StringgetSubject()Gets the subject (sub) claim.java.net.URIgetURIClaim(java.lang.String name)Gets the specified claim (registered or custom) as aURI.inthashCode()static JWTClaimsSetparse(java.lang.String s)Parses a JSON Web Token (JWT) claims set from the specified JSON object string representation.static JWTClaimsSetparse(java.util.Map<java.lang.String,java.lang.Object> json)Parses a JSON Web Token (JWT) claims set from the specified JSON object representation.java.util.Map<java.lang.String,java.lang.Object>toJSONObject()Returns the JSON object representation of this claims set.java.util.Map<java.lang.String,java.lang.Object>toJSONObject(boolean includeClaimsWithNullValues)Returns the JSON object representation of this claims set.PayloadtoPayload()Returns a JOSE object payload representation of this claims set.java.lang.StringtoString()Returns a JSON object string representation of this claims set.java.lang.StringtoString(boolean includeClaimsWithNullValues)Returns a JSON object string representation of this claims set.<T> TtoType(JWTClaimsSetTransformer<T> transformer)Returns a transformation of this JWT claims set.
-
-
-
Method Detail
-
getRegisteredNames
public static java.util.Set<java.lang.String> getRegisteredNames()
Gets the registered JWT claim names.- Returns:
- The registered claim names, as a unmodifiable set.
-
getIssuer
public java.lang.String getIssuer()
Gets the issuer (iss) claim.- Returns:
- The issuer claim,
nullif not specified.
-
getSubject
public java.lang.String getSubject()
Gets the subject (sub) claim.- Returns:
- The subject claim,
nullif not specified.
-
getAudience
public java.util.List<java.lang.String> getAudience()
Gets the audience (aud) claim.- Returns:
- The audience claim, empty list if not specified.
-
getExpirationTime
public java.util.Date getExpirationTime()
Gets the expiration time (exp) claim.- Returns:
- The expiration time,
nullif not specified.
-
getNotBeforeTime
public java.util.Date getNotBeforeTime()
Gets the not-before (nbf) claim.- Returns:
- The not-before claim,
nullif not specified.
-
getIssueTime
public java.util.Date getIssueTime()
Gets the issued-at (iat) claim.- Returns:
- The issued-at claim,
nullif not specified.
-
getJWTID
public java.lang.String getJWTID()
Gets the JWT ID (jti) claim.- Returns:
- The JWT ID claim,
nullif not specified.
-
getClaim
public java.lang.Object getClaim(java.lang.String name)
Gets the specified claim (registered or custom).- Parameters:
name- The name of the claim. Must not benull.- Returns:
- The value of the claim,
nullif not specified.
-
getStringClaim
public java.lang.String getStringClaim(java.lang.String name) throws java.text.ParseExceptionGets the specified claim (registered or custom) asString.- Parameters:
name- The name of the claim. Must not benull.- Returns:
- The value of the claim,
nullif not specified. - Throws:
java.text.ParseException- If the claim value is not of the required type.
-
getStringArrayClaim
public java.lang.String[] getStringArrayClaim(java.lang.String name) throws java.text.ParseExceptionGets the specified claims (registered or custom) as aStringarray.- Parameters:
name- The name of the claim. Must not benull.- Returns:
- The value of the claim,
nullif not specified. - Throws:
java.text.ParseException- If the claim value is not of the required type.
-
getStringListClaim
public java.util.List<java.lang.String> getStringListClaim(java.lang.String name) throws java.text.ParseExceptionGets the specified claims (registered or custom) as aListlist of strings.- Parameters:
name- The name of the claim. Must not benull.- Returns:
- The value of the claim,
nullif not specified. - Throws:
java.text.ParseException- If the claim value is not of the required type.
-
getURIClaim
public java.net.URI getURIClaim(java.lang.String name) throws java.text.ParseExceptionGets the specified claim (registered or custom) as aURI.- Parameters:
name- The name of the claim. Must not benull.- Returns:
- The value of the claim,
nullif not specified. - Throws:
java.text.ParseException- If the claim couldn't be parsed to a URI.
-
getBooleanClaim
public java.lang.Boolean getBooleanClaim(java.lang.String name) throws java.text.ParseExceptionGets the specified claim (registered or custom) asBoolean.- Parameters:
name- The name of the claim. Must not benull.- Returns:
- The value of the claim,
nullif not specified. - Throws:
java.text.ParseException- If the claim value is not of the required type.
-
getIntegerClaim
public java.lang.Integer getIntegerClaim(java.lang.String name) throws java.text.ParseExceptionGets the specified claim (registered or custom) asInteger.- Parameters:
name- The name of the claim. Must not benull.- Returns:
- The value of the claim,
nullif not specified. - Throws:
java.text.ParseException- If the claim value is not of the required type.
-
getLongClaim
public java.lang.Long getLongClaim(java.lang.String name) throws java.text.ParseExceptionGets the specified claim (registered or custom) asLong.- Parameters:
name- The name of the claim. Must not benull.- Returns:
- The value of the claim,
nullif not specified. - Throws:
java.text.ParseException- If the claim value is not of the required type.
-
getDateClaim
public java.util.Date getDateClaim(java.lang.String name) throws java.text.ParseExceptionGets the specified claim (registered or custom) asDate. The claim may be represented by a Date object or a number of a seconds since the Unix epoch.- Parameters:
name- The name of the claim. Must not benull.- Returns:
- The value of the claim,
nullif not specified. - Throws:
java.text.ParseException- If the claim value is not of the required type.
-
getFloatClaim
public java.lang.Float getFloatClaim(java.lang.String name) throws java.text.ParseExceptionGets the specified claim (registered or custom) asFloat.- Parameters:
name- The name of the claim. Must not benull.- Returns:
- The value of the claim,
nullif not specified. - Throws:
java.text.ParseException- If the claim value is not of the required type.
-
getDoubleClaim
public java.lang.Double getDoubleClaim(java.lang.String name) throws java.text.ParseExceptionGets the specified claim (registered or custom) asDouble.- Parameters:
name- The name of the claim. Must not benull.- Returns:
- The value of the claim,
nullif not specified. - Throws:
java.text.ParseException- If the claim value is not of the required type.
-
getJSONObjectClaim
public java.util.Map<java.lang.String,java.lang.Object> getJSONObjectClaim(java.lang.String name) throws java.text.ParseExceptionGets the specified claim (registered or custom) as a JSON object.- Parameters:
name- The name of the claim. Must not benull.- Returns:
- The value of the claim,
nullif not specified. - Throws:
java.text.ParseException- If the claim value is not of the required type.
-
getClaims
public java.util.Map<java.lang.String,java.lang.Object> getClaims()
Gets the claims (registered and custom).Note that the registered claims Expiration-Time (
exp), Not-Before-Time (nbf) and Issued-At (iat) will be returned asjava.util.Dateinstances.- Returns:
- The claims, as an unmodifiable map, empty map if none.
-
toPayload
public Payload toPayload()
Returns a JOSE object payload representation of this claims set.- Returns:
- The payload representation.
-
toJSONObject
public java.util.Map<java.lang.String,java.lang.Object> toJSONObject()
Returns the JSON object representation of this claims set. The claims are serialised according to their insertion order. Claims withnullvalues are not output.- Returns:
- The JSON object representation.
-
toJSONObject
public java.util.Map<java.lang.String,java.lang.Object> toJSONObject(boolean includeClaimsWithNullValues)
Returns the JSON object representation of this claims set. The claims are serialised according to their insertion order.- Parameters:
includeClaimsWithNullValues- Iftrueclaims withnullvalues will also be output.- Returns:
- The JSON object representation.
-
toString
public java.lang.String toString()
Returns a JSON object string representation of this claims set. The claims are serialised according to their insertion order. Claims withnullvalues are not output.- Overrides:
toStringin classjava.lang.Object- Returns:
- The JSON object string representation.
-
toString
public java.lang.String toString(boolean includeClaimsWithNullValues)
Returns a JSON object string representation of this claims set. The claims are serialised according to their insertion order.- Parameters:
includeClaimsWithNullValues- Iftrueclaims withnullvalues will also be output.- Returns:
- The JSON object string representation.
-
toType
public <T> T toType(JWTClaimsSetTransformer<T> transformer)
Returns a transformation of this JWT claims set.- Type Parameters:
T- Type of the result.- Parameters:
transformer- The JWT claims set transformer. Must not benull.- Returns:
- The transformed JWT claims set.
-
parse
public static JWTClaimsSet parse(java.util.Map<java.lang.String,java.lang.Object> json) throws java.text.ParseException
Parses a JSON Web Token (JWT) claims set from the specified JSON object representation.- Parameters:
json- The JSON object to parse. Must not benull.- Returns:
- The JWT claims set.
- Throws:
java.text.ParseException- If the specified JSON object doesn't represent a valid JWT claims set.
-
parse
public static JWTClaimsSet parse(java.lang.String s) throws java.text.ParseException
Parses a JSON Web Token (JWT) claims set from the specified JSON object string representation.- Parameters:
s- The JSON object string to parse. Must not benull.- Returns:
- The JWT claims set.
- Throws:
java.text.ParseException- If the specified JSON object string doesn't represent a valid JWT claims set.
-
equals
public boolean equals(java.lang.Object o)
- Overrides:
equalsin classjava.lang.Object
-
hashCode
public int hashCode()
- Overrides:
hashCodein classjava.lang.Object
-
-