diff --git a/.travis.yml b/.travis.yml index 02ae7ab..86336cb 100644 --- a/.travis.yml +++ b/.travis.yml @@ -17,22 +17,33 @@ matrix: - php: 7.1 env: PHPCS=1 DEFAULT=0 + - php: 7.1 + env: PHPSTAN=1 DEFAULT=0 + + - php: 5.6 + env: PREFER_LOWEST=1 + before_script: - - if [[ $TRAVIS_PHP_VERSION != 7.0 ]]; then phpenv config-rm xdebug.ini; fi + - if [[ $TRAVIS_PHP_VERSION != 7.1 ]]; then phpenv config-rm xdebug.ini; fi - - composer install --prefer-dist --no-interaction + - if [[ $PREFER_LOWEST != 1 ]]; then composer update --no-interaction; fi + - if [[ $PREFER_LOWEST == 1 ]]; then composer update --no-interaction --prefer-lowest --prefer-stable; fi - if [[ $DEFAULT = 1 ]]; then mysql -e 'CREATE DATABASE cakephp_test;'; fi - if [[ $PHPCS = 1 ]]; then composer require cakephp/cakephp-codesniffer:^3.0; fi + - if [[ $PHPSTAN = 1 ]]; then composer require phpstan/phpstan; fi script: - - if [[ $DEFAULT = 1 && $TRAVIS_PHP_VERSION = 7.0 ]]; then vendor/bin/phpunit --coverage-clover=clover.xml; fi - - if [[ $DEFAULT = 1 && $TRAVIS_PHP_VERSION != 7.0 ]]; then vendor/bin/phpunit; fi + - if [[ $DEFAULT = 1 && $TRAVIS_PHP_VERSION = 7.1 ]]; then vendor/bin/phpunit --coverage-clover=clover.xml; fi + - if [[ $DEFAULT = 1 && $TRAVIS_PHP_VERSION != 7.1 ]]; then vendor/bin/phpunit; fi + - if [[ $PHPCS = 1 ]]; then vendor/bin/phpcs -p --extensions=php --standard=vendor/cakephp/cakephp-codesniffer/CakePHP ./src ./tests; fi + - if [[ $PHPSTAN = 1 ]]; then vendor/bin/phpstan analyse -c phpstan.neon -l 5 src; fi + after_success: -- if [[ $DEFAULT = 1 && $TRAVIS_PHP_VERSION = 7.0 ]]; then bash <(curl -s https://codecov.io/bash); fi +- if [[ $DEFAULT = 1 && $TRAVIS_PHP_VERSION = 7.1 ]]; then bash <(curl -s https://codecov.io/bash); fi notifications: email: false diff --git a/composer.json b/composer.json index 1872216..d59d0ca 100644 --- a/composer.json +++ b/composer.json @@ -22,10 +22,11 @@ "issues":"https://github.com/ADmad/cakephp-jwt-auth/issues" }, "require": { - "cakephp/cakephp": "^3.6", + "cakephp/cakephp": "^3.5", "firebase/php-jwt": "^5.0" }, "require-dev": { + "cakephp/chronos": "^1.1", "phpunit/phpunit": "^5.7.14|^6.0" }, "autoload": { diff --git a/phpstan.neon b/phpstan.neon new file mode 100644 index 0000000..3cf2159 --- /dev/null +++ b/phpstan.neon @@ -0,0 +1,3 @@ +parameters: + ignoreErrors: + - '#Call to an undefined method object::getConfig\(\)#' diff --git a/src/Auth/JwtAuthenticate.php b/src/Auth/JwtAuthenticate.php index 9afe4cb..9ea6a2d 100644 --- a/src/Auth/JwtAuthenticate.php +++ b/src/Auth/JwtAuthenticate.php @@ -88,7 +88,7 @@ class JwtAuthenticate extends BaseAuthenticate */ public function __construct(ComponentRegistry $registry, $config) { - $this->setConfig([ + $defaultConfig = [ 'header' => 'authorization', 'prefix' => 'bearer', 'parameter' => 'token', @@ -96,7 +96,13 @@ public function __construct(ComponentRegistry $registry, $config) 'fields' => ['username' => 'id'], 'unauthenticatedException' => UnauthorizedException::class, 'key' => null, - ]); + ]; + + if (!class_exists(UnauthorizedException::class, false)) { + $defaultConfig['unauthenticatedException'] = 'Cake\Network\Exception\UnauthorizedException'; + } + + $this->setConfig($defaultConfig); if (empty($config['allowedAlgs'])) { $config['allowedAlgs'] = ['HS256']; @@ -177,7 +183,7 @@ public function getPayload($request = null) /** * Get token from header or query string. * - * @param \Cake\Network\Request|null $request Request object. + * @param \Cake\Http\ServerRequest|null $request Request object. * * @return string|null Token string if found else null. */ @@ -185,7 +191,7 @@ public function getToken($request = null) { $config = $this->_config; - if (!$request) { + if ($request === null) { return $this->_token; } @@ -195,7 +201,10 @@ public function getToken($request = null) } if (!empty($this->_config['parameter'])) { - $this->_token = $request->getQuery($this->_config['parameter']); + $token = $request->getQuery($this->_config['parameter']); + if ($token !== null) { + $token = (string)$token; + } } return $this->_token; @@ -246,7 +255,9 @@ public function unauthenticated(ServerRequest $request, Response $response) return; } - $message = $this->_error ? $this->_error->getMessage() : $this->_registry->Auth->_config['authError']; + $message = $this->_error + ? $this->_error->getMessage() + : $this->_registry->get('Auth')->getConfig('authError'); $exception = new $this->_config['unauthenticatedException']($message); throw $exception; diff --git a/tests/TestCase/Auth/JwtAuthenticateTest.php b/tests/TestCase/Auth/JwtAuthenticateTest.php index cd76258..bc19dbd 100644 --- a/tests/TestCase/Auth/JwtAuthenticateTest.php +++ b/tests/TestCase/Auth/JwtAuthenticateTest.php @@ -4,6 +4,7 @@ use ADmad\JwtAuth\Auth\JwtAuthenticate; use Cake\Controller\ComponentRegistry; use Cake\Core\Configure; +use Cake\Http\Exception\UnauthorizedException; use Cake\Http\Response; use Cake\Http\ServerRequest; use Cake\I18n\Time; @@ -253,13 +254,18 @@ public function testExceptionForInvalidToken() } /** - * @expectedException Cake\Http\Exception\UnauthorizedException - * @expectedExceptionMessage Auth error + * testUnauthenticated */ public function testUnauthenticated() { $this->Registry->Auth->setConfig('authError', 'Auth error'); + if (!class_exists(UnauthorizedException::class)) { + $exceptionClass = 'Cake\Network\Exception\UnauthorizedException'; + } + $this->expectException($exceptionClass); + $this->expectExceptionMessage('Auth error'); + $result = $this->auth->unauthenticated(new ServerRequest(), $this->response); }