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.
-
-
Field Summary
Fields Modifier and Type Field Description private java.util.Map<java.lang.String,java.lang.Object>headerClaimsprivate java.util.Map<java.lang.String,java.lang.Object>payloadClaims
-
Constructor Summary
Constructors Constructor Description Builder()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private voidaddClaim(java.lang.String name, java.lang.Object value)private voidassertNonNull(java.lang.String name)java.lang.Stringsign(Algorithm algorithm)Creates a new JWT and signs is with the given algorithmJWTCreator.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.util.Date value)Add a custom Claim value.JWTCreator.BuilderwithExpiresAt(java.util.Date expiresAt)Add a specific Expires At ("exp") claim to the Payload.JWTCreator.BuilderwithHeader(java.util.Map<java.lang.String,java.lang.Object> headerClaims)Add specific Claims to set as the Header.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.util.Date notBefore)Add a specific Not Before ("nbf") claim to 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.- Parameters:
headerClaims- the values to use as Claims in the token's Header.- Returns:
- this same Builder instance.
-
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.- 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.- 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.- 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.- 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.
-
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.
-
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.
-
assertNonNull
private void assertNonNull(java.lang.String name)
-
addClaim
private void addClaim(java.lang.String name, java.lang.Object value)
-
-