Skip to content

Commit d6186e0

Browse files
Maks3wrobertdimarco
authored andcommitted
Fix exceptions classes (#81)
* Use InvalidArgumentException when $allowed_algs is not array > Exception thrown if an argument is not of the expected type. http://php.net/manual/en/class.invalidargumentexception.php * Use RuntimeExceptions for exceptions related with unencoded data. RuntimeExceptions is the correct exception error source is the decoded data. Note LogicExceptions as defined in PHP documentation implies a modification in the code by the developer. > Exception that represents error in the program logic. This kind of exception should lead directly to a fix in your code. http://php.net/manual/en/class.logicexception.php But the token is a data provided by an external source which is out side of the control of the developer so there is no way of prevent malformed tokens.
1 parent 19860fa commit d6186e0

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

src/JWT.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ class JWT
5555
*
5656
* @return object The JWT's payload as a PHP object
5757
*
58-
* @throws DomainException Algorithm was not provided
5958
* @throws UnexpectedValueException Provided JWT was invalid
6059
* @throws SignatureInvalidException Provided JWT was invalid because the signature verification failed
6160
* @throws BeforeValidException Provided JWT is trying to be used before it's eligible as defined by 'nbf'
@@ -72,6 +71,9 @@ public static function decode($jwt, $key, $allowed_algs = array())
7271
if (empty($key)) {
7372
throw new InvalidArgumentException('Key may not be empty');
7473
}
74+
if (!is_array($allowed_algs)) {
75+
throw new InvalidArgumentException('Algorithm not allowed');
76+
}
7577
$tks = explode('.', $jwt);
7678
if (count($tks) != 3) {
7779
throw new UnexpectedValueException('Wrong number of segments');
@@ -86,19 +88,19 @@ public static function decode($jwt, $key, $allowed_algs = array())
8688
$sig = JWT::urlsafeB64Decode($cryptob64);
8789

8890
if (empty($header->alg)) {
89-
throw new DomainException('Empty algorithm');
91+
throw new UnexpectedValueException('Empty algorithm');
9092
}
9193
if (empty(self::$supported_algs[$header->alg])) {
92-
throw new DomainException('Algorithm not supported');
94+
throw new UnexpectedValueException('Algorithm not supported');
9395
}
94-
if (!is_array($allowed_algs) || !in_array($header->alg, $allowed_algs)) {
95-
throw new DomainException('Algorithm not allowed');
96+
if (!in_array($header->alg, $allowed_algs)) {
97+
throw new UnexpectedValueException('Algorithm not allowed');
9698
}
9799
if (is_array($key) || $key instanceof \ArrayAccess) {
98100
if (isset($header->kid)) {
99101
$key = $key[$header->kid];
100102
} else {
101-
throw new DomainException('"kid" empty, unable to lookup correct key');
103+
throw new UnexpectedValueException('"kid" empty, unable to lookup correct key');
102104
}
103105
}
104106

tests/JWTTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -232,21 +232,21 @@ public function testArrayAccessKIDChooser()
232232
public function testNoneAlgorithm()
233233
{
234234
$msg = JWT::encode('abc', 'my_key');
235-
$this->setExpectedException('DomainException');
235+
$this->setExpectedException('UnexpectedValueException');
236236
JWT::decode($msg, 'my_key', array('none'));
237237
}
238238

239239
public function testIncorrectAlgorithm()
240240
{
241241
$msg = JWT::encode('abc', 'my_key');
242-
$this->setExpectedException('DomainException');
242+
$this->setExpectedException('UnexpectedValueException');
243243
JWT::decode($msg, 'my_key', array('RS256'));
244244
}
245245

246246
public function testMissingAlgorithm()
247247
{
248248
$msg = JWT::encode('abc', 'my_key');
249-
$this->setExpectedException('DomainException');
249+
$this->setExpectedException('UnexpectedValueException');
250250
JWT::decode($msg, 'my_key');
251251
}
252252

0 commit comments

Comments
 (0)