Skip to content
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

Updates JWT::verify to handle openssl errors #159

Merged
merged 1 commit into from
Jun 21, 2017
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
14 changes: 9 additions & 5 deletions src/JWT.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public static function decode($jwt, $key, $allowed_algs = array())
throw new UnexpectedValueException('Invalid claims encoding');
}
$sig = static::urlsafeB64Decode($cryptob64);

if (empty($header->alg)) {
throw new UnexpectedValueException('Empty algorithm');
}
Expand Down Expand Up @@ -230,11 +230,15 @@ private static function verify($msg, $signature, $key, $alg)
switch($function) {
case 'openssl':
$success = openssl_verify($msg, $signature, $key, $algorithm);
if (!$success) {
throw new DomainException("OpenSSL unable to verify data: " . openssl_error_string());
} else {
return $signature;
if ($success === 1) {
return true;
} elseif ($success === 0) {
return false;
}
// returns 1 on success, 0 on failure, -1 on error.
throw new DomainException(
'OpenSSL error: ' . openssl_error_string()
);
case 'hash_hmac':
default:
$hash = hash_hmac($algorithm, $msg, $key, true);
Expand Down
29 changes: 27 additions & 2 deletions tests/JWTTest.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
<?php
use \Firebase\JWT\JWT;
namespace Firebase\JWT;

use ArrayObject;
use PHPUnit_Framework_TestCase;

class JWTTest extends PHPUnit_Framework_TestCase
{
public static $opensslVerifyReturnValue;

public function testEncodeDecode()
{
$msg = JWT::encode('abc', 'my_key');
Expand Down Expand Up @@ -253,12 +258,32 @@ public function testMissingAlgorithm()
public function testAdditionalHeaders()
{
$msg = JWT::encode('abc', 'my_key', 'HS256', null, array('cty' => 'test-eit;v=1'));
$this->assertEquals(JWT::decode($msg, 'my_key', array('HS256')), 'abc');
$this->assertEquals(JWT::decode($msg, 'my_key', array('HS256')), 'abc');
}

public function testInvalidSegmentCount()
{
$this->setExpectedException('UnexpectedValueException');
JWT::decode('brokenheader.brokenbody', 'my_key', array('HS256'));
}

public function testVerifyError()
{
$this->setExpectedException('DomainException');
$pkey = openssl_pkey_new();
$msg = JWT::encode('abc', $pkey, 'RS256');
self::$opensslVerifyReturnValue = -1;
JWT::decode($msg, $pkey, array('RS256'));
}
}

/*
* Allows the testing of openssl_verify with an error return value
*/
function openssl_verify($msg, $signature, $key, $algorithm)
{
if (null !== JWTTest::$opensslVerifyReturnValue) {
return JWTTest::$opensslVerifyReturnValue;
}
return \openssl_verify($msg, $signature, $key, $algorithm);
}