Skip to content

fix: phpdoc and exception #371

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions src/JWT.php
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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);
}
}