From 36ef9489999418364d0ca6d98a592834a8f42ad3 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Mon, 8 Nov 2021 08:40:23 -0800 Subject: [PATCH 1/3] better phpdoc / BC support for addition of Key class --- src/JWT.php | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/JWT.php b/src/JWT.php index f46e8372..ec1641bc 100644 --- a/src/JWT.php +++ b/src/JWT.php @@ -6,6 +6,7 @@ use DomainException; use Exception; use InvalidArgumentException; +use OpenSSLAsymmetricKey; use UnexpectedValueException; use DateTime; @@ -59,7 +60,7 @@ class JWT * Decodes a JWT string into a PHP object. * * @param string $jwt The JWT - * @param Key|array $keyOrKeyArray The Key or array of Key objects. + * @param Key|array|mixed $keyOrKeyArray The Key or array of Key objects. * If the algorithm used is asymmetric, this is the public key * Each Key object contains an algorithm and matching key. * Supported algorithms are 'ES384','ES256', 'HS256', 'HS384', @@ -385,14 +386,20 @@ public static function urlsafeB64Encode($input) /** * Determine if an algorithm has been provided for each Key * - * @param string|array $keyOrKeyArray + * @param Key|array|mixed $keyOrKeyArray * @param string|null $kid * - * @return an array containing the keyMaterial and algorithm + * @throws UnexpectedValueException + * + * @return array containing the keyMaterial and algorithm */ private static function getKeyMaterialAndAlgorithm($keyOrKeyArray, $kid = null) { - if (is_string($keyOrKeyArray)) { + if ( + is_string($keyOrKeyArray) + || is_resource($keyOrKeyArray) + || $keyOrKeyArray instanceof OpenSSLAsymmetricKey + ) { return array($keyOrKeyArray, null); } @@ -418,7 +425,7 @@ private static function getKeyMaterialAndAlgorithm($keyOrKeyArray, $kid = null) } throw new UnexpectedValueException( - '$keyOrKeyArray must be a string key, an array of string keys, ' + '$keyOrKeyArray must be a string|resource key, an array of string|resource keys, ' . 'an instance of Firebase\JWT\Key key or an array of Firebase\JWT\Key keys' ); } From a692e0da29e6a5e2943f16927be8f5ad041978bd Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Mon, 8 Nov 2021 09:01:15 -0800 Subject: [PATCH 2/3] add test for resource --- tests/JWTTest.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/tests/JWTTest.php b/tests/JWTTest.php index 63386d88..8973d3df 100644 --- a/tests/JWTTest.php +++ b/tests/JWTTest.php @@ -381,4 +381,19 @@ public function provideEncodeDecode() array(__DIR__ . '/ed25519-1.sec', __DIR__ . '/ed25519-1.pub', 'EdDSA'), ); } + + public function testEncodeDecodeWithResource() + { + $pem = file_get_contents(__DIR__ . '/ecdsa-public.pem'); + $resource = openssl_pkey_get_public($pem); + $privateKey = file_get_contents(__DIR__ . '/ecdsa-private.pem'); + + $payload = array('foo' => 'bar'); + $encoded = JWT::encode($payload, $privateKey, 'ES256'); + + // Verify decoding succeeds + $decoded = JWT::decode($encoded, $resource, array('ES256')); + + $this->assertEquals('bar', $decoded->foo); + } } From d0f40f9c9e9dad4f13d38afdccaaa253163817d7 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Mon, 8 Nov 2021 11:18:17 -0800 Subject: [PATCH 3/3] fix test for PHP 5.3 --- tests/JWTTest.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/JWTTest.php b/tests/JWTTest.php index 8973d3df..1c81c1ed 100644 --- a/tests/JWTTest.php +++ b/tests/JWTTest.php @@ -384,15 +384,15 @@ public function provideEncodeDecode() public function testEncodeDecodeWithResource() { - $pem = file_get_contents(__DIR__ . '/ecdsa-public.pem'); + $pem = file_get_contents(__DIR__ . '/rsa1-public.pub'); $resource = openssl_pkey_get_public($pem); - $privateKey = file_get_contents(__DIR__ . '/ecdsa-private.pem'); + $privateKey = file_get_contents(__DIR__ . '/rsa1-private.pem'); $payload = array('foo' => 'bar'); - $encoded = JWT::encode($payload, $privateKey, 'ES256'); + $encoded = JWT::encode($payload, $privateKey, 'RS512'); // Verify decoding succeeds - $decoded = JWT::decode($encoded, $resource, array('ES256')); + $decoded = JWT::decode($encoded, $resource, array('RS512')); $this->assertEquals('bar', $decoded->foo); }