Skip to content
Snippets Groups Projects
Commit aa6419a5 authored by Brent Shaffer's avatar Brent Shaffer Committed by GitHub
Browse files

Updates JWT::verify to handle openssl errors (#159)

parent 0f8f85aa
No related merge requests found
......@@ -88,7 +88,7 @@ class JWT
throw new UnexpectedValueException('Invalid claims encoding');
}
$sig = static::urlsafeB64Decode($cryptob64);
if (empty($header->alg)) {
throw new UnexpectedValueException('Empty algorithm');
}
......@@ -230,11 +230,15 @@ class JWT
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);
......
<?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');
......@@ -253,7 +258,7 @@ class JWTTest extends PHPUnit_Framework_TestCase
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()
......@@ -261,4 +266,24 @@ class JWTTest extends PHPUnit_Framework_TestCase
$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);
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment