Skip to content

Commit ea4db8d

Browse files
author
Brendan Abbott
committed
Add specific exceptions for checking claims. Fix bad tests for exp and nbf. RE: firebase#20
1 parent 0b01cd0 commit ea4db8d

File tree

6 files changed

+28
-9
lines changed

6 files changed

+28
-9
lines changed

Authentication/JWT.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,21 +64,22 @@ public static function decode($jwt, $key = null, $verify = true)
6464

6565
// Check the signature
6666
if (!JWT::verify("$headb64.$bodyb64", $sig, $key, $header->alg)) {
67-
throw new UnexpectedValueException('Signature verification failed');
67+
throw new SignatureInvalidException('Signature verification failed');
6868
}
6969

7070
// Check token expiry time if defined.
7171
if (isset($payload->exp) && time() >= $payload->exp) {
72-
throw new UnexpectedValueException('Expired token');
72+
throw new ExpiredException('Expired token');
7373
}
7474

7575
// Check if the nbf if it is defined.
7676
if (isset($payload->nbf) && $payload->nbf > time()) {
77-
throw new UnexpectedValueException(
77+
throw new TooEarlyException(
7878
'Cannot handle token prior to ' . date(DateTime::ISO8601, $payload->nbf)
7979
);
8080
}
8181
}
82+
8283
return $payload;
8384
}
8485

Exceptions/ExpiredException.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
3+
class ExpiredException extends UnexpectedValueException
4+
{
5+
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
3+
class SignatureInvalidException extends UnexpectedValueException
4+
{
5+
6+
}

Exceptions/TooEarlyException.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
3+
class TooEarlyException extends UnexpectedValueException
4+
{
5+
6+
}

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"php": ">=5.2.0"
2020
},
2121
"autoload": {
22-
"classmap": ["Authentication/"]
22+
"classmap": ["Authentication/", "Exceptions/"]
2323
},
2424
"target-dir": "Firebase/PHP-JWT",
2525
"minimum-stability": "dev"

tests/JWTTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,22 +37,22 @@ public function testMalformedJsonThrowsException()
3737

3838
public function testExpiredToken()
3939
{
40-
$this->setExpectedException('UnexpectedValueException');
40+
$this->setExpectedException('ExpiredException');
4141
$payload = array(
4242
"message" => "abc",
4343
"exp" => time() - 20); // time in the past
4444
$encoded = JWT::encode($payload, 'my_key');
45-
JWT::decode($encoded);
45+
JWT::decode($encoded, 'my_key');
4646
}
4747

4848
public function testTooEarlyToken()
4949
{
50-
$this->setExpectedException('UnexpectedValueException');
50+
$this->setExpectedException('TooEarlyException');
5151
$payload = array(
5252
"message" => "abc",
5353
"nbf" => time() + 20); // time in the past
5454
$encoded = JWT::encode($payload, 'my_key');
55-
JWT::decode($encoded);
55+
JWT::decode($encoded, 'my_key');
5656
}
5757

5858
public function testValidToken()
@@ -82,7 +82,7 @@ public function testInvalidToken()
8282
"message" => "abc",
8383
"exp" => time() + 20); // time in the future
8484
$encoded = JWT::encode($payload, 'my_key');
85-
$this->setExpectedException('UnexpectedValueException');
85+
$this->setExpectedException('SignatureInvalidException');
8686
$decoded = JWT::decode($encoded, 'my_key2');
8787
}
8888

0 commit comments

Comments
 (0)