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

Fix exceptions classes #81

Merged
merged 2 commits into from
Jun 16, 2016
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: 8 additions & 6 deletions src/JWT.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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');
Expand All @@ -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');
}
}

Expand Down
6 changes: 3 additions & 3 deletions tests/JWTTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

Expand Down