diff --git a/src/JWT.php b/src/JWT.php index b3532df7..0a8c0633 100644 --- a/src/JWT.php +++ b/src/JWT.php @@ -47,7 +47,6 @@ class JWT * * @return object The JWT's payload as a PHP object * - * @throws DomainException Algorithm was not provided * @throws UnexpectedValueException Provided JWT was invalid * @throws SignatureInvalidException Provided JWT was invalid because the signature verification failed * @throws BeforeValidException Provided JWT is trying to be used before it's eligible as defined by 'nbf' @@ -62,6 +61,9 @@ public static function decode($jwt, $key, $allowed_algs = array()) if (empty($key)) { throw new InvalidArgumentException('Key may not be empty'); } + if (!is_array($allowed_algs)) { + throw new InvalidArgumentException('Algorithm not allowed'); + } $tks = explode('.', $jwt); if (count($tks) != 3) { throw new UnexpectedValueException('Wrong number of segments'); @@ -76,19 +78,19 @@ public static function decode($jwt, $key, $allowed_algs = array()) $sig = JWT::urlsafeB64Decode($cryptob64); if (empty($header->alg)) { - throw new DomainException('Empty algorithm'); + throw new UnexpectedValueException('Empty algorithm'); } if (empty(self::$supported_algs[$header->alg])) { - throw new DomainException('Algorithm not supported'); + throw new UnexpectedValueException('Algorithm not supported'); } - if (!is_array($allowed_algs) || !in_array($header->alg, $allowed_algs)) { - throw new DomainException('Algorithm not allowed'); + if (!in_array($header->alg, $allowed_algs)) { + throw new UnexpectedValueException('Algorithm not allowed'); } if (is_array($key) || $key instanceof \ArrayAccess) { if (isset($header->kid)) { $key = $key[$header->kid]; } else { - throw new DomainException('"kid" empty, unable to lookup correct key'); + throw new UnexpectedValueException('"kid" empty, unable to lookup correct key'); } } diff --git a/tests/JWTTest.php b/tests/JWTTest.php index 89de8d28..e99ea03a 100644 --- a/tests/JWTTest.php +++ b/tests/JWTTest.php @@ -232,21 +232,21 @@ public function testArrayAccessKIDChooser() public function testNoneAlgorithm() { $msg = JWT::encode('abc', 'my_key'); - $this->setExpectedException('DomainException'); + $this->setExpectedException('UnexpectedValueException'); JWT::decode($msg, 'my_key', array('none')); } public function testIncorrectAlgorithm() { $msg = JWT::encode('abc', 'my_key'); - $this->setExpectedException('DomainException'); + $this->setExpectedException('UnexpectedValueException'); JWT::decode($msg, 'my_key', array('RS256')); } public function testMissingAlgorithm() { $msg = JWT::encode('abc', 'my_key'); - $this->setExpectedException('DomainException'); + $this->setExpectedException('UnexpectedValueException'); JWT::decode($msg, 'my_key'); }