Package com.auth0.jwt
Class JWTCreator.Builder
- java.lang.Object
-
- com.auth0.jwt.JWTCreator.Builder
-
- Enclosing class:
- JWTCreator
public static class JWTCreator.Builder extends java.lang.ObjectThe Builder class holds the Claims that defines the JWT to be created.
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description java.lang.Stringsign(Algorithm algorithm)Creates a new JWT and signs is with the given algorithm.JWTCreator.BuilderwithArrayClaim(java.lang.String name, java.lang.Integer[] items)Add a custom Array Claim with the given items.JWTCreator.BuilderwithArrayClaim(java.lang.String name, java.lang.Long[] items)Add a custom Array Claim with the given items.JWTCreator.BuilderwithArrayClaim(java.lang.String name, java.lang.String[] items)Add a custom Array Claim with the given items.JWTCreator.BuilderwithAudience(java.lang.String... audience)Add a specific Audience ("aud") claim to the Payload.JWTCreator.BuilderwithClaim(java.lang.String name, java.lang.Boolean value)Add a custom Claim value.JWTCreator.BuilderwithClaim(java.lang.String name, java.lang.Double value)Add a custom Claim value.JWTCreator.BuilderwithClaim(java.lang.String name, java.lang.Integer value)Add a custom Claim value.JWTCreator.BuilderwithClaim(java.lang.String name, java.lang.Long value)Add a custom Claim value.JWTCreator.BuilderwithClaim(java.lang.String name, java.lang.String value)Add a custom Claim value.JWTCreator.BuilderwithClaim(java.lang.String name, java.time.Instant value)Add a custom Claim value.JWTCreator.BuilderwithClaim(java.lang.String name, java.util.Date value)Add a custom Claim value.JWTCreator.BuilderwithClaim(java.lang.String name, java.util.List<?> list)Add a custom List Claim with the given items.JWTCreator.BuilderwithClaim(java.lang.String name, java.util.Map<java.lang.String,?> map)Add a custom Map Claim with the given items.JWTCreator.BuilderwithExpiresAt(java.time.Instant expiresAt)Add a specific Expires At ("exp") claim to the payload.JWTCreator.BuilderwithExpiresAt(java.util.Date expiresAt)Add a specific Expires At ("exp") claim to the payload.JWTCreator.BuilderwithHeader(java.lang.String headerClaimsJson)Add specific Claims to set as the Header.JWTCreator.BuilderwithHeader(java.util.Map<java.lang.String,java.lang.Object> headerClaims)Add specific Claims to set as the Header.JWTCreator.BuilderwithIssuedAt(java.time.Instant issuedAt)Add a specific Issued At ("iat") claim to the Payload.JWTCreator.BuilderwithIssuedAt(java.util.Date issuedAt)Add a specific Issued At ("iat") claim to the Payload.JWTCreator.BuilderwithIssuer(java.lang.String issuer)Add a specific Issuer ("iss") claim to the Payload.JWTCreator.BuilderwithJWTId(java.lang.String jwtId)Add a specific JWT Id ("jti") claim to the Payload.JWTCreator.BuilderwithKeyId(java.lang.String keyId)Add a specific Key Id ("kid") claim to the Header.JWTCreator.BuilderwithNotBefore(java.time.Instant notBefore)Add a specific Not Before ("nbf") claim to the Payload.JWTCreator.BuilderwithNotBefore(java.util.Date notBefore)Add a specific Not Before ("nbf") claim to the Payload.JWTCreator.BuilderwithNullClaim(java.lang.String name)Add a custom claim with null value.JWTCreator.BuilderwithPayload(java.lang.String payloadClaimsJson)Add specific Claims to set as the Payload.JWTCreator.BuilderwithPayload(java.util.Map<java.lang.String,?> payloadClaims)Add specific Claims to set as the Payload.JWTCreator.BuilderwithSubject(java.lang.String subject)Add a specific Subject ("sub") claim to the Payload.
-
-
-
Method Detail
-
withHeader
public JWTCreator.Builder withHeader(java.util.Map<java.lang.String,java.lang.Object> headerClaims)
Add specific Claims to set as the Header. If provided map is null then nothing is changed- Parameters:
headerClaims- the values to use as Claims in the token's Header.- Returns:
- this same Builder instance.
-
withHeader
public JWTCreator.Builder withHeader(java.lang.String headerClaimsJson) throws java.lang.IllegalArgumentException
Add specific Claims to set as the Header. If provided json is null then nothing is changed- Parameters:
headerClaimsJson- the values to use as Claims in the token's Header.- Returns:
- this same Builder instance.
- Throws:
java.lang.IllegalArgumentException- if json value has invalid structure
-
withKeyId
public JWTCreator.Builder withKeyId(java.lang.String keyId)
Add a specific Key Id ("kid") claim to the Header. If theAlgorithmused to sign this token was instantiated with a KeyProvider, the 'kid' value will be taken from that provider and this one will be ignored.- Parameters:
keyId- the Key Id value.- Returns:
- this same Builder instance.
-
withIssuer
public JWTCreator.Builder withIssuer(java.lang.String issuer)
Add a specific Issuer ("iss") claim to the Payload.- Parameters:
issuer- the Issuer value.- Returns:
- this same Builder instance.
-
withSubject
public JWTCreator.Builder withSubject(java.lang.String subject)
Add a specific Subject ("sub") claim to the Payload.- Parameters:
subject- the Subject value.- Returns:
- this same Builder instance.
-
withAudience
public JWTCreator.Builder withAudience(java.lang.String... audience)
Add a specific Audience ("aud") claim to the Payload.- Parameters:
audience- the Audience value.- Returns:
- this same Builder instance.
-
withExpiresAt
public JWTCreator.Builder withExpiresAt(java.util.Date expiresAt)
Add a specific Expires At ("exp") claim to the payload. The claim will be written as seconds since the epoch. Milliseconds will be truncated by rounding down to the nearest second.- Parameters:
expiresAt- the Expires At value.- Returns:
- this same Builder instance.
-
withExpiresAt
public JWTCreator.Builder withExpiresAt(java.time.Instant expiresAt)
Add a specific Expires At ("exp") claim to the payload. The claim will be written as seconds since the epoch; Milliseconds will be truncated by rounding down to the nearest second.- Parameters:
expiresAt- the Expires At value.- Returns:
- this same Builder instance.
-
withNotBefore
public JWTCreator.Builder withNotBefore(java.util.Date notBefore)
Add a specific Not Before ("nbf") claim to the Payload. The claim will be written as seconds since the epoch; Milliseconds will be truncated by rounding down to the nearest second.- Parameters:
notBefore- the Not Before value.- Returns:
- this same Builder instance.
-
withNotBefore
public JWTCreator.Builder withNotBefore(java.time.Instant notBefore)
Add a specific Not Before ("nbf") claim to the Payload. The claim will be written as seconds since the epoch; Milliseconds will be truncated by rounding down to the nearest second.- Parameters:
notBefore- the Not Before value.- Returns:
- this same Builder instance.
-
withIssuedAt
public JWTCreator.Builder withIssuedAt(java.util.Date issuedAt)
Add a specific Issued At ("iat") claim to the Payload. The claim will be written as seconds since the epoch; Milliseconds will be truncated by rounding down to the nearest second.- Parameters:
issuedAt- the Issued At value.- Returns:
- this same Builder instance.
-
withIssuedAt
public JWTCreator.Builder withIssuedAt(java.time.Instant issuedAt)
Add a specific Issued At ("iat") claim to the Payload. The claim will be written as seconds since the epoch; Milliseconds will be truncated by rounding down to the nearest second.- Parameters:
issuedAt- the Issued At value.- Returns:
- this same Builder instance.
-
withJWTId
public JWTCreator.Builder withJWTId(java.lang.String jwtId)
Add a specific JWT Id ("jti") claim to the Payload.- Parameters:
jwtId- the Token Id value.- Returns:
- this same Builder instance.
-
withClaim
public JWTCreator.Builder withClaim(java.lang.String name, java.lang.Boolean value) throws java.lang.IllegalArgumentException
Add a custom Claim value.- Parameters:
name- the Claim's name.value- the Claim's value.- Returns:
- this same Builder instance.
- Throws:
java.lang.IllegalArgumentException- if the name is null.
-
withClaim
public JWTCreator.Builder withClaim(java.lang.String name, java.lang.Integer value) throws java.lang.IllegalArgumentException
Add a custom Claim value.- Parameters:
name- the Claim's name.value- the Claim's value.- Returns:
- this same Builder instance.
- Throws:
java.lang.IllegalArgumentException- if the name is null.
-
withClaim
public JWTCreator.Builder withClaim(java.lang.String name, java.lang.Long value) throws java.lang.IllegalArgumentException
Add a custom Claim value.- Parameters:
name- the Claim's name.value- the Claim's value.- Returns:
- this same Builder instance.
- Throws:
java.lang.IllegalArgumentException- if the name is null.
-
withClaim
public JWTCreator.Builder withClaim(java.lang.String name, java.lang.Double value) throws java.lang.IllegalArgumentException
Add a custom Claim value.- Parameters:
name- the Claim's name.value- the Claim's value.- Returns:
- this same Builder instance.
- Throws:
java.lang.IllegalArgumentException- if the name is null.
-
withClaim
public JWTCreator.Builder withClaim(java.lang.String name, java.lang.String value) throws java.lang.IllegalArgumentException
Add a custom Claim value.- Parameters:
name- the Claim's name.value- the Claim's value.- Returns:
- this same Builder instance.
- Throws:
java.lang.IllegalArgumentException- if the name is null.
-
withClaim
public JWTCreator.Builder withClaim(java.lang.String name, java.util.Date value) throws java.lang.IllegalArgumentException
Add a custom Claim value. The claim will be written as seconds since the epoch. Milliseconds will be truncated by rounding down to the nearest second.- Parameters:
name- the Claim's name.value- the Claim's value.- Returns:
- this same Builder instance.
- Throws:
java.lang.IllegalArgumentException- if the name is null.
-
withClaim
public JWTCreator.Builder withClaim(java.lang.String name, java.time.Instant value) throws java.lang.IllegalArgumentException
Add a custom Claim value. The claim will be written as seconds since the epoch. Milliseconds will be truncated by rounding down to the nearest second.- Parameters:
name- the Claim's name.value- the Claim's value.- Returns:
- this same Builder instance.
- Throws:
java.lang.IllegalArgumentException- if the name is null.
-
withClaim
public JWTCreator.Builder withClaim(java.lang.String name, java.util.Map<java.lang.String,?> map) throws java.lang.IllegalArgumentException
Add a custom Map Claim with the given items.Accepted nested types are Map and List with basic types Boolean, Integer, Long, Double, String and Date. Maps cannot contain null keys or values. Lists can contain null elements.
- Parameters:
name- the Claim's name.map- the Claim's key-values.- Returns:
- this same Builder instance.
- Throws:
java.lang.IllegalArgumentException- if the name is null, or if the map contents does not validate.
-
withClaim
public JWTCreator.Builder withClaim(java.lang.String name, java.util.List<?> list) throws java.lang.IllegalArgumentException
Add a custom List Claim with the given items.Accepted nested types are Map and List with basic types Boolean, Integer, Long, Double, String and Date. Maps cannot contain null keys or values. Lists can contain null elements.
- Parameters:
name- the Claim's name.list- the Claim's list of values.- Returns:
- this same Builder instance.
- Throws:
java.lang.IllegalArgumentException- if the name is null, or if the list contents does not validate.
-
withNullClaim
public JWTCreator.Builder withNullClaim(java.lang.String name) throws java.lang.IllegalArgumentException
Add a custom claim with null value.- Parameters:
name- the Claim's name.- Returns:
- this same Builder instance.
- Throws:
java.lang.IllegalArgumentException- if the name is null
-
withArrayClaim
public JWTCreator.Builder withArrayClaim(java.lang.String name, java.lang.String[] items) throws java.lang.IllegalArgumentException
Add a custom Array Claim with the given items.- Parameters:
name- the Claim's name.items- the Claim's value.- Returns:
- this same Builder instance.
- Throws:
java.lang.IllegalArgumentException- if the name is null.
-
withArrayClaim
public JWTCreator.Builder withArrayClaim(java.lang.String name, java.lang.Integer[] items) throws java.lang.IllegalArgumentException
Add a custom Array Claim with the given items.- Parameters:
name- the Claim's name.items- the Claim's value.- Returns:
- this same Builder instance.
- Throws:
java.lang.IllegalArgumentException- if the name is null.
-
withArrayClaim
public JWTCreator.Builder withArrayClaim(java.lang.String name, java.lang.Long[] items) throws java.lang.IllegalArgumentException
Add a custom Array Claim with the given items.- Parameters:
name- the Claim's name.items- the Claim's value.- Returns:
- this same Builder instance.
- Throws:
java.lang.IllegalArgumentException- if the name is null
-
withPayload
public JWTCreator.Builder withPayload(java.util.Map<java.lang.String,?> payloadClaims) throws java.lang.IllegalArgumentException
Add specific Claims to set as the Payload. If the provided map is null then nothing is changed.Accepted types are Map and List with basic types Boolean, Integer, Long, Double, String and Date. Maps and Lists can contain null elements.
If any of the claims are invalid, none will be added.
- Parameters:
payloadClaims- the values to use as Claims in the token's payload.- Returns:
- this same Builder instance.
- Throws:
java.lang.IllegalArgumentException- if any of the claim keys or null, or if the values are not of a supported type.
-
withPayload
public JWTCreator.Builder withPayload(java.lang.String payloadClaimsJson) throws java.lang.IllegalArgumentException
Add specific Claims to set as the Payload. If the provided json is null then nothing is changed.If any of the claims are invalid, none will be added.
- Parameters:
payloadClaimsJson- the values to use as Claims in the token's payload.- Returns:
- this same Builder instance.
- Throws:
java.lang.IllegalArgumentException- if any of the claim keys or null, or if the values are not of a supported type, or if json value has invalid structure.
-
sign
public java.lang.String sign(Algorithm algorithm) throws java.lang.IllegalArgumentException, JWTCreationException
Creates a new JWT and signs is with the given algorithm.- Parameters:
algorithm- used to sign the JWT- Returns:
- a new JWT token
- Throws:
java.lang.IllegalArgumentException- if the provided algorithm is null.JWTCreationException- if the claims could not be converted to a valid JSON or there was a problem with the signing key.
-
-