From 974b84858a0f5a0f8147bca3157564e23d77c2a2 Mon Sep 17 00:00:00 2001 From: Jody Mickey Date: Thu, 16 May 2013 16:53:43 -0400 Subject: [PATCH 1/7] namespaced, updated readme, composer --- .gitignore | 1 + Authentication/JWT.php | 32 ++++++++++++++++++-------------- README.md | 22 ++++++++++++++++++++++ composer.json | 18 ++++++++++++++++++ tests/JWTTest.php | 6 +++--- 5 files changed, 62 insertions(+), 17 deletions(-) create mode 100644 .gitignore create mode 100644 composer.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..485dee64 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea diff --git a/Authentication/JWT.php b/Authentication/JWT.php index 7a7b4a0e..5bea58bf 100644 --- a/Authentication/JWT.php +++ b/Authentication/JWT.php @@ -24,6 +24,9 @@ * @license http://opensource.org/licenses/BSD-3-Clause 3-clause BSD * @link https://github.com/firebase/php-jwt */ + +namespace JWT\Authentication; + class JWT { /** @@ -34,8 +37,8 @@ class JWT * @param bool $verify Don't skip verification process * * @return object The JWT's payload as a PHP object - * @throws UnexpectedValueException Provided JWT was invalid - * @throws DomainException Algorithm was not provided + * @throws \UnexpectedValueException Provided JWT was invalid + * @throws \DomainException Algorithm was not provided * * @uses jsonDecode * @uses urlsafeB64Decode @@ -44,22 +47,22 @@ public static function decode($jwt, $key = null, $verify = true) { $tks = explode('.', $jwt); if (count($tks) != 3) { - throw new UnexpectedValueException('Wrong number of segments'); + throw new \UnexpectedValueException('Wrong number of segments'); } list($headb64, $bodyb64, $cryptob64) = $tks; if (null === ($header = JWT::jsonDecode(JWT::urlsafeB64Decode($headb64)))) { - throw new UnexpectedValueException('Invalid segment encoding'); + throw new \UnexpectedValueException('Invalid segment encoding'); } if (null === $payload = JWT::jsonDecode(JWT::urlsafeB64Decode($bodyb64))) { - throw new UnexpectedValueException('Invalid segment encoding'); + throw new \UnexpectedValueException('Invalid segment encoding'); } $sig = JWT::urlsafeB64Decode($cryptob64); if ($verify) { if (empty($header->alg)) { - throw new DomainException('Empty algorithm'); + throw new \DomainException('Empty algorithm'); } if ($sig != JWT::sign("$headb64.$bodyb64", $key, $header->alg)) { - throw new UnexpectedValueException('Signature verification failed'); + throw new \UnexpectedValueException('Signature verification failed'); } } return $payload; @@ -101,7 +104,7 @@ public static function encode($payload, $key, $algo = 'HS256') * algorithms are 'HS256', 'HS384' and 'HS512' * * @return string An encrypted message - * @throws DomainException Unsupported algorithm was specified + * @throws \DomainException Unsupported algorithm was specified */ public static function sign($msg, $key, $method = 'HS256') { @@ -111,7 +114,7 @@ public static function sign($msg, $key, $method = 'HS256') 'HS512' => 'sha512', ); if (empty($methods[$method])) { - throw new DomainException('Algorithm not supported'); + throw new \DomainException('Algorithm not supported'); } return hash_hmac($methods[$method], $msg, $key, true); } @@ -122,7 +125,7 @@ public static function sign($msg, $key, $method = 'HS256') * @param string $input JSON string * * @return object Object representation of JSON string - * @throws DomainException Provided string was invalid JSON + * @throws \DomainException Provided string was invalid JSON */ public static function jsonDecode($input) { @@ -130,7 +133,7 @@ public static function jsonDecode($input) if (function_exists('json_last_error') && $errno = json_last_error()) { JWT::_handleJsonError($errno); } else if ($obj === null && $input !== 'null') { - throw new DomainException('Null result with non-null input'); + throw new \DomainException('Null result with non-null input'); } return $obj; } @@ -141,7 +144,7 @@ public static function jsonDecode($input) * @param object|array $input A PHP object or array * * @return string JSON representation of the PHP object or array - * @throws DomainException Provided object could not be encoded to valid JSON + * @throws \DomainException Provided object could not be encoded to valid JSON */ public static function jsonEncode($input) { @@ -149,7 +152,7 @@ public static function jsonEncode($input) if (function_exists('json_last_error') && $errno = json_last_error()) { JWT::_handleJsonError($errno); } else if ($json === 'null' && $input !== null) { - throw new DomainException('Null result with non-null input'); + throw new \DomainException('Null result with non-null input'); } return $json; } @@ -188,6 +191,7 @@ public static function urlsafeB64Encode($input) * * @param int $errno An error number from json_last_error() * + * @throws \DomainException * @return void */ private static function _handleJsonError($errno) @@ -197,7 +201,7 @@ private static function _handleJsonError($errno) JSON_ERROR_CTRL_CHAR => 'Unexpected control character found', JSON_ERROR_SYNTAX => 'Syntax error, malformed JSON' ); - throw new DomainException( + throw new \DomainException( isset($messages[$errno]) ? $messages[$errno] : 'Unknown JSON error: ' . $errno diff --git a/README.md b/README.md index 2f9e4392..84e6a0a8 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,25 @@ +NOTICE +====== + +This project is forked from firebase/php-jwt with some small changes: + + 1) Namespaced + 2) Added composer.json + +To use the library, composer autoloader will take care of include_once, so skip +that step below. + +```php + +``` + PHP-JWT ======= diff --git a/composer.json b/composer.json new file mode 100644 index 00000000..16d3b9c6 --- /dev/null +++ b/composer.json @@ -0,0 +1,18 @@ +{ + "name": "php-jwt", + "description": "Json Web Token for PHP", + "authors": [ + { + "name": "Jody Mickey", + "email": "jody.mickey@gmail.com" + } + ], + "require": { + "php": ">=5.3.2" + }, + "autoload": { + "psr-0": { + "JWT\\Authentication\\JWT": "" + } + } +} \ No newline at end of file diff --git a/tests/JWTTest.php b/tests/JWTTest.php index db50ee61..859c2f62 100644 --- a/tests/JWTTest.php +++ b/tests/JWTTest.php @@ -1,6 +1,8 @@ From 586c06c28e3368949572299151294a0209ae2c74 Mon Sep 17 00:00:00 2001 From: Janusz Slota Date: Tue, 21 May 2013 14:01:03 +0100 Subject: [PATCH 2/7] Made it easier to install via Composer --- .gitignore | 5 +++ .travis.yml | 12 +++++++ README.md | 22 ++++++------- composer.json | 19 ++++++++++-- example.php | 19 ++++++++++++ .../JWT/Authentication}/JWT.php | 11 ------- phpunit.xml.dist | 23 ++++++++++++++ .../JWT/Test/Authentication}/JWTTest.php | 31 +++++++++++++------ 8 files changed, 108 insertions(+), 34 deletions(-) create mode 100644 .travis.yml create mode 100644 example.php rename {Authentication => lib/JWT/Authentication}/JWT.php (94%) create mode 100644 phpunit.xml.dist rename {tests => test/JWT/Test/Authentication}/JWTTest.php (53%) mode change 100644 => 100755 diff --git a/.gitignore b/.gitignore index 485dee64..d6a04058 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,6 @@ .idea +*.iml +bin +build +composer.lock +vendor diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 00000000..f4958dd8 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,12 @@ +language: php + +php: + - 5.3.3 + - 5.4 + - 5.5 + +before_script: + - composer self-update + - composer install --dev --prefer-source --no-interaction + +script: ./bin/phpunit diff --git a/README.md b/README.md index 84e6a0a8..56c8db89 100644 --- a/README.md +++ b/README.md @@ -3,17 +3,17 @@ NOTICE This project is forked from firebase/php-jwt with some small changes: - 1) Namespaced + 1) Properly namespaced (psr-0) 2) Added composer.json + 3) Added phpunit as require-dev + 4) Added .travis.yml for Travis CI To use the library, composer autoloader will take care of include_once, so skip that step below. ```php =5.3.2" }, + "require-dev": { + "phpunit/phpunit": "3.7.*@dev" + }, "autoload": { "psr-0": { - "JWT\\Authentication\\JWT": "" + "JWT": "lib/" } + }, + "config": { + "bin-dir": "bin" } } \ No newline at end of file diff --git a/example.php b/example.php new file mode 100644 index 00000000..28527586 --- /dev/null +++ b/example.php @@ -0,0 +1,19 @@ + "http://example.org/", + "aud" => "http://example.com/", + "iat" => 1356999524, + "nbf" => 1357000000 +); + +$jwt = JWT::encode($token, $key); +$decoded = JWT::decode($jwt, $key); + +print_r($decoded); diff --git a/Authentication/JWT.php b/lib/JWT/Authentication/JWT.php similarity index 94% rename from Authentication/JWT.php rename to lib/JWT/Authentication/JWT.php index 5bea58bf..a5023b0d 100644 --- a/Authentication/JWT.php +++ b/lib/JWT/Authentication/JWT.php @@ -13,17 +13,6 @@ * @license http://opensource.org/licenses/BSD-3-Clause 3-clause BSD * @link https://github.com/firebase/php-jwt */ -/** - * JSON Web Token implementation, based on this spec: - * http://tools.ietf.org/html/draft-ietf-oauth-json-web-token-06 - * - * @category Authentication - * @package Authentication_JWT - * @author Neuman Vong - * @author Anant Narayanan - * @license http://opensource.org/licenses/BSD-3-Clause 3-clause BSD - * @link https://github.com/firebase/php-jwt - */ namespace JWT\Authentication; diff --git a/phpunit.xml.dist b/phpunit.xml.dist new file mode 100644 index 00000000..993e73b0 --- /dev/null +++ b/phpunit.xml.dist @@ -0,0 +1,23 @@ + + + + + + ./test/JWT/ + + + + + + ./lib/JWT/ + + + + + + + + + \ No newline at end of file diff --git a/tests/JWTTest.php b/test/JWT/Test/Authentication/JWTTest.php old mode 100644 new mode 100755 similarity index 53% rename from tests/JWTTest.php rename to test/JWT/Test/Authentication/JWTTest.php index 859c2f62..9672c838 --- a/tests/JWTTest.php +++ b/test/JWT/Test/Authentication/JWTTest.php @@ -1,16 +1,19 @@ assertEquals(JWT::decode($msg, 'my_key'), 'abc'); } - function testDecodeFromPython() { + function testDecodeFromPython() + { $msg = 'eyJhbGciOiAiSFMyNTYiLCAidHlwIjogIkpXVCJ9.Iio6aHR0cDovL2FwcGxpY2F0aW9uL2NsaWNreT9ibGFoPTEuMjMmZi5vbz00NTYgQUMwMDAgMTIzIg.E_U8X2YpMT5K1cEiT_3-IvBYfrdIFIeVYeOqre_Z5Cg'; $this->assertEquals( JWT::decode($msg, 'my_key'), @@ -18,18 +21,26 @@ function testDecodeFromPython() { ); } - function testUrlSafeCharacters() { + function testUrlSafeCharacters() + { $encoded = JWT::encode('f?', 'a'); $this->assertEquals('f?', JWT::decode($encoded, 'a')); } - function testMalformedUtf8StringsFail() { - $this->setExpectedException('DomainException'); - JWT::encode(pack('c', 128), 'a'); + /** + * @expectedException \DomainException + */ + function testMalformedUtf8StringsFail() + { + \PHPUnit_Framework_Error_Warning::$enabled = false; + @JWT::encode(pack('c', 128), 'a'); } - function testMalformedJsonThrowsException() { - $this->setExpectedException('DomainException'); + /** + * @expectedException \DomainException + */ + function testMalformedJsonThrowsException() + { JWT::jsonDecode('this is not valid JSON string'); } } From 4afde6fbcff96b93ecfb9fc5f5920b21b97ce5b1 Mon Sep 17 00:00:00 2001 From: Janusz Slota Date: Tue, 21 May 2013 14:14:53 +0100 Subject: [PATCH 3/7] Added installation instructions --- README.md | 55 ++++++++++++++++++++++++++----------------------------- 1 file changed, 26 insertions(+), 29 deletions(-) diff --git a/README.md b/README.md index 56c8db89..58f74d7a 100644 --- a/README.md +++ b/README.md @@ -1,25 +1,22 @@ -NOTICE -====== - -This project is forked from firebase/php-jwt with some small changes: - - 1) Properly namespaced (psr-0) - 2) Added composer.json - 3) Added phpunit as require-dev - 4) Added .travis.yml for Travis CI - -To use the library, composer autoloader will take care of include_once, so skip -that step below. - ```php ``` +Installation +============ + +```json +{ + "require": { + "nixilla/php-jwt": "dev-master" + } +} +``` PHP-JWT ======= @@ -30,20 +27,20 @@ Example ------- ```php "http://example.org/", "aud" => "http://example.com/", "iat" => 1356999524, "nbf" => 1357000000 - ); +); - $jwt = JWT::encode($token, $key); - $decoded = JWT::decode($jwt, $key); +$jwt = JWT::encode($token, $key); +$decoded = JWT::decode($jwt, $key); - print_r($decoded); +print_r($decoded); ?> ``` @@ -52,12 +49,12 @@ Tests Run the tests using phpunit: ```bash - git clone https://github.com/nixilla/php-jwt.git && \ - cd php-jwt && \ - mkdir bin && \ - curl -sS https://getcomposer.org/installer | php -- --install-dir=bin && \ - ./bin/composer.phar install --dev && \ - ./bin/phpunit +git clone https://github.com/nixilla/php-jwt.git && \ +cd php-jwt && \ +mkdir bin && \ +curl -sS https://getcomposer.org/installer | php -- --install-dir=bin && \ +./bin/composer.phar install --dev && \ +./bin/phpunit ``` License From 7fd5c11062ae209c8e5956c7ec3090aabc577a06 Mon Sep 17 00:00:00 2001 From: Janusz Slota Date: Tue, 21 May 2013 14:23:29 +0100 Subject: [PATCH 4/7] Added Travis status image --- README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.md b/README.md index 58f74d7a..ea600750 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,8 @@ +JWT Implementation in PHP +========================= + +[![Travis](https://travis-ci.org/nixilla/php-jwt.png)](https://travis-ci.org/nixilla/php-jwt) + ```php Date: Tue, 21 May 2013 14:32:31 +0100 Subject: [PATCH 5/7] Added contributors --- composer.json | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/composer.json b/composer.json index 07f85a98..d2b72823 100644 --- a/composer.json +++ b/composer.json @@ -5,6 +5,14 @@ "keywords": [ "jwt" ], "license": "BSD-3-Clause", "authors": [ + { + "name": "Neuman Vong", + "email": "neuman+pear@twilio.com" + }, + { + "name": "Anant Narayanan", + "email": "anant@php.net" + }, { "name": "Jody Mickey", "email": "jody.mickey@gmail.com" From e484a9fd556ccd66bfbb7c49718c53c172ead4bf Mon Sep 17 00:00:00 2001 From: Janusz Slota Date: Tue, 21 May 2013 14:50:12 +0100 Subject: [PATCH 6/7] Removed tabs --- test/JWT/Test/Authentication/JWTTest.php | 42 ++++++++++++------------ 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/test/JWT/Test/Authentication/JWTTest.php b/test/JWT/Test/Authentication/JWTTest.php index 9672c838..41241ab0 100755 --- a/test/JWT/Test/Authentication/JWTTest.php +++ b/test/JWT/Test/Authentication/JWTTest.php @@ -6,41 +6,41 @@ class JWTTest extends \PHPUnit_Framework_TestCase { - function testEncodeDecode() + public function testEncodeDecode() { - $msg = JWT::encode('abc', 'my_key'); - $this->assertEquals(JWT::decode($msg, 'my_key'), 'abc'); - } + $msg = JWT::encode('abc', 'my_key'); + $this->assertEquals(JWT::decode($msg, 'my_key'), 'abc'); + } - function testDecodeFromPython() + public function testDecodeFromPython() { - $msg = 'eyJhbGciOiAiSFMyNTYiLCAidHlwIjogIkpXVCJ9.Iio6aHR0cDovL2FwcGxpY2F0aW9uL2NsaWNreT9ibGFoPTEuMjMmZi5vbz00NTYgQUMwMDAgMTIzIg.E_U8X2YpMT5K1cEiT_3-IvBYfrdIFIeVYeOqre_Z5Cg'; - $this->assertEquals( - JWT::decode($msg, 'my_key'), - '*:http://application/clicky?blah=1.23&f.oo=456 AC000 123' - ); - } + $msg = 'eyJhbGciOiAiSFMyNTYiLCAidHlwIjogIkpXVCJ9.Iio6aHR0cDovL2FwcGxpY2F0aW9uL2NsaWNreT9ibGFoPTEuMjMmZi5vbz00NTYgQUMwMDAgMTIzIg.E_U8X2YpMT5K1cEiT_3-IvBYfrdIFIeVYeOqre_Z5Cg'; + $this->assertEquals( + JWT::decode($msg, 'my_key'), + '*:http://application/clicky?blah=1.23&f.oo=456 AC000 123' + ); + } - function testUrlSafeCharacters() + public function testUrlSafeCharacters() { - $encoded = JWT::encode('f?', 'a'); - $this->assertEquals('f?', JWT::decode($encoded, 'a')); - } + $encoded = JWT::encode('f?', 'a'); + $this->assertEquals('f?', JWT::decode($encoded, 'a')); + } /** * @expectedException \DomainException */ - function testMalformedUtf8StringsFail() + public function testMalformedUtf8StringsFail() { \PHPUnit_Framework_Error_Warning::$enabled = false; - @JWT::encode(pack('c', 128), 'a'); - } + @JWT::encode(pack('c', 128), 'a'); + } /** * @expectedException \DomainException */ - function testMalformedJsonThrowsException() + public function testMalformedJsonThrowsException() { - JWT::jsonDecode('this is not valid JSON string'); - } + JWT::jsonDecode('this is not valid JSON string'); + } } From aa6661420e1fc01d288a5151aeb5a23c37ff370f Mon Sep 17 00:00:00 2001 From: Janusz Slota Date: Tue, 21 May 2013 14:55:14 +0100 Subject: [PATCH 7/7] Updated README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index ea600750..3eb7e96e 100644 --- a/README.md +++ b/README.md @@ -54,7 +54,7 @@ Tests Run the tests using phpunit: ```bash -git clone https://github.com/nixilla/php-jwt.git && \ +git clone https://github.com/jwmickey/php-jwt.git && \ cd php-jwt && \ mkdir bin && \ curl -sS https://getcomposer.org/installer | php -- --install-dir=bin && \