Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
3 changes: 3 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
parameters:
ignoreErrors:
- '#Call to an undefined method object::getConfig\(\)#'
23 changes: 17 additions & 6 deletions src/Auth/JwtAuthenticate.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,21 @@ class JwtAuthenticate extends BaseAuthenticate
*/
public function __construct(ComponentRegistry $registry, $config)
{
$this->setConfig([
$defaultConfig = [
'header' => 'authorization',
'prefix' => 'bearer',
'parameter' => 'token',
'queryDatasource' => true,
'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'];
Expand Down Expand Up @@ -177,15 +183,15 @@ 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.
*/
public function getToken($request = null)
{
$config = $this->_config;

if (!$request) {
if ($request === null) {
return $this->_token;
}

Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
10 changes: 8 additions & 2 deletions tests/TestCase/Auth/JwtAuthenticateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

Expand Down