Package com.auth0.jwt

Class JWTCreator.Builder

  • Enclosing class:
    JWTCreator

    public static class JWTCreator.Builder
    extends java.lang.Object
    The Builder class holds the Claims that defines the JWT to be created.
    • Field Detail

      • payloadClaims

        private final java.util.Map<java.lang.String,​java.lang.Object> payloadClaims
      • headerClaims

        private java.util.Map<java.lang.String,​java.lang.Object> headerClaims
    • Constructor Detail

      • Builder

        Builder()
    • 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 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​(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)