Skip to content

Commit f61b4f2

Browse files
committed
Updates JWT::verify to handle openssl errors
1 parent 3aa3d97 commit f61b4f2

File tree

2 files changed

+36
-7
lines changed

2 files changed

+36
-7
lines changed

src/JWT.php

+9-5
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public static function decode($jwt, $key, $allowed_algs = array())
8686
throw new UnexpectedValueException('Invalid claims encoding');
8787
}
8888
$sig = static::urlsafeB64Decode($cryptob64);
89-
89+
9090
if (empty($header->alg)) {
9191
throw new UnexpectedValueException('Empty algorithm');
9292
}
@@ -225,11 +225,15 @@ private static function verify($msg, $signature, $key, $alg)
225225
switch($function) {
226226
case 'openssl':
227227
$success = openssl_verify($msg, $signature, $key, $algorithm);
228-
if (!$success) {
229-
throw new DomainException("OpenSSL unable to verify data: " . openssl_error_string());
230-
} else {
231-
return $signature;
228+
if ($success === 1) {
229+
return true;
230+
} elseif ($success === 0) {
231+
return false;
232232
}
233+
// returns 1 on success, 0 on failure, -1 on error.
234+
throw new DomainException(
235+
'OpenSSL error: ' . openssl_error_string()
236+
);
233237
case 'hash_hmac':
234238
default:
235239
$hash = hash_hmac($algorithm, $msg, $key, true);

tests/JWTTest.php

+27-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
<?php
2-
use \Firebase\JWT\JWT;
2+
namespace Firebase\JWT;
3+
4+
use ArrayObject;
5+
use PHPUnit_Framework_TestCase;
36

47
class JWTTest extends PHPUnit_Framework_TestCase
58
{
9+
public static $opensslVerifyReturnValue;
10+
611
public function testEncodeDecode()
712
{
813
$msg = JWT::encode('abc', 'my_key');
@@ -253,12 +258,32 @@ public function testMissingAlgorithm()
253258
public function testAdditionalHeaders()
254259
{
255260
$msg = JWT::encode('abc', 'my_key', 'HS256', null, array('cty' => 'test-eit;v=1'));
256-
$this->assertEquals(JWT::decode($msg, 'my_key', array('HS256')), 'abc');
261+
$this->assertEquals(JWT::decode($msg, 'my_key', array('HS256')), 'abc');
257262
}
258263

259264
public function testInvalidSegmentCount()
260265
{
261266
$this->setExpectedException('UnexpectedValueException');
262267
JWT::decode('brokenheader.brokenbody', 'my_key', array('HS256'));
263268
}
269+
270+
public function testVerifyError()
271+
{
272+
$this->setExpectedException('DomainException');
273+
$pkey = openssl_pkey_new();
274+
$msg = JWT::encode('abc', $pkey, 'RS256');
275+
self::$opensslVerifyReturnValue = -1;
276+
JWT::decode($msg, $pkey, array('RS256'));
277+
}
278+
}
279+
280+
/*
281+
* Allows the testing of openssl_verify with an error return value
282+
*/
283+
function openssl_verify($msg, $signature, $key, $algorithm)
284+
{
285+
if (null !== JWTTest::$opensslVerifyReturnValue) {
286+
return JWTTest::$opensslVerifyReturnValue;
287+
}
288+
return \openssl_verify($msg, $signature, $key, $algorithm);
264289
}

0 commit comments

Comments
 (0)