Package com.nimbusds.jwt
Class JWTClaimsSet
- java.lang.Object
-
- com.nimbusds.jwt.JWTClaimsSet
-
- All Implemented Interfaces:
Serializable
@Immutable public final class JWTClaimsSet extends Object implements Serializable
JSON 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:
- 2023-10-16
- Author:
- Vladimir Dzhuvinov, Justin Richer
- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
JWTClaimsSet.Builder
Builder for constructing JSON Web Token (JWT) claims sets.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
equals(Object o)
List<String>
getAudience()
Gets the audience (aud
) claim.Boolean
getBooleanClaim(String name)
Gets the specified claim (registered or custom) asBoolean
.Object
getClaim(String name)
Gets the specified claim (registered or custom).Map<String,Object>
getClaims()
Gets the claims (registered and custom).Date
getDateClaim(String name)
Gets the specified claim (registered or custom) asDate
.Double
getDoubleClaim(String name)
Gets the specified claim (registered or custom) asDouble
.Date
getExpirationTime()
Gets the expiration time (exp
) claim.Float
getFloatClaim(String name)
Gets the specified claim (registered or custom) asFloat
.Integer
getIntegerClaim(String name)
Gets the specified claim (registered or custom) asInteger
.String
getIssuer()
Gets the issuer (iss
) claim.Date
getIssueTime()
Gets the issued-at (iat
) claim.Map<String,Object>
getJSONObjectClaim(String name)
Gets the specified claim (registered or custom) as a JSON object.String
getJWTID()
Gets the JWT ID (jti
) claim.List<Object>
getListClaim(String name)
Gets the specified claims (registered or custom) as aList
list of objects.Long
getLongClaim(String name)
Gets the specified claim (registered or custom) asLong
.Date
getNotBeforeTime()
Gets the not-before (nbf
) claim.static Set<String>
getRegisteredNames()
Gets the registered JWT claim names.String[]
getStringArrayClaim(String name)
Gets the specified claims (registered or custom) as aString
array.String
getStringClaim(String name)
Gets the specified claim (registered or custom) asString
.List<String>
getStringListClaim(String name)
Gets the specified claims (registered or custom) as aList
list of strings.String
getSubject()
Gets the subject (sub
) claim.URI
getURIClaim(String name)
Gets the specified claim (registered or custom) as aURI
.int
hashCode()
static JWTClaimsSet
parse(String s)
Parses a JSON Web Token (JWT) claims set from the specified JSON object string representation.static JWTClaimsSet
parse(Map<String,Object> json)
Parses a JSON Web Token (JWT) claims set from the specified JSON object representation.Map<String,Object>
toJSONObject()
Returns the JSON object representation of this claims set.Map<String,Object>
toJSONObject(boolean includeClaimsWithNullValues)
Returns the JSON object representation of this claims set.Payload
toPayload()
Returns a JOSE object payload representation of this claims set.String
toString()
Returns a JSON object string representation of this claims set.String
toString(boolean includeClaimsWithNullValues)
Returns a JSON object string representation of this claims set.<T> T
toType(JWTClaimsSetTransformer<T> transformer)
Returns a transformation of this JWT claims set.
-
-
-
Method Detail
-
getRegisteredNames
public static Set<String> getRegisteredNames()
Gets the registered JWT claim names.- Returns:
- The registered claim names, as a unmodifiable set.
-
getIssuer
public String getIssuer()
Gets the issuer (iss
) claim.- Returns:
- The issuer claim,
null
if not specified.
-
getSubject
public String getSubject()
Gets the subject (sub
) claim.- Returns:
- The subject claim,
null
if not specified.
-
getAudience
public List<String> getAudience()
Gets the audience (aud
) claim.- Returns:
- The audience claim, empty list if not specified.
-
getExpirationTime
public Date getExpirationTime()
Gets the expiration time (exp
) claim.- Returns:
- The expiration time,
null
if not specified.
-
getNotBeforeTime
public Date getNotBeforeTime()
Gets the not-before (nbf
) claim.- Returns:
- The not-before claim,
null
if not specified.
-
getIssueTime
public Date getIssueTime()
Gets the issued-at (iat
) claim.- Returns:
- The issued-at claim,
null
if not specified.
-
getJWTID
public String getJWTID()
Gets the JWT ID (jti
) claim.- Returns:
- The JWT ID claim,
null
if not specified.
-
getClaim
public Object getClaim(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,
null
if not specified.
-
getStringClaim
public String getStringClaim(String name) throws ParseException
Gets the specified claim (registered or custom) asString
.- Parameters:
name
- The name of the claim. Must not benull
.- Returns:
- The value of the claim,
null
if not specified. - Throws:
ParseException
- If the claim value is not of the required type.
-
getListClaim
public List<Object> getListClaim(String name) throws ParseException
Gets the specified claims (registered or custom) as aList
list of objects.- Parameters:
name
- The name of the claim. Must not benull
.- Returns:
- The value of the claim,
null
if not specified. - Throws:
ParseException
- If the claim value is not of the required type.
-
getStringArrayClaim
public String[] getStringArrayClaim(String name) throws ParseException
Gets the specified claims (registered or custom) as aString
array.- Parameters:
name
- The name of the claim. Must not benull
.- Returns:
- The value of the claim,
null
if not specified. - Throws:
ParseException
- If the claim value is not of the required type.
-
getStringListClaim
public List<String> getStringListClaim(String name) throws ParseException
Gets the specified claims (registered or custom) as aList
list of strings.- Parameters:
name
- The name of the claim. Must not benull
.- Returns:
- The value of the claim,
null
if not specified. - Throws:
ParseException
- If the claim value is not of the required type.
-
getURIClaim
public URI getURIClaim(String name) throws ParseException
Gets the specified claim (registered or custom) as aURI
.- Parameters:
name
- The name of the claim. Must not benull
.- Returns:
- The value of the claim,
null
if not specified. - Throws:
ParseException
- If the claim couldn't be parsed to a URI.
-
getBooleanClaim
public Boolean getBooleanClaim(String name) throws ParseException
Gets the specified claim (registered or custom) asBoolean
.- Parameters:
name
- The name of the claim. Must not benull
.- Returns:
- The value of the claim,
null
if not specified. - Throws:
ParseException
- If the claim value is not of the required type.
-
getIntegerClaim
public Integer getIntegerClaim(String name) throws ParseException
Gets the specified claim (registered or custom) asInteger
.- Parameters:
name
- The name of the claim. Must not benull
.- Returns:
- The value of the claim,
null
if not specified. - Throws:
ParseException
- If the claim value is not of the required type.
-
getLongClaim
public Long getLongClaim(String name) throws ParseException
Gets the specified claim (registered or custom) asLong
.- Parameters:
name
- The name of the claim. Must not benull
.- Returns:
- The value of the claim,
null
if not specified. - Throws:
ParseException
- If the claim value is not of the required type.
-
getDateClaim
public Date getDateClaim(String name) throws ParseException
Gets 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,
null
if not specified. - Throws:
ParseException
- If the claim value is not of the required type.
-
getFloatClaim
public Float getFloatClaim(String name) throws ParseException
Gets the specified claim (registered or custom) asFloat
.- Parameters:
name
- The name of the claim. Must not benull
.- Returns:
- The value of the claim,
null
if not specified. - Throws:
ParseException
- If the claim value is not of the required type.
-
getDoubleClaim
public Double getDoubleClaim(String name) throws ParseException
Gets the specified claim (registered or custom) asDouble
.- Parameters:
name
- The name of the claim. Must not benull
.- Returns:
- The value of the claim,
null
if not specified. - Throws:
ParseException
- If the claim value is not of the required type.
-
getJSONObjectClaim
public Map<String,Object> getJSONObjectClaim(String name) throws ParseException
Gets 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,
null
if not specified. - Throws:
ParseException
- If the claim value is not of the required type.
-
getClaims
public Map<String,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.Date
instances.- 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 Map<String,Object> toJSONObject()
Returns the JSON object representation of this claims set. The claims are serialised according to their insertion order. Claims withnull
values are not output.- Returns:
- The JSON object representation.
-
toJSONObject
public Map<String,Object> toJSONObject(boolean includeClaimsWithNullValues)
Returns the JSON object representation of this claims set. The claims are serialised according to their insertion order.- Parameters:
includeClaimsWithNullValues
- Iftrue
claims withnull
values will also be output.- Returns:
- The JSON object representation.
-
toString
public String toString()
Returns a JSON object string representation of this claims set. The claims are serialised according to their insertion order. Claims withnull
values are not output.
-
toString
public 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
- Iftrue
claims withnull
values 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(Map<String,Object> json) throws 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:
ParseException
- If the specified JSON object doesn't represent a valid JWT claims set.
-
parse
public static JWTClaimsSet parse(String s) throws 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:
ParseException
- If the specified JSON object string doesn't represent a valid JWT claims set.
-
-