Skip to content

Commit 928d80e

Browse files
authored
Merge pull request #63 from ADmad/cake-update
Cake update
2 parents db8e14d + 9da583d commit 928d80e

File tree

5 files changed

+46
-14
lines changed

5 files changed

+46
-14
lines changed

.travis.yml

+16-5
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,33 @@ matrix:
1717
- php: 7.1
1818
env: PHPCS=1 DEFAULT=0
1919

20+
- php: 7.1
21+
env: PHPSTAN=1 DEFAULT=0
22+
23+
- php: 5.6
24+
env: PREFER_LOWEST=1
25+
2026
before_script:
21-
- if [[ $TRAVIS_PHP_VERSION != 7.0 ]]; then phpenv config-rm xdebug.ini; fi
27+
- if [[ $TRAVIS_PHP_VERSION != 7.1 ]]; then phpenv config-rm xdebug.ini; fi
2228

23-
- composer install --prefer-dist --no-interaction
29+
- if [[ $PREFER_LOWEST != 1 ]]; then composer update --no-interaction; fi
30+
- if [[ $PREFER_LOWEST == 1 ]]; then composer update --no-interaction --prefer-lowest --prefer-stable; fi
2431

2532
- if [[ $DEFAULT = 1 ]]; then mysql -e 'CREATE DATABASE cakephp_test;'; fi
2633

2734
- if [[ $PHPCS = 1 ]]; then composer require cakephp/cakephp-codesniffer:^3.0; fi
35+
- if [[ $PHPSTAN = 1 ]]; then composer require phpstan/phpstan; fi
2836

2937
script:
30-
- if [[ $DEFAULT = 1 && $TRAVIS_PHP_VERSION = 7.0 ]]; then vendor/bin/phpunit --coverage-clover=clover.xml; fi
31-
- if [[ $DEFAULT = 1 && $TRAVIS_PHP_VERSION != 7.0 ]]; then vendor/bin/phpunit; fi
38+
- if [[ $DEFAULT = 1 && $TRAVIS_PHP_VERSION = 7.1 ]]; then vendor/bin/phpunit --coverage-clover=clover.xml; fi
39+
- if [[ $DEFAULT = 1 && $TRAVIS_PHP_VERSION != 7.1 ]]; then vendor/bin/phpunit; fi
40+
3241
- if [[ $PHPCS = 1 ]]; then vendor/bin/phpcs -p --extensions=php --standard=vendor/cakephp/cakephp-codesniffer/CakePHP ./src ./tests; fi
3342

43+
- if [[ $PHPSTAN = 1 ]]; then vendor/bin/phpstan analyse -c phpstan.neon -l 5 src; fi
44+
3445
after_success:
35-
- if [[ $DEFAULT = 1 && $TRAVIS_PHP_VERSION = 7.0 ]]; then bash <(curl -s https://codecov.io/bash); fi
46+
- if [[ $DEFAULT = 1 && $TRAVIS_PHP_VERSION = 7.1 ]]; then bash <(curl -s https://codecov.io/bash); fi
3647

3748
notifications:
3849
email: false

composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,11 @@
2222
"issues":"https://github.com/ADmad/cakephp-jwt-auth/issues"
2323
},
2424
"require": {
25-
"cakephp/cakephp": "^3.6",
25+
"cakephp/cakephp": "^3.5",
2626
"firebase/php-jwt": "^5.0"
2727
},
2828
"require-dev": {
29+
"cakephp/chronos": "^1.1",
2930
"phpunit/phpunit": "^5.7.14|^6.0"
3031
},
3132
"autoload": {

phpstan.neon

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
parameters:
2+
ignoreErrors:
3+
- '#Call to an undefined method object::getConfig\(\)#'

src/Auth/JwtAuthenticate.php

+17-6
Original file line numberDiff line numberDiff line change
@@ -88,15 +88,21 @@ class JwtAuthenticate extends BaseAuthenticate
8888
*/
8989
public function __construct(ComponentRegistry $registry, $config)
9090
{
91-
$this->setConfig([
91+
$defaultConfig = [
9292
'header' => 'authorization',
9393
'prefix' => 'bearer',
9494
'parameter' => 'token',
9595
'queryDatasource' => true,
9696
'fields' => ['username' => 'id'],
9797
'unauthenticatedException' => UnauthorizedException::class,
9898
'key' => null,
99-
]);
99+
];
100+
101+
if (!class_exists(UnauthorizedException::class, false)) {
102+
$defaultConfig['unauthenticatedException'] = 'Cake\Network\Exception\UnauthorizedException';
103+
}
104+
105+
$this->setConfig($defaultConfig);
100106

101107
if (empty($config['allowedAlgs'])) {
102108
$config['allowedAlgs'] = ['HS256'];
@@ -177,15 +183,15 @@ public function getPayload($request = null)
177183
/**
178184
* Get token from header or query string.
179185
*
180-
* @param \Cake\Network\Request|null $request Request object.
186+
* @param \Cake\Http\ServerRequest|null $request Request object.
181187
*
182188
* @return string|null Token string if found else null.
183189
*/
184190
public function getToken($request = null)
185191
{
186192
$config = $this->_config;
187193

188-
if (!$request) {
194+
if ($request === null) {
189195
return $this->_token;
190196
}
191197

@@ -195,7 +201,10 @@ public function getToken($request = null)
195201
}
196202

197203
if (!empty($this->_config['parameter'])) {
198-
$this->_token = $request->getQuery($this->_config['parameter']);
204+
$token = $request->getQuery($this->_config['parameter']);
205+
if ($token !== null) {
206+
$token = (string)$token;
207+
}
199208
}
200209

201210
return $this->_token;
@@ -246,7 +255,9 @@ public function unauthenticated(ServerRequest $request, Response $response)
246255
return;
247256
}
248257

249-
$message = $this->_error ? $this->_error->getMessage() : $this->_registry->Auth->_config['authError'];
258+
$message = $this->_error
259+
? $this->_error->getMessage()
260+
: $this->_registry->get('Auth')->getConfig('authError');
250261

251262
$exception = new $this->_config['unauthenticatedException']($message);
252263
throw $exception;

tests/TestCase/Auth/JwtAuthenticateTest.php

+8-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
use ADmad\JwtAuth\Auth\JwtAuthenticate;
55
use Cake\Controller\ComponentRegistry;
66
use Cake\Core\Configure;
7+
use Cake\Http\Exception\UnauthorizedException;
78
use Cake\Http\Response;
89
use Cake\Http\ServerRequest;
910
use Cake\I18n\Time;
@@ -253,13 +254,18 @@ public function testExceptionForInvalidToken()
253254
}
254255

255256
/**
256-
* @expectedException Cake\Http\Exception\UnauthorizedException
257-
* @expectedExceptionMessage Auth error
257+
* testUnauthenticated
258258
*/
259259
public function testUnauthenticated()
260260
{
261261
$this->Registry->Auth->setConfig('authError', 'Auth error');
262262

263+
if (!class_exists(UnauthorizedException::class)) {
264+
$exceptionClass = 'Cake\Network\Exception\UnauthorizedException';
265+
}
266+
$this->expectException($exceptionClass);
267+
$this->expectExceptionMessage('Auth error');
268+
263269
$result = $this->auth->unauthenticated(new ServerRequest(), $this->response);
264270
}
265271

0 commit comments

Comments
 (0)