diff --git a/.gitignore b/.gitignore index 340d96b0..5556efb4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,2 @@ -.buildpath -.project -.settings/ vendor -build +phpunit.xml diff --git a/.scrutinizer.yml b/.scrutinizer.yml new file mode 100644 index 00000000..f7b4ba8f --- /dev/null +++ b/.scrutinizer.yml @@ -0,0 +1,54 @@ +build: + environment: + mysql: false + redis: false + php: + version: 5.6 +tools: + php_sim: true + php_pdepend: true + php_analyzer: true + php_changetracking: true + php_code_sniffer: + config: + standard: "PSR2" + php_mess_detector: true +checks: + php: + argument_type_checks: true + assignment_of_null_return: true + avoid_conflicting_incrementers: true + avoid_useless_overridden_methods: true + catch_class_exists: true + closure_use_modifiable: true + closure_use_not_conflicting: true + code_rating: true + deprecated_code_usage: true + duplication: true + method_calls_on_non_object: true + missing_arguments: true + no_duplicate_arguments: true + no_non_implemented_abstract_methods: true + no_property_on_interface: true + parameter_non_unique: true + precedence_in_conditions: true + precedence_mistakes: true + require_php_tag_first: true + security_vulnerabilities: true + sql_injection_vulnerabilities: true + too_many_arguments: true + unreachable_code: true + unused_methods: true + unused_parameters: true + unused_properties: true + unused_variables: true + use_statement_alias_conflict: true + useless_calls: true + variable_existence: true + verify_access_scope_valid: true + verify_argument_usable_as_reference: true + verify_property_names: true + +filter: + excluded_paths: + - test/* diff --git a/.travis.yml b/.travis.yml index 530e7f31..ef2c105e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,17 @@ language: php php: - 5.5 + - 5.6 + - 7.0 + - hhvm + - hhvm-nightly + +matrix: + allow_failures: + - php: 7.0 + - php: hhvm + - php: hhvm-nightly + before_script: - composer selfupdate - composer install --prefer-dist -o diff --git a/LICENSE b/LICENSE index d0f6cd90..8721a7a5 100644 --- a/LICENSE +++ b/LICENSE @@ -1,4 +1,4 @@ -Copyright (c) 2014, Luís Otávio Cobucci Oblonczyk +Copyright (c) 2014-2015, Luís Otávio Cobucci Oblonczyk All rights reserved. Redistribution and use in source and binary forms, with or without @@ -24,4 +24,4 @@ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. \ No newline at end of file +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/README.md b/README.md index 8877b067..5683464f 100644 --- a/README.md +++ b/README.md @@ -1,24 +1,33 @@ -# JWT +k# JWT +[![Gitter](https://img.shields.io/badge/GITTER-JOIN%20CHAT%20%E2%86%92-brightgreen.svg?style=flat-square)](https://gitter.im/lcobucci/jwt?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) [![Total Downloads](https://img.shields.io/packagist/dt/lcobucci/jwt.svg?style=flat-square)](https://packagist.org/packages/lcobucci/jwt) [![Latest Stable Version](https://img.shields.io/packagist/v/lcobucci/jwt.svg?style=flat-square)](https://packagist.org/packages/lcobucci/jwt) -master -[![Build Status](https://secure.travis-ci.org/lcobucci/jwt.png?branch=master)](http://travis-ci.org/#!/lcobucci/jwt) -[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/lcobucci/jwt/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/lcobucci/jwt/?branch=master) -[![Code Coverage](https://scrutinizer-ci.com/g/lcobucci/jwt/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/lcobucci/jwt/?branch=master) +![Branch master](https://img.shields.io/badge/branch-master-brightgreen.svg?style=flat-square) +[![Build Status](https://img.shields.io/travis/lcobucci/jwt/master.svg?style=flat-square)](http://travis-ci.org/#!/lcobucci/jwt) +[![Scrutinizer Code Quality](https://img.shields.io/scrutinizer/g/lcobucci/jwt/master.svg?style=flat-square)](https://scrutinizer-ci.com/g/lcobucci/jwt/?branch=master) +[![Code Coverage](https://img.shields.io/scrutinizer/coverage/g/lcobucci/jwt/master.svg?style=flat-square)](https://scrutinizer-ci.com/g/lcobucci/jwt/?branch=master) -develop -[![Build Status](https://secure.travis-ci.org/lcobucci/jwt.png?branch=develop)](http://travis-ci.org/#!/lcobucci/jwt) -[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/lcobucci/jwt/badges/quality-score.png?b=develop)](https://scrutinizer-ci.com/g/lcobucci/jwt/?branch=develop) -[![Code Coverage](https://scrutinizer-ci.com/g/lcobucci/jwt/badges/coverage.png?b=develop)](https://scrutinizer-ci.com/g/lcobucci/jwt/?branch=develop) +![Branch develop](https://img.shields.io/badge/branch-develop-brightgreen.svg?style=flat-square) +[![Build Status](https://img.shields.io/travis/lcobucci/jwt/develop.svg?style=flat-square)](http://travis-ci.org/#!/lcobucci/jwt) +[![Scrutinizer Code Quality](https://img.shields.io/scrutinizer/g/lcobucci/jwt/develop.svg?style=flat-square)](https://scrutinizer-ci.com/g/lcobucci/jwt/?branch=develop) +[![Code Coverage](https://img.shields.io/scrutinizer/coverage/g/lcobucci/jwt/develop.svg?style=flat-square)](https://scrutinizer-ci.com/g/lcobucci/jwt/?branch=develop) -[![Total Downloads](https://poser.pugx.org/lcobucci/jwt/downloads.png)](https://packagist.org/packages/lcobucci/jwt) -[![Latest Stable Version](https://poser.pugx.org/lcobucci/jwt/v/stable.png)](https://packagist.org/packages/lcobucci/jwt) A simple library to work with JSON Web Token and JSON Web Signature (requires PHP 5.5+). -The implementation is based on the [current draft](http://tools.ietf.org/html/draft-ietf-oauth-json-web-token-31). +The implementation is based on the [current draft](http://tools.ietf.org/html/draft-ietf-oauth-json-web-token-32). -## Instalation +## Installation -Just add to your composer.json: ```"lcobucci/jwt": "*"``` +Package is available on [Packagist](http://packagist.org/packages/lcobucci/jwt), +you can install it using [Composer](http://getcomposer.org). + +```shell +composer require lcobucci/jwt +``` + +### Dependencies + +- PHP 5.5+ +- OpenSSL Extension ## Basic usage @@ -27,29 +36,110 @@ Just add to your composer.json: ```"lcobucci/jwt": "*"``` Just use the builder to create a new JWT/JWS tokens: ```php -setIssuer('http://example.com') // Configures the issuer (iss claim) ->setAudience('http://example.org') // Configures the audience (aud claim) ->setId('4f1g23a12aa', true) // Configures the id (jti claim), replicating as a header item + ->setIssuedAt(time()) // Configures the time that the token was issue (iat claim) + ->setNotBefore(time() + 60) // Configures the time that the token can be used (nbf claim) + ->setExpiration(time() + 3600) // Configures the expiration time of the token (nbf claim) ->set('uid', 1) // Configures a new claim, called "uid" - ->sign(new Sha256(), 'my key') // Signs the token with HS256 using "my key" as key ->getToken(); // Retrieves the generated token - + + +$token->getHeader(); // Retrieves the token header +$token->getClaims(); // Retrieves the token claims + +echo $token->getClaim('iss'); // will print "http://example.com" +echo $token->getClaim('uid'); // will print "1" echo $token; // The string representation of the object is a JWT string (pretty easy, right?) ``` + ### Parsing from strings -Use the parser to create a new token from a JWT string: +Use the parser to create a new token from a JWT string (using the previous token as example): ```php -parse('...'); // Parses from a string +$token = (new Parser())->parse((string) $token); // Parses from a string $token->getHeader(); // Retrieves the token header $token->getClaims(); // Retrieves the token claims -$token->verify('my key'); // Verifies if the signature was created with given key (if token is signed) + +echo $token->getClaim('iss'); // will print "http://example.com" +echo $token->getClaim('uid'); // will print "1" +``` + +### Validating + +We can easily validate if the token is valid (using the previous token as example): + +```php +use Lcobucci\JWT\ValidationData; + +$data = new ValidationData(); // It will use the current time to validate (iat, nbf and exp) +$data->setIssuer('http://example.com'); +$data->setAudience('http://example.org'); +$data->setId('4f1g23a12aa'); + +var_dump($token->validate($data)); // true, because validation information is equals to data contained on the token + +$data->setCurrentTime(time() + 4000); // changing the validation time to future + +var_dump($token->validate($data)); // false, because token is expired since current time is greater than exp +``` + +## Token signature + +We can use signatures to be able to verify if the token was not modified after its generation. This library implements Hmac, RSA and ECDSA signatures (using 256, 384 and 512). + +### Hmac + +Hmac signatures are really simple to be used: + +```php +use Lcobucci\JWT\Builder; +use Lcobucci\JWT\Signer\Hmac\Sha256; + +$token = (new Builder())->setIssuer('http://example.com') // Configures the issuer (iss claim) + ->setAudience('http://example.org') // Configures the audience (aud claim) + ->setId('4f1g23a12aa', true) // Configures the id (jti claim), replicating as a header item + ->setIssuedAt(time()) // Configures the time that the token was issue (iat claim) + ->setNotBefore(time() + 60) // Configures the time that the token can be used (nbf claim) + ->setExpiration(time() + 3600) // Configures the expiration time of the token (nbf claim) + ->set('uid', 1) // Configures a new claim, called "uid" + ->sign(new Sha256(), 'testing') // creates a signature using "testing" as key + ->getToken(); // Retrieves the generated token + + +var_dump($token->verify('testing 1')); // false, because the key is different +var_dump($token->verify('testing')); // true, because the key is the same ``` + +### RSA and ECDSA + +RSA and ECDSA signatures are based on public and private keys so you have to generate using the private key and verify using the public key: + +```php +use Lcobucci\JWT\Builder; +use Lcobucci\JWT\Signer\Keychain; // just to make our life simpler +use Lcobucci\JWT\Signer\Rsa\Sha256; // you can use Lcobucci\JWT\Signer\Ecdsa\Sha256 if you're using ECDSA keys + +$keychain = new Keychain(); + +$token = (new Builder())->setIssuer('http://example.com') // Configures the issuer (iss claim) + ->setAudience('http://example.org') // Configures the audience (aud claim) + ->setId('4f1g23a12aa', true) // Configures the id (jti claim), replicating as a header item + ->setIssuedAt(time()) // Configures the time that the token was issue (iat claim) + ->setNotBefore(time() + 60) // Configures the time that the token can be used (nbf claim) + ->setExpiration(time() + 3600) // Configures the expiration time of the token (nbf claim) + ->set('uid', 1) // Configures a new claim, called "uid" + ->sign(new Sha256(), $keychain->getPrivateKey('file://{path to your private key}')) // creates a signature using your private key + ->getToken(); // Retrieves the generated token + + +var_dump($token->verify($keychain->getPublicKey('file://{path to your public key}')); // true when the public key was generated by the private one =) +``` + +**It's important to say that if you're using RSA keys your shouldn't invoke ECDSA signers (and vice-versa), otherwise ```sign()``` and ```verify()``` will raise an exception!** diff --git a/composer.json b/composer.json index 9ffd4457..270a22c0 100644 --- a/composer.json +++ b/composer.json @@ -1,27 +1,37 @@ { - "name" : "lcobucci/jwt", - "description" : "A simple library to work with JSON Web Token and JSON Web Signature", - "type" : "library", - "authors" : [ + "name": "lcobucci/jwt", + "description": "A simple library to work with JSON Web Token and JSON Web Signature", + "type": "library", + "authors": [ { - "name" : "Luís Otávio Cobucci Obloncz", - "email" : "lcobucci@gmail.com", + "name": "Luís Otávio Cobucci Obloncz", + "email": "lcobucci@gmail.com", "role": "Developer" } ], - "keywords" : ["JWT", "JWS"], - "license" : ["BSD-3-Clause"], - "require" : { - "php" : ">=5.5" + "keywords": [ + "JWT", + "JWS" + ], + "license": [ + "BSD-3-Clause" + ], + "require": { + "php": ">=5.5", + "ext-openssl": "*" }, - "require-dev" : { - "phpunit/phpunit" : "~4.3", - "squizlabs/php_codesniffer" : "~1.5", - "phpmd/phpmd" : "~2.1" + "require-dev": { + "phpunit/phpunit": "~4.5", + "squizlabs/php_codesniffer": "~2.3", + "phpmd/phpmd": "~2.2", + "phpunit/php-invoker": "~1.1" }, - "autoload" : { - "psr-4" : { - "Lcobucci\\JWT\\" : ["src", "test"] + "autoload": { + "psr-4": { + "Lcobucci\\JWT\\": [ + "src", + "test" + ] } } } diff --git a/composer.lock b/composer.lock index 6f8a7b84..3237deb1 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "hash": "a631bbb387d91587e2e34654e2ce7aea", + "hash": "cb2652fb8d160f0bdb1f43ec2931db2e", "packages": [], "packages-dev": [ { @@ -63,25 +63,25 @@ }, { "name": "pdepend/pdepend", - "version": "2.0.3", + "version": "2.0.6", "source": { "type": "git", "url": "https://github.com/pdepend/pdepend.git", - "reference": "dc582a3c0180664a8fbfc5a34efaf4cc13fccc60" + "reference": "a15ffcbfbcc4570d4a733ca7b76e9cac0a56c3f4" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/pdepend/pdepend/zipball/dc582a3c0180664a8fbfc5a34efaf4cc13fccc60", - "reference": "dc582a3c0180664a8fbfc5a34efaf4cc13fccc60", + "url": "https://api.github.com/repos/pdepend/pdepend/zipball/a15ffcbfbcc4570d4a733ca7b76e9cac0a56c3f4", + "reference": "a15ffcbfbcc4570d4a733ca7b76e9cac0a56c3f4", "shasum": "" }, "require": { - "symfony/config": "@stable", - "symfony/dependency-injection": "@stable", - "symfony/filesystem": "@stable" + "symfony/config": ">=2.4", + "symfony/dependency-injection": ">=2.4", + "symfony/filesystem": ">=2.4" }, "require-dev": { - "phpunit/phpunit": "3.*@stable", + "phpunit/phpunit": "4.*@stable", "squizlabs/php_codesniffer": "@stable" }, "bin": [ @@ -98,33 +98,86 @@ "BSD-3-Clause" ], "description": "Official version of pdepend to be handled with Composer", - "time": "2014-10-08 06:54:50" + "time": "2015-03-02 08:06:43" + }, + { + "name": "phpdocumentor/reflection-docblock", + "version": "2.0.4", + "source": { + "type": "git", + "url": "https://github.com/phpDocumentor/ReflectionDocBlock.git", + "reference": "d68dbdc53dc358a816f00b300704702b2eaff7b8" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/phpDocumentor/ReflectionDocBlock/zipball/d68dbdc53dc358a816f00b300704702b2eaff7b8", + "reference": "d68dbdc53dc358a816f00b300704702b2eaff7b8", + "shasum": "" + }, + "require": { + "php": ">=5.3.3" + }, + "require-dev": { + "phpunit/phpunit": "~4.0" + }, + "suggest": { + "dflydev/markdown": "~1.0", + "erusev/parsedown": "~1.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "2.0.x-dev" + } + }, + "autoload": { + "psr-0": { + "phpDocumentor": [ + "src/" + ] + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Mike van Riel", + "email": "mike.vanriel@naenius.com" + } + ], + "time": "2015-02-03 12:10:50" }, { "name": "phpmd/phpmd", - "version": "2.1.3", + "version": "2.2.1", "source": { "type": "git", "url": "https://github.com/phpmd/phpmd.git", - "reference": "1a485d9db869137af5e9678bd844568c92998b25" + "reference": "58c4b00f924d301e8c5281f40cfa9a66f3df9eee" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpmd/phpmd/zipball/1a485d9db869137af5e9678bd844568c92998b25", - "reference": "1a485d9db869137af5e9678bd844568c92998b25", + "url": "https://api.github.com/repos/phpmd/phpmd/zipball/58c4b00f924d301e8c5281f40cfa9a66f3df9eee", + "reference": "58c4b00f924d301e8c5281f40cfa9a66f3df9eee", "shasum": "" }, "require": { "pdepend/pdepend": "2.0.*", "php": ">=5.3.0", - "symfony/config": "2.5.*", - "symfony/dependency-injection": "2.5.*", - "symfony/filesystem": "2.5.*" + "symfony/config": ">=2.4", + "symfony/dependency-injection": ">=2.4", + "symfony/filesystem": ">=2.4" + }, + "require-dev": { + "phpunit/phpunit": "*", + "squizlabs/php_codesniffer": "*" }, "bin": [ "src/bin/phpmd" ], - "type": "library", + "type": "project", "autoload": { "psr-0": { "PHPMD\\": "src/main/php" @@ -134,21 +187,101 @@ "license": [ "BSD-3-Clause" ], - "description": "Official version of PHPMD handled with Composer.", - "time": "2014-09-25 15:56:22" + "authors": [ + { + "name": "Manuel Pichler", + "email": "github@manuel-pichler.de", + "homepage": "https://github.com/manuelpichler", + "role": "Project founder" + }, + { + "name": "Other contributors", + "homepage": "https://github.com/phpmd/phpmd/graphs/contributors", + "role": "Contributors" + } + ], + "description": "PHPMD is a spin-off project of PHP Depend and aims to be a PHP equivalent of the well known Java tool PMD.", + "homepage": "http://phpmd.org/", + "keywords": [ + "mess detection", + "mess detector", + "pdepend", + "phpmd", + "pmd" + ], + "time": "2015-03-02 10:26:50" + }, + { + "name": "phpspec/prophecy", + "version": "v1.3.1", + "source": { + "type": "git", + "url": "https://github.com/phpspec/prophecy.git", + "reference": "9ca52329bcdd1500de24427542577ebf3fc2f1c9" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/phpspec/prophecy/zipball/9ca52329bcdd1500de24427542577ebf3fc2f1c9", + "reference": "9ca52329bcdd1500de24427542577ebf3fc2f1c9", + "shasum": "" + }, + "require": { + "doctrine/instantiator": "~1.0,>=1.0.2", + "phpdocumentor/reflection-docblock": "~2.0" + }, + "require-dev": { + "phpspec/phpspec": "~2.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.2.x-dev" + } + }, + "autoload": { + "psr-0": { + "Prophecy\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Konstantin Kudryashov", + "email": "ever.zet@gmail.com", + "homepage": "http://everzet.com" + }, + { + "name": "Marcello Duarte", + "email": "marcello.duarte@gmail.com" + } + ], + "description": "Highly opinionated mocking framework for PHP 5.3+", + "homepage": "http://phpspec.org", + "keywords": [ + "Double", + "Dummy", + "fake", + "mock", + "spy", + "stub" + ], + "time": "2014-11-17 16:23:49" }, { "name": "phpunit/php-code-coverage", - "version": "2.0.11", + "version": "2.0.15", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/php-code-coverage.git", - "reference": "53603b3c995f5aab6b59c8e08c3a663d2cc810b7" + "reference": "34cc484af1ca149188d0d9e91412191e398e0b67" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/53603b3c995f5aab6b59c8e08c3a663d2cc810b7", - "reference": "53603b3c995f5aab6b59c8e08c3a663d2cc810b7", + "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/34cc484af1ca149188d0d9e91412191e398e0b67", + "reference": "34cc484af1ca149188d0d9e91412191e398e0b67", "shasum": "" }, "require": { @@ -161,7 +294,7 @@ }, "require-dev": { "ext-xdebug": ">=2.1.4", - "phpunit/phpunit": "~4.1" + "phpunit/phpunit": "~4" }, "suggest": { "ext-dom": "*", @@ -180,9 +313,6 @@ ] }, "notification-url": "https://packagist.org/downloads/", - "include-path": [ - "" - ], "license": [ "BSD-3-Clause" ], @@ -200,7 +330,7 @@ "testing", "xunit" ], - "time": "2014-08-31 06:33:04" + "time": "2015-01-24 10:06:35" }, { "name": "phpunit/php-file-iterator", @@ -247,6 +377,52 @@ ], "time": "2013-10-10 15:34:57" }, + { + "name": "phpunit/php-invoker", + "version": "1.1.3", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/php-invoker.git", + "reference": "8696484458cb43eed025ab46260846de5b74655c" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/php-invoker/zipball/8696484458cb43eed025ab46260846de5b74655c", + "reference": "8696484458cb43eed025ab46260846de5b74655c", + "shasum": "" + }, + "require": { + "ext-pcntl": "*", + "php": ">=5.2.7", + "phpunit/php-timer": ">=1.0.4,<1.1.0" + }, + "type": "library", + "autoload": { + "classmap": [ + "PHP/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "include-path": [ + "" + ], + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sb@sebastian-bergmann.de", + "role": "lead" + } + ], + "description": "Utility class for invoking callables with a timeout.", + "homepage": "https://github.com/sebastianbergmann/php-invoker/", + "keywords": [ + "process" + ], + "time": "2013-07-16 05:20:21" + }, { "name": "phpunit/php-text-template", "version": "1.2.0", @@ -337,16 +513,16 @@ }, { "name": "phpunit/php-token-stream", - "version": "1.3.0", + "version": "1.4.0", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/php-token-stream.git", - "reference": "f8d5d08c56de5cfd592b3340424a81733259a876" + "reference": "db32c18eba00b121c145575fcbcd4d4d24e6db74" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/php-token-stream/zipball/f8d5d08c56de5cfd592b3340424a81733259a876", - "reference": "f8d5d08c56de5cfd592b3340424a81733259a876", + "url": "https://api.github.com/repos/sebastianbergmann/php-token-stream/zipball/db32c18eba00b121c145575fcbcd4d4d24e6db74", + "reference": "db32c18eba00b121c145575fcbcd4d4d24e6db74", "shasum": "" }, "require": { @@ -359,7 +535,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-master": "1.3-dev" + "dev-master": "1.4-dev" } }, "autoload": { @@ -382,20 +558,20 @@ "keywords": [ "tokenizer" ], - "time": "2014-08-31 06:12:13" + "time": "2015-01-17 09:51:32" }, { "name": "phpunit/phpunit", - "version": "4.3.5", + "version": "4.5.0", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/phpunit.git", - "reference": "2dab9d593997db4abcf58d0daf798eb4e9cecfe1" + "reference": "5b578d3865a9128b9c209b011fda6539ec06e7a5" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/2dab9d593997db4abcf58d0daf798eb4e9cecfe1", - "reference": "2dab9d593997db4abcf58d0daf798eb4e9cecfe1", + "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/5b578d3865a9128b9c209b011fda6539ec06e7a5", + "reference": "5b578d3865a9128b9c209b011fda6539ec06e7a5", "shasum": "" }, "require": { @@ -405,15 +581,17 @@ "ext-reflection": "*", "ext-spl": "*", "php": ">=5.3.3", + "phpspec/prophecy": "~1.3.1", "phpunit/php-code-coverage": "~2.0", "phpunit/php-file-iterator": "~1.3.2", "phpunit/php-text-template": "~1.2", "phpunit/php-timer": "~1.0.2", "phpunit/phpunit-mock-objects": "~2.3", - "sebastian/comparator": "~1.0", + "sebastian/comparator": "~1.1", "sebastian/diff": "~1.1", - "sebastian/environment": "~1.0", - "sebastian/exporter": "~1.0", + "sebastian/environment": "~1.2", + "sebastian/exporter": "~1.2", + "sebastian/global-state": "~1.0", "sebastian/version": "~1.0", "symfony/yaml": "~2.0" }, @@ -426,7 +604,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-master": "4.3.x-dev" + "dev-master": "4.5.x-dev" } }, "autoload": { @@ -435,10 +613,6 @@ ] }, "notification-url": "https://packagist.org/downloads/", - "include-path": [ - "", - "../../symfony/yaml/" - ], "license": [ "BSD-3-Clause" ], @@ -450,13 +624,13 @@ } ], "description": "The PHP Unit Testing framework.", - "homepage": "http://www.phpunit.de/", + "homepage": "https://phpunit.de/", "keywords": [ "phpunit", "testing", "xunit" ], - "time": "2014-11-11 10:11:09" + "time": "2015-02-05 15:51:19" }, { "name": "phpunit/phpunit-mock-objects", @@ -515,30 +689,30 @@ }, { "name": "sebastian/comparator", - "version": "1.0.1", + "version": "1.1.1", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/comparator.git", - "reference": "e54a01c0da1b87db3c5a3c4c5277ddf331da4aef" + "reference": "1dd8869519a225f7f2b9eb663e225298fade819e" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/comparator/zipball/e54a01c0da1b87db3c5a3c4c5277ddf331da4aef", - "reference": "e54a01c0da1b87db3c5a3c4c5277ddf331da4aef", + "url": "https://api.github.com/repos/sebastianbergmann/comparator/zipball/1dd8869519a225f7f2b9eb663e225298fade819e", + "reference": "1dd8869519a225f7f2b9eb663e225298fade819e", "shasum": "" }, "require": { "php": ">=5.3.3", - "sebastian/diff": "~1.1", - "sebastian/exporter": "~1.0" + "sebastian/diff": "~1.2", + "sebastian/exporter": "~1.2" }, "require-dev": { - "phpunit/phpunit": "~4.1" + "phpunit/phpunit": "~4.4" }, "type": "library", "extra": { "branch-alias": { - "dev-master": "1.0.x-dev" + "dev-master": "1.1.x-dev" } }, "autoload": { @@ -575,7 +749,7 @@ "compare", "equality" ], - "time": "2014-05-11 23:00:21" + "time": "2015-01-29 16:28:08" }, { "name": "sebastian/diff", @@ -631,16 +805,16 @@ }, { "name": "sebastian/environment", - "version": "1.2.0", + "version": "1.2.1", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/environment.git", - "reference": "0d9bf79554d2a999da194a60416c15cf461eb67d" + "reference": "6e6c71d918088c251b181ba8b3088af4ac336dd7" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/environment/zipball/0d9bf79554d2a999da194a60416c15cf461eb67d", - "reference": "0d9bf79554d2a999da194a60416c15cf461eb67d", + "url": "https://api.github.com/repos/sebastianbergmann/environment/zipball/6e6c71d918088c251b181ba8b3088af4ac336dd7", + "reference": "6e6c71d918088c251b181ba8b3088af4ac336dd7", "shasum": "" }, "require": { @@ -677,32 +851,33 @@ "environment", "hhvm" ], - "time": "2014-10-22 06:38:05" + "time": "2014-10-25 08:00:45" }, { "name": "sebastian/exporter", - "version": "1.0.2", + "version": "1.2.0", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/exporter.git", - "reference": "c7d59948d6e82818e1bdff7cadb6c34710eb7dc0" + "reference": "84839970d05254c73cde183a721c7af13aede943" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/exporter/zipball/c7d59948d6e82818e1bdff7cadb6c34710eb7dc0", - "reference": "c7d59948d6e82818e1bdff7cadb6c34710eb7dc0", + "url": "https://api.github.com/repos/sebastianbergmann/exporter/zipball/84839970d05254c73cde183a721c7af13aede943", + "reference": "84839970d05254c73cde183a721c7af13aede943", "shasum": "" }, "require": { - "php": ">=5.3.3" + "php": ">=5.3.3", + "sebastian/recursion-context": "~1.0" }, "require-dev": { - "phpunit/phpunit": "~4.0" + "phpunit/phpunit": "~4.4" }, "type": "library", "extra": { "branch-alias": { - "dev-master": "1.0.x-dev" + "dev-master": "1.2.x-dev" } }, "autoload": { @@ -742,20 +917,124 @@ "export", "exporter" ], - "time": "2014-09-10 00:51:36" + "time": "2015-01-27 07:23:06" + }, + { + "name": "sebastian/global-state", + "version": "1.0.0", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/global-state.git", + "reference": "c7428acdb62ece0a45e6306f1ae85e1c05b09c01" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/global-state/zipball/c7428acdb62ece0a45e6306f1ae85e1c05b09c01", + "reference": "c7428acdb62ece0a45e6306f1ae85e1c05b09c01", + "shasum": "" + }, + "require": { + "php": ">=5.3.3" + }, + "require-dev": { + "phpunit/phpunit": "~4.2" + }, + "suggest": { + "ext-uopz": "*" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de" + } + ], + "description": "Snapshotting of global state", + "homepage": "http://www.github.com/sebastianbergmann/global-state", + "keywords": [ + "global state" + ], + "time": "2014-10-06 09:23:50" + }, + { + "name": "sebastian/recursion-context", + "version": "1.0.0", + "source": { + "type": "git", + "url": "https://github.com/sebastianbergmann/recursion-context.git", + "reference": "3989662bbb30a29d20d9faa04a846af79b276252" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/sebastianbergmann/recursion-context/zipball/3989662bbb30a29d20d9faa04a846af79b276252", + "reference": "3989662bbb30a29d20d9faa04a846af79b276252", + "shasum": "" + }, + "require": { + "php": ">=5.3.3" + }, + "require-dev": { + "phpunit/phpunit": "~4.4" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Jeff Welch", + "email": "whatthejeff@gmail.com" + }, + { + "name": "Sebastian Bergmann", + "email": "sebastian@phpunit.de" + }, + { + "name": "Adam Harvey", + "email": "aharvey@php.net" + } + ], + "description": "Provides functionality to recursively process PHP variables", + "homepage": "http://www.github.com/sebastianbergmann/recursion-context", + "time": "2015-01-24 09:48:32" }, { "name": "sebastian/version", - "version": "1.0.3", + "version": "1.0.4", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/version.git", - "reference": "b6e1f0cf6b9e1ec409a0d3e2f2a5fb0998e36b43" + "reference": "a77d9123f8e809db3fbdea15038c27a95da4058b" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/version/zipball/b6e1f0cf6b9e1ec409a0d3e2f2a5fb0998e36b43", - "reference": "b6e1f0cf6b9e1ec409a0d3e2f2a5fb0998e36b43", + "url": "https://api.github.com/repos/sebastianbergmann/version/zipball/a77d9123f8e809db3fbdea15038c27a95da4058b", + "reference": "a77d9123f8e809db3fbdea15038c27a95da4058b", "shasum": "" }, "type": "library", @@ -777,36 +1056,35 @@ ], "description": "Library that helps with managing the version number of Git-hosted PHP projects", "homepage": "https://github.com/sebastianbergmann/version", - "time": "2014-03-07 15:35:33" + "time": "2014-12-15 14:25:24" }, { "name": "squizlabs/php_codesniffer", - "version": "1.5.5", + "version": "2.3.0", "source": { "type": "git", "url": "https://github.com/squizlabs/PHP_CodeSniffer.git", - "reference": "5d973e59cf58a0c847f298de84374c96b42b17b3" + "reference": "5046b0e01c416fc2b06df961d0673c85bcdc896c" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/squizlabs/PHP_CodeSniffer/zipball/5d973e59cf58a0c847f298de84374c96b42b17b3", - "reference": "5d973e59cf58a0c847f298de84374c96b42b17b3", + "url": "https://api.github.com/repos/squizlabs/PHP_CodeSniffer/zipball/5046b0e01c416fc2b06df961d0673c85bcdc896c", + "reference": "5046b0e01c416fc2b06df961d0673c85bcdc896c", "shasum": "" }, "require": { "ext-tokenizer": "*", + "ext-xmlwriter": "*", "php": ">=5.1.2" }, - "suggest": { - "phpunit/php-timer": "dev-master" - }, "bin": [ - "scripts/phpcs" + "scripts/phpcs", + "scripts/phpcbf" ], "type": "library", "extra": { "branch-alias": { - "dev-phpcs-fixer": "2.0.x-dev" + "dev-master": "2.0.x-dev" } }, "autoload": { @@ -815,12 +1093,12 @@ "CodeSniffer/CLI.php", "CodeSniffer/Exception.php", "CodeSniffer/File.php", + "CodeSniffer/Fixer.php", "CodeSniffer/Report.php", "CodeSniffer/Reporting.php", "CodeSniffer/Sniff.php", "CodeSniffer/Tokens.php", "CodeSniffer/Reports/", - "CodeSniffer/CommentParser/", "CodeSniffer/Tokenizers/", "CodeSniffer/DocGenerators/", "CodeSniffer/Standards/AbstractPatternSniff.php", @@ -846,27 +1124,27 @@ "role": "lead" } ], - "description": "PHP_CodeSniffer tokenises PHP, JavaScript and CSS files and detects violations of a defined set of coding standards.", + "description": "PHP_CodeSniffer tokenizes PHP, JavaScript and CSS files and detects violations of a defined set of coding standards.", "homepage": "http://www.squizlabs.com/php-codesniffer", "keywords": [ "phpcs", "standards" ], - "time": "2014-09-25 03:33:46" + "time": "2015-03-04 02:07:03" }, { "name": "symfony/config", - "version": "v2.5.6", + "version": "v2.6.4", "target-dir": "Symfony/Component/Config", "source": { "type": "git", "url": "https://github.com/symfony/Config.git", - "reference": "0316364bfebc8b080077c731a99f189341476bd7" + "reference": "a9f781ba1221067d1f07c8cec0bc50f81b8d7408" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/Config/zipball/0316364bfebc8b080077c731a99f189341476bd7", - "reference": "0316364bfebc8b080077c731a99f189341476bd7", + "url": "https://api.github.com/repos/symfony/Config/zipball/a9f781ba1221067d1f07c8cec0bc50f81b8d7408", + "reference": "a9f781ba1221067d1f07c8cec0bc50f81b8d7408", "shasum": "" }, "require": { @@ -876,7 +1154,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-master": "2.5-dev" + "dev-master": "2.6-dev" } }, "autoload": { @@ -900,30 +1178,33 @@ ], "description": "Symfony Config Component", "homepage": "http://symfony.com", - "time": "2014-09-23 05:25:11" + "time": "2015-01-21 20:57:55" }, { "name": "symfony/dependency-injection", - "version": "v2.5.6", + "version": "v2.6.4", "target-dir": "Symfony/Component/DependencyInjection", "source": { "type": "git", "url": "https://github.com/symfony/DependencyInjection.git", - "reference": "fb40e4ea115d331346fbbc4da8f42825e524c7a0" + "reference": "42bbb43fab66292a1865dc9616c299904c3d4d14" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/DependencyInjection/zipball/fb40e4ea115d331346fbbc4da8f42825e524c7a0", - "reference": "fb40e4ea115d331346fbbc4da8f42825e524c7a0", + "url": "https://api.github.com/repos/symfony/DependencyInjection/zipball/42bbb43fab66292a1865dc9616c299904c3d4d14", + "reference": "42bbb43fab66292a1865dc9616c299904c3d4d14", "shasum": "" }, "require": { "php": ">=5.3.3" }, + "conflict": { + "symfony/expression-language": "<2.6" + }, "require-dev": { "symfony/config": "~2.2", - "symfony/expression-language": "~2.4", - "symfony/yaml": "~2.0" + "symfony/expression-language": "~2.6", + "symfony/yaml": "~2.1" }, "suggest": { "symfony/config": "", @@ -933,7 +1214,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-master": "2.5-dev" + "dev-master": "2.6-dev" } }, "autoload": { @@ -957,21 +1238,21 @@ ], "description": "Symfony DependencyInjection Component", "homepage": "http://symfony.com", - "time": "2014-10-01 05:50:18" + "time": "2015-01-25 04:39:26" }, { "name": "symfony/filesystem", - "version": "v2.5.6", + "version": "v2.6.4", "target-dir": "Symfony/Component/Filesystem", "source": { "type": "git", "url": "https://github.com/symfony/Filesystem.git", - "reference": "4e62fab0060a826561c78b665925b37c870c45f5" + "reference": "a1f566d1f92e142fa1593f4555d6d89e3044a9b7" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/Filesystem/zipball/4e62fab0060a826561c78b665925b37c870c45f5", - "reference": "4e62fab0060a826561c78b665925b37c870c45f5", + "url": "https://api.github.com/repos/symfony/Filesystem/zipball/a1f566d1f92e142fa1593f4555d6d89e3044a9b7", + "reference": "a1f566d1f92e142fa1593f4555d6d89e3044a9b7", "shasum": "" }, "require": { @@ -980,7 +1261,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-master": "2.5-dev" + "dev-master": "2.6-dev" } }, "autoload": { @@ -1004,21 +1285,21 @@ ], "description": "Symfony Filesystem Component", "homepage": "http://symfony.com", - "time": "2014-09-22 09:14:18" + "time": "2015-01-03 21:13:09" }, { "name": "symfony/yaml", - "version": "v2.5.6", + "version": "v2.6.4", "target-dir": "Symfony/Component/Yaml", "source": { "type": "git", "url": "https://github.com/symfony/Yaml.git", - "reference": "2d9f527449cabfa8543dd7fa3a466d6ae83d6726" + "reference": "60ed7751671113cf1ee7d7778e691642c2e9acd8" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/Yaml/zipball/2d9f527449cabfa8543dd7fa3a466d6ae83d6726", - "reference": "2d9f527449cabfa8543dd7fa3a466d6ae83d6726", + "url": "https://api.github.com/repos/symfony/Yaml/zipball/60ed7751671113cf1ee7d7778e691642c2e9acd8", + "reference": "60ed7751671113cf1ee7d7778e691642c2e9acd8", "shasum": "" }, "require": { @@ -1027,7 +1308,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-master": "2.5-dev" + "dev-master": "2.6-dev" } }, "autoload": { @@ -1051,15 +1332,17 @@ ], "description": "Symfony Yaml Component", "homepage": "http://symfony.com", - "time": "2014-10-01 05:50:18" + "time": "2015-01-25 04:39:26" } ], "aliases": [], "minimum-stability": "stable", "stability-flags": [], "prefer-stable": false, + "prefer-lowest": false, "platform": { - "php": ">=5.5" + "php": ">=5.5", + "ext-openssl": "*" }, "platform-dev": [] } diff --git a/phpunit.xml b/phpunit.xml.dist similarity index 59% rename from phpunit.xml rename to phpunit.xml.dist index a4a260a7..6252bf5b 100644 --- a/phpunit.xml +++ b/phpunit.xml.dist @@ -1,11 +1,17 @@ - + beStrictAboutOutputDuringTests="true" + beStrictAboutTestSize="true" + beStrictAboutTestsThatDoNotTestAnything="true" + beStrictAboutTodoAnnotatedTests="true" + checkForUnintentionallyCoveredCode="true" + forceCoversAnnotation="true"> test diff --git a/src/Signature.php b/src/Signature.php index 618faf83..e5efb71c 100644 --- a/src/Signature.php +++ b/src/Signature.php @@ -52,7 +52,7 @@ public function __construct(Signer $signer, $hash) */ public function verify($payload, $key) { - return $this->hash == $this->signer->createHash($payload, $key); + return $this->signer->verify($this->hash, $payload, $key); } /** diff --git a/src/Signer.php b/src/Signer.php index b9398580..7932fb15 100644 --- a/src/Signer.php +++ b/src/Signer.php @@ -7,6 +7,8 @@ namespace Lcobucci\JWT; +use InvalidArgumentException; + /** * Basic interface for token signers * @@ -36,16 +38,21 @@ public function modifyHeader(array &$headers); * @param string $key * * @return Signature + * + * @throws InvalidArgumentException When given key is invalid */ public function sign($payload, $key); /** - * Creates a hash with the given data + * Returns if the expected hash matches with the data and key * + * @param string $expected * @param string $payload - * @param string $key + * @param string|resource $key * - * @return string + * @return boolean + * + * @throws InvalidArgumentException When given key is invalid */ - public function createHash($payload, $key); + public function verify($expected, $payload, $key); } diff --git a/src/Signer/BaseSigner.php b/src/Signer/BaseSigner.php index 4de881d4..68507db4 100644 --- a/src/Signer/BaseSigner.php +++ b/src/Signer/BaseSigner.php @@ -34,4 +34,14 @@ public function sign($payload, $key) { return new Signature($this, $this->createHash($payload, $key)); } + + /** + * Creates a hash with the given data + * + * @param string $payload + * @param string|resource $key + * + * @return string + */ + abstract public function createHash($payload, $key); } diff --git a/src/Signer/Ecdsa.php b/src/Signer/Ecdsa.php new file mode 100644 index 00000000..bf833def --- /dev/null +++ b/src/Signer/Ecdsa.php @@ -0,0 +1,29 @@ + + * @since 2.1.0 + */ +abstract class Ecdsa extends OpenSSL +{ + /** + * Returns the key type + * + * @return string + */ + public function getType() + { + return OPENSSL_KEYTYPE_EC; + } +} diff --git a/src/Signer/Ecdsa/Sha256.php b/src/Signer/Ecdsa/Sha256.php new file mode 100644 index 00000000..50bfef95 --- /dev/null +++ b/src/Signer/Ecdsa/Sha256.php @@ -0,0 +1,35 @@ + + * @since 2.1.0 + */ +class Sha256 extends Ecdsa +{ + /** + * {@inheritdoc} + */ + public function getAlgorithmId() + { + return 'ES256'; + } + + /** + * {@inheritdoc} + */ + public function getAlgorithm() + { + return OPENSSL_ALGO_SHA256; + } +} diff --git a/src/Signer/Ecdsa/Sha384.php b/src/Signer/Ecdsa/Sha384.php new file mode 100644 index 00000000..40a4fbc6 --- /dev/null +++ b/src/Signer/Ecdsa/Sha384.php @@ -0,0 +1,35 @@ + + * @since 2.1.0 + */ +class Sha384 extends Ecdsa +{ + /** + * {@inheritdoc} + */ + public function getAlgorithmId() + { + return 'ES384'; + } + + /** + * {@inheritdoc} + */ + public function getAlgorithm() + { + return OPENSSL_ALGO_SHA384; + } +} diff --git a/src/Signer/Ecdsa/Sha512.php b/src/Signer/Ecdsa/Sha512.php new file mode 100644 index 00000000..284bde38 --- /dev/null +++ b/src/Signer/Ecdsa/Sha512.php @@ -0,0 +1,35 @@ + + * @since 2.1.0 + */ +class Sha512 extends Ecdsa +{ + /** + * {@inheritdoc} + */ + public function getAlgorithmId() + { + return 'ES512'; + } + + /** + * {@inheritdoc} + */ + public function getAlgorithm() + { + return OPENSSL_ALGO_SHA512; + } +} diff --git a/src/Signer/Factory.php b/src/Signer/Factory.php index 16b633cc..84fa6a63 100644 --- a/src/Signer/Factory.php +++ b/src/Signer/Factory.php @@ -9,9 +9,15 @@ use InvalidArgumentException; use Lcobucci\JWT\Signer; -use Lcobucci\JWT\Signer\Hmac\Sha256; -use Lcobucci\JWT\Signer\Hmac\Sha384; -use Lcobucci\JWT\Signer\Hmac\Sha512; +use Lcobucci\JWT\Signer\Ecdsa\Sha256 as EcdsaSha256; +use Lcobucci\JWT\Signer\Ecdsa\Sha384 as EcdsaSha384; +use Lcobucci\JWT\Signer\Ecdsa\Sha512 as EcdsaSha512; +use Lcobucci\JWT\Signer\Hmac\Sha256 as HmacSha256; +use Lcobucci\JWT\Signer\Hmac\Sha384 as HmacSha384; +use Lcobucci\JWT\Signer\Hmac\Sha512 as HmacSha512; +use Lcobucci\JWT\Signer\Rsa\Sha256 as RsaSha256; +use Lcobucci\JWT\Signer\Rsa\Sha384 as RsaSha384; +use Lcobucci\JWT\Signer\Rsa\Sha512 as RsaSha512; /** * Factory that returns instance of signers @@ -39,7 +45,13 @@ public function __construct(array $callbacks = []) [ 'HS256' => [$this, 'createHmacSha256'], 'HS384' => [$this, 'createHmacSha384'], - 'HS512' => [$this, 'createHmacSha512'] + 'HS512' => [$this, 'createHmacSha512'], + 'ES256' => [$this, 'createEcdsaSha256'], + 'ES384' => [$this, 'createEcdsaSha384'], + 'ES512' => [$this, 'createEcdsaSha512'], + 'RS256' => [$this, 'createRsaSha256'], + 'RS384' => [$this, 'createRsaSha384'], + 'RS512' => [$this, 'createRsaSha512'] ], $callbacks ); @@ -64,26 +76,74 @@ public function create($id) } /** - * @return Sha256 + * @return HmacSha256 */ private function createHmacSha256() { - return new Sha256(); + return new HmacSha256(); } /** - * @return Sha384 + * @return HmacSha384 */ private function createHmacSha384() { - return new Sha384(); + return new HmacSha384(); } /** - * @return Sha512 + * @return HmacSha512 */ private function createHmacSha512() { - return new Sha512(); + return new HmacSha512(); + } + + /** + * @return RsaSha256 + */ + private function createRsaSha256() + { + return new RsaSha256(); + } + + /** + * @return RsaSha384 + */ + private function createRsaSha384() + { + return new RsaSha384(); + } + + /** + * @return RsaSha512 + */ + private function createRsaSha512() + { + return new RsaSha512(); + } + + /** + * @return EcdsaSha256 + */ + private function createEcdsaSha256() + { + return new EcdsaSha256(); + } + + /** + * @return EcdsaSha384 + */ + private function createEcdsaSha384() + { + return new EcdsaSha384(); + } + + /** + * @return EcdsaSha512 + */ + private function createEcdsaSha512() + { + return new EcdsaSha512(); } } diff --git a/src/Signer/Hmac.php b/src/Signer/Hmac.php index 7bf971e4..b73bf83e 100644 --- a/src/Signer/Hmac.php +++ b/src/Signer/Hmac.php @@ -23,6 +23,41 @@ public function createHash($payload, $key) return hash_hmac($this->getAlgorithm(), $payload, $key, true); } + /** + * {@inheritdoc} + */ + public function verify($expected, $payload, $key) + { + $callback = function_exists('hash_equals') ? 'hash_equals' : [$this, 'hashEquals']; + + return call_user_func($callback, $expected, $this->createHash($payload, $key)); + } + + /** + * PHP < 5.6 timing attack safe hash comparison + * + * @param string $expected + * @param string $generated + * + * @return boolean + */ + public function hashEquals($expected, $generated) + { + $expectedLength = strlen($expected); + + if ($expectedLength !== strlen($generated)) { + return false; + } + + $res = 0; + + for ($i = 0; $i < $expectedLength; ++$i) { + $res |= ord($expected[$i]) ^ ord($generated[$i]); + } + + return $res === 0; + } + /** * Returns the algorithm name * diff --git a/src/Signer/Keychain.php b/src/Signer/Keychain.php new file mode 100644 index 00000000..dff10f5f --- /dev/null +++ b/src/Signer/Keychain.php @@ -0,0 +1,58 @@ + + * @since 2.1.0 + */ +class Keychain +{ + /** + * Returns a private key from file path or content + * + * @param string $key + * @param string $passphrase + * + * @return resource + * + * @throws InvalidArgumentException + */ + public function getPrivateKey($key, $passphrase = '') + { + if ($privateKey = openssl_pkey_get_private($key, $passphrase)) { + return $privateKey; + } + + throw new InvalidArgumentException( + 'You should provid a valid private key (with its passphrase when used)' + ); + } + + /** + * Returns a public key from file path or content + * + * @param string $certificate + * + * @return resource + * + * @throws InvalidArgumentException + */ + public function getPublicKey($certificate) + { + if ($publicKey = openssl_pkey_get_public($certificate)) { + return $publicKey; + } + + throw new InvalidArgumentException('You should provid a valid certificate'); + } +} diff --git a/src/Signer/OpenSSL.php b/src/Signer/OpenSSL.php new file mode 100644 index 00000000..d4eedea1 --- /dev/null +++ b/src/Signer/OpenSSL.php @@ -0,0 +1,72 @@ + + * @since 2.1.0 + */ +abstract class OpenSSL extends BaseSigner +{ + /** + * {@inheritdoc} + */ + public function createHash($payload, $key) + { + $this->validateKey($key); + + $signature = ''; + openssl_sign($payload, $signature, $key, $this->getAlgorithm()); + + return $signature; + } + + /** + * {@inheritdoc} + */ + public function verify($expected, $payload, $key) + { + $this->validateKey($key); + + return openssl_verify($payload, $expected, $key, $this->getAlgorithm()) === 1; + } + + /** + * Returns if the key type is equals with expected type + * + * @param resource $key + * + * @return boolean + */ + private function validateKey($key) + { + $details = openssl_pkey_get_details($key); + + if (!isset($details['key']) || $details['type'] !== $this->getType()) { + throw new InvalidArgumentException('The type of given key does not match with this signer'); + } + } + + /** + * Returns the key type + * + * @return int + */ + abstract public function getType(); + + /** + * Returns the algorithm name + * + * @return string + */ + abstract public function getAlgorithm(); +} diff --git a/src/Signer/Rsa.php b/src/Signer/Rsa.php new file mode 100644 index 00000000..47e56eee --- /dev/null +++ b/src/Signer/Rsa.php @@ -0,0 +1,29 @@ + + * @since 2.1.0 + */ +abstract class Rsa extends OpenSSL +{ + /** + * Returns the key type + * + * @return string + */ + public function getType() + { + return OPENSSL_KEYTYPE_RSA; + } +} diff --git a/src/Signer/Rsa/Sha256.php b/src/Signer/Rsa/Sha256.php new file mode 100644 index 00000000..4e873e16 --- /dev/null +++ b/src/Signer/Rsa/Sha256.php @@ -0,0 +1,35 @@ + + * @since 2.1.0 + */ +class Sha256 extends Rsa +{ + /** + * {@inheritdoc} + */ + public function getAlgorithmId() + { + return 'RS256'; + } + + /** + * {@inheritdoc} + */ + public function getAlgorithm() + { + return OPENSSL_ALGO_SHA256; + } +} diff --git a/src/Signer/Rsa/Sha384.php b/src/Signer/Rsa/Sha384.php new file mode 100644 index 00000000..c5d26313 --- /dev/null +++ b/src/Signer/Rsa/Sha384.php @@ -0,0 +1,35 @@ + + * @since 2.1.0 + */ +class Sha384 extends Rsa +{ + /** + * {@inheritdoc} + */ + public function getAlgorithmId() + { + return 'RS384'; + } + + /** + * {@inheritdoc} + */ + public function getAlgorithm() + { + return OPENSSL_ALGO_SHA384; + } +} diff --git a/src/Signer/Rsa/Sha512.php b/src/Signer/Rsa/Sha512.php new file mode 100644 index 00000000..0d8074b5 --- /dev/null +++ b/src/Signer/Rsa/Sha512.php @@ -0,0 +1,35 @@ + + * @since 2.1.0 + */ +class Sha512 extends Rsa +{ + /** + * {@inheritdoc} + */ + public function getAlgorithmId() + { + return 'RS512'; + } + + /** + * {@inheritdoc} + */ + public function getAlgorithm() + { + return OPENSSL_ALGO_SHA512; + } +} diff --git a/src/Token.php b/src/Token.php index 27edf19c..4236d3fa 100644 --- a/src/Token.php +++ b/src/Token.php @@ -11,6 +11,7 @@ use Generator; use Lcobucci\JWT\Parsing\Encoder; use Lcobucci\JWT\Claim\Validatable; +use OutOfBoundsException; /** * Basic structure of the JWT @@ -95,6 +96,24 @@ public function getClaims() return $this->claims; } + /** + * Returns the value of a token claim + * + * @param string $name + * + * @return mixed + * + * @throws OutOfBoundsException + */ + public function getClaim($name) + { + if (!isset($this->claims[$name])) { + throw new OutOfBoundsException('Requested claim is not configured'); + } + + return $this->claims[$name]->getValue(); + } + /** * Returns the token signature * diff --git a/src/ValidationData.php b/src/ValidationData.php index 137b3007..502c2f03 100644 --- a/src/ValidationData.php +++ b/src/ValidationData.php @@ -85,7 +85,7 @@ public function setSubject($subject) /** * Configures the time that "iat", "nbf" and "exp" should be based on * - * @param int $subject + * @param int $currentTime */ public function setCurrentTime($currentTime) { diff --git a/test/BuilderTest.php b/test/BuilderTest.php index 5da72d49..563c8cd8 100644 --- a/test/BuilderTest.php +++ b/test/BuilderTest.php @@ -13,8 +13,6 @@ /** * @author Luís Otávio Cobucci Oblonczyk * @since 0.1.0 - * - * @coversDefaultClass Lcobucci\JWT\Builder */ class BuilderTest extends \PHPUnit_Framework_TestCase { @@ -57,7 +55,8 @@ private function createBuilder() /** * @test - * @covers ::__construct + * + * @covers Lcobucci\JWT\Builder::__construct */ public function constructMustInitializeTheAttributes() { @@ -72,10 +71,12 @@ public function constructMustInitializeTheAttributes() /** * @test - * @covers ::__construct - * @covers ::setAudience - * @covers ::setRegisteredClaim - * @covers ::set + * + * @uses Lcobucci\JWT\Builder::__construct + * @uses Lcobucci\JWT\Builder::set + * + * @covers Lcobucci\JWT\Builder::setAudience + * @covers Lcobucci\JWT\Builder::setRegisteredClaim */ public function setAudienceMustChangeTheAudClaim() { @@ -88,10 +89,12 @@ public function setAudienceMustChangeTheAudClaim() /** * @test - * @covers ::__construct - * @covers ::setAudience - * @covers ::setRegisteredClaim - * @covers ::set + * + * @uses Lcobucci\JWT\Builder::__construct + * @uses Lcobucci\JWT\Builder::set + * + * @covers Lcobucci\JWT\Builder::setAudience + * @covers Lcobucci\JWT\Builder::setRegisteredClaim */ public function setAudienceCanReplicateItemOnHeader() { @@ -109,10 +112,12 @@ public function setAudienceCanReplicateItemOnHeader() /** * @test - * @covers ::__construct - * @covers ::setAudience - * @covers ::setRegisteredClaim - * @covers ::set + * + * @uses Lcobucci\JWT\Builder::__construct + * @uses Lcobucci\JWT\Builder::set + * + * @covers Lcobucci\JWT\Builder::setAudience + * @covers Lcobucci\JWT\Builder::setRegisteredClaim */ public function setAudienceMustKeepAFluentInterface() { @@ -123,10 +128,12 @@ public function setAudienceMustKeepAFluentInterface() /** * @test - * @covers ::__construct - * @covers ::setExpiration - * @covers ::setRegisteredClaim - * @covers ::set + * + * @uses Lcobucci\JWT\Builder::__construct + * @uses Lcobucci\JWT\Builder::set + * + * @covers Lcobucci\JWT\Builder::setExpiration + * @covers Lcobucci\JWT\Builder::setRegisteredClaim */ public function setExpirationMustChangeTheExpClaim() { @@ -139,10 +146,12 @@ public function setExpirationMustChangeTheExpClaim() /** * @test - * @covers ::__construct - * @covers ::setExpiration - * @covers ::setRegisteredClaim - * @covers ::set + * + * @uses Lcobucci\JWT\Builder::__construct + * @uses Lcobucci\JWT\Builder::set + * + * @covers Lcobucci\JWT\Builder::setExpiration + * @covers Lcobucci\JWT\Builder::setRegisteredClaim */ public function setExpirationCanReplicateItemOnHeader() { @@ -160,10 +169,12 @@ public function setExpirationCanReplicateItemOnHeader() /** * @test - * @covers ::__construct - * @covers ::setExpiration - * @covers ::setRegisteredClaim - * @covers ::set + * + * @uses Lcobucci\JWT\Builder::__construct + * @uses Lcobucci\JWT\Builder::set + * + * @covers Lcobucci\JWT\Builder::setExpiration + * @covers Lcobucci\JWT\Builder::setRegisteredClaim */ public function setExpirationMustKeepAFluentInterface() { @@ -174,10 +185,12 @@ public function setExpirationMustKeepAFluentInterface() /** * @test - * @covers ::__construct - * @covers ::setId - * @covers ::setRegisteredClaim - * @covers ::set + * + * @uses Lcobucci\JWT\Builder::__construct + * @uses Lcobucci\JWT\Builder::set + * + * @covers Lcobucci\JWT\Builder::setId + * @covers Lcobucci\JWT\Builder::setRegisteredClaim */ public function setIdMustChangeTheJtiClaim() { @@ -190,10 +203,12 @@ public function setIdMustChangeTheJtiClaim() /** * @test - * @covers ::__construct - * @covers ::setId - * @covers ::setRegisteredClaim - * @covers ::set + * + * @uses Lcobucci\JWT\Builder::__construct + * @uses Lcobucci\JWT\Builder::set + * + * @covers Lcobucci\JWT\Builder::setId + * @covers Lcobucci\JWT\Builder::setRegisteredClaim */ public function setIdCanReplicateItemOnHeader() { @@ -211,10 +226,12 @@ public function setIdCanReplicateItemOnHeader() /** * @test - * @covers ::__construct - * @covers ::setId - * @covers ::setRegisteredClaim - * @covers ::set + * + * @uses Lcobucci\JWT\Builder::__construct + * @uses Lcobucci\JWT\Builder::set + * + * @covers Lcobucci\JWT\Builder::setId + * @covers Lcobucci\JWT\Builder::setRegisteredClaim */ public function setIdMustKeepAFluentInterface() { @@ -225,10 +242,12 @@ public function setIdMustKeepAFluentInterface() /** * @test - * @covers ::__construct - * @covers ::setIssueAt - * @covers ::setRegisteredClaim - * @covers ::set + * + * @uses Lcobucci\JWT\Builder::__construct + * @uses Lcobucci\JWT\Builder::set + * + * @covers Lcobucci\JWT\Builder::setIssueAt + * @covers Lcobucci\JWT\Builder::setRegisteredClaim */ public function setIssueAtMustChangeTheIatClaim() { @@ -241,10 +260,12 @@ public function setIssueAtMustChangeTheIatClaim() /** * @test - * @covers ::__construct - * @covers ::setIssueAt - * @covers ::setRegisteredClaim - * @covers ::set + * + * @uses Lcobucci\JWT\Builder::__construct + * @uses Lcobucci\JWT\Builder::set + * + * @covers Lcobucci\JWT\Builder::setIssueAt + * @covers Lcobucci\JWT\Builder::setRegisteredClaim */ public function setIssueAtCanReplicateItemOnHeader() { @@ -262,10 +283,12 @@ public function setIssueAtCanReplicateItemOnHeader() /** * @test - * @covers ::__construct - * @covers ::setIssueAt - * @covers ::setRegisteredClaim - * @covers ::set + * + * @uses Lcobucci\JWT\Builder::__construct + * @uses Lcobucci\JWT\Builder::set + * + * @covers Lcobucci\JWT\Builder::setIssueAt + * @covers Lcobucci\JWT\Builder::setRegisteredClaim */ public function setIssueAtMustKeepAFluentInterface() { @@ -276,10 +299,12 @@ public function setIssueAtMustKeepAFluentInterface() /** * @test - * @covers ::__construct - * @covers ::setIssuer - * @covers ::setRegisteredClaim - * @covers ::set + * + * @uses Lcobucci\JWT\Builder::__construct + * @uses Lcobucci\JWT\Builder::set + * + * @covers Lcobucci\JWT\Builder::setIssuer + * @covers Lcobucci\JWT\Builder::setRegisteredClaim */ public function setIssuerMustChangeTheIssClaim() { @@ -292,10 +317,12 @@ public function setIssuerMustChangeTheIssClaim() /** * @test - * @covers ::__construct - * @covers ::setIssuer - * @covers ::setRegisteredClaim - * @covers ::set + * + * @uses Lcobucci\JWT\Builder::__construct + * @uses Lcobucci\JWT\Builder::set + * + * @covers Lcobucci\JWT\Builder::setIssuer + * @covers Lcobucci\JWT\Builder::setRegisteredClaim */ public function setIssuerCanReplicateItemOnHeader() { @@ -313,10 +340,12 @@ public function setIssuerCanReplicateItemOnHeader() /** * @test - * @covers ::__construct - * @covers ::setIssuer - * @covers ::setRegisteredClaim - * @covers ::set + * + * @uses Lcobucci\JWT\Builder::__construct + * @uses Lcobucci\JWT\Builder::set + * + * @covers Lcobucci\JWT\Builder::setIssuer + * @covers Lcobucci\JWT\Builder::setRegisteredClaim */ public function setIssuerMustKeepAFluentInterface() { @@ -327,10 +356,12 @@ public function setIssuerMustKeepAFluentInterface() /** * @test - * @covers ::__construct - * @covers ::setNotBefore - * @covers ::setRegisteredClaim - * @covers ::set + * + * @uses Lcobucci\JWT\Builder::__construct + * @uses Lcobucci\JWT\Builder::set + * + * @covers Lcobucci\JWT\Builder::setNotBefore + * @covers Lcobucci\JWT\Builder::setRegisteredClaim */ public function setNotBeforeMustChangeTheNbfClaim() { @@ -343,10 +374,12 @@ public function setNotBeforeMustChangeTheNbfClaim() /** * @test - * @covers ::__construct - * @covers ::setNotBefore - * @covers ::setRegisteredClaim - * @covers ::set + * + * @uses Lcobucci\JWT\Builder::__construct + * @uses Lcobucci\JWT\Builder::set + * + * @covers Lcobucci\JWT\Builder::setNotBefore + * @covers Lcobucci\JWT\Builder::setRegisteredClaim */ public function setNotBeforeCanReplicateItemOnHeader() { @@ -364,10 +397,12 @@ public function setNotBeforeCanReplicateItemOnHeader() /** * @test - * @covers ::__construct - * @covers ::setNotBefore - * @covers ::setRegisteredClaim - * @covers ::set + * + * @uses Lcobucci\JWT\Builder::__construct + * @uses Lcobucci\JWT\Builder::set + * + * @covers Lcobucci\JWT\Builder::setNotBefore + * @covers Lcobucci\JWT\Builder::setRegisteredClaim */ public function setNotBeforeMustKeepAFluentInterface() { @@ -378,10 +413,12 @@ public function setNotBeforeMustKeepAFluentInterface() /** * @test - * @covers ::__construct - * @covers ::setSubject - * @covers ::setRegisteredClaim - * @covers ::set + * + * @uses Lcobucci\JWT\Builder::__construct + * @uses Lcobucci\JWT\Builder::set + * + * @covers Lcobucci\JWT\Builder::setSubject + * @covers Lcobucci\JWT\Builder::setRegisteredClaim */ public function setSubjectMustChangeTheSubClaim() { @@ -394,10 +431,12 @@ public function setSubjectMustChangeTheSubClaim() /** * @test - * @covers ::__construct - * @covers ::setSubject - * @covers ::setRegisteredClaim - * @covers ::set + * + * @uses Lcobucci\JWT\Builder::__construct + * @uses Lcobucci\JWT\Builder::set + * + * @covers Lcobucci\JWT\Builder::setSubject + * @covers Lcobucci\JWT\Builder::setRegisteredClaim */ public function setSubjectCanReplicateItemOnHeader() { @@ -415,10 +454,12 @@ public function setSubjectCanReplicateItemOnHeader() /** * @test - * @covers ::__construct - * @covers ::setSubject - * @covers ::setRegisteredClaim - * @covers ::set + * + * @uses Lcobucci\JWT\Builder::__construct + * @uses Lcobucci\JWT\Builder::set + * + * @covers Lcobucci\JWT\Builder::setSubject + * @covers Lcobucci\JWT\Builder::setRegisteredClaim */ public function setSubjectMustKeepAFluentInterface() { @@ -429,8 +470,10 @@ public function setSubjectMustKeepAFluentInterface() /** * @test - * @covers ::__construct - * @covers ::set + * + * @uses Lcobucci\JWT\Builder::__construct + * + * @covers Lcobucci\JWT\Builder::set */ public function setMustConfigureTheGivenClaim() { @@ -442,8 +485,10 @@ public function setMustConfigureTheGivenClaim() /** * @test - * @covers ::__construct - * @covers ::set + * + * @uses Lcobucci\JWT\Builder::__construct + * + * @covers Lcobucci\JWT\Builder::set */ public function setMustKeepAFluentInterface() { @@ -454,14 +499,16 @@ public function setMustKeepAFluentInterface() /** * @test - * @covers ::__construct - * @covers ::set - * @covers ::getToken - * @covers Lcobucci\JWT\Token::__construct - * @covers Lcobucci\JWT\Token::setEncoder - * @covers Lcobucci\JWT\Token::getHeader - * @covers Lcobucci\JWT\Token::getClaims - * @covers Lcobucci\JWT\Token::getSignature + * + * @uses Lcobucci\JWT\Builder::__construct + * @uses Lcobucci\JWT\Builder::set + * @uses Lcobucci\JWT\Token::__construct + * @uses Lcobucci\JWT\Token::setEncoder + * @uses Lcobucci\JWT\Token::getHeader + * @uses Lcobucci\JWT\Token::getClaims + * @uses Lcobucci\JWT\Token::getSignature + * + * @covers Lcobucci\JWT\Builder::getToken */ public function getTokenMustReturnANewTokenWithCurrentConfiguration() { @@ -475,12 +522,14 @@ public function getTokenMustReturnANewTokenWithCurrentConfiguration() /** * @test - * @covers ::__construct - * @covers ::sign - * @covers ::getToken - * @covers Lcobucci\JWT\Token::__construct - * @covers Lcobucci\JWT\Token::setEncoder - * @covers Lcobucci\JWT\Token::getPayload + * + * @uses Lcobucci\JWT\Builder::__construct + * @uses Lcobucci\JWT\Builder::getToken + * @uses Lcobucci\JWT\Token::__construct + * @uses Lcobucci\JWT\Token::setEncoder + * @uses Lcobucci\JWT\Token::getPayload + * + * @covers Lcobucci\JWT\Builder::sign */ public function signMustChangeTheSignature() { @@ -499,12 +548,14 @@ public function signMustChangeTheSignature() /** * @test - * @covers ::__construct - * @covers ::sign - * @covers ::getToken - * @covers Lcobucci\JWT\Token::__construct - * @covers Lcobucci\JWT\Token::setEncoder - * @covers Lcobucci\JWT\Token::getPayload + * + * @uses Lcobucci\JWT\Builder::__construct + * @uses Lcobucci\JWT\Builder::getToken + * @uses Lcobucci\JWT\Token::__construct + * @uses Lcobucci\JWT\Token::setEncoder + * @uses Lcobucci\JWT\Token::getPayload + * + * @covers Lcobucci\JWT\Builder::sign */ public function signMustKeepAFluentInterface() { @@ -524,8 +575,10 @@ public function signMustKeepAFluentInterface() /** * @test + * * @depends signMustKeepAFluentInterface - * @covers ::unsign + * + * @covers Lcobucci\JWT\Builder::unsign */ public function unsignMustRemoveTheSignature(Builder $builder) { @@ -536,8 +589,10 @@ public function unsignMustRemoveTheSignature(Builder $builder) /** * @test + * * @depends signMustKeepAFluentInterface - * @covers ::unsign + * + * @covers Lcobucci\JWT\Builder::unsign */ public function unsignMustKeepAFluentInterface(Builder $builder) { @@ -546,13 +601,15 @@ public function unsignMustKeepAFluentInterface(Builder $builder) /** * @test - * @covers ::__construct - * @covers ::set - * @covers ::sign - * @covers ::getToken - * @covers Lcobucci\JWT\Token::__construct - * @covers Lcobucci\JWT\Token::setEncoder - * @covers Lcobucci\JWT\Token::getPayload + * + * @uses Lcobucci\JWT\Builder::__construct + * @uses Lcobucci\JWT\Builder::sign + * @uses Lcobucci\JWT\Builder::getToken + * @uses Lcobucci\JWT\Token::__construct + * @uses Lcobucci\JWT\Token::setEncoder + * @uses Lcobucci\JWT\Token::getPayload + * + * @covers Lcobucci\JWT\Builder::set * * @expectedException BadMethodCallException */ diff --git a/test/Claim/BasicTest.php b/test/Claim/BasicTest.php index 4867879f..a7b67fa9 100644 --- a/test/Claim/BasicTest.php +++ b/test/Claim/BasicTest.php @@ -10,15 +10,13 @@ /** * @author Luís Otávio Cobucci Oblonczyk * @since 2.0.0 - * - * @coversDefaultClass Lcobucci\JWT\Claim\Basic */ class BasicTest extends \PHPUnit_Framework_TestCase { /** * @test * - * @covers ::__construct + * @covers Lcobucci\JWT\Claim\Basic::__construct */ public function constructorShouldConfigureTheAttributes() { @@ -31,8 +29,9 @@ public function constructorShouldConfigureTheAttributes() /** * @test * - * @covers ::__construct - * @covers ::getName + * @uses Lcobucci\JWT\Claim\Basic::__construct + * + * @covers Lcobucci\JWT\Claim\Basic::getName */ public function getNameShouldReturnTheClaimName() { @@ -44,8 +43,9 @@ public function getNameShouldReturnTheClaimName() /** * @test * - * @covers ::__construct - * @covers ::getValue + * @uses Lcobucci\JWT\Claim\Basic::__construct + * + * @covers Lcobucci\JWT\Claim\Basic::getValue */ public function getValueShouldReturnTheClaimValue() { @@ -57,8 +57,9 @@ public function getValueShouldReturnTheClaimValue() /** * @test * - * @covers ::__construct - * @covers ::jsonSerialize + * @uses Lcobucci\JWT\Claim\Basic::__construct + * + * @covers Lcobucci\JWT\Claim\Basic::jsonSerialize */ public function jsonSerializeShouldReturnTheClaimValue() { @@ -70,8 +71,9 @@ public function jsonSerializeShouldReturnTheClaimValue() /** * @test * - * @covers ::__construct - * @covers ::__toString + * @uses Lcobucci\JWT\Claim\Basic::__construct + * + * @covers Lcobucci\JWT\Claim\Basic::__toString */ public function toStringShouldReturnTheClaimValue() { diff --git a/test/Claim/EqualsToTest.php b/test/Claim/EqualsToTest.php index 3838f6ae..ab061698 100644 --- a/test/Claim/EqualsToTest.php +++ b/test/Claim/EqualsToTest.php @@ -12,19 +12,18 @@ /** * @author Luís Otávio Cobucci Oblonczyk * @since 2.0.0 - * - * @coversDefaultClass Lcobucci\JWT\Claim\EqualsTo */ class EqualsToTest extends \PHPUnit_Framework_TestCase { /** * @test * - * @covers ::validate - * @covers Lcobucci\JWT\Claim\Basic::__construct - * @covers Lcobucci\JWT\Claim\Basic::getName - * @covers Lcobucci\JWT\ValidationData::__construct - * @covers Lcobucci\JWT\ValidationData::has + * @uses Lcobucci\JWT\Claim\Basic::__construct + * @uses Lcobucci\JWT\Claim\Basic::getName + * @uses Lcobucci\JWT\ValidationData::__construct + * @uses Lcobucci\JWT\ValidationData::has + * + * @covers Lcobucci\JWT\Claim\EqualsTo::validate */ public function validateShouldReturnTrueWhenValidationDontHaveTheClaim() { @@ -36,14 +35,15 @@ public function validateShouldReturnTrueWhenValidationDontHaveTheClaim() /** * @test * - * @covers ::validate - * @covers Lcobucci\JWT\Claim\Basic::__construct - * @covers Lcobucci\JWT\Claim\Basic::getName - * @covers Lcobucci\JWT\Claim\Basic::getValue - * @covers Lcobucci\JWT\ValidationData::__construct - * @covers Lcobucci\JWT\ValidationData::setIssuer - * @covers Lcobucci\JWT\ValidationData::has - * @covers Lcobucci\JWT\ValidationData::get + * @uses Lcobucci\JWT\Claim\Basic::__construct + * @uses Lcobucci\JWT\Claim\Basic::getName + * @uses Lcobucci\JWT\Claim\Basic::getValue + * @uses Lcobucci\JWT\ValidationData::__construct + * @uses Lcobucci\JWT\ValidationData::setIssuer + * @uses Lcobucci\JWT\ValidationData::has + * @uses Lcobucci\JWT\ValidationData::get + * + * @covers Lcobucci\JWT\Claim\EqualsTo::validate */ public function validateShouldReturnTrueWhenValueIsEqualsToValidationData() { @@ -58,14 +58,15 @@ public function validateShouldReturnTrueWhenValueIsEqualsToValidationData() /** * @test * - * @covers ::validate - * @covers Lcobucci\JWT\Claim\Basic::__construct - * @covers Lcobucci\JWT\Claim\Basic::getName - * @covers Lcobucci\JWT\Claim\Basic::getValue - * @covers Lcobucci\JWT\ValidationData::__construct - * @covers Lcobucci\JWT\ValidationData::setIssuer - * @covers Lcobucci\JWT\ValidationData::has - * @covers Lcobucci\JWT\ValidationData::get + * @uses Lcobucci\JWT\Claim\Basic::__construct + * @uses Lcobucci\JWT\Claim\Basic::getName + * @uses Lcobucci\JWT\Claim\Basic::getValue + * @uses Lcobucci\JWT\ValidationData::__construct + * @uses Lcobucci\JWT\ValidationData::setIssuer + * @uses Lcobucci\JWT\ValidationData::has + * @uses Lcobucci\JWT\ValidationData::get + * + * @covers Lcobucci\JWT\Claim\EqualsTo::validate */ public function validateShouldReturnFalseWhenValueIsNotEqualsToValidationData() { diff --git a/test/Claim/FactoryTest.php b/test/Claim/FactoryTest.php index d60b7b38..e7cca2cb 100644 --- a/test/Claim/FactoryTest.php +++ b/test/Claim/FactoryTest.php @@ -10,18 +10,42 @@ /** * @author Luís Otávio Cobucci Oblonczyk * @since 2.0.0 - * - * @coversDefaultClass Lcobucci\JWT\Claim\Factory */ class FactoryTest extends \PHPUnit_Framework_TestCase { /** * @test * - * @covers ::__construct - * @covers ::create - * @covers ::createLesserOrEqualsTo - * @covers Lcobucci\JWT\Claim\Basic::__construct + * @covers Lcobucci\JWT\Claim\Factory::__construct + */ + public function constructMustConfigureTheCallbacks() + { + $callback = function () { + }; + $factory = new Factory(['test' => $callback]); + + $expected = [ + 'iat' => [$factory, 'createLesserOrEqualsTo'], + 'nbf' => [$factory, 'createLesserOrEqualsTo'], + 'exp' => [$factory, 'createGreaterOrEqualsTo'], + 'iss' => [$factory, 'createEqualsTo'], + 'aud' => [$factory, 'createEqualsTo'], + 'sub' => [$factory, 'createEqualsTo'], + 'jti' => [$factory, 'createEqualsTo'], + 'test' => $callback + ]; + + $this->assertAttributeEquals($expected, 'callbacks', $factory); + } + + /** + * @test + * + * @uses Lcobucci\JWT\Claim\Factory::__construct + * @uses Lcobucci\JWT\Claim\Basic::__construct + * + * @covers Lcobucci\JWT\Claim\Factory::create + * @covers Lcobucci\JWT\Claim\Factory::createLesserOrEqualsTo */ public function createShouldReturnALesserOrEqualsToClaimForIssuedAt() { @@ -33,10 +57,11 @@ public function createShouldReturnALesserOrEqualsToClaimForIssuedAt() /** * @test * - * @covers ::__construct - * @covers ::create - * @covers ::createLesserOrEqualsTo - * @covers Lcobucci\JWT\Claim\Basic::__construct + * @uses Lcobucci\JWT\Claim\Factory::__construct + * @uses Lcobucci\JWT\Claim\Basic::__construct + * + * @covers Lcobucci\JWT\Claim\Factory::create + * @covers Lcobucci\JWT\Claim\Factory::createLesserOrEqualsTo */ public function createShouldReturnALesserOrEqualsToClaimForNotBefore() { @@ -48,10 +73,11 @@ public function createShouldReturnALesserOrEqualsToClaimForNotBefore() /** * @test * - * @covers ::__construct - * @covers ::create - * @covers ::createGreaterOrEqualsTo - * @covers Lcobucci\JWT\Claim\Basic::__construct + * @uses Lcobucci\JWT\Claim\Factory::__construct + * @uses Lcobucci\JWT\Claim\Basic::__construct + * + * @covers Lcobucci\JWT\Claim\Factory::create + * @covers Lcobucci\JWT\Claim\Factory::createGreaterOrEqualsTo */ public function createShouldReturnAGreaterOrEqualsToClaimForExpiration() { @@ -63,10 +89,11 @@ public function createShouldReturnAGreaterOrEqualsToClaimForExpiration() /** * @test * - * @covers ::__construct - * @covers ::create - * @covers ::createEqualsTo - * @covers Lcobucci\JWT\Claim\Basic::__construct + * @uses Lcobucci\JWT\Claim\Factory::__construct + * @uses Lcobucci\JWT\Claim\Basic::__construct + * + * @covers Lcobucci\JWT\Claim\Factory::create + * @covers Lcobucci\JWT\Claim\Factory::createEqualsTo */ public function createShouldReturnAnEqualsToClaimForId() { @@ -78,10 +105,11 @@ public function createShouldReturnAnEqualsToClaimForId() /** * @test * - * @covers ::__construct - * @covers ::create - * @covers ::createEqualsTo - * @covers Lcobucci\JWT\Claim\Basic::__construct + * @uses Lcobucci\JWT\Claim\Factory::__construct + * @uses Lcobucci\JWT\Claim\Basic::__construct + * + * @covers Lcobucci\JWT\Claim\Factory::create + * @covers Lcobucci\JWT\Claim\Factory::createEqualsTo */ public function createShouldReturnAnEqualsToClaimForIssuer() { @@ -93,10 +121,11 @@ public function createShouldReturnAnEqualsToClaimForIssuer() /** * @test * - * @covers ::__construct - * @covers ::create - * @covers ::createEqualsTo - * @covers Lcobucci\JWT\Claim\Basic::__construct + * @uses Lcobucci\JWT\Claim\Factory::__construct + * @uses Lcobucci\JWT\Claim\Basic::__construct + * + * @covers Lcobucci\JWT\Claim\Factory::create + * @covers Lcobucci\JWT\Claim\Factory::createEqualsTo */ public function createShouldReturnAnEqualsToClaimForAudience() { @@ -108,10 +137,11 @@ public function createShouldReturnAnEqualsToClaimForAudience() /** * @test * - * @covers ::__construct - * @covers ::create - * @covers ::createEqualsTo - * @covers Lcobucci\JWT\Claim\Basic::__construct + * @uses Lcobucci\JWT\Claim\Factory::__construct + * @uses Lcobucci\JWT\Claim\Basic::__construct + * + * @covers Lcobucci\JWT\Claim\Factory::create + * @covers Lcobucci\JWT\Claim\Factory::createEqualsTo */ public function createShouldReturnAnEqualsToClaimForSubject() { @@ -123,10 +153,11 @@ public function createShouldReturnAnEqualsToClaimForSubject() /** * @test * - * @covers ::__construct - * @covers ::create - * @covers ::createBasic - * @covers Lcobucci\JWT\Claim\Basic::__construct + * @uses Lcobucci\JWT\Claim\Factory::__construct + * @uses Lcobucci\JWT\Claim\Basic::__construct + * + * @covers Lcobucci\JWT\Claim\Factory::create + * @covers Lcobucci\JWT\Claim\Factory::createBasic */ public function createShouldReturnABasiclaimForOtherClaims() { diff --git a/test/Claim/GreaterOrEqualsToTest.php b/test/Claim/GreaterOrEqualsToTest.php index a7ccfe74..83d94bda 100644 --- a/test/Claim/GreaterOrEqualsToTest.php +++ b/test/Claim/GreaterOrEqualsToTest.php @@ -12,19 +12,18 @@ /** * @author Luís Otávio Cobucci Oblonczyk * @since 2.0.0 - * - * @coversDefaultClass Lcobucci\JWT\Claim\GreaterOrEqualsTo */ class GreaterOrEqualsToTest extends \PHPUnit_Framework_TestCase { /** * @test * - * @covers ::validate - * @covers Lcobucci\JWT\Claim\Basic::__construct - * @covers Lcobucci\JWT\Claim\Basic::getName - * @covers Lcobucci\JWT\ValidationData::__construct - * @covers Lcobucci\JWT\ValidationData::has + * @uses Lcobucci\JWT\Claim\Basic::__construct + * @uses Lcobucci\JWT\Claim\Basic::getName + * @uses Lcobucci\JWT\ValidationData::__construct + * @uses Lcobucci\JWT\ValidationData::has + * + * @covers Lcobucci\JWT\Claim\GreaterOrEqualsTo::validate */ public function validateShouldReturnTrueWhenValidationDontHaveTheClaim() { @@ -36,14 +35,15 @@ public function validateShouldReturnTrueWhenValidationDontHaveTheClaim() /** * @test * - * @covers ::validate - * @covers Lcobucci\JWT\Claim\Basic::__construct - * @covers Lcobucci\JWT\Claim\Basic::getName - * @covers Lcobucci\JWT\Claim\Basic::getValue - * @covers Lcobucci\JWT\ValidationData::__construct - * @covers Lcobucci\JWT\ValidationData::setIssuer - * @covers Lcobucci\JWT\ValidationData::has - * @covers Lcobucci\JWT\ValidationData::get + * @uses Lcobucci\JWT\Claim\Basic::__construct + * @uses Lcobucci\JWT\Claim\Basic::getName + * @uses Lcobucci\JWT\Claim\Basic::getValue + * @uses Lcobucci\JWT\ValidationData::__construct + * @uses Lcobucci\JWT\ValidationData::setIssuer + * @uses Lcobucci\JWT\ValidationData::has + * @uses Lcobucci\JWT\ValidationData::get + * + * @covers Lcobucci\JWT\Claim\GreaterOrEqualsTo::validate */ public function validateShouldReturnTrueWhenValueIsGreaterThanValidationData() { @@ -58,14 +58,15 @@ public function validateShouldReturnTrueWhenValueIsGreaterThanValidationData() /** * @test * - * @covers ::validate - * @covers Lcobucci\JWT\Claim\Basic::__construct - * @covers Lcobucci\JWT\Claim\Basic::getName - * @covers Lcobucci\JWT\Claim\Basic::getValue - * @covers Lcobucci\JWT\ValidationData::__construct - * @covers Lcobucci\JWT\ValidationData::setIssuer - * @covers Lcobucci\JWT\ValidationData::has - * @covers Lcobucci\JWT\ValidationData::get + * @uses Lcobucci\JWT\Claim\Basic::__construct + * @uses Lcobucci\JWT\Claim\Basic::getName + * @uses Lcobucci\JWT\Claim\Basic::getValue + * @uses Lcobucci\JWT\ValidationData::__construct + * @uses Lcobucci\JWT\ValidationData::setIssuer + * @uses Lcobucci\JWT\ValidationData::has + * @uses Lcobucci\JWT\ValidationData::get + * + * @covers Lcobucci\JWT\Claim\GreaterOrEqualsTo::validate */ public function validateShouldReturnTrueWhenValueIsEqualsToValidationData() { @@ -80,14 +81,15 @@ public function validateShouldReturnTrueWhenValueIsEqualsToValidationData() /** * @test * - * @covers ::validate - * @covers Lcobucci\JWT\Claim\Basic::__construct - * @covers Lcobucci\JWT\Claim\Basic::getName - * @covers Lcobucci\JWT\Claim\Basic::getValue - * @covers Lcobucci\JWT\ValidationData::__construct - * @covers Lcobucci\JWT\ValidationData::setIssuer - * @covers Lcobucci\JWT\ValidationData::has - * @covers Lcobucci\JWT\ValidationData::get + * @uses Lcobucci\JWT\Claim\Basic::__construct + * @uses Lcobucci\JWT\Claim\Basic::getName + * @uses Lcobucci\JWT\Claim\Basic::getValue + * @uses Lcobucci\JWT\ValidationData::__construct + * @uses Lcobucci\JWT\ValidationData::setIssuer + * @uses Lcobucci\JWT\ValidationData::has + * @uses Lcobucci\JWT\ValidationData::get + * + * @covers Lcobucci\JWT\Claim\GreaterOrEqualsTo::validate */ public function validateShouldReturnFalseWhenValueIsLesserThanValidationData() { diff --git a/test/Claim/LesserOrEqualsToTest.php b/test/Claim/LesserOrEqualsToTest.php index af9cbc46..fc4e0ed0 100644 --- a/test/Claim/LesserOrEqualsToTest.php +++ b/test/Claim/LesserOrEqualsToTest.php @@ -12,19 +12,18 @@ /** * @author Luís Otávio Cobucci Oblonczyk * @since 2.0.0 - * - * @coversDefaultClass Lcobucci\JWT\Claim\LesserOrEqualsTo */ class LesserOrEqualsToTest extends \PHPUnit_Framework_TestCase { /** * @test * - * @covers ::validate - * @covers Lcobucci\JWT\Claim\Basic::__construct - * @covers Lcobucci\JWT\Claim\Basic::getName - * @covers Lcobucci\JWT\ValidationData::__construct - * @covers Lcobucci\JWT\ValidationData::has + * @uses Lcobucci\JWT\Claim\Basic::__construct + * @uses Lcobucci\JWT\Claim\Basic::getName + * @uses Lcobucci\JWT\ValidationData::__construct + * @uses Lcobucci\JWT\ValidationData::has + * + * @covers Lcobucci\JWT\Claim\LesserOrEqualsTo::validate */ public function validateShouldReturnTrueWhenValidationDontHaveTheClaim() { @@ -36,14 +35,15 @@ public function validateShouldReturnTrueWhenValidationDontHaveTheClaim() /** * @test * - * @covers ::validate - * @covers Lcobucci\JWT\Claim\Basic::__construct - * @covers Lcobucci\JWT\Claim\Basic::getName - * @covers Lcobucci\JWT\Claim\Basic::getValue - * @covers Lcobucci\JWT\ValidationData::__construct - * @covers Lcobucci\JWT\ValidationData::setIssuer - * @covers Lcobucci\JWT\ValidationData::has - * @covers Lcobucci\JWT\ValidationData::get + * @uses Lcobucci\JWT\Claim\Basic::__construct + * @uses Lcobucci\JWT\Claim\Basic::getName + * @uses Lcobucci\JWT\Claim\Basic::getValue + * @uses Lcobucci\JWT\ValidationData::__construct + * @uses Lcobucci\JWT\ValidationData::setIssuer + * @uses Lcobucci\JWT\ValidationData::has + * @uses Lcobucci\JWT\ValidationData::get + * + * @covers Lcobucci\JWT\Claim\LesserOrEqualsTo::validate */ public function validateShouldReturnTrueWhenValueIsLesserThanValidationData() { @@ -58,14 +58,15 @@ public function validateShouldReturnTrueWhenValueIsLesserThanValidationData() /** * @test * - * @covers ::validate - * @covers Lcobucci\JWT\Claim\Basic::__construct - * @covers Lcobucci\JWT\Claim\Basic::getName - * @covers Lcobucci\JWT\Claim\Basic::getValue - * @covers Lcobucci\JWT\ValidationData::__construct - * @covers Lcobucci\JWT\ValidationData::setIssuer - * @covers Lcobucci\JWT\ValidationData::has - * @covers Lcobucci\JWT\ValidationData::get + * @uses Lcobucci\JWT\Claim\Basic::__construct + * @uses Lcobucci\JWT\Claim\Basic::getName + * @uses Lcobucci\JWT\Claim\Basic::getValue + * @uses Lcobucci\JWT\ValidationData::__construct + * @uses Lcobucci\JWT\ValidationData::setIssuer + * @uses Lcobucci\JWT\ValidationData::has + * @uses Lcobucci\JWT\ValidationData::get + * + * @covers Lcobucci\JWT\Claim\LesserOrEqualsTo::validate */ public function validateShouldReturnTrueWhenValueIsEqualsToValidationData() { @@ -80,14 +81,15 @@ public function validateShouldReturnTrueWhenValueIsEqualsToValidationData() /** * @test * - * @covers ::validate - * @covers Lcobucci\JWT\Claim\Basic::__construct - * @covers Lcobucci\JWT\Claim\Basic::getName - * @covers Lcobucci\JWT\Claim\Basic::getValue - * @covers Lcobucci\JWT\ValidationData::__construct - * @covers Lcobucci\JWT\ValidationData::setIssuer - * @covers Lcobucci\JWT\ValidationData::has - * @covers Lcobucci\JWT\ValidationData::get + * @uses Lcobucci\JWT\Claim\Basic::__construct + * @uses Lcobucci\JWT\Claim\Basic::getName + * @uses Lcobucci\JWT\Claim\Basic::getValue + * @uses Lcobucci\JWT\ValidationData::__construct + * @uses Lcobucci\JWT\ValidationData::setIssuer + * @uses Lcobucci\JWT\ValidationData::has + * @uses Lcobucci\JWT\ValidationData::get + * + * @covers Lcobucci\JWT\Claim\LesserOrEqualsTo::validate */ public function validateShouldReturnFalseWhenValueIsGreaterThanValidationData() { diff --git a/test/EcdsaKeys.php b/test/EcdsaKeys.php new file mode 100644 index 00000000..67fb6b14 --- /dev/null +++ b/test/EcdsaKeys.php @@ -0,0 +1,88 @@ + + * @since 2.1.0 + */ +trait EcdsaKeys +{ + /** + * @return string + */ + protected function privateEcdsa() + { + return openssl_pkey_get_private( + '-----BEGIN EC PARAMETERS-----' . PHP_EOL + . 'BgUrgQQAJw==' . PHP_EOL + . '-----END EC PARAMETERS-----' . PHP_EOL + . '-----BEGIN EC PRIVATE KEY-----' . PHP_EOL + . 'MIHuAgEBBEgDRUM4kmPRmsGwTcnIO1s9sZIHKzqwXKaveOX0NFWbP6HC8heO/2YV' . PHP_EOL + . 'SWDgKYJa905X2j9YgZxYBSUVk+sBg6rqgjjrpDRIXWWgBwYFK4EEACehgZUDgZIA' . PHP_EOL + . 'BAS/53Ebxy202oxYbDUMuf4r+VXe4JBZt00dOFnfAKU7iIF42vHaq11rYlny4xhm' . PHP_EOL + . '1VC8Mp7vzVT8aAMPVyzeoxny7fukbDHutAXhPSLHoP3FqVzBzNPKyrLMGtethIF2' . PHP_EOL + . '9GXPyg/HD96OUFnoIRzl61da4C1QpLQRY5wUUUSHvwCo3IpOBPR9PpLhp/hwb5lq' . PHP_EOL + . 'Tw==' . PHP_EOL + . '-----END EC PRIVATE KEY-----' + ); + } + + /** + * @return string + */ + protected function publicEcdsa() + { + return openssl_pkey_get_public( + '-----BEGIN CERTIFICATE-----' . PHP_EOL + . 'MIIC3TCCAjKgAwIBAgIJAIRSqqEhTGdPMAoGCCqGSM49BAMCMHsxCzAJBgNVBAYT' . PHP_EOL + . 'AkJSMRcwFQYDVQQIDA5TYW50YSBDYXRhcmluYTETMBEGA1UEBwwKUGFsaG/Dg8Kn' . PHP_EOL + . 'YTETMBEGA1UECgwKRGFyd2luc29mdDEPMA0GA1UEAwwGYWEuZGV2MRgwFgYJKoZI' . PHP_EOL + . 'hvcNAQkBFglhYUBhYS5jb20wHhcNMTUwMzA3MDIyOTM3WhcNMTYwMzA2MDIyOTM3' . PHP_EOL + . 'WjB7MQswCQYDVQQGEwJCUjEXMBUGA1UECAwOU2FudGEgQ2F0YXJpbmExEzARBgNV' . PHP_EOL + . 'BAcMClBhbGhvw4PCp2ExEzARBgNVBAoMCkRhcndpbnNvZnQxDzANBgNVBAMMBmFh' . PHP_EOL + . 'LmRldjEYMBYGCSqGSIb3DQEJARYJYWFAYWEuY29tMIGnMBAGByqGSM49AgEGBSuB' . PHP_EOL + . 'BAAnA4GSAAQEv+dxG8cttNqMWGw1DLn+K/lV3uCQWbdNHThZ3wClO4iBeNrx2qtd' . PHP_EOL + . 'a2JZ8uMYZtVQvDKe781U/GgDD1cs3qMZ8u37pGwx7rQF4T0ix6D9xalcwczTysqy' . PHP_EOL + . 'zBrXrYSBdvRlz8oPxw/ejlBZ6CEc5etXWuAtUKS0EWOcFFFEh78AqNyKTgT0fT6S' . PHP_EOL + . '4af4cG+Zak+jUDBOMB0GA1UdDgQWBBTr67eZAsb3gl+2GN5zubBTH2JZVDAfBgNV' . PHP_EOL + . 'HSMEGDAWgBTr67eZAsb3gl+2GN5zubBTH2JZVDAMBgNVHRMEBTADAQH/MAoGCCqG' . PHP_EOL + . 'SM49BAMCA4GYADCBlAJIASbENQZGlxky0cVbPyEnolSbYaX7vmXS64cQ0jjZWng4' . PHP_EOL + . 'nVauE803oH1kQTpLedDvGnz5Gl+w2uKpkTlGvYv0IrE6CBPtxsc9AkgCtO4RDWWd' . PHP_EOL + . 'xVKqbZZ9XN5PkdWD12g3xWwYcxgWVI++r4FHRZKE8izH9Mw+lDTrkJJ+G80Q9r24' . PHP_EOL + . 'zI1qdkGoueCawNCmGHpl0VE=' . PHP_EOL + . '-----END CERTIFICATE-----' + ); + } + + /** + * @return string + */ + protected function otherPublicEcdsa() + { + return openssl_pkey_get_public( + '-----BEGIN CERTIFICATE-----' . PHP_EOL + . 'MIICqTCCAf6gAwIBAgIJAIeHgJ7CfZWbMAoGCCqGSM49BAMCMGExCzAJBgNVBAYT' . PHP_EOL + . 'AkJSMRcwFQYDVQQIDA5TYW50YSBDYXRhcmluYTEWMBQGA1UEBwwNRmxvcmlhbm9w' . PHP_EOL + . 'b2xpczEhMB8GA1UECgwYSW50ZXJuZXQgV2lkZ2l0cyBQdHkgTHRkMB4XDTE1MDMw' . PHP_EOL + . 'NzA2MzI0N1oXDTE2MDMwNjA2MzI0N1owYTELMAkGA1UEBhMCQlIxFzAVBgNVBAgM' . PHP_EOL + . 'DlNhbnRhIENhdGFyaW5hMRYwFAYDVQQHDA1GbG9yaWFub3BvbGlzMSEwHwYDVQQK' . PHP_EOL + . 'DBhJbnRlcm5ldCBXaWRnaXRzIFB0eSBMdGQwgacwEAYHKoZIzj0CAQYFK4EEACcD' . PHP_EOL + . 'gZIABAVUWiWwhtdoh1AhicYxrVpXgfH11uGuqKPZWl3NN55lyjQryR11eyYTeGdK' . PHP_EOL + . 'jA/rlY6x6aZQlwJPOlx9OsIpJihmQHmRmjCKQQVQ0g09bvG1p+1iwypcwktbolwy' . PHP_EOL + . '/PTQXskrd+yDRqac/OswAT3wH+4wCB4n7sIOhR0fVcZTLP6J6eMZsEH5gEymmTEU' . PHP_EOL + . '9KOrqKNQME4wHQYDVR0OBBYEFLZX0T3crVAYaRtx6y2gKp5nTxAiMB8GA1UdIwQY' . PHP_EOL + . 'MBaAFLZX0T3crVAYaRtx6y2gKp5nTxAiMAwGA1UdEwQFMAMBAf8wCgYIKoZIzj0E' . PHP_EOL + . 'AwIDgZgAMIGUAkgAhamrl47ei0cJi39mVEqjVBcK3t9+K7jUz3fdd+OEoLEU2uGa' . PHP_EOL + . 'uFlG58cEOH87/qETstM0balx2brY09nawmec/7M+5zmDO+MCSAEnkFjdz9Ty8zp6' . PHP_EOL + . 'DaTaBhPeyQ93QJwgohSBNPLcNqt9fLFReL0xW6tr+wsuP9SEVoqP+gkOScjjWwBv' . PHP_EOL + . 'KgmBR9HFiPBw+oHadw==' . PHP_EOL + . '-----END CERTIFICATE-----' + ); + } +} diff --git a/test/FunctionalTests/EcdsaTokenTest.php b/test/FunctionalTests/EcdsaTokenTest.php new file mode 100644 index 00000000..de92689d --- /dev/null +++ b/test/FunctionalTests/EcdsaTokenTest.php @@ -0,0 +1,123 @@ + + * @since 2.1.0 + */ +class EcdsaTokenTest extends \PHPUnit_Framework_TestCase +{ + use EcdsaKeys; + + /** + * @test + * + * @covers Lcobucci\JWT\Builder + * @covers Lcobucci\JWT\Token + * @covers Lcobucci\JWT\Signature + * @covers Lcobucci\JWT\Claim\Factory + * @covers Lcobucci\JWT\Claim\Basic + * @covers Lcobucci\JWT\Parsing\Encoder + * @covers Lcobucci\JWT\Signer\BaseSigner + * @covers Lcobucci\JWT\Signer\OpenSSL + * @covers Lcobucci\JWT\Signer\Ecdsa + * @covers Lcobucci\JWT\Signer\Ecdsa\Sha256 + */ + public function builderCanGenerateAToken() + { + $user = ['name' => 'testing', 'email' => 'testing@abc.com']; + + $token = (new Builder())->setId(1) + ->setAudience('http://client.abc.com') + ->setIssuer('http://api.abc.com') + ->set('user', $user) + ->sign(new Sha256(), $this->privateEcdsa()) + ->getToken(); + + $this->assertAttributeInstanceOf(Signature::class, 'signature', $token); + $this->assertEquals('http://client.abc.com', $token->getClaim('aud')); + $this->assertEquals('http://api.abc.com', $token->getClaim('iss')); + $this->assertEquals($user, $token->getClaim('user')); + + return $token; + } + + /** + * @test + * + * @depends builderCanGenerateAToken + * + * @covers Lcobucci\JWT\Builder + * @covers Lcobucci\JWT\Parser + * @covers Lcobucci\JWT\Token + * @covers Lcobucci\JWT\Signature + * @covers Lcobucci\JWT\Claim\Factory + * @covers Lcobucci\JWT\Claim\Basic + * @covers Lcobucci\JWT\Parsing\Encoder + * @covers Lcobucci\JWT\Parsing\Decoder + * @covers Lcobucci\JWT\Signer\Factory + */ + public function parserCanReadAToken(Token $generated) + { + $read = (new Parser())->parse((string) $generated); + + $this->assertEquals($generated, $read); + } + + /** + * @test + * + * @depends builderCanGenerateAToken + * + * @covers Lcobucci\JWT\Builder + * @covers Lcobucci\JWT\Parser + * @covers Lcobucci\JWT\Token + * @covers Lcobucci\JWT\Signature + * @covers Lcobucci\JWT\Parsing\Encoder + * @covers Lcobucci\JWT\Claim\Factory + * @covers Lcobucci\JWT\Claim\Basic + * @covers Lcobucci\JWT\Signer\Factory + * @covers Lcobucci\JWT\Signer\OpenSSL + * @covers Lcobucci\JWT\Signer\Ecdsa + * @covers Lcobucci\JWT\Signer\Ecdsa\Sha256 + */ + public function verifyShouldReturnFalseWhenKeyIsNotRight(Token $token) + { + $this->assertFalse($token->verify($this->otherPublicEcdsa())); + } + + /** + * @test + * + * @depends builderCanGenerateAToken + * + * @covers Lcobucci\JWT\Builder + * @covers Lcobucci\JWT\Parser + * @covers Lcobucci\JWT\Token + * @covers Lcobucci\JWT\Signature + * @covers Lcobucci\JWT\Parsing\Encoder + * @covers Lcobucci\JWT\Claim\Factory + * @covers Lcobucci\JWT\Claim\Basic + * @covers Lcobucci\JWT\Signer\Factory + * @covers Lcobucci\JWT\Signer\OpenSSL + * @covers Lcobucci\JWT\Signer\Ecdsa + * @covers Lcobucci\JWT\Signer\Ecdsa\Sha256 + */ + public function verifyShouldReturnTrueWhenKeyIsRight(Token $token) + { + $this->assertTrue($token->verify($this->publicEcdsa())); + } +} diff --git a/test/FunctionalTests/HmacTokenTest.php b/test/FunctionalTests/HmacTokenTest.php new file mode 100644 index 00000000..18047c09 --- /dev/null +++ b/test/FunctionalTests/HmacTokenTest.php @@ -0,0 +1,117 @@ + + * @since 2.1.0 + */ +class HmacTokenTest extends \PHPUnit_Framework_TestCase +{ + /** + * @test + * + * @covers Lcobucci\JWT\Builder + * @covers Lcobucci\JWT\Token + * @covers Lcobucci\JWT\Signature + * @covers Lcobucci\JWT\Claim\Factory + * @covers Lcobucci\JWT\Claim\Basic + * @covers Lcobucci\JWT\Parsing\Encoder + * @covers Lcobucci\JWT\Signer\BaseSigner + * @covers Lcobucci\JWT\Signer\Hmac + * @covers Lcobucci\JWT\Signer\Hmac\Sha256 + */ + public function builderCanGenerateAToken() + { + $user = ['name' => 'testing', 'email' => 'testing@abc.com']; + + $token = (new Builder())->setId(1) + ->setAudience('http://client.abc.com') + ->setIssuer('http://api.abc.com') + ->set('user', $user) + ->sign(new Sha256(), 'testing') + ->getToken(); + + $this->assertAttributeInstanceOf(Signature::class, 'signature', $token); + $this->assertEquals('http://client.abc.com', $token->getClaim('aud')); + $this->assertEquals('http://api.abc.com', $token->getClaim('iss')); + $this->assertEquals($user, $token->getClaim('user')); + + return $token; + } + + /** + * @test + * + * @depends builderCanGenerateAToken + * + * @covers Lcobucci\JWT\Builder + * @covers Lcobucci\JWT\Parser + * @covers Lcobucci\JWT\Token + * @covers Lcobucci\JWT\Signature + * @covers Lcobucci\JWT\Claim\Factory + * @covers Lcobucci\JWT\Claim\Basic + * @covers Lcobucci\JWT\Parsing\Encoder + * @covers Lcobucci\JWT\Parsing\Decoder + * @covers Lcobucci\JWT\Signer\Factory + */ + public function parserCanReadAToken(Token $generated) + { + $read = (new Parser())->parse((string) $generated); + + $this->assertEquals($generated, $read); + } + + /** + * @test + * + * @depends builderCanGenerateAToken + * + * @covers Lcobucci\JWT\Builder + * @covers Lcobucci\JWT\Parser + * @covers Lcobucci\JWT\Token + * @covers Lcobucci\JWT\Signature + * @covers Lcobucci\JWT\Parsing\Encoder + * @covers Lcobucci\JWT\Claim\Factory + * @covers Lcobucci\JWT\Claim\Basic + * @covers Lcobucci\JWT\Signer\Factory + * @covers Lcobucci\JWT\Signer\Hmac + * @covers Lcobucci\JWT\Signer\Hmac\Sha256 + */ + public function verifyShouldReturnFalseWhenKeyIsNotRight(Token $token) + { + $this->assertFalse($token->verify('testing1')); + } + + /** + * @test + * + * @depends builderCanGenerateAToken + * + * @covers Lcobucci\JWT\Builder + * @covers Lcobucci\JWT\Parser + * @covers Lcobucci\JWT\Token + * @covers Lcobucci\JWT\Signature + * @covers Lcobucci\JWT\Parsing\Encoder + * @covers Lcobucci\JWT\Claim\Factory + * @covers Lcobucci\JWT\Claim\Basic + * @covers Lcobucci\JWT\Signer\Factory + * @covers Lcobucci\JWT\Signer\Hmac + * @covers Lcobucci\JWT\Signer\Hmac\Sha256 + */ + public function verifyShouldReturnTrueWhenKeyIsRight(Token $token) + { + $this->assertTrue($token->verify('testing')); + } +} diff --git a/test/FunctionalTests/RsaTokenTest.php b/test/FunctionalTests/RsaTokenTest.php new file mode 100644 index 00000000..37b8d702 --- /dev/null +++ b/test/FunctionalTests/RsaTokenTest.php @@ -0,0 +1,123 @@ + + * @since 2.1.0 + */ +class RsaTokenTest extends \PHPUnit_Framework_TestCase +{ + use RsaKeys; + + /** + * @test + * + * @covers Lcobucci\JWT\Builder + * @covers Lcobucci\JWT\Token + * @covers Lcobucci\JWT\Signature + * @covers Lcobucci\JWT\Claim\Factory + * @covers Lcobucci\JWT\Claim\Basic + * @covers Lcobucci\JWT\Parsing\Encoder + * @covers Lcobucci\JWT\Signer\BaseSigner + * @covers Lcobucci\JWT\Signer\OpenSSL + * @covers Lcobucci\JWT\Signer\Rsa + * @covers Lcobucci\JWT\Signer\Rsa\Sha256 + */ + public function builderCanGenerateAToken() + { + $user = ['name' => 'testing', 'email' => 'testing@abc.com']; + + $token = (new Builder())->setId(1) + ->setAudience('http://client.abc.com') + ->setIssuer('http://api.abc.com') + ->set('user', $user) + ->sign(new Sha256(), $this->privateRsa()) + ->getToken(); + + $this->assertAttributeInstanceOf(Signature::class, 'signature', $token); + $this->assertEquals('http://client.abc.com', $token->getClaim('aud')); + $this->assertEquals('http://api.abc.com', $token->getClaim('iss')); + $this->assertEquals($user, $token->getClaim('user')); + + return $token; + } + + /** + * @test + * + * @depends builderCanGenerateAToken + * + * @covers Lcobucci\JWT\Builder + * @covers Lcobucci\JWT\Parser + * @covers Lcobucci\JWT\Token + * @covers Lcobucci\JWT\Signature + * @covers Lcobucci\JWT\Claim\Factory + * @covers Lcobucci\JWT\Claim\Basic + * @covers Lcobucci\JWT\Parsing\Encoder + * @covers Lcobucci\JWT\Parsing\Decoder + * @covers Lcobucci\JWT\Signer\Factory + */ + public function parserCanReadAToken(Token $generated) + { + $read = (new Parser())->parse((string) $generated); + + $this->assertEquals($generated, $read); + } + + /** + * @test + * + * @depends builderCanGenerateAToken + * + * @covers Lcobucci\JWT\Builder + * @covers Lcobucci\JWT\Parser + * @covers Lcobucci\JWT\Token + * @covers Lcobucci\JWT\Signature + * @covers Lcobucci\JWT\Parsing\Encoder + * @covers Lcobucci\JWT\Claim\Factory + * @covers Lcobucci\JWT\Claim\Basic + * @covers Lcobucci\JWT\Signer\Factory + * @covers Lcobucci\JWT\Signer\OpenSSL + * @covers Lcobucci\JWT\Signer\Rsa + * @covers Lcobucci\JWT\Signer\Rsa\Sha256 + */ + public function verifyShouldReturnFalseWhenKeyIsNotRight(Token $token) + { + $this->assertFalse($token->verify($this->encryptedPublicRsa())); + } + + /** + * @test + * + * @depends builderCanGenerateAToken + * + * @covers Lcobucci\JWT\Builder + * @covers Lcobucci\JWT\Parser + * @covers Lcobucci\JWT\Token + * @covers Lcobucci\JWT\Signature + * @covers Lcobucci\JWT\Parsing\Encoder + * @covers Lcobucci\JWT\Claim\Factory + * @covers Lcobucci\JWT\Claim\Basic + * @covers Lcobucci\JWT\Signer\Factory + * @covers Lcobucci\JWT\Signer\OpenSSL + * @covers Lcobucci\JWT\Signer\Rsa + * @covers Lcobucci\JWT\Signer\Rsa\Sha256 + */ + public function verifyShouldReturnTrueWhenKeyIsRight(Token $token) + { + $this->assertTrue($token->verify($this->publicRsa())); + } +} diff --git a/test/FunctionalTests/UnsignedTokenTest.php b/test/FunctionalTests/UnsignedTokenTest.php new file mode 100644 index 00000000..e3bda3a3 --- /dev/null +++ b/test/FunctionalTests/UnsignedTokenTest.php @@ -0,0 +1,138 @@ + + * @since 2.1.0 + */ +class UnsignedTokenTest extends \PHPUnit_Framework_TestCase +{ + const CURRENT_TIME = 100000; + + /** + * @test + * + * @covers Lcobucci\JWT\Builder + * @covers Lcobucci\JWT\Token + * @covers Lcobucci\JWT\Claim\Factory + * @covers Lcobucci\JWT\Claim\Basic + */ + public function builderCanGenerateAToken() + { + $user = ['name' => 'testing', 'email' => 'testing@abc.com']; + + $token = (new Builder())->setId(1) + ->setAudience('http://client.abc.com') + ->setIssuer('http://api.abc.com') + ->setExpiration(self::CURRENT_TIME + 3000) + ->set('user', $user) + ->getToken(); + + $this->assertAttributeEquals(null, 'signature', $token); + $this->assertEquals('http://client.abc.com', $token->getClaim('aud')); + $this->assertEquals('http://api.abc.com', $token->getClaim('iss')); + $this->assertEquals(self::CURRENT_TIME + 3000, $token->getClaim('exp')); + $this->assertEquals($user, $token->getClaim('user')); + + return $token; + } + + /** + * @test + * + * @depends builderCanGenerateAToken + * + * @covers Lcobucci\JWT\Builder + * @covers Lcobucci\JWT\Parser + * @covers Lcobucci\JWT\Token + * @covers Lcobucci\JWT\Claim\Factory + * @covers Lcobucci\JWT\Claim\Basic + * @covers Lcobucci\JWT\Parsing\Encoder + * @covers Lcobucci\JWT\Parsing\Decoder + * @covers Lcobucci\JWT\Signer\Factory + */ + public function parserCanReadAToken(Token $generated) + { + $read = (new Parser())->parse((string) $generated); + + $this->assertEquals($generated, $read); + } + + /** + * @test + * + * @depends builderCanGenerateAToken + * + * @covers Lcobucci\JWT\Builder + * @covers Lcobucci\JWT\Parser + * @covers Lcobucci\JWT\Token + * @covers Lcobucci\JWT\ValidationData + * @covers Lcobucci\JWT\Claim\Factory + * @covers Lcobucci\JWT\Claim\Basic + * @covers Lcobucci\JWT\Claim\EqualsTo + * @covers Lcobucci\JWT\Claim\GreaterOrEqualsTo + * @covers Lcobucci\JWT\Parsing\Encoder + * @covers Lcobucci\JWT\Parsing\Decoder + * @covers Lcobucci\JWT\Signer\Factory + */ + public function tokenValidationShouldReturnWhenEverythingIsFile(Token $generated) + { + $data = new ValidationData(self::CURRENT_TIME - 10); + $data->setAudience('http://client.abc.com'); + $data->setIssuer('http://api.abc.com'); + + $this->assertTrue($generated->validate($data)); + } + + /** + * @test + * + * @dataProvider invalidValidationData + * + * @depends builderCanGenerateAToken + * + * @covers Lcobucci\JWT\Builder + * @covers Lcobucci\JWT\Parser + * @covers Lcobucci\JWT\Token + * @covers Lcobucci\JWT\ValidationData + * @covers Lcobucci\JWT\Claim\Factory + * @covers Lcobucci\JWT\Claim\Basic + * @covers Lcobucci\JWT\Claim\EqualsTo + * @covers Lcobucci\JWT\Claim\GreaterOrEqualsTo + * @covers Lcobucci\JWT\Parsing\Encoder + * @covers Lcobucci\JWT\Parsing\Decoder + * @covers Lcobucci\JWT\Signer\Factory + */ + public function tokenValidationShouldReturnFalseWhenExpectedDataDontMatch(ValidationData $data, Token $generated) + { + $this->assertFalse($generated->validate($data)); + } + + public function invalidValidationData() + { + $expired = new ValidationData(self::CURRENT_TIME + 3020); + $expired->setAudience('http://client.abc.com'); + $expired->setIssuer('http://api.abc.com'); + + $invalidAudience = new ValidationData(self::CURRENT_TIME - 10); + $invalidAudience->setAudience('http://cclient.abc.com'); + $invalidAudience->setIssuer('http://api.abc.com'); + + $invalidIssuer = new ValidationData(self::CURRENT_TIME - 10); + $invalidIssuer->setAudience('http://client.abc.com'); + $invalidIssuer->setIssuer('http://aapi.abc.com'); + + return [[$expired], [$invalidAudience], [$invalidIssuer]]; + } +} diff --git a/test/ParserTest.php b/test/ParserTest.php index f3538664..90c04a1b 100644 --- a/test/ParserTest.php +++ b/test/ParserTest.php @@ -16,8 +16,6 @@ /** * @author Luís Otávio Cobucci Oblonczyk * @since 0.1.0 - * - * @coversDefaultClass Lcobucci\JWT\Parser */ class ParserTest extends \PHPUnit_Framework_TestCase { @@ -77,7 +75,8 @@ private function createParser() /** * @test - * @covers ::__construct + * + * @covers Lcobucci\JWT\Parser::__construct */ public function constructMustConfigureTheAttributes() { @@ -91,9 +90,11 @@ public function constructMustConfigureTheAttributes() /** * @test - * @covers ::__construct - * @covers ::parse - * @covers ::splitJwt + * + * @uses Lcobucci\JWT\Parser::__construct + * + * @covers Lcobucci\JWT\Parser::parse + * @covers Lcobucci\JWT\Parser::splitJwt * * @expectedException InvalidArgumentException */ @@ -105,9 +106,11 @@ public function parseMustRaiseExceptionWhenJWSIsNotAString() /** * @test - * @covers ::__construct - * @covers ::parse - * @covers ::splitJwt + * + * @uses Lcobucci\JWT\Parser::__construct + * + * @covers Lcobucci\JWT\Parser::parse + * @covers Lcobucci\JWT\Parser::splitJwt * * @expectedException InvalidArgumentException */ @@ -119,11 +122,13 @@ public function parseMustRaiseExceptionWhenJWSDontHaveThreeParts() /** * @test - * @covers ::__construct - * @covers ::parse - * @covers ::splitJwt - * @covers ::createToken - * @covers ::parseHeader + * + * @uses Lcobucci\JWT\Parser::__construct + * + * @covers Lcobucci\JWT\Parser::parse + * @covers Lcobucci\JWT\Parser::splitJwt + * @covers Lcobucci\JWT\Parser::createToken + * @covers Lcobucci\JWT\Parser::parseHeader * * @expectedException RuntimeException */ @@ -139,11 +144,13 @@ public function parseMustRaiseExceptionWhenHeaderCannotBeDecoded() /** * @test - * @covers ::__construct - * @covers ::parse - * @covers ::splitJwt - * @covers ::createToken - * @covers ::parseHeader + * + * @uses Lcobucci\JWT\Parser::__construct + * + * @covers Lcobucci\JWT\Parser::parse + * @covers Lcobucci\JWT\Parser::splitJwt + * @covers Lcobucci\JWT\Parser::createToken + * @covers Lcobucci\JWT\Parser::parseHeader * * @expectedException InvalidArgumentException */ @@ -159,15 +166,18 @@ public function parseMustRaiseExceptionWhenHeaderIsFromAnEncryptedToken() /** * @test - * @covers ::__construct - * @covers ::parse - * @covers ::splitJwt - * @covers ::createToken - * @covers ::parseHeader - * @covers ::parseClaims - * @covers ::parseSignature - * @covers Lcobucci\JWT\Token::__construct - * @covers Lcobucci\JWT\Token::setEncoder + * + * @uses Lcobucci\JWT\Parser::__construct + * @uses Lcobucci\JWT\Token::__construct + * @uses Lcobucci\JWT\Token::setEncoder + * + * @covers Lcobucci\JWT\Parser::parse + * @covers Lcobucci\JWT\Parser::splitJwt + * @covers Lcobucci\JWT\Parser::createToken + * @covers Lcobucci\JWT\Parser::parseHeader + * @covers Lcobucci\JWT\Parser::parseClaims + * @covers Lcobucci\JWT\Parser::parseSignature + * */ public function parseMustReturnANonSignedTokenWhenSignatureIsNotInformed() { @@ -190,15 +200,17 @@ public function parseMustReturnANonSignedTokenWhenSignatureIsNotInformed() /** * @test - * @covers ::__construct - * @covers ::parse - * @covers ::splitJwt - * @covers ::createToken - * @covers ::parseHeader - * @covers ::parseClaims - * @covers ::parseSignature - * @covers Lcobucci\JWT\Token::__construct - * @covers Lcobucci\JWT\Token::setEncoder + * + * @uses Lcobucci\JWT\Parser::__construct + * @uses Lcobucci\JWT\Token::__construct + * @uses Lcobucci\JWT\Token::setEncoder + * + * @covers Lcobucci\JWT\Parser::parse + * @covers Lcobucci\JWT\Parser::splitJwt + * @covers Lcobucci\JWT\Parser::createToken + * @covers Lcobucci\JWT\Parser::parseHeader + * @covers Lcobucci\JWT\Parser::parseClaims + * @covers Lcobucci\JWT\Parser::parseSignature */ public function parseShouldReplicateClaimValueOnHeaderWhenNeeded() { @@ -226,16 +238,18 @@ public function parseShouldReplicateClaimValueOnHeaderWhenNeeded() /** * @test - * @covers ::__construct - * @covers ::parse - * @covers ::splitJwt - * @covers ::createToken - * @covers ::parseHeader - * @covers ::parseClaims - * @covers ::parseSignature - * @covers Lcobucci\JWT\Token::__construct - * @covers Lcobucci\JWT\Token::setEncoder - * @covers Lcobucci\JWT\Signature::__construct + * + * @uses Lcobucci\JWT\Parser::__construct + * @uses Lcobucci\JWT\Token::__construct + * @uses Lcobucci\JWT\Token::setEncoder + * @uses Lcobucci\JWT\Signature::__construct + * + * @covers Lcobucci\JWT\Parser::parse + * @covers Lcobucci\JWT\Parser::splitJwt + * @covers Lcobucci\JWT\Parser::createToken + * @covers Lcobucci\JWT\Parser::parseHeader + * @covers Lcobucci\JWT\Parser::parseClaims + * @covers Lcobucci\JWT\Parser::parseSignature */ public function parseMustReturnASignedTokenWhenSignatureIsInformed() { diff --git a/test/Parsing/DecoderTest.php b/test/Parsing/DecoderTest.php index 7c163ff7..e737b24c 100644 --- a/test/Parsing/DecoderTest.php +++ b/test/Parsing/DecoderTest.php @@ -10,14 +10,13 @@ /** * @author Luís Otávio Cobucci Oblonczyk * @since 0.1.0 - * - * @coversDefaultClass Lcobucci\JWT\Parsing\Decoder */ class DecoderTest extends \PHPUnit_Framework_TestCase { /** * @test - * @covers ::jsonDecode + * + * @covers Lcobucci\JWT\Parsing\Decoder::jsonDecode */ public function jsonDecodeMustReturnTheDecodedData() { @@ -28,7 +27,8 @@ public function jsonDecodeMustReturnTheDecodedData() /** * @test - * @covers ::jsonDecode + * + * @covers Lcobucci\JWT\Parsing\Decoder::jsonDecode * * @expectedException \RuntimeException */ @@ -41,7 +41,8 @@ public function jsonDecodeMustRaiseExceptionWhenResultIsNotAnArray() /** * @test - * @covers ::jsonDecode + * + * @covers Lcobucci\JWT\Parsing\Decoder::jsonDecode * * @expectedException \RuntimeException */ @@ -53,7 +54,8 @@ public function jsonDecodeMustRaiseExceptionWhenAnErrorHasOccured() /** * @test - * @covers ::base64UrlDecode + * + * @covers Lcobucci\JWT\Parsing\Decoder::base64UrlDecode */ public function base64UrlDecodeMustReturnTheRightData() { diff --git a/test/Parsing/EncoderTest.php b/test/Parsing/EncoderTest.php index 463bdedd..6628b248 100644 --- a/test/Parsing/EncoderTest.php +++ b/test/Parsing/EncoderTest.php @@ -10,14 +10,13 @@ /** * @author Luís Otávio Cobucci Oblonczyk * @since 0.1.0 - * - * @coversDefaultClass Lcobucci\JWT\Parsing\Encoder */ class EncoderTest extends \PHPUnit_Framework_TestCase { /** * @test - * @covers ::jsonEncode + * + * @covers Lcobucci\JWT\Parsing\Encoder::jsonEncode */ public function jsonEncodeMustReturnAJSONString() { @@ -28,7 +27,8 @@ public function jsonEncodeMustReturnAJSONString() /** * @test - * @covers ::jsonEncode + * + * @covers Lcobucci\JWT\Parsing\Encoder::jsonEncode * * @expectedException \RuntimeException */ @@ -40,7 +40,8 @@ public function jsonEncodeMustRaiseExceptionWhenAnErrorHasOccured() /** * @test - * @covers ::base64UrlEncode + * + * @covers Lcobucci\JWT\Parsing\Encoder::base64UrlEncode */ public function base64UrlEncodeMustReturnAnUrlSafeBase64() { diff --git a/test/RsaKeys.php b/test/RsaKeys.php new file mode 100644 index 00000000..7c0d75cf --- /dev/null +++ b/test/RsaKeys.php @@ -0,0 +1,151 @@ + + * @since 2.1.0 + */ +trait RsaKeys +{ + /** + * @return string + */ + protected function encryptedPrivateRsaContent() + { + return '-----BEGIN RSA PRIVATE KEY-----' . PHP_EOL + . 'Proc-Type: 4,ENCRYPTED' . PHP_EOL + . 'DEK-Info: AES-128-CBC,0D71668CE71033CB9150ED82FC87F4A1' . PHP_EOL + . '' . PHP_EOL + . 'uLzPNDdlHnZ77tAGMHyPYERDMBcdV4SsQJYcSjiHhR2o0dLGTdgOpQrXTHPX4GJF' . PHP_EOL + . 'LlEWLhAAV9wx2mM/2kHDWB4uZwThtT9/v+RFoW1WbVO/d3lhI9fg4/73/DWAH/7/' . PHP_EOL + . 'afMRc7ZOVoAmVCESotPx4khCHoE97RdY/JtkLTzc3+peqmL53AbYXrg9rTN1B+ZV' . PHP_EOL + . 'U3w4ciQS8Uki87zDYIBjYtaOCyMUTvug25CvdssvUMBoc/Jc0xps6/vAyXrnzlGT' . PHP_EOL + . 'pZD0Tst8idswfDi613BhAaxJspeY0AErWA59qJ3eGzbiQq5RDWcbJe/Tz5r/6+NN' . PHP_EOL + . 'DkvNQ7DaEZ6LpeWX0MUq6/QWfrM8yE95XhjyC1d3LYn32lXHUygbgTFWIgLDoOE6' . PHP_EOL + . 'nBhu34SWtbLAnqYGewaJFxhlYVS9rb/uvYQg70r5X9Sx6alCQPiPyIv39IItezn2' . PHP_EOL + . 'HF2GRfE91MPZUeDhdqdvvOlSZVM5KnYc1fhamGAwM48gdDDXe8Czu/JEGoANNvC3' . PHP_EOL + . 'l/Z1p5RtGF4hrel9WpeX9zQq3pvtfVcVIiWuRUwCOSQytXlieRK37sMuYeggvmjV' . PHP_EOL + . 'VvaCods3mS/panWg9T/D/deIXjhzNJLvyiJg8+3sY5H4yNe0XpbaAc/ySwt9Rcxy' . PHP_EOL + . 'FzFQ+5pghLSZgR1uV3AhdcnzXBU2GkYhdGKt2tUsH0UeVQ2BXxTlBFsCOh2dWqcj' . PHP_EOL + . 'y3suIG65bukDAAWidQ4q3S6ZIMpXBhhCj7nwB5jQ7wSlU3U9So0ndr7zxdUILiMm' . PHP_EOL + . 'chHi3q5apVZnMGcwv2B33rt4nD7HgGEmRKkCelrSrBATY1ut+T4rCDzKDqDs3jpv' . PHP_EOL + . 'hYIWrlNPTkJyQz3eWly6Db+FJEfdYGadYJusc7/nOxCh/QmUu8Sh3NhKT6TH0bS7' . PHP_EOL + . '1AAqd8H+2hJ9I32Dhd2qwAF7PkNe2LGi+P8tbAtepKGim5w65wnsPePMnrfxumsG' . PHP_EOL + . 'PeDnMrqeCKy+fME7a/MS5kmEBpmD4BMhVC6/OhFVz8gBty1f8yIEZggHNQN2QK7m' . PHP_EOL + . 'NIrG+PwqW2w8HoxOlAi2Ix4LTPifrdfsH02U7aM1pgo1rZzD4AOzqvzCaK43H2VB' . PHP_EOL + . 'BHLeTBGoLEUxXA9C+iGbeQlKXkMC00QKkjK5+nvkvnvePFfsrTQIpuyGufD/MoPb' . PHP_EOL + . '6fpwsyHZDxhxMN1PJk1b1lPq2Ui4hXpVNOYd4Q6OQz7bwxTMRX9XQromUlKMMgAT' . PHP_EOL + . 'edX8v2NdM7Ssy1IwHuGVbDEpZdjoeaWZ1iNRV17i/EaJAqwYDQLfsuHBlzZL1ov1' . PHP_EOL + . 'xkKVJdL8Y3q80oRAzTQDVdzL/rI44LLAfv609YByCnw29feYJY2W6gV0O7ZSw413' . PHP_EOL + . 'XUkc5CaEbR1LuG8NtnOOPJV4Tb/hNsIDtvVm7Hl5npBKBe4iVgQ2LNuC2eT69d/z' . PHP_EOL + . 'uvzgjISlumPiO5ivuYe0QtLPuJSc+/Bl8bPL8gcNQEtqkzj7IftHPPZNs+bJC2uY' . PHP_EOL + . 'bPjq5KoDNAMF6VHuKHwu48MBYpnXDIg3ZenmJwGRULRBhK6324hDS6NJ7ULTBU2M' . PHP_EOL + . 'TZCHmg89ySLBfCAspVeo63o/R7bs9a7BP9x2h5uwCBogSvkEwhhPKnboVN45bp9c' . PHP_EOL + . '-----END RSA PRIVATE KEY-----'; + } + + /** + * @return string + */ + protected function privateRsaContent() + { + return '-----BEGIN PRIVATE KEY-----' . PHP_EOL + . 'MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDTvwE87MtgREYL' . PHP_EOL + . 'TL4aHhQo3ZzogmxxvMUsKnPzyxRs1YrXOSOpwN0npsXarBKKVIUMNLfFODp/vnQn' . PHP_EOL + . '2Zp06N8XG59WAOKwvC4MfxLDQkA+JXggzHlkbVoTN+dUkdYIFqSKuAPGwiWToRK2' . PHP_EOL + . 'SxEhij3rE2FON8jQZvDxZkiP9a4vxJO3OTPQwKredXFiObsXD/c3RtLFhKctjCyH' . PHP_EOL + . 'OIrP0bQEsee/m7JNtG4ry6BPusN6wb+vJo5ieBYPa3c19akNq6q/nYWhplhkkJSu' . PHP_EOL + . 'aOrL5xXEFzI5TvcvnXR568GVcxK8YLfFkdxpsXGt5rAbeh0h/U5kILEAqv8P9PGT' . PHP_EOL + . 'ZpicKbrnAgMBAAECggEAd3yTQEQHR91/ASVfKPHMQns77eCbPVtekFusbugsMHYY' . PHP_EOL + . 'EPdHbqVMpvFvOMRc+f5Tzd15ziq6qBdbCJm8lThLm4iU0z1QrpaiDZ8vgUvDYM5Y' . PHP_EOL + . 'CXoZDli+uZWUTp60/n94fmb0ipZIChScsI2PrzOJWTvobvD/uso8MJydWc8zafQm' . PHP_EOL + . 'uqYzygOfjFZvU4lSfgzpefhpquy0JUy5TiKRmGUnwLb3TtcsVavjsn4QmNwLYgOF' . PHP_EOL + . '2OE+R12ex3pAKTiRE6FcnE1xFIo1GKhBa2Otgw3MDO6Gg+kn8Q4alKz6C6RRlgaH' . PHP_EOL + . 'R7sYzEfJhsk/GGFTYOzXKQz2lSaStKt9wKCor04RcQKBgQDzPOu5jCTfayUo7xY2' . PHP_EOL + . 'jHtiogHyKLLObt9l3qbwgXnaD6rnxYNvCrA0OMvT+iZXsFZKJkYzJr8ZOxOpPROk' . PHP_EOL + . '10WdOaefiwUyL5dypueSwlIDwVm+hI4Bs82MajHtzOozh+73wA+aw5rPs84Uix9w' . PHP_EOL + . 'VbbwaVR6qP/BV09yJYS5kQ7fmwKBgQDe2xjywX2d2MC+qzRr+LfU+1+gq0jjhBCX' . PHP_EOL + . 'WHqRN6IECB0xTnXUf9WL/VCoI1/55BhdbbEja+4btYgcXSPmlXBIRKQ4VtFfVmYB' . PHP_EOL + . 'kPXeD8oZ7LyuNdCsbKNe+x1IHXDe6Wfs3L9ulCfXxeIE84wy3fd66mQahyXV9iD9' . PHP_EOL + . 'CkuifMqUpQKBgQCiydHlY1LGJ/o9tA2Ewm5Na6mrvOs2V2Ox1NqbObwoYbX62eiF' . PHP_EOL + . '53xX5u8bVl5U75JAm+79it/4bd5RtKux9dUETbLOhwcaOFm+hM+VG/IxyzRZ2nMD' . PHP_EOL + . '1qcpY2U5BpxzknUvYF3RMTop6edxPk7zKpp9ubCtSu+oINvtxAhY/SkcIwKBgGP1' . PHP_EOL + . 'upcImyO2GZ5shLL5eNubdSVILwV+M0LveOqyHYXZbd6z5r5OKKcGFKuWUnJwEU22' . PHP_EOL + . '6gGNY9wh7M9sJ7JBzX9c6pwqtPcidda2AtJ8GpbOTUOG9/afNBhiYpv6OKqD3w2r' . PHP_EOL + . 'ZmJfKg/qvpqh83zNezgy8nvDqwDxyZI2j/5uIx/RAoGBAMWRmxtv6H2cKhibI/aI' . PHP_EOL + . 'MTJM4QRjyPNxQqvAQsv+oHUbid06VK3JE+9iQyithjcfNOwnCaoO7I7qAj9QEfJS' . PHP_EOL + . 'MZQc/W/4DHJebo2kd11yoXPVTXXOuEwLSKCejBXABBY0MPNuPUmiXeU0O3Tyi37J' . PHP_EOL + . 'TUKzrgcd7NvlA41Y4xKcOqEA' . PHP_EOL + . '-----END PRIVATE KEY-----'; + } + + /** + * @return string + */ + protected function publicRsaContent() + { + return '-----BEGIN PUBLIC KEY-----' . PHP_EOL + . 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA078BPOzLYERGC0y+Gh4U' . PHP_EOL + . 'KN2c6IJscbzFLCpz88sUbNWK1zkjqcDdJ6bF2qwSilSFDDS3xTg6f750J9madOjf' . PHP_EOL + . 'FxufVgDisLwuDH8Sw0JAPiV4IMx5ZG1aEzfnVJHWCBakirgDxsIlk6EStksRIYo9' . PHP_EOL + . '6xNhTjfI0Gbw8WZIj/WuL8STtzkz0MCq3nVxYjm7Fw/3N0bSxYSnLYwshziKz9G0' . PHP_EOL + . 'BLHnv5uyTbRuK8ugT7rDesG/ryaOYngWD2t3NfWpDauqv52FoaZYZJCUrmjqy+cV' . PHP_EOL + . 'xBcyOU73L510eevBlXMSvGC3xZHcabFxreawG3odIf1OZCCxAKr/D/Txk2aYnCm6' . PHP_EOL + . '5wIDAQAB' . PHP_EOL + . '-----END PUBLIC KEY-----'; + } + + /** + * @return string + */ + protected function encryptedPublicRsaContent() + { + return '-----BEGIN PUBLIC KEY-----' . PHP_EOL + . 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAwLpbUP8a9yflt5LKUUS3' . PHP_EOL + . 'NPuRM7yEouPWg0VKeY5AURu4i8bqQ20K5jwfRJ+w05FvlywG4EuxpnpTFTVS2/do' . PHP_EOL + . 'q3xufzTf/C3KIDOAHEifkdx4140btKxxm4mD9Eu2CQ32adZyScha50KUFlfnAAic' . PHP_EOL + . 'Hb8wYxjFyWo3PAbGYmCQCn2z97Ab0Ar6NR1e+V9f8EL9Orr2f04puKJfQTZdWVDF' . PHP_EOL + . 'UJR4w7QZ/CPY0LEsiFLW3QQCNraka1mtrLJwPqreBtDEkj8IoISNkrguu/97RQZz' . PHP_EOL + . 'miJgBQkVjr6OfqG5WIFr0MzbRZc1/aK9g8ft88nhhQm0E3GqkCxBKTwgA03HtK07' . PHP_EOL + . 'qQIDAQAB' . PHP_EOL + . '-----END PUBLIC KEY-----'; + } + + /** + * @return string + */ + protected function privateRsa() + { + return openssl_pkey_get_private($this->privateRsaContent()); + } + + /** + * @return string + */ + protected function encryptedPrivateRsa() + { + return openssl_pkey_get_private($this->encryptedPrivateRsaContent(), 'testing'); + } + + /** + * @return string + */ + protected function publicRsa() + { + return openssl_pkey_get_public($this->publicRsaContent()); + } + + /** + * @return string + */ + protected function encryptedPublicRsa() + { + return openssl_pkey_get_public($this->encryptedPublicRsaContent()); + } +} diff --git a/test/SignatureTest.php b/test/SignatureTest.php index 461dcbce..54b36be7 100644 --- a/test/SignatureTest.php +++ b/test/SignatureTest.php @@ -10,8 +10,6 @@ /** * @author Luís Otávio Cobucci Oblonczyk * @since 0.1.0 - * - * @coversDefaultClass Lcobucci\JWT\Signature */ class SignatureTest extends \PHPUnit_Framework_TestCase { @@ -30,7 +28,8 @@ protected function setUp() /** * @test - * @covers ::__construct + * + * @covers Lcobucci\JWT\Signature::__construct */ public function constructorMustConfigureAttributes() { @@ -42,8 +41,10 @@ public function constructorMustConfigureAttributes() /** * @test - * @covers ::__construct - * @covers ::__toString + * + * @uses Lcobucci\JWT\Signature::__construct + * + * @covers Lcobucci\JWT\Signature::__toString */ public function toStringMustReturnTheHash() { @@ -54,35 +55,20 @@ public function toStringMustReturnTheHash() /** * @test - * @covers ::__construct - * @covers ::__toString - * @covers ::verify + * + * @uses Lcobucci\JWT\Signature::__construct + * @uses Lcobucci\JWT\Signature::__toString + * + * @covers Lcobucci\JWT\Signature::verify */ - public function verifyMustReturnTrueWhenHashMatches() + public function verifyMustReturnWhatSignerSays() { $this->signer->expects($this->any()) - ->method('createHash') - ->willReturn('test'); + ->method('verify') + ->willReturn(true); $signature = new Signature($this->signer, 'test'); $this->assertTrue($signature->verify('one', 'key')); } - - /** - * @test - * @covers ::__construct - * @covers ::__toString - * @covers ::verify - */ - public function verifyMustReturnFalseWhenHashDoesNotMatch() - { - $this->signer->expects($this->any()) - ->method('createHash') - ->willReturn('testing'); - - $signature = new Signature($this->signer, 'test'); - - $this->assertFalse($signature->verify('one', 'key')); - } } diff --git a/test/Signer/BaseSignerTest.php b/test/Signer/BaseSignerTest.php index e890999a..58b11b59 100644 --- a/test/Signer/BaseSignerTest.php +++ b/test/Signer/BaseSignerTest.php @@ -12,8 +12,6 @@ /** * @author Luís Otávio Cobucci Oblonczyk * @since 0.1.0 - * - * @coversDefaultClass Lcobucci\JWT\Signer\BaseSigner */ class BaseSignerTest extends \PHPUnit_Framework_TestCase { @@ -40,7 +38,8 @@ protected function setUp() /** * @test - * @covers ::modifyHeader + * + * @covers Lcobucci\JWT\Signer\BaseSigner::modifyHeader */ public function modifyHeaderShouldChangeAlgorithmAndType() { @@ -54,8 +53,10 @@ public function modifyHeaderShouldChangeAlgorithmAndType() /** * @test - * @covers ::sign - * @covers Lcobucci\JWT\Signature::__construct + * + * @uses Lcobucci\JWT\Signature::__construct + * + * @covers Lcobucci\JWT\Signer\BaseSigner::sign */ public function signMustReturnANewSignature() { diff --git a/test/Signer/Ecdsa/Sha256Test.php b/test/Signer/Ecdsa/Sha256Test.php new file mode 100644 index 00000000..cc1264a1 --- /dev/null +++ b/test/Signer/Ecdsa/Sha256Test.php @@ -0,0 +1,39 @@ + + * @since 2.1.0 + */ +class Sha256Test extends \PHPUnit_Framework_TestCase +{ + /** + * @test + * + * @covers Lcobucci\JWT\Signer\Ecdsa\Sha256::getAlgorithmId + */ + public function getAlgorithmIdMustBeCorrect() + { + $signer = new Sha256(); + + $this->assertEquals('ES256', $signer->getAlgorithmId()); + } + + /** + * @test + * + * @covers Lcobucci\JWT\Signer\Ecdsa\Sha256::getAlgorithm + */ + public function getAlgorithmMustBeCorrect() + { + $signer = new Sha256(); + + $this->assertEquals(OPENSSL_ALGO_SHA256, $signer->getAlgorithm()); + } +} diff --git a/test/Signer/Ecdsa/Sha384Test.php b/test/Signer/Ecdsa/Sha384Test.php new file mode 100644 index 00000000..fb3b5f24 --- /dev/null +++ b/test/Signer/Ecdsa/Sha384Test.php @@ -0,0 +1,39 @@ + + * @since 2.1.0 + */ +class Sha384Test extends \PHPUnit_Framework_TestCase +{ + /** + * @test + * + * @covers Lcobucci\JWT\Signer\Ecdsa\Sha384::getAlgorithmId + */ + public function getAlgorithmIdMustBeCorrect() + { + $signer = new Sha384(); + + $this->assertEquals('ES384', $signer->getAlgorithmId()); + } + + /** + * @test + * + * @covers Lcobucci\JWT\Signer\Ecdsa\Sha384::getAlgorithm + */ + public function getAlgorithmMustBeCorrect() + { + $signer = new Sha384(); + + $this->assertEquals(OPENSSL_ALGO_SHA384, $signer->getAlgorithm()); + } +} diff --git a/test/Signer/Ecdsa/Sha512Test.php b/test/Signer/Ecdsa/Sha512Test.php new file mode 100644 index 00000000..14f8bd9e --- /dev/null +++ b/test/Signer/Ecdsa/Sha512Test.php @@ -0,0 +1,39 @@ + + * @since 2.1.0 + */ +class Sha512Test extends \PHPUnit_Framework_TestCase +{ + /** + * @test + * + * @covers Lcobucci\JWT\Signer\Ecdsa\Sha512::getAlgorithmId + */ + public function getAlgorithmIdMustBeCorrect() + { + $signer = new Sha512(); + + $this->assertEquals('ES512', $signer->getAlgorithmId()); + } + + /** + * @test + * + * @covers Lcobucci\JWT\Signer\Ecdsa\Sha512::getAlgorithm + */ + public function getAlgorithmMustBeCorrect() + { + $signer = new Sha512(); + + $this->assertEquals(OPENSSL_ALGO_SHA512, $signer->getAlgorithm()); + } +} diff --git a/test/Signer/EcdsaTest.php b/test/Signer/EcdsaTest.php new file mode 100644 index 00000000..3124aa38 --- /dev/null +++ b/test/Signer/EcdsaTest.php @@ -0,0 +1,27 @@ + + * @since 2.1.0 + */ +class EcdsaTest extends \PHPUnit_Framework_TestCase +{ + /** + * @test + * + * @covers Lcobucci\JWT\Signer\Ecdsa::getType + */ + public function getTypeMustBeCorrect() + { + $signer = $this->getMockForAbstractClass(Ecdsa::class); + + $this->assertEquals(OPENSSL_KEYTYPE_EC, $signer->getType()); + } +} diff --git a/test/Signer/FactoryTest.php b/test/Signer/FactoryTest.php index 1d75fd00..8ca977ea 100644 --- a/test/Signer/FactoryTest.php +++ b/test/Signer/FactoryTest.php @@ -7,61 +7,190 @@ namespace Lcobucci\JWT\Signer; -use Lcobucci\JWT\Signer\Hmac\Sha256; -use Lcobucci\JWT\Signer\Hmac\Sha384; -use Lcobucci\JWT\Signer\Hmac\Sha512; +use Lcobucci\JWT\Signer\Ecdsa\Sha256 as EcdsaSha256; +use Lcobucci\JWT\Signer\Ecdsa\Sha384 as EcdsaSha384; +use Lcobucci\JWT\Signer\Ecdsa\Sha512 as EcdsaSha512; +use Lcobucci\JWT\Signer\Hmac\Sha256 as HmacSha256; +use Lcobucci\JWT\Signer\Hmac\Sha384 as HmacSha384; +use Lcobucci\JWT\Signer\Hmac\Sha512 as HmacSha512; +use Lcobucci\JWT\Signer\Rsa\Sha256 as RsaSha256; +use Lcobucci\JWT\Signer\Rsa\Sha384 as RsaSha384; +use Lcobucci\JWT\Signer\Rsa\Sha512 as RsaSha512; /** * @author Luís Otávio Cobucci Oblonczyk * @since 0.1.0 - * - * @coversDefaultClass Lcobucci\JWT\Signer\Factory */ class FactoryTest extends \PHPUnit_Framework_TestCase { /** * @test - * @covers ::__construct - * @covers ::create - * @covers ::createHmacSha256 + * + * @covers Lcobucci\JWT\Signer\Factory::__construct + */ + public function constructMustConfigureTheCallbacks() + { + $callback = function () { + }; + $factory = new Factory(['test' => $callback]); + + $expected = [ + 'HS256' => [$factory, 'createHmacSha256'], + 'HS384' => [$factory, 'createHmacSha384'], + 'HS512' => [$factory, 'createHmacSha512'], + 'RS256' => [$factory, 'createRsaSha256'], + 'RS384' => [$factory, 'createRsaSha384'], + 'RS512' => [$factory, 'createRsaSha512'], + 'ES256' => [$factory, 'createEcdsaSha256'], + 'ES384' => [$factory, 'createEcdsaSha384'], + 'ES512' => [$factory, 'createEcdsaSha512'], + 'test' => $callback + ]; + + $this->assertAttributeEquals($expected, 'callbacks', $factory); + } + + /** + * @test + * + * @uses Lcobucci\JWT\Signer\Factory::__construct + * + * @covers Lcobucci\JWT\Signer\Factory::create + * @covers Lcobucci\JWT\Signer\Factory::createHmacSha256 + */ + public function createMustBeAbleReturnAHmacSha256Signer() + { + $factory = new Factory(); + + $this->assertInstanceOf(HmacSha256::class, $factory->create('HS256')); + } + + /** + * @test + * + * @uses Lcobucci\JWT\Signer\Factory::__construct + * + * @covers Lcobucci\JWT\Signer\Factory::create + * @covers Lcobucci\JWT\Signer\Factory::createHmacSha384 + */ + public function createMustBeAbleReturnAHmacSha384Signer() + { + $factory = new Factory(); + + $this->assertInstanceOf(HmacSha384::class, $factory->create('HS384')); + } + + /** + * @test + * + * @uses Lcobucci\JWT\Signer\Factory::__construct + * + * @covers Lcobucci\JWT\Signer\Factory::create + * @covers Lcobucci\JWT\Signer\Factory::createHmacSha512 + */ + public function createMustBeAbleReturnAHmacSha512Signer() + { + $factory = new Factory(); + + $this->assertInstanceOf(HmacSha512::class, $factory->create('HS512')); + } + + /** + * @test + * + * @uses Lcobucci\JWT\Signer\Factory::__construct + * + * @covers Lcobucci\JWT\Signer\Factory::create + * @covers Lcobucci\JWT\Signer\Factory::createRsaSha256 + */ + public function createMustBeAbleReturnARsaSha256Signer() + { + $factory = new Factory(); + + $this->assertInstanceOf(RsaSha256::class, $factory->create('RS256')); + } + + /** + * @test + * + * @uses Lcobucci\JWT\Signer\Factory::__construct + * + * @covers Lcobucci\JWT\Signer\Factory::create + * @covers Lcobucci\JWT\Signer\Factory::createRsaSha384 */ - public function createMustBeAbleReturnASha256Signer() + public function createMustBeAbleReturnARsaSha384Signer() { $factory = new Factory(); - $this->assertInstanceOf(Sha256::class, $factory->create('HS256')); + $this->assertInstanceOf(RsaSha384::class, $factory->create('RS384')); } /** * @test - * @covers ::__construct - * @covers ::create - * @covers ::createHmacSha384 + * + * @uses Lcobucci\JWT\Signer\Factory::__construct + * + * @covers Lcobucci\JWT\Signer\Factory::create + * @covers Lcobucci\JWT\Signer\Factory::createRsaSha512 */ - public function createMustBeAbleReturnASha384Signer() + public function createMustBeAbleReturnARsaSha512Signer() { $factory = new Factory(); - $this->assertInstanceOf(Sha384::class, $factory->create('HS384')); + $this->assertInstanceOf(RsaSha512::class, $factory->create('RS512')); } /** * @test - * @covers ::__construct - * @covers ::create - * @covers ::createHmacSha512 + * + * @uses Lcobucci\JWT\Signer\Factory::__construct + * + * @covers Lcobucci\JWT\Signer\Factory::create + * @covers Lcobucci\JWT\Signer\Factory::createEcdsaSha256 */ - public function createMustBeAbleReturnASha512Signer() + public function createMustBeAbleReturnAEcdsaSha256Signer() { $factory = new Factory(); - $this->assertInstanceOf(Sha512::class, $factory->create('HS512')); + $this->assertInstanceOf(EcdsaSha256::class, $factory->create('ES256')); } /** * @test - * @covers ::__construct - * @covers ::create + * + * @uses Lcobucci\JWT\Signer\Factory::__construct + * + * @covers Lcobucci\JWT\Signer\Factory::create + * @covers Lcobucci\JWT\Signer\Factory::createEcdsaSha384 + */ + public function createMustBeAbleReturnAEcdsaSha384Signer() + { + $factory = new Factory(); + + $this->assertInstanceOf(EcdsaSha384::class, $factory->create('ES384')); + } + + /** + * @test + * + * @uses Lcobucci\JWT\Signer\Factory::__construct + * + * @covers Lcobucci\JWT\Signer\Factory::create + * @covers Lcobucci\JWT\Signer\Factory::createEcdsaSha512 + */ + public function createMustBeAbleReturnAEcdsaSha512Signer() + { + $factory = new Factory(); + + $this->assertInstanceOf(EcdsaSha512::class, $factory->create('ES512')); + } + + /** + * @test + * + * @uses Lcobucci\JWT\Signer\Factory::__construct + * + * @covers Lcobucci\JWT\Signer\Factory::create * * @expectedException InvalidArgumentException */ diff --git a/test/Signer/Hmac/Sha256Test.php b/test/Signer/Hmac/Sha256Test.php index 52b21f6f..f6db0bda 100644 --- a/test/Signer/Hmac/Sha256Test.php +++ b/test/Signer/Hmac/Sha256Test.php @@ -10,14 +10,13 @@ /** * @author Luís Otávio Cobucci Oblonczyk * @since 0.1.0 - * - * @coversDefaultClass Lcobucci\JWT\Signer\Hmac\Sha256 */ class Sha256Test extends \PHPUnit_Framework_TestCase { /** * @test - * @covers ::getAlgorithmId + * + * @covers Lcobucci\JWT\Signer\Hmac\Sha256::getAlgorithmId */ public function getAlgorithmIdMustBeCorrect() { @@ -28,7 +27,8 @@ public function getAlgorithmIdMustBeCorrect() /** * @test - * @covers ::getAlgorithm + * + * @covers Lcobucci\JWT\Signer\Hmac\Sha256::getAlgorithm */ public function getAlgorithmMustBeCorrect() { diff --git a/test/Signer/Hmac/Sha384Test.php b/test/Signer/Hmac/Sha384Test.php index 8a9c585f..b4de61bf 100644 --- a/test/Signer/Hmac/Sha384Test.php +++ b/test/Signer/Hmac/Sha384Test.php @@ -10,14 +10,13 @@ /** * @author Luís Otávio Cobucci Oblonczyk * @since 0.1.0 - * - * @coversDefaultClass Lcobucci\JWT\Signer\Hmac\Sha384 */ class Sha384Test extends \PHPUnit_Framework_TestCase { /** * @test - * @covers ::getAlgorithmId + * + * @covers Lcobucci\JWT\Signer\Hmac\Sha384::getAlgorithmId */ public function getAlgorithmIdMustBeCorrect() { @@ -28,7 +27,8 @@ public function getAlgorithmIdMustBeCorrect() /** * @test - * @covers ::getAlgorithm + * + * @covers Lcobucci\JWT\Signer\Hmac\Sha384::getAlgorithm */ public function getAlgorithmMustBeCorrect() { diff --git a/test/Signer/Hmac/Sha512Test.php b/test/Signer/Hmac/Sha512Test.php index ebfb3f15..e6fd3153 100644 --- a/test/Signer/Hmac/Sha512Test.php +++ b/test/Signer/Hmac/Sha512Test.php @@ -10,14 +10,13 @@ /** * @author Luís Otávio Cobucci Oblonczyk * @since 0.1.0 - * - * @coversDefaultClass Lcobucci\JWT\Signer\Hmac\Sha512 */ class Sha512Test extends \PHPUnit_Framework_TestCase { /** * @test - * @covers ::getAlgorithmId + * + * @covers Lcobucci\JWT\Signer\Hmac\Sha512::getAlgorithmId */ public function getAlgorithmIdMustBeCorrect() { @@ -28,7 +27,8 @@ public function getAlgorithmIdMustBeCorrect() /** * @test - * @covers ::getAlgorithm + * + * @covers Lcobucci\JWT\Signer\Hmac\Sha512::getAlgorithm */ public function getAlgorithmMustBeCorrect() { diff --git a/test/Signer/HmacTest.php b/test/Signer/HmacTest.php index ee69572a..5bd3032a 100644 --- a/test/Signer/HmacTest.php +++ b/test/Signer/HmacTest.php @@ -10,8 +10,6 @@ /** * @author Luís Otávio Cobucci Oblonczyk * @since 0.1.0 - * - * @coversDefaultClass Lcobucci\JWT\Signer\Hmac */ class HmacTest extends \PHPUnit_Framework_TestCase { @@ -38,13 +36,81 @@ protected function setUp() /** * @test - * @covers ::createHash + * + * @covers Lcobucci\JWT\Signer\Hmac::createHash */ public function createHashMustReturnAHashAccordingWithTheAlgorithm() { - $this->assertEquals( - hash_hmac('sha256', 'test', '123', true), - $this->signer->createHash('test', '123') - ); + $hash = hash_hmac('sha256', 'test', '123', true); + + $this->assertEquals($hash, $this->signer->createHash('test', '123')); + + return $hash; + } + + /** + * @test + * + * @depends createHashMustReturnAHashAccordingWithTheAlgorithm + * + * @uses Lcobucci\JWT\Signer\Hmac::createHash + * + * @covers Lcobucci\JWT\Signer\Hmac::verify + */ + public function verifyShouldReturnTrueWhenExpectedHashWasCreatedWithSameInformation($expected) + { + $this->assertTrue($this->signer->verify($expected, 'test', '123')); + } + + /** + * @test + * + * @depends createHashMustReturnAHashAccordingWithTheAlgorithm + * + * @uses Lcobucci\JWT\Signer\Hmac::createHash + * + * @covers Lcobucci\JWT\Signer\Hmac::verify + */ + public function verifyShouldReturnFalseWhenExpectedHashWasNotCreatedWithSameInformation($expected) + { + $this->assertFalse($this->signer->verify($expected, 'test', '1234')); + } + + /** + * @test + * + * @covers Lcobucci\JWT\Signer\Hmac::hashEquals + */ + public function hashEqualsShouldReturnFalseWhenExpectedHashHasDifferentLengthThanGenerated() + { + $this->assertFalse($this->signer->hashEquals('123', '1234')); + } + + /** + * @test + * + * @depends createHashMustReturnAHashAccordingWithTheAlgorithm + * + * @uses Lcobucci\JWT\Signer\Hmac::createHash + * + * @covers Lcobucci\JWT\Signer\Hmac::hashEquals + */ + public function hashEqualsShouldReturnFalseWhenExpectedHashIsDifferentThanGenerated($expected) + { + $this->assertFalse($this->signer->hashEquals($expected, $this->signer->createHash('test', '1234'))); + } + + /** + * @test + * + * @depends createHashMustReturnAHashAccordingWithTheAlgorithm + * + * @uses Lcobucci\JWT\Signer\Hmac::createHash + * + * @covers Lcobucci\JWT\Signer\Hmac::hashEquals + */ + public function hashEqualsShouldReturnTrueWhenExpectedHashIsEqualsThanGenerated($expected) + { + $this->assertTrue($this->signer->hashEquals($expected, $this->signer->createHash('test', '123'))); } } diff --git a/test/Signer/KeychainTest.php b/test/Signer/KeychainTest.php new file mode 100644 index 00000000..897d60b7 --- /dev/null +++ b/test/Signer/KeychainTest.php @@ -0,0 +1,100 @@ + + * @since 2.1.0 + */ +class KeychainTest extends \PHPUnit_Framework_TestCase +{ + use RsaKeys; + + /** + * @test + * + * @covers Lcobucci\JWT\Signer\Keychain::getPrivateKey + * + * @expectedException \InvalidArgumentException + */ + public function getPrivateKeyShouldRaiseExceptionWhenInvalidKeyIsInformed() + { + $keychain = new Keychain(); + $keychain->getPrivateKey('blablabla'); + } + + /** + * @test + * + * @uses Lcobucci\JWT\RsaKeys + * + * @covers Lcobucci\JWT\Signer\Keychain::getPrivateKey + */ + public function getPrivateKeyShouldReturnAValidResource() + { + $keychain = new Keychain(); + + $privateKey = $keychain->getPrivateKey($this->privateRsaContent()); + + $this->assertInternalType('resource', $privateKey); + $this->assertEquals(openssl_pkey_get_details($privateKey), openssl_pkey_get_details($this->privateRsa())); + } + + /** + * @test + * + * @uses Lcobucci\JWT\RsaKeys + * + * @covers Lcobucci\JWT\Signer\Keychain::getPrivateKey + */ + public function getPrivateKeyShouldBeAbleToUseAPassphrase() + { + $keychain = new Keychain(); + + $privateKey = $keychain->getPrivateKey($this->encryptedPrivateRsaContent(), 'testing'); + + $this->assertInternalType('resource', $privateKey); + + $this->assertEquals( + openssl_pkey_get_details($privateKey), + openssl_pkey_get_details($this->encryptedPrivateRsa()) + ); + } + + /** + * @test + * + * @covers Lcobucci\JWT\Signer\Keychain::getPublicKey + * + * @expectedException \InvalidArgumentException + */ + public function getPublicKeyShouldRaiseExceptionWhenInvalidCertificateIsInformed() + { + $keychain = new Keychain(); + $keychain->getPublicKey('blablabla'); + } + + /** + * @test + * + * @uses Lcobucci\JWT\RsaKeys + * + * @covers Lcobucci\JWT\Signer\Keychain::getPublicKey + */ + public function getPublicKeyShouldReturnAValidResource() + { + $keychain = new Keychain(); + + $publicKey = $keychain->getPublicKey($this->publicRsaContent()); + + $this->assertInternalType('resource', $publicKey); + $this->assertEquals(openssl_pkey_get_details($publicKey), openssl_pkey_get_details($this->publicRsa())); + } +} diff --git a/test/Signer/OpenSSLTest.php b/test/Signer/OpenSSLTest.php new file mode 100644 index 00000000..d7a2a4be --- /dev/null +++ b/test/Signer/OpenSSLTest.php @@ -0,0 +1,121 @@ + + * @since 2.1.0 + */ +class OpenSSLTest extends \PHPUnit_Framework_TestCase +{ + use EcdsaKeys, RsaKeys; + + /** + * @var OpenSSL|\PHPUnit_Framework_MockObject_MockObject + */ + protected $signer; + + /** + * {@inheritdoc} + */ + protected function setUp() + { + $this->signer = $this->getMockForAbstractClass(OpenSSL::class); + + $this->signer->expects($this->any()) + ->method('getAlgorithmId') + ->willReturn('TEST123'); + + $this->signer->expects($this->any()) + ->method('getType') + ->willReturn(OPENSSL_KEYTYPE_RSA); + + $this->signer->expects($this->any()) + ->method('getAlgorithm') + ->willReturn(OPENSSL_ALGO_SHA256); + } + + /** + * @test + * + * @uses Lcobucci\JWT\RsaKeys + * + * @covers Lcobucci\JWT\Signer\OpenSSL::validateKey + * @covers Lcobucci\JWT\Signer\OpenSSL::createHash + * + * @expectedException \InvalidArgumentException + */ + public function createHashShouldRaiseExceptionWhenKeyTypeDontMatch() + { + $this->signer->createHash('test', $this->privateEcdsa()); + } + + /** + * @test + * + * @uses Lcobucci\JWT\RsaKeys + * @uses Lcobucci\JWT\Signer\OpenSSL::validateKey + * + * @covers Lcobucci\JWT\Signer\OpenSSL::createHash + */ + public function createHashShouldReturnASignatureForGivenPayload() + { + $signature = $this->signer->createHash('test', $this->privateRsa()); + + $this->assertInternalType('string', $signature); + $this->assertNotEmpty($signature); + + return $signature; + } + + /** + * @test + * + * @uses Lcobucci\JWT\RsaKeys + * + * @covers Lcobucci\JWT\Signer\OpenSSL::validateKey + * @covers Lcobucci\JWT\Signer\OpenSSL::verify + * + * @expectedException \InvalidArgumentException + */ + public function verifyShouldRaiseExceptionWhenKeyTypeDontMatch() + { + $this->signer->verify('test', 'test', $this->publicEcdsa()); + } + + /** + * @test + * + * @uses Lcobucci\JWT\RsaKeys + * + * @covers Lcobucci\JWT\Signer\OpenSSL::validateKey + * @covers Lcobucci\JWT\Signer\OpenSSL::verify + */ + public function verifyShouldReturnFalseWhenSignatureIsDifferent() + { + $this->assertFalse($this->signer->verify('123', 'test', $this->publicRsa())); + } + + /** + * @test + * + * @depends createHashShouldReturnASignatureForGivenPayload + * + * @uses Lcobucci\JWT\RsaKeys + * @uses Lcobucci\JWT\Signer\OpenSSL::validateKey + * + * @covers Lcobucci\JWT\Signer\OpenSSL::verify + */ + public function verifyShouldReturnTrueWhenSignatureMatches($signature) + { + $this->assertTrue($this->signer->verify($signature, 'test', $this->publicRsa())); + } +} diff --git a/test/Signer/Rsa/Sha256Test.php b/test/Signer/Rsa/Sha256Test.php new file mode 100644 index 00000000..2e4248a4 --- /dev/null +++ b/test/Signer/Rsa/Sha256Test.php @@ -0,0 +1,39 @@ + + * @since 2.1.0 + */ +class Sha256Test extends \PHPUnit_Framework_TestCase +{ + /** + * @test + * + * @covers Lcobucci\JWT\Signer\Rsa\Sha256::getAlgorithmId + */ + public function getAlgorithmIdMustBeCorrect() + { + $signer = new Sha256(); + + $this->assertEquals('RS256', $signer->getAlgorithmId()); + } + + /** + * @test + * + * @covers Lcobucci\JWT\Signer\Rsa\Sha256::getAlgorithm + */ + public function getAlgorithmMustBeCorrect() + { + $signer = new Sha256(); + + $this->assertEquals(OPENSSL_ALGO_SHA256, $signer->getAlgorithm()); + } +} diff --git a/test/Signer/Rsa/Sha384Test.php b/test/Signer/Rsa/Sha384Test.php new file mode 100644 index 00000000..18632127 --- /dev/null +++ b/test/Signer/Rsa/Sha384Test.php @@ -0,0 +1,39 @@ + + * @since 2.1.0 + */ +class Sha384Test extends \PHPUnit_Framework_TestCase +{ + /** + * @test + * + * @covers Lcobucci\JWT\Signer\Rsa\Sha384::getAlgorithmId + */ + public function getAlgorithmIdMustBeCorrect() + { + $signer = new Sha384(); + + $this->assertEquals('RS384', $signer->getAlgorithmId()); + } + + /** + * @test + * + * @covers Lcobucci\JWT\Signer\Rsa\Sha384::getAlgorithm + */ + public function getAlgorithmMustBeCorrect() + { + $signer = new Sha384(); + + $this->assertEquals(OPENSSL_ALGO_SHA384, $signer->getAlgorithm()); + } +} diff --git a/test/Signer/Rsa/Sha512Test.php b/test/Signer/Rsa/Sha512Test.php new file mode 100644 index 00000000..03e31fda --- /dev/null +++ b/test/Signer/Rsa/Sha512Test.php @@ -0,0 +1,39 @@ + + * @since 2.1.0 + */ +class Sha512Test extends \PHPUnit_Framework_TestCase +{ + /** + * @test + * + * @covers Lcobucci\JWT\Signer\Rsa\Sha512::getAlgorithmId + */ + public function getAlgorithmIdMustBeCorrect() + { + $signer = new Sha512(); + + $this->assertEquals('RS512', $signer->getAlgorithmId()); + } + + /** + * @test + * + * @covers Lcobucci\JWT\Signer\Rsa\Sha512::getAlgorithm + */ + public function getAlgorithmMustBeCorrect() + { + $signer = new Sha512(); + + $this->assertEquals(OPENSSL_ALGO_SHA512, $signer->getAlgorithm()); + } +} diff --git a/test/Signer/RsaTest.php b/test/Signer/RsaTest.php new file mode 100644 index 00000000..62985492 --- /dev/null +++ b/test/Signer/RsaTest.php @@ -0,0 +1,27 @@ + + * @since 2.1.0 + */ +class RsaTest extends \PHPUnit_Framework_TestCase +{ + /** + * @test + * + * @covers Lcobucci\JWT\Signer\Rsa::getType + */ + public function getTypeMustBeCorrect() + { + $signer = $this->getMockForAbstractClass(Rsa::class); + + $this->assertEquals(OPENSSL_KEYTYPE_RSA, $signer->getType()); + } +} diff --git a/test/TokenTest.php b/test/TokenTest.php index fb4f9daa..ea961b60 100644 --- a/test/TokenTest.php +++ b/test/TokenTest.php @@ -16,8 +16,6 @@ /** * @author Luís Otávio Cobucci Oblonczyk * @since 0.1.0 - * - * @coversDefaultClass Lcobucci\JWT\Token */ class TokenTest extends \PHPUnit_Framework_TestCase { @@ -36,7 +34,8 @@ protected function setUp() /** * @test - * @covers ::__construct + * + * @covers Lcobucci\JWT\Token::__construct */ public function constructMustInitializeAnEmptyPlainTextTokenWhenNoArgumentsArePassed() { @@ -49,8 +48,10 @@ public function constructMustInitializeAnEmptyPlainTextTokenWhenNoArgumentsArePa /** * @test - * @covers ::__construct - * @covers ::setEncoder + * + * @uses Lcobucci\JWT\Token::__construct + * + * @covers Lcobucci\JWT\Token::setEncoder */ public function setEncoderMustConfigureTheEncoderAttribute() { @@ -62,8 +63,10 @@ public function setEncoderMustConfigureTheEncoderAttribute() /** * @test - * @covers ::__construct - * @covers ::getHeader + * + * @uses Lcobucci\JWT\Token::__construct + * + * @covers Lcobucci\JWT\Token::getHeader */ public function getHeaderMustReturnTheConfiguredHeader() { @@ -74,8 +77,10 @@ public function getHeaderMustReturnTheConfiguredHeader() /** * @test - * @covers ::__construct - * @covers ::getClaims + * + * @uses Lcobucci\JWT\Token::__construct + * + * @covers Lcobucci\JWT\Token::getClaims */ public function getClaimsMustReturnTheConfiguredClaims() { @@ -86,8 +91,42 @@ public function getClaimsMustReturnTheConfiguredClaims() /** * @test - * @covers ::__construct - * @covers ::getSignature + * + * @uses Lcobucci\JWT\Token::__construct + * @uses Lcobucci\JWT\Claim\Basic::__construct + * + * @covers Lcobucci\JWT\Token::getClaim + * + * @expectedException \OutOfBoundsException + */ + public function getClaimShouldRaiseExceptionWhenClaimIsNotConfigured() + { + $token = new Token(); + $token->getClaim('testing'); + } + + /** + * @test + * + * @uses Lcobucci\JWT\Token::__construct + * @uses Lcobucci\JWT\Claim\Basic::__construct + * @uses Lcobucci\JWT\Claim\Basic::getValue + * + * @covers Lcobucci\JWT\Token::getClaim + */ + public function getClaimShouldReturnTheClaimValueWhenItExists() + { + $token = new Token([], ['testing' => new Basic('testing', 'test')]); + + $this->assertEquals('test', $token->getClaim('testing')); + } + + /** + * @test + * + * @uses Lcobucci\JWT\Token::__construct + * + * @covers Lcobucci\JWT\Token::getSignature */ public function getSignatureMustReturnTheConfiguredSignature() { @@ -99,8 +138,10 @@ public function getSignatureMustReturnTheConfiguredSignature() /** * @test - * @covers ::__construct - * @covers ::verify + * + * @uses Lcobucci\JWT\Token::__construct + * + * @covers Lcobucci\JWT\Token::verify * * @expectedException BadMethodCallException */ @@ -112,10 +153,12 @@ public function verifyMustRaiseExceptionWhenTokenIsUnsigned() /** * @test - * @covers ::__construct - * @covers ::setEncoder - * @covers ::getPayload - * @covers ::verify + * + * @uses Lcobucci\JWT\Token::__construct + * @uses Lcobucci\JWT\Token::setEncoder + * @uses Lcobucci\JWT\Token::getPayload + * + * @covers Lcobucci\JWT\Token::verify */ public function verifyMustDelegateTheValidationToSignature() { @@ -133,10 +176,12 @@ public function verifyMustDelegateTheValidationToSignature() /** * @test - * @covers ::__construct - * @covers ::validate - * @covers ::getValidatableClaims - * @covers Lcobucci\JWT\ValidationData::__construct + * + * @uses Lcobucci\JWT\Token::__construct + * @uses Lcobucci\JWT\ValidationData::__construct + * + * @covers Lcobucci\JWT\Token::validate + * @covers Lcobucci\JWT\Token::getValidatableClaims */ public function validateShouldReturnTrueWhenClaimsAreEmpty() { @@ -147,11 +192,13 @@ public function validateShouldReturnTrueWhenClaimsAreEmpty() /** * @test - * @covers ::__construct - * @covers ::validate - * @covers ::getValidatableClaims - * @covers Lcobucci\JWT\ValidationData::__construct - * @covers Lcobucci\JWT\Claim\Basic::__construct + * + * @uses Lcobucci\JWT\Token::__construct + * @uses Lcobucci\JWT\ValidationData::__construct + * @uses Lcobucci\JWT\Claim\Basic::__construct + * + * @covers Lcobucci\JWT\Token::validate + * @covers Lcobucci\JWT\Token::getValidatableClaims */ public function validateShouldReturnTrueWhenThereAreNoValidatableClaims() { @@ -162,18 +209,20 @@ public function validateShouldReturnTrueWhenThereAreNoValidatableClaims() /** * @test - * @covers ::__construct - * @covers ::validate - * @covers ::getValidatableClaims - * @covers Lcobucci\JWT\ValidationData::__construct - * @covers Lcobucci\JWT\ValidationData::get - * @covers Lcobucci\JWT\ValidationData::has - * @covers Lcobucci\JWT\ValidationData::setIssuer - * @covers Lcobucci\JWT\Claim\Basic::__construct - * @covers Lcobucci\JWT\Claim\Basic::getName - * @covers Lcobucci\JWT\Claim\Basic::getValue - * @covers Lcobucci\JWT\Claim\EqualsTo::__construct - * @covers Lcobucci\JWT\Claim\EqualsTo::validate + * + * @uses Lcobucci\JWT\Token::__construct + * @uses Lcobucci\JWT\ValidationData::__construct + * @uses Lcobucci\JWT\ValidationData::get + * @uses Lcobucci\JWT\ValidationData::has + * @uses Lcobucci\JWT\ValidationData::setIssuer + * @uses Lcobucci\JWT\Claim\Basic::__construct + * @uses Lcobucci\JWT\Claim\Basic::getName + * @uses Lcobucci\JWT\Claim\Basic::getValue + * @uses Lcobucci\JWT\Claim\EqualsTo::__construct + * @uses Lcobucci\JWT\Claim\EqualsTo::validate + * + * @covers Lcobucci\JWT\Token::validate + * @covers Lcobucci\JWT\Token::getValidatableClaims */ public function validateShouldReturnFalseWhenThereIsAtLeastOneFailedValidatableClaim() { @@ -193,22 +242,24 @@ public function validateShouldReturnFalseWhenThereIsAtLeastOneFailedValidatableC /** * @test - * @covers ::__construct - * @covers ::validate - * @covers ::getValidatableClaims - * @covers Lcobucci\JWT\ValidationData::__construct - * @covers Lcobucci\JWT\ValidationData::get - * @covers Lcobucci\JWT\ValidationData::has - * @covers Lcobucci\JWT\ValidationData::setIssuer - * @covers Lcobucci\JWT\Claim\Basic::__construct - * @covers Lcobucci\JWT\Claim\Basic::getName - * @covers Lcobucci\JWT\Claim\Basic::getValue - * @covers Lcobucci\JWT\Claim\EqualsTo::__construct - * @covers Lcobucci\JWT\Claim\EqualsTo::validate - * @covers Lcobucci\JWT\Claim\LesserOrEqualsTo::__construct - * @covers Lcobucci\JWT\Claim\LesserOrEqualsTo::validate - * @covers Lcobucci\JWT\Claim\GreaterOrEqualsTo::__construct - * @covers Lcobucci\JWT\Claim\GreaterOrEqualsTo::validate + * + * @uses Lcobucci\JWT\Token::__construct + * @uses Lcobucci\JWT\ValidationData::__construct + * @uses Lcobucci\JWT\ValidationData::get + * @uses Lcobucci\JWT\ValidationData::has + * @uses Lcobucci\JWT\ValidationData::setIssuer + * @uses Lcobucci\JWT\Claim\Basic::__construct + * @uses Lcobucci\JWT\Claim\Basic::getName + * @uses Lcobucci\JWT\Claim\Basic::getValue + * @uses Lcobucci\JWT\Claim\EqualsTo::__construct + * @uses Lcobucci\JWT\Claim\EqualsTo::validate + * @uses Lcobucci\JWT\Claim\LesserOrEqualsTo::__construct + * @uses Lcobucci\JWT\Claim\LesserOrEqualsTo::validate + * @uses Lcobucci\JWT\Claim\GreaterOrEqualsTo::__construct + * @uses Lcobucci\JWT\Claim\GreaterOrEqualsTo::validate + * + * @covers Lcobucci\JWT\Token::validate + * @covers Lcobucci\JWT\Token::getValidatableClaims */ public function validateShouldReturnTrueWhenThereAreNoFailedValidatableClaims() { @@ -231,9 +282,11 @@ public function validateShouldReturnTrueWhenThereAreNoFailedValidatableClaims() /** * @test - * @covers ::__construct - * @covers ::getPayload - * @covers ::__toString + * + * @uses Lcobucci\JWT\Token::__construct + * + * @covers Lcobucci\JWT\Token::__toString + * @covers Lcobucci\JWT\Token::getPayload */ public function toStringMustReturnAnEmptyStringWhenEncoderIsNotDefined() { @@ -244,10 +297,12 @@ public function toStringMustReturnAnEmptyStringWhenEncoderIsNotDefined() /** * @test - * @covers ::__construct - * @covers ::setEncoder - * @covers ::getPayload - * @covers ::__toString + * + * @uses Lcobucci\JWT\Token::__construct + * @uses Lcobucci\JWT\Token::setEncoder + * + * @covers Lcobucci\JWT\Token::__toString + * @covers Lcobucci\JWT\Token::getPayload */ public function toStringMustReturnEncodedDataWithEmptySignature() { @@ -261,10 +316,12 @@ public function toStringMustReturnEncodedDataWithEmptySignature() /** * @test - * @covers ::__construct - * @covers ::setEncoder - * @covers ::getPayload - * @covers ::__toString + * + * @uses Lcobucci\JWT\Token::__construct + * @uses Lcobucci\JWT\Token::setEncoder + * + * @covers Lcobucci\JWT\Token::__toString + * @covers Lcobucci\JWT\Token::getPayload */ public function toStringMustReturnEncodedData() { diff --git a/test/ValidationDataTest.php b/test/ValidationDataTest.php index ff2afb5d..d27f398b 100644 --- a/test/ValidationDataTest.php +++ b/test/ValidationDataTest.php @@ -10,15 +10,13 @@ /** * @author Luís Otávio Cobucci Oblonczyk * @since 2.0.0 - * - * @coversDefaultClass Lcobucci\JWT\ValidationData */ class ValidationDataTest extends \PHPUnit_Framework_TestCase { /** * @test * - * @covers ::__construct + * @covers Lcobucci\JWT\ValidationData::__construct */ public function constructorShouldConfigureTheItems() { @@ -32,8 +30,9 @@ public function constructorShouldConfigureTheItems() /** * @test * - * @covers ::__construct - * @covers ::setId + * @uses Lcobucci\JWT\ValidationData::__construct + * + * @covers Lcobucci\JWT\ValidationData::setId */ public function setIdShouldChangeTheId() { @@ -49,8 +48,9 @@ public function setIdShouldChangeTheId() /** * @test * - * @covers ::__construct - * @covers ::setIssuer + * @uses Lcobucci\JWT\ValidationData::__construct + * + * @covers Lcobucci\JWT\ValidationData::setIssuer */ public function setIssuerShouldChangeTheIssuer() { @@ -66,8 +66,9 @@ public function setIssuerShouldChangeTheIssuer() /** * @test * - * @covers ::__construct - * @covers ::setAudience + * @uses Lcobucci\JWT\ValidationData::__construct + * + * @covers Lcobucci\JWT\ValidationData::setAudience */ public function setAudienceShouldChangeTheAudience() { @@ -83,8 +84,9 @@ public function setAudienceShouldChangeTheAudience() /** * @test * - * @covers ::__construct - * @covers ::setSubject + * @uses Lcobucci\JWT\ValidationData::__construct + * + * @covers Lcobucci\JWT\ValidationData::setSubject */ public function setSubjectShouldChangeTheSubject() { @@ -100,8 +102,9 @@ public function setSubjectShouldChangeTheSubject() /** * @test * - * @covers ::__construct - * @covers ::setCurrentTime + * @uses Lcobucci\JWT\ValidationData::__construct + * + * @covers Lcobucci\JWT\ValidationData::setCurrentTime */ public function setCurrentTimeShouldChangeTheTimeBasedValues() { @@ -117,8 +120,9 @@ public function setCurrentTimeShouldChangeTheTimeBasedValues() /** * @test * - * @covers ::__construct - * @covers ::has + * @uses Lcobucci\JWT\ValidationData::__construct + * + * @covers Lcobucci\JWT\ValidationData::has */ public function hasShouldReturnTrueWhenItemIsNotEmpty() { @@ -130,8 +134,9 @@ public function hasShouldReturnTrueWhenItemIsNotEmpty() /** * @test * - * @covers ::__construct - * @covers ::has + * @uses Lcobucci\JWT\ValidationData::__construct + * + * @covers Lcobucci\JWT\ValidationData::has */ public function hasShouldReturnFalseWhenItemIsEmpty() { @@ -143,8 +148,9 @@ public function hasShouldReturnFalseWhenItemIsEmpty() /** * @test * - * @covers ::__construct - * @covers ::has + * @uses Lcobucci\JWT\ValidationData::__construct + * + * @covers Lcobucci\JWT\ValidationData::has */ public function hasShouldReturnFalseWhenItemIsNotDefined() { @@ -156,8 +162,9 @@ public function hasShouldReturnFalseWhenItemIsNotDefined() /** * @test * - * @covers ::__construct - * @covers ::get + * @uses Lcobucci\JWT\ValidationData::__construct + * + * @covers Lcobucci\JWT\ValidationData::get */ public function getShouldReturnTheItemValue() { @@ -169,8 +176,9 @@ public function getShouldReturnTheItemValue() /** * @test * - * @covers ::__construct - * @covers ::get + * @uses Lcobucci\JWT\ValidationData::__construct + * + * @covers Lcobucci\JWT\ValidationData::get */ public function getShouldReturnNullWhenItemIsNotDefined() {