Skip to content

Commit 83b6090

Browse files
authored
fix: phpdoc and exception (#371)
1 parent cf81444 commit 83b6090

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

src/JWT.php

+12-5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use DomainException;
77
use Exception;
88
use InvalidArgumentException;
9+
use OpenSSLAsymmetricKey;
910
use UnexpectedValueException;
1011
use DateTime;
1112

@@ -59,7 +60,7 @@ class JWT
5960
* Decodes a JWT string into a PHP object.
6061
*
6162
* @param string $jwt The JWT
62-
* @param Key|array<Key> $keyOrKeyArray The Key or array of Key objects.
63+
* @param Key|array<Key>|mixed $keyOrKeyArray The Key or array of Key objects.
6364
* If the algorithm used is asymmetric, this is the public key
6465
* Each Key object contains an algorithm and matching key.
6566
* Supported algorithms are 'ES384','ES256', 'HS256', 'HS384',
@@ -385,14 +386,20 @@ public static function urlsafeB64Encode($input)
385386
/**
386387
* Determine if an algorithm has been provided for each Key
387388
*
388-
* @param string|array $keyOrKeyArray
389+
* @param Key|array<Key>|mixed $keyOrKeyArray
389390
* @param string|null $kid
390391
*
391-
* @return an array containing the keyMaterial and algorithm
392+
* @throws UnexpectedValueException
393+
*
394+
* @return array containing the keyMaterial and algorithm
392395
*/
393396
private static function getKeyMaterialAndAlgorithm($keyOrKeyArray, $kid = null)
394397
{
395-
if (is_string($keyOrKeyArray)) {
398+
if (
399+
is_string($keyOrKeyArray)
400+
|| is_resource($keyOrKeyArray)
401+
|| $keyOrKeyArray instanceof OpenSSLAsymmetricKey
402+
) {
396403
return array($keyOrKeyArray, null);
397404
}
398405

@@ -418,7 +425,7 @@ private static function getKeyMaterialAndAlgorithm($keyOrKeyArray, $kid = null)
418425
}
419426

420427
throw new UnexpectedValueException(
421-
'$keyOrKeyArray must be a string key, an array of string keys, '
428+
'$keyOrKeyArray must be a string|resource key, an array of string|resource keys, '
422429
. 'an instance of Firebase\JWT\Key key or an array of Firebase\JWT\Key keys'
423430
);
424431
}

tests/JWTTest.php

+15
Original file line numberDiff line numberDiff line change
@@ -381,4 +381,19 @@ public function provideEncodeDecode()
381381
array(__DIR__ . '/ed25519-1.sec', __DIR__ . '/ed25519-1.pub', 'EdDSA'),
382382
);
383383
}
384+
385+
public function testEncodeDecodeWithResource()
386+
{
387+
$pem = file_get_contents(__DIR__ . '/rsa1-public.pub');
388+
$resource = openssl_pkey_get_public($pem);
389+
$privateKey = file_get_contents(__DIR__ . '/rsa1-private.pem');
390+
391+
$payload = array('foo' => 'bar');
392+
$encoded = JWT::encode($payload, $privateKey, 'RS512');
393+
394+
// Verify decoding succeeds
395+
$decoded = JWT::decode($encoded, $resource, array('RS512'));
396+
397+
$this->assertEquals('bar', $decoded->foo);
398+
}
384399
}

0 commit comments

Comments
 (0)