Package com.auth0.jwt

Class JWTCreator.Builder

java.lang.Object
com.auth0.jwt.JWTCreator.Builder
Enclosing class:
JWTCreator

public static class JWTCreator.Builder extends Object
The Builder class holds the Claims that defines the JWT to be created.
  • Method Details

    • withHeader

      public JWTCreator.Builder withHeader(Map<String,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(String headerClaimsJson) throws 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:
      IllegalArgumentException - if json value has invalid structure
    • withKeyId

      public JWTCreator.Builder withKeyId(String keyId)
      Add a specific Key Id ("kid") claim to the Header. If the Algorithm used 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(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(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(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(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(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(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(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(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(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(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(String name, Boolean value) throws IllegalArgumentException
      Add a custom Claim value.
      Parameters:
      name - the Claim's name.
      value - the Claim's value.
      Returns:
      this same Builder instance.
      Throws:
      IllegalArgumentException - if the name is null.
    • withClaim

      public JWTCreator.Builder withClaim(String name, Integer value) throws IllegalArgumentException
      Add a custom Claim value.
      Parameters:
      name - the Claim's name.
      value - the Claim's value.
      Returns:
      this same Builder instance.
      Throws:
      IllegalArgumentException - if the name is null.
    • withClaim

      public JWTCreator.Builder withClaim(String name, Long value) throws IllegalArgumentException
      Add a custom Claim value.
      Parameters:
      name - the Claim's name.
      value - the Claim's value.
      Returns:
      this same Builder instance.
      Throws:
      IllegalArgumentException - if the name is null.
    • withClaim

      public JWTCreator.Builder withClaim(String name, Double value) throws IllegalArgumentException
      Add a custom Claim value.
      Parameters:
      name - the Claim's name.
      value - the Claim's value.
      Returns:
      this same Builder instance.
      Throws:
      IllegalArgumentException - if the name is null.
    • withClaim

      public JWTCreator.Builder withClaim(String name, String value) throws IllegalArgumentException
      Add a custom Claim value.
      Parameters:
      name - the Claim's name.
      value - the Claim's value.
      Returns:
      this same Builder instance.
      Throws:
      IllegalArgumentException - if the name is null.
    • withClaim

      public JWTCreator.Builder withClaim(String name, Date value) throws 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:
      IllegalArgumentException - if the name is null.
    • withClaim

      public JWTCreator.Builder withClaim(String name, Instant value) throws 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:
      IllegalArgumentException - if the name is null.
    • withClaim

      public JWTCreator.Builder withClaim(String name, Map<String,?> map) throws 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:
      IllegalArgumentException - if the name is null, or if the map contents does not validate.
    • withClaim

      public JWTCreator.Builder withClaim(String name, List<?> list) throws 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:
      IllegalArgumentException - if the name is null, or if the list contents does not validate.
    • withNullClaim

      public JWTCreator.Builder withNullClaim(String name) throws IllegalArgumentException
      Add a custom claim with null value.
      Parameters:
      name - the Claim's name.
      Returns:
      this same Builder instance.
      Throws:
      IllegalArgumentException - if the name is null
    • withArrayClaim

      public JWTCreator.Builder withArrayClaim(String name, String[] items) throws 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:
      IllegalArgumentException - if the name is null.
    • withArrayClaim

      public JWTCreator.Builder withArrayClaim(String name, Integer[] items) throws 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:
      IllegalArgumentException - if the name is null.
    • withArrayClaim

      public JWTCreator.Builder withArrayClaim(String name, Long[] items) throws 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:
      IllegalArgumentException - if the name is null
    • withPayload

      public JWTCreator.Builder withPayload(Map<String,?> payloadClaims) throws 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:
      IllegalArgumentException - if any of the claim keys or null, or if the values are not of a supported type.
    • withPayload

      public JWTCreator.Builder withPayload(String payloadClaimsJson) throws 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:
      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

      Creates a new JWT and signs is with the given algorithm.
      Parameters:
      algorithm - used to sign the JWT
      Returns:
      a new JWT token
      Throws:
      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.