From 3a48a27693c0de928d3adfffc651368d431a573f Mon Sep 17 00:00:00 2001 From: Aaron Hedges Date: Wed, 1 Jul 2015 13:18:06 -0400 Subject: [PATCH 1/3] Add firespace\jwt namespace --- Exceptions/BeforeValidException.php | 6 ------ Exceptions/ExpiredException.php | 6 ------ Exceptions/SignatureInvalidException.php | 6 ------ composer.json | 6 ++++-- src/BeforeValidException.php | 7 +++++++ src/ExpiredException.php | 7 +++++++ {Authentication => src}/JWT.php | 4 ++++ src/SignatureInvalidException.php | 7 +++++++ tests/JWTTest.php | 15 ++++++++------- 9 files changed, 37 insertions(+), 27 deletions(-) delete mode 100644 Exceptions/BeforeValidException.php delete mode 100644 Exceptions/ExpiredException.php delete mode 100644 Exceptions/SignatureInvalidException.php create mode 100644 src/BeforeValidException.php create mode 100644 src/ExpiredException.php rename {Authentication => src}/JWT.php (99%) create mode 100644 src/SignatureInvalidException.php diff --git a/Exceptions/BeforeValidException.php b/Exceptions/BeforeValidException.php deleted file mode 100644 index 5a84975e..00000000 --- a/Exceptions/BeforeValidException.php +++ /dev/null @@ -1,6 +0,0 @@ -=5.2.0" + "php": ">=5.3.0" }, "autoload": { - "classmap": ["Authentication/", "Exceptions/"] + "psr-4": { + "Firebase\\JWT\\": "src" + } }, "minimum-stability": "dev" } diff --git a/src/BeforeValidException.php b/src/BeforeValidException.php new file mode 100644 index 00000000..a6ee2f7c --- /dev/null +++ b/src/BeforeValidException.php @@ -0,0 +1,7 @@ +setExpectedException('ExpiredException'); + $this->setExpectedException('Firebase\JWT\ExpiredException'); $payload = array( "message" => "abc", "exp" => time() - 20); // time in the past @@ -47,7 +48,7 @@ public function testExpiredToken() public function testBeforeValidTokenWithNbf() { - $this->setExpectedException('BeforeValidException'); + $this->setExpectedException('Firebase\JWT\BeforeValidException'); $payload = array( "message" => "abc", "nbf" => time() + 20); // time in the future @@ -57,7 +58,7 @@ public function testBeforeValidTokenWithNbf() public function testBeforeValidTokenWithIat() { - $this->setExpectedException('BeforeValidException'); + $this->setExpectedException('Firebase\JWT\BeforeValidException'); $payload = array( "message" => "abc", "iat" => time() + 20); // time in the future @@ -93,7 +94,7 @@ public function testExpiredTokenWithLeeway() $payload = array( "message" => "abc", "exp" => time() - 70); // time far in the past - $this->setExpectedException('ExpiredException'); + $this->setExpectedException('Firebase\JWT\ExpiredException'); $encoded = JWT::encode($payload, 'my_key'); $decoded = JWT::decode($encoded, 'my_key', array('HS256')); $this->assertEquals($decoded->message, 'abc'); @@ -141,7 +142,7 @@ public function testInvalidTokenWithNbfLeeway() "message" => "abc", "nbf" => time() + 65); // not before too far in future $encoded = JWT::encode($payload, 'my_key'); - $this->setExpectedException('BeforeValidException'); + $this->setExpectedException('Firebase\JWT\BeforeValidException'); $decoded = JWT::decode($encoded, 'my_key', array('HS256')); JWT::$leeway = 0; } @@ -165,7 +166,7 @@ public function testInvalidTokenWithIatLeeway() "message" => "abc", "iat" => time() + 65); // issued too far in future $encoded = JWT::encode($payload, 'my_key'); - $this->setExpectedException('BeforeValidException'); + $this->setExpectedException('Firebase\JWT\BeforeValidException'); $decoded = JWT::decode($encoded, 'my_key', array('HS256')); JWT::$leeway = 0; } @@ -176,7 +177,7 @@ public function testInvalidToken() "message" => "abc", "exp" => time() + 20); // time in the future $encoded = JWT::encode($payload, 'my_key'); - $this->setExpectedException('SignatureInvalidException'); + $this->setExpectedException('Firebase\JWT\SignatureInvalidException'); $decoded = JWT::decode($encoded, 'my_key2', array('HS256')); } From d4fa25d9afd52cc1cdfe00b9d3dac33b30aa3877 Mon Sep 17 00:00:00 2001 From: Aaron Hedges Date: Wed, 1 Jul 2015 13:22:41 -0400 Subject: [PATCH 2/3] update example to use new namespace --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 3fa2f304..b891c2c1 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,7 @@ Example ------- ```php Date: Wed, 1 Jul 2015 14:18:43 -0400 Subject: [PATCH 3/3] fix unexpectedvalueexceptions and add unit tests --- src/JWT.php | 1 + tests/JWTTest.php | 6 ++++++ 2 files changed, 7 insertions(+) diff --git a/src/JWT.php b/src/JWT.php index 855b7429..d1a108f3 100644 --- a/src/JWT.php +++ b/src/JWT.php @@ -2,6 +2,7 @@ namespace Firebase\JWT; use \DomainException; +use \UnexpectedValueException; use \DateTime; /** diff --git a/tests/JWTTest.php b/tests/JWTTest.php index 6c996b27..f06f34e4 100644 --- a/tests/JWTTest.php +++ b/tests/JWTTest.php @@ -235,4 +235,10 @@ 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'); } + + public function testInvalidSegmentCount() + { + $this->setExpectedException('UnexpectedValueException'); + JWT::decode('brokenheader.brokenbody', 'my_key', array('HS256')); + } }