Class JwtUtil


  • public final class JwtUtil
    extends Object
    Utilities for JWT and JWK parsing.
    • Method Detail

      • asBigInteger

        public static BigInteger asBigInteger​(JsonObject json,
                                              String key,
                                              String description)
                                       throws JwtException
        Extract a key value from json object that is base64-url encoded and convert it to big integer.
        Parameters:
        json - JsonObject to read key from
        key - key of the value we want to read
        description - description of the field for error handling
        Returns:
        BigInteger value
        Throws:
        JwtException - in case the key is not present or is of invalid content
      • asString

        public static String asString​(JsonObject json,
                                      String key,
                                      String description)
                               throws JwtException
        Extract a key value from json object that is string.
        Parameters:
        json - JsonObject to read key from
        key - key of the value we want to read
        description - description of the field for error handling
        Returns:
        String value
        Throws:
        JwtException - in case the key is not present or is of invalid content
      • getBigInteger

        public static Optional<BigInteger> getBigInteger​(JsonObject json,
                                                         String key,
                                                         String description)
                                                  throws JwtException
        Extract a key value from json object that is base64-url encoded and convert it to big integer if present.
        Parameters:
        json - JsonObject to read key from
        key - key of the value we want to read
        description - description of the field for error handling
        Returns:
        BigInteger value if present
        Throws:
        JwtException - in case the key is of invalid content
      • getStrings

        public static Optional<List<String>> getStrings​(JsonObject json,
                                                        String key)
                                                 throws JwtException
        Extract a key value from json object that is a list of strings if present.
        Parameters:
        json - JsonObject to read key from
        key - key of the value we want to read
        Returns:
        List of String value if present
        Throws:
        JwtException - in case the key is of invalid content
      • getString

        public static Optional<String> getString​(JsonObject json,
                                                 String key)
                                          throws JwtException
        Extract a key value from json object that is string if present.
        Parameters:
        json - JsonObject to read key from
        key - key of the value we want to read
        Returns:
        String value if present
        Throws:
        JwtException - in case the key is of invalid content
      • getByteArray

        public static Optional<byte[]> getByteArray​(JsonObject json,
                                                    String key,
                                                    String description)
                                             throws JwtException
        Extract a key value from json object that is a base64-url encoded byte array, if present.
        Parameters:
        json - JsonObject to read key from
        key - key of the value we want to read
        description - description of the field for error handling
        Returns:
        byte array value if present
        Throws:
        JwtException - in case the key is of invalid content or not base64 encoded
      • asByteArray

        public static byte[] asByteArray​(JsonObject json,
                                         String key,
                                         String description)
                                  throws JwtException
        Extract a key value from json object that is a base64-url encoded byte array.
        Parameters:
        json - JsonObject to read key from
        key - key of the value we want to read
        description - description of the field for error handling
        Returns:
        byte array value
        Throws:
        JwtException - in case the key is not present, is of invalid content or not base64 encoded
      • getKeyFactory

        public static KeyFactory getKeyFactory​(String algorithm)
                                        throws JwtException
        Create a key factory for algorithm.
        Parameters:
        algorithm - security algorithm (such as RSA, EC)
        Returns:
        KeyFactory instance
        Throws:
        JwtException - in case the algorithm is invalid
      • getSignature

        public static Signature getSignature​(String signatureAlgorithm)
                                      throws JwtException
        Create a signature for algorithm.
        Parameters:
        signatureAlgorithm - security signature algorithm (such as "SHA512withRSA")
        Returns:
        Signature instance
        Throws:
        JwtException - in case the algorithm is invalid or not supported by this JVM
      • getMac

        public static Mac getMac​(String algorithm)
                          throws JwtException
        Create a MAC for algorithm. Similar to signature for symmetric ciphers (such as "HmacSHA256").
        Parameters:
        algorithm - security MAC algorithm
        Returns:
        Mac instance
        Throws:
        JwtException - in case the algorithm is invalid or not supported by this JVM
      • transformToJson

        public static Map<String,​JsonValue> transformToJson​(Map<String,​Object> claims)
        Transform a map of strings to objects to a map of string to JSON values. Each object is checked for type and if supported, transformed to appropriate JSON value.
        Parameters:
        claims - map to transform from
        Returns:
        resulting map
      • toObject

        public static Object toObject​(JsonValue jsonValue)
        Transform from json to object.
        Parameters:
        jsonValue - json value
        Returns:
        object most correct for the type, or string value if not understood (e.g. json object)