Skip to content

Commit

Permalink
fix: phpdoc and exception (#371)
Browse files Browse the repository at this point in the history
  • Loading branch information
bshaffer committed Nov 8, 2021
1 parent cf81444 commit 83b6090
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
17 changes: 12 additions & 5 deletions src/JWT.php
Expand Up @@ -6,6 +6,7 @@
use DomainException;
use Exception;
use InvalidArgumentException;
use OpenSSLAsymmetricKey;
use UnexpectedValueException;
use DateTime;

Expand Down Expand Up @@ -59,7 +60,7 @@ class JWT
* Decodes a JWT string into a PHP object.
*
* @param string $jwt The JWT
* @param Key|array<Key> $keyOrKeyArray The Key or array of Key objects.
* @param Key|array<Key>|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',
Expand Down Expand Up @@ -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<Key>|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);
}

Expand All @@ -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'
);
}
Expand Down
15 changes: 15 additions & 0 deletions tests/JWTTest.php
Expand Up @@ -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__ . '/rsa1-public.pub');
$resource = openssl_pkey_get_public($pem);
$privateKey = file_get_contents(__DIR__ . '/rsa1-private.pem');

$payload = array('foo' => 'bar');
$encoded = JWT::encode($payload, $privateKey, 'RS512');

// Verify decoding succeeds
$decoded = JWT::decode($encoded, $resource, array('RS512'));

$this->assertEquals('bar', $decoded->foo);
}
}

0 comments on commit 83b6090

Please sign in to comment.