diff --git a/lib/php-jwt/README.md b/lib/php-jwt/README.md
index b1a7a3a2063..9c8b5455b69 100644
--- a/lib/php-jwt/README.md
+++ b/lib/php-jwt/README.md
@@ -23,7 +23,7 @@ Example
use \Firebase\JWT\JWT;
$key = "example_key";
-$token = array(
+$payload = array(
"iss" => "http://example.org",
"aud" => "http://example.com",
"iat" => 1356999524,
@@ -36,7 +36,7 @@ $token = array(
* https://tools.ietf.org/html/draft-ietf-jose-json-web-algorithms-40
* for a list of spec-compliant algorithms.
*/
-$jwt = JWT::encode($token, $key);
+$jwt = JWT::encode($payload, $key);
$decoded = JWT::decode($jwt, $key, array('HS256'));
print_r($decoded);
@@ -93,14 +93,14 @@ ehde/zUxo6UvS7UrBQIDAQAB
-----END PUBLIC KEY-----
EOD;
-$token = array(
+$payload = array(
"iss" => "example.org",
"aud" => "example.com",
"iat" => 1356999524,
"nbf" => 1357000000
);
-$jwt = JWT::encode($token, $privateKey, 'RS256');
+$jwt = JWT::encode($payload, $privateKey, 'RS256');
echo "Encode:\n" . print_r($jwt, true) . "\n";
$decoded = JWT::decode($jwt, $publicKey, array('RS256'));
diff --git a/lib/php-jwt/composer.json b/lib/php-jwt/composer.json
index b76ffd19106..25d1cfa96ad 100644
--- a/lib/php-jwt/composer.json
+++ b/lib/php-jwt/composer.json
@@ -2,6 +2,10 @@
"name": "firebase/php-jwt",
"description": "A simple library to encode and decode JSON Web Tokens (JWT) in PHP. Should conform to the current spec.",
"homepage": "https://github.com/firebase/php-jwt",
+ "keywords": [
+ "php",
+ "jwt"
+ ],
"authors": [
{
"name": "Neuman Vong",
@@ -24,6 +28,6 @@
}
},
"require-dev": {
- "phpunit/phpunit": " 4.8.35"
+ "phpunit/phpunit": ">=4.8 <=9"
}
}
diff --git a/lib/php-jwt/src/BeforeValidException.php b/lib/php-jwt/src/BeforeValidException.php
index a6ee2f7c690..fdf82bd9429 100644
--- a/lib/php-jwt/src/BeforeValidException.php
+++ b/lib/php-jwt/src/BeforeValidException.php
@@ -3,5 +3,4 @@ namespace Firebase\JWT;
class BeforeValidException extends \UnexpectedValueException
{
-
}
diff --git a/lib/php-jwt/src/ExpiredException.php b/lib/php-jwt/src/ExpiredException.php
index 3597370a6de..7f7d0568248 100644
--- a/lib/php-jwt/src/ExpiredException.php
+++ b/lib/php-jwt/src/ExpiredException.php
@@ -3,5 +3,4 @@ namespace Firebase\JWT;
class ExpiredException extends \UnexpectedValueException
{
-
}
diff --git a/lib/php-jwt/src/JWK.php b/lib/php-jwt/src/JWK.php
new file mode 100644
index 00000000000..1d273917403
--- /dev/null
+++ b/lib/php-jwt/src/JWK.php
@@ -0,0 +1,171 @@
+
+ * @license http://opensource.org/licenses/BSD-3-Clause 3-clause BSD
+ * @link https://github.com/firebase/php-jwt
+ */
+class JWK
+{
+ /**
+ * Parse a set of JWK keys
+ *
+ * @param array $jwks The JSON Web Key Set as an associative array
+ *
+ * @return array An associative array that represents the set of keys
+ *
+ * @throws InvalidArgumentException Provided JWK Set is empty
+ * @throws UnexpectedValueException Provided JWK Set was invalid
+ * @throws DomainException OpenSSL failure
+ *
+ * @uses parseKey
+ */
+ public static function parseKeySet(array $jwks)
+ {
+ $keys = array();
+
+ if (!isset($jwks['keys'])) {
+ throw new UnexpectedValueException('"keys" member must exist in the JWK Set');
+ }
+ if (empty($jwks['keys'])) {
+ throw new InvalidArgumentException('JWK Set did not contain any keys');
+ }
+
+ foreach ($jwks['keys'] as $k => $v) {
+ $kid = isset($v['kid']) ? $v['kid'] : $k;
+ if ($key = self::parseKey($v)) {
+ $keys[$kid] = $key;
+ }
+ }
+
+ if (0 === \count($keys)) {
+ throw new UnexpectedValueException('No supported algorithms found in JWK Set');
+ }
+
+ return $keys;
+ }
+
+ /**
+ * Parse a JWK key
+ *
+ * @param array $jwk An individual JWK
+ *
+ * @return resource|array An associative array that represents the key
+ *
+ * @throws InvalidArgumentException Provided JWK is empty
+ * @throws UnexpectedValueException Provided JWK was invalid
+ * @throws DomainException OpenSSL failure
+ *
+ * @uses createPemFromModulusAndExponent
+ */
+ private static function parseKey(array $jwk)
+ {
+ if (empty($jwk)) {
+ throw new InvalidArgumentException('JWK must not be empty');
+ }
+ if (!isset($jwk['kty'])) {
+ throw new UnexpectedValueException('JWK must contain a "kty" parameter');
+ }
+
+ switch ($jwk['kty']) {
+ case 'RSA':
+ if (\array_key_exists('d', $jwk)) {
+ throw new UnexpectedValueException('RSA private keys are not supported');
+ }
+ if (!isset($jwk['n']) || !isset($jwk['e'])) {
+ throw new UnexpectedValueException('RSA keys must contain values for both "n" and "e"');
+ }
+
+ $pem = self::createPemFromModulusAndExponent($jwk['n'], $jwk['e']);
+ $publicKey = \openssl_pkey_get_public($pem);
+ if (false === $publicKey) {
+ throw new DomainException(
+ 'OpenSSL error: ' . \openssl_error_string()
+ );
+ }
+ return $publicKey;
+ default:
+ // Currently only RSA is supported
+ break;
+ }
+ }
+
+ /**
+ * Create a public key represented in PEM format from RSA modulus and exponent information
+ *
+ * @param string $n The RSA modulus encoded in Base64
+ * @param string $e The RSA exponent encoded in Base64
+ *
+ * @return string The RSA public key represented in PEM format
+ *
+ * @uses encodeLength
+ */
+ private static function createPemFromModulusAndExponent($n, $e)
+ {
+ $modulus = JWT::urlsafeB64Decode($n);
+ $publicExponent = JWT::urlsafeB64Decode($e);
+
+ $components = array(
+ 'modulus' => \pack('Ca*a*', 2, self::encodeLength(\strlen($modulus)), $modulus),
+ 'publicExponent' => \pack('Ca*a*', 2, self::encodeLength(\strlen($publicExponent)), $publicExponent)
+ );
+
+ $rsaPublicKey = \pack(
+ 'Ca*a*a*',
+ 48,
+ self::encodeLength(\strlen($components['modulus']) + \strlen($components['publicExponent'])),
+ $components['modulus'],
+ $components['publicExponent']
+ );
+
+ // sequence(oid(1.2.840.113549.1.1.1), null)) = rsaEncryption.
+ $rsaOID = \pack('H*', '300d06092a864886f70d0101010500'); // hex version of MA0GCSqGSIb3DQEBAQUA
+ $rsaPublicKey = \chr(0) . $rsaPublicKey;
+ $rsaPublicKey = \chr(3) . self::encodeLength(\strlen($rsaPublicKey)) . $rsaPublicKey;
+
+ $rsaPublicKey = \pack(
+ 'Ca*a*',
+ 48,
+ self::encodeLength(\strlen($rsaOID . $rsaPublicKey)),
+ $rsaOID . $rsaPublicKey
+ );
+
+ $rsaPublicKey = "-----BEGIN PUBLIC KEY-----\r\n" .
+ \chunk_split(\base64_encode($rsaPublicKey), 64) .
+ '-----END PUBLIC KEY-----';
+
+ return $rsaPublicKey;
+ }
+
+ /**
+ * DER-encode the length
+ *
+ * DER supports lengths up to (2**8)**127, however, we'll only support lengths up to (2**8)**4. See
+ * {@link http://itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf#p=13 X.690 paragraph 8.1.3} for more information.
+ *
+ * @param int $length
+ * @return string
+ */
+ private static function encodeLength($length)
+ {
+ if ($length <= 0x7F) {
+ return \chr($length);
+ }
+
+ $temp = \ltrim(\pack('N', $length), \chr(0));
+
+ return \pack('Ca*', 0x80 | \strlen($temp), $temp);
+ }
+}
diff --git a/lib/php-jwt/src/JWT.php b/lib/php-jwt/src/JWT.php
index 33e2ed4edf3..4ccc1a96c23 100644
--- a/lib/php-jwt/src/JWT.php
+++ b/lib/php-jwt/src/JWT.php
@@ -1,6 +1,7 @@
array('openssl', 'SHA256'),
'HS256' => array('hash_hmac', 'SHA256'),
- 'HS512' => array('hash_hmac', 'SHA512'),
'HS384' => array('hash_hmac', 'SHA384'),
+ 'HS512' => array('hash_hmac', 'SHA512'),
'RS256' => array('openssl', 'SHA256'),
'RS384' => array('openssl', 'SHA384'),
'RS512' => array('openssl', 'SHA512'),
@@ -49,11 +54,11 @@ class JWT
/**
* Decodes a JWT string into a PHP object.
*
- * @param string $jwt The JWT
- * @param string|array $key The key, or map of keys.
- * If the algorithm used is asymmetric, this is the public key
- * @param array $allowed_algs List of supported verification algorithms
- * Supported algorithms are 'HS256', 'HS384', 'HS512' and 'RS256'
+ * @param string $jwt The JWT
+ * @param string|array|resource $key The key, or map of keys.
+ * If the algorithm used is asymmetric, this is the public key
+ * @param array $allowed_algs List of supported verification algorithms
+ * Supported algorithms are 'ES256', 'HS256', 'HS384', 'HS512', 'RS256', 'RS384', and 'RS512'
*
* @return object The JWT's payload as a PHP object
*
@@ -68,13 +73,13 @@ class JWT
*/
public static function decode($jwt, $key, array $allowed_algs = array())
{
- $timestamp = is_null(static::$timestamp) ? time() : static::$timestamp;
+ $timestamp = \is_null(static::$timestamp) ? \time() : static::$timestamp;
if (empty($key)) {
throw new InvalidArgumentException('Key may not be empty');
}
- $tks = explode('.', $jwt);
- if (count($tks) != 3) {
+ $tks = \explode('.', $jwt);
+ if (\count($tks) != 3) {
throw new UnexpectedValueException('Wrong number of segments');
}
list($headb64, $bodyb64, $cryptob64) = $tks;
@@ -93,10 +98,15 @@ class JWT
if (empty(static::$supported_algs[$header->alg])) {
throw new UnexpectedValueException('Algorithm not supported');
}
- if (!in_array($header->alg, $allowed_algs)) {
+ if (!\in_array($header->alg, $allowed_algs)) {
throw new UnexpectedValueException('Algorithm not allowed');
}
- if (is_array($key) || $key instanceof \ArrayAccess) {
+ if ($header->alg === 'ES256') {
+ // OpenSSL expects an ASN.1 DER sequence for ES256 signatures
+ $sig = self::signatureToDER($sig);
+ }
+
+ if (\is_array($key) || $key instanceof \ArrayAccess) {
if (isset($header->kid)) {
if (!isset($key[$header->kid])) {
throw new UnexpectedValueException('"kid" invalid, unable to lookup correct key');
@@ -112,11 +122,11 @@ class JWT
throw new SignatureInvalidException('Signature verification failed');
}
- // Check if the nbf if it is defined. This is the time that the
+ // Check the nbf if it is defined. This is the time that the
// token can actually be used. If it's not yet that time, abort.
if (isset($payload->nbf) && $payload->nbf > ($timestamp + static::$leeway)) {
throw new BeforeValidException(
- 'Cannot handle token prior to ' . date(DateTime::ISO8601, $payload->nbf)
+ 'Cannot handle token prior to ' . \date(DateTime::ISO8601, $payload->nbf)
);
}
@@ -125,7 +135,7 @@ class JWT
// correctly used the nbf claim).
if (isset($payload->iat) && $payload->iat > ($timestamp + static::$leeway)) {
throw new BeforeValidException(
- 'Cannot handle token prior to ' . date(DateTime::ISO8601, $payload->iat)
+ 'Cannot handle token prior to ' . \date(DateTime::ISO8601, $payload->iat)
);
}
@@ -144,7 +154,7 @@ class JWT
* @param string $key The secret key.
* If the algorithm used is asymmetric, this is the private key
* @param string $alg The signing algorithm.
- * Supported algorithms are 'HS256', 'HS384', 'HS512' and 'RS256'
+ * Supported algorithms are 'ES256', 'HS256', 'HS384', 'HS512', 'RS256', 'RS384', and 'RS512'
* @param mixed $keyId
* @param array $head An array with header elements to attach
*
@@ -159,18 +169,18 @@ class JWT
if ($keyId !== null) {
$header['kid'] = $keyId;
}
- if ( isset($head) && is_array($head) ) {
- $header = array_merge($head, $header);
+ if (isset($head) && \is_array($head)) {
+ $header = \array_merge($head, $header);
}
$segments = array();
$segments[] = static::urlsafeB64Encode(static::jsonEncode($header));
$segments[] = static::urlsafeB64Encode(static::jsonEncode($payload));
- $signing_input = implode('.', $segments);
+ $signing_input = \implode('.', $segments);
$signature = static::sign($signing_input, $key, $alg);
$segments[] = static::urlsafeB64Encode($signature);
- return implode('.', $segments);
+ return \implode('.', $segments);
}
/**
@@ -179,7 +189,7 @@ class JWT
* @param string $msg The message to sign
* @param string|resource $key The secret key
* @param string $alg The signing algorithm.
- * Supported algorithms are 'HS256', 'HS384', 'HS512' and 'RS256'
+ * Supported algorithms are 'ES256', 'HS256', 'HS384', 'HS512', 'RS256', 'RS384', and 'RS512'
*
* @return string An encrypted message
*
@@ -191,15 +201,18 @@ class JWT
throw new DomainException('Algorithm not supported');
}
list($function, $algorithm) = static::$supported_algs[$alg];
- switch($function) {
+ switch ($function) {
case 'hash_hmac':
- return hash_hmac($algorithm, $msg, $key, true);
+ return \hash_hmac($algorithm, $msg, $key, true);
case 'openssl':
$signature = '';
- $success = openssl_sign($msg, $signature, $key, $algorithm);
+ $success = \openssl_sign($msg, $signature, $key, $algorithm);
if (!$success) {
throw new DomainException("OpenSSL unable to sign data");
} else {
+ if ($alg === 'ES256') {
+ $signature = self::signatureFromDER($signature, 256);
+ }
return $signature;
}
}
@@ -225,9 +238,9 @@ class JWT
}
list($function, $algorithm) = static::$supported_algs[$alg];
- switch($function) {
+ switch ($function) {
case 'openssl':
- $success = openssl_verify($msg, $signature, $key, $algorithm);
+ $success = \openssl_verify($msg, $signature, $key, $algorithm);
if ($success === 1) {
return true;
} elseif ($success === 0) {
@@ -235,19 +248,19 @@ class JWT
}
// returns 1 on success, 0 on failure, -1 on error.
throw new DomainException(
- 'OpenSSL error: ' . openssl_error_string()
+ 'OpenSSL error: ' . \openssl_error_string()
);
case 'hash_hmac':
default:
- $hash = hash_hmac($algorithm, $msg, $key, true);
- if (function_exists('hash_equals')) {
- return hash_equals($signature, $hash);
+ $hash = \hash_hmac($algorithm, $msg, $key, true);
+ if (\function_exists('hash_equals')) {
+ return \hash_equals($signature, $hash);
}
- $len = min(static::safeStrlen($signature), static::safeStrlen($hash));
+ $len = \min(static::safeStrlen($signature), static::safeStrlen($hash));
$status = 0;
for ($i = 0; $i < $len; $i++) {
- $status |= (ord($signature[$i]) ^ ord($hash[$i]));
+ $status |= (\ord($signature[$i]) ^ \ord($hash[$i]));
}
$status |= (static::safeStrlen($signature) ^ static::safeStrlen($hash));
@@ -266,23 +279,23 @@ class JWT
*/
public static function jsonDecode($input)
{
- if (version_compare(PHP_VERSION, '5.4.0', '>=') && !(defined('JSON_C_VERSION') && PHP_INT_SIZE > 4)) {
+ if (\version_compare(PHP_VERSION, '5.4.0', '>=') && !(\defined('JSON_C_VERSION') && PHP_INT_SIZE > 4)) {
/** In PHP >=5.4.0, json_decode() accepts an options parameter, that allows you
* to specify that large ints (like Steam Transaction IDs) should be treated as
* strings, rather than the PHP default behaviour of converting them to floats.
*/
- $obj = json_decode($input, false, 512, JSON_BIGINT_AS_STRING);
+ $obj = \json_decode($input, false, 512, JSON_BIGINT_AS_STRING);
} else {
/** Not all servers will support that, however, so for older versions we must
* manually detect large ints in the JSON string and quote them (thus converting
*them to strings) before decoding, hence the preg_replace() call.
*/
- $max_int_length = strlen((string) PHP_INT_MAX) - 1;
- $json_without_bigints = preg_replace('/:\s*(-?\d{'.$max_int_length.',})/', ': "$1"', $input);
- $obj = json_decode($json_without_bigints);
+ $max_int_length = \strlen((string) PHP_INT_MAX) - 1;
+ $json_without_bigints = \preg_replace('/:\s*(-?\d{'.$max_int_length.',})/', ': "$1"', $input);
+ $obj = \json_decode($json_without_bigints);
}
- if (function_exists('json_last_error') && $errno = json_last_error()) {
+ if ($errno = \json_last_error()) {
static::handleJsonError($errno);
} elseif ($obj === null && $input !== 'null') {
throw new DomainException('Null result with non-null input');
@@ -301,8 +314,8 @@ class JWT
*/
public static function jsonEncode($input)
{
- $json = json_encode($input);
- if (function_exists('json_last_error') && $errno = json_last_error()) {
+ $json = \json_encode($input);
+ if ($errno = \json_last_error()) {
static::handleJsonError($errno);
} elseif ($json === 'null' && $input !== null) {
throw new DomainException('Null result with non-null input');
@@ -319,12 +332,12 @@ class JWT
*/
public static function urlsafeB64Decode($input)
{
- $remainder = strlen($input) % 4;
+ $remainder = \strlen($input) % 4;
if ($remainder) {
$padlen = 4 - $remainder;
- $input .= str_repeat('=', $padlen);
+ $input .= \str_repeat('=', $padlen);
}
- return base64_decode(strtr($input, '-_', '+/'));
+ return \base64_decode(\strtr($input, '-_', '+/'));
}
/**
@@ -336,7 +349,7 @@ class JWT
*/
public static function urlsafeB64Encode($input)
{
- return str_replace('=', '', strtr(base64_encode($input), '+/', '-_'));
+ return \str_replace('=', '', \strtr(\base64_encode($input), '+/', '-_'));
}
/**
@@ -365,15 +378,135 @@ class JWT
/**
* Get the number of bytes in cryptographic strings.
*
- * @param string
+ * @param string $str
*
* @return int
*/
private static function safeStrlen($str)
{
- if (function_exists('mb_strlen')) {
- return mb_strlen($str, '8bit');
+ if (\function_exists('mb_strlen')) {
+ return \mb_strlen($str, '8bit');
}
- return strlen($str);
+ return \strlen($str);
+ }
+
+ /**
+ * Convert an ECDSA signature to an ASN.1 DER sequence
+ *
+ * @param string $sig The ECDSA signature to convert
+ * @return string The encoded DER object
+ */
+ private static function signatureToDER($sig)
+ {
+ // Separate the signature into r-value and s-value
+ list($r, $s) = \str_split($sig, (int) (\strlen($sig) / 2));
+
+ // Trim leading zeros
+ $r = \ltrim($r, "\x00");
+ $s = \ltrim($s, "\x00");
+
+ // Convert r-value and s-value from unsigned big-endian integers to
+ // signed two's complement
+ if (\ord($r[0]) > 0x7f) {
+ $r = "\x00" . $r;
+ }
+ if (\ord($s[0]) > 0x7f) {
+ $s = "\x00" . $s;
+ }
+
+ return self::encodeDER(
+ self::ASN1_SEQUENCE,
+ self::encodeDER(self::ASN1_INTEGER, $r) .
+ self::encodeDER(self::ASN1_INTEGER, $s)
+ );
+ }
+
+ /**
+ * Encodes a value into a DER object.
+ *
+ * @param int $type DER tag
+ * @param string $value the value to encode
+ * @return string the encoded object
+ */
+ private static function encodeDER($type, $value)
+ {
+ $tag_header = 0;
+ if ($type === self::ASN1_SEQUENCE) {
+ $tag_header |= 0x20;
+ }
+
+ // Type
+ $der = \chr($tag_header | $type);
+
+ // Length
+ $der .= \chr(\strlen($value));
+
+ return $der . $value;
+ }
+
+ /**
+ * Encodes signature from a DER object.
+ *
+ * @param string $der binary signature in DER format
+ * @param int $keySize the number of bits in the key
+ * @return string the signature
+ */
+ private static function signatureFromDER($der, $keySize)
+ {
+ // OpenSSL returns the ECDSA signatures as a binary ASN.1 DER SEQUENCE
+ list($offset, $_) = self::readDER($der);
+ list($offset, $r) = self::readDER($der, $offset);
+ list($offset, $s) = self::readDER($der, $offset);
+
+ // Convert r-value and s-value from signed two's compliment to unsigned
+ // big-endian integers
+ $r = \ltrim($r, "\x00");
+ $s = \ltrim($s, "\x00");
+
+ // Pad out r and s so that they are $keySize bits long
+ $r = \str_pad($r, $keySize / 8, "\x00", STR_PAD_LEFT);
+ $s = \str_pad($s, $keySize / 8, "\x00", STR_PAD_LEFT);
+
+ return $r . $s;
+ }
+
+ /**
+ * Reads binary DER-encoded data and decodes into a single object
+ *
+ * @param string $der the binary data in DER format
+ * @param int $offset the offset of the data stream containing the object
+ * to decode
+ * @return array [$offset, $data] the new offset and the decoded object
+ */
+ private static function readDER($der, $offset = 0)
+ {
+ $pos = $offset;
+ $size = \strlen($der);
+ $constructed = (\ord($der[$pos]) >> 5) & 0x01;
+ $type = \ord($der[$pos++]) & 0x1f;
+
+ // Length
+ $len = \ord($der[$pos++]);
+ if ($len & 0x80) {
+ $n = $len & 0x1f;
+ $len = 0;
+ while ($n-- && $pos < $size) {
+ $len = ($len << 8) | \ord($der[$pos++]);
+ }
+ }
+
+ // Value
+ if ($type == self::ASN1_BIT_STRING) {
+ $pos++; // Skip the first contents octet (padding indicator)
+ $data = \substr($der, $pos, $len - 1);
+ $pos += $len - 1;
+ } elseif (!$constructed) {
+ $data = \substr($der, $pos, $len);
+ $pos += $len;
+ } else {
+ $data = null;
+ }
+
+ return array($pos, $data);
}
}
diff --git a/lib/php-jwt/src/SignatureInvalidException.php b/lib/php-jwt/src/SignatureInvalidException.php
index 27332b21bea..87cb34df79b 100644
--- a/lib/php-jwt/src/SignatureInvalidException.php
+++ b/lib/php-jwt/src/SignatureInvalidException.php
@@ -3,5 +3,4 @@ namespace Firebase\JWT;
class SignatureInvalidException extends \UnexpectedValueException
{
-
}
diff --git a/lib/thirdpartylibs.xml b/lib/thirdpartylibs.xml
index 9c01c712051..f90da3dd580 100644
--- a/lib/thirdpartylibs.xml
+++ b/lib/thirdpartylibs.xml
@@ -299,7 +299,7 @@
php-jwt
A simple library to encode and decode JSON Web Tokens (JWT) in PHP, conforming to RFC 7519
BSD
- 5.0.0
+ 5.2.0
3-Clause