Skip to content

Commit c2948f9

Browse files
committed
Update code for Cake 4
1 parent 3111bf6 commit c2948f9

File tree

4 files changed

+44
-80
lines changed

4 files changed

+44
-80
lines changed

phpunit.xml.dist

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
colors="true"
44
processIsolation="false"
55
stopOnFailure="false"
6-
syntaxCheck="false"
76
bootstrap="./tests/bootstrap.php"
87
>
98
<php>

src/Auth/JwtAuthenticate.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
<?php
2+
declare(strict_types=1);
3+
24
namespace ADmad\JwtAuth\Auth;
35

46
use Cake\Auth\BaseAuthenticate;
@@ -26,7 +28,7 @@
2628
* ]);
2729
* ```
2830
*
29-
* @copyright 2015-2018 ADmad
31+
* @copyright 2015-Present ADmad
3032
* @license MIT
3133
*
3234
* @see http://jwt.io
@@ -87,7 +89,7 @@ class JwtAuthenticate extends BaseAuthenticate
8789
* used on this request.
8890
* @param array $config Array of config to use.
8991
*/
90-
public function __construct(ComponentRegistry $registry, $config)
92+
public function __construct(ComponentRegistry $registry, array $config)
9193
{
9294
$defaultConfig = [
9395
'cookie' => false,
@@ -100,10 +102,6 @@ public function __construct(ComponentRegistry $registry, $config)
100102
'key' => null,
101103
];
102104

103-
if (!class_exists(UnauthorizedException::class)) {
104-
$defaultConfig['unauthenticatedException'] = 'Cake\Network\Exception\UnauthorizedException';
105-
}
106-
107105
$this->setConfig($defaultConfig);
108106

109107
if (empty($config['allowedAlgs'])) {
@@ -149,7 +147,7 @@ public function getUser(ServerRequest $request)
149147
return false;
150148
}
151149

152-
$user = $this->_findUser($payload->sub);
150+
$user = $this->_findUser((string)$payload->sub);
153151
if (!$user) {
154152
return false;
155153
}
@@ -166,7 +164,7 @@ public function getUser(ServerRequest $request)
166164
*
167165
* @return object|null Payload object on success, null on failurec
168166
*/
169-
public function getPayload($request = null)
167+
public function getPayload(?ServerRequest $request = null)
170168
{
171169
if (!$request) {
172170
return $this->_payload;
@@ -189,7 +187,7 @@ public function getPayload($request = null)
189187
*
190188
* @return string|null Token string if found else null.
191189
*/
192-
public function getToken($request = null)
190+
public function getToken(?ServerRequest $request = null)
193191
{
194192
$config = $this->_config;
195193

@@ -230,7 +228,7 @@ public function getToken($request = null)
230228
*
231229
* @return object|null The JWT's payload as a PHP object, null on failure.
232230
*/
233-
protected function _decode($token)
231+
protected function _decode(string $token)
234232
{
235233
$config = $this->_config;
236234
try {
@@ -247,6 +245,8 @@ protected function _decode($token)
247245
}
248246
$this->_error = $e;
249247
}
248+
249+
return null;
250250
}
251251

252252
/**

tests/TestCase/Auth/JwtAuthenticateTest.php

Lines changed: 33 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
<?php
2+
declare(strict_types=1);
3+
24
namespace ADmad\JwtAuth\Auth\Test\TestCase\Auth;
35

46
use ADmad\JwtAuth\Auth\JwtAuthenticate;
@@ -7,10 +9,11 @@
79
use Cake\Http\Exception\UnauthorizedException;
810
use Cake\Http\Response;
911
use Cake\Http\ServerRequest;
12+
use Cake\I18n\FrozenTime;
1013
use Cake\I18n\Time;
11-
use Cake\ORM\TableRegistry;
1214
use Cake\TestSuite\TestCase;
1315
use Cake\Utility\Security;
16+
use DomainException;
1417
use Firebase\JWT\JWT;
1518

1619
/**
@@ -19,16 +22,16 @@
1922
class JwtAuthenticateTest extends TestCase
2023
{
2124
public $fixtures = [
22-
'plugin.ADmad\JwtAuth.users',
23-
'plugin.ADmad\JwtAuth.groups',
25+
'plugin.ADmad\JwtAuth.Users',
26+
'plugin.ADmad\JwtAuth.Groups',
2427
];
2528

2629
/**
2730
* setup.
2831
*
2932
* @return void
3033
*/
31-
public function setUp()
34+
public function setUp(): void
3235
{
3336
parent::setUp();
3437

@@ -69,7 +72,7 @@ public function testConfig()
6972
*/
7073
public function testAuthenticateTokenParameter()
7174
{
72-
$request = new ServerRequest('posts/index');
75+
$request = new ServerRequest();
7376

7477
$result = $this->auth->getUser($request, $this->response);
7578
$this->assertFalse($result);
@@ -79,19 +82,19 @@ public function testAuthenticateTokenParameter()
7982
'group_id' => 1,
8083
'user_name' => 'admad',
8184
'email' => '[email protected]',
82-
'created' => new Time('2014-03-17 01:18:23'),
83-
'updated' => new Time('2014-03-17 01:20:31'),
85+
'created' => new FrozenTime('2014-03-17 01:18:23'),
86+
'updated' => new FrozenTime('2014-03-17 01:20:31'),
8487
];
85-
$request = new ServerRequest('posts/index?token=' . $this->token);
88+
$request = new ServerRequest(['url' => 'posts/index?token=' . $this->token]);
8689
$result = $this->auth->getUser($request, $this->response);
8790
$this->assertEquals($expected, $result);
8891

8992
$this->auth->setConfig('parameter', 'tokenname');
90-
$request = new ServerRequest('posts/index?tokenname=' . $this->token);
93+
$request = new ServerRequest(['url' => 'posts/index?tokenname=' . $this->token]);
9194
$result = $this->auth->getUser($request, $this->response);
9295
$this->assertEquals($expected, $result);
9396

94-
$request = new ServerRequest('posts/index?wrongtoken=' . $this->token);
97+
$request = new ServerRequest(['url' => 'posts/index?wrongtoken=' . $this->token]);
9598
$result = $this->auth->getUser($request, $this->response);
9699
$this->assertFalse($result);
97100
}
@@ -103,15 +106,15 @@ public function testAuthenticateTokenParameter()
103106
*/
104107
public function testAuthenticateTokenHeader()
105108
{
106-
$request = new ServerRequest('posts/index');
109+
$request = new ServerRequest();
107110

108111
$expected = [
109112
'id' => 1,
110113
'group_id' => 1,
111114
'user_name' => 'admad',
112115
'email' => '[email protected]',
113-
'created' => new Time('2014-03-17 01:18:23'),
114-
'updated' => new Time('2014-03-17 01:20:31'),
116+
'created' => new FrozenTime('2014-03-17 01:18:23'),
117+
'updated' => new FrozenTime('2014-03-17 01:20:31'),
115118
];
116119
$request = $request->withEnv('HTTP_AUTHORIZATION', 'Bearer ' . $this->token);
117120
$result = $this->auth->getUser($request, $this->response);
@@ -134,7 +137,7 @@ public function testAuthenticateTokenHeader()
134137
*/
135138
public function testAuthenticateNoHeaderWithParameterDisabled()
136139
{
137-
$request = new ServerRequest('posts/index');
140+
$request = new ServerRequest();
138141

139142
$this->auth = new JwtAuthenticate($this->Registry, [
140143
'userModel' => 'Users',
@@ -144,7 +147,7 @@ public function testAuthenticateNoHeaderWithParameterDisabled()
144147
$result = $this->auth->getUser($request, $this->response);
145148
$this->assertFalse($result);
146149

147-
$request = new ServerRequest('posts/index?token=' . $this->token);
150+
$request = new ServerRequest(['url' => 'posts/index?token=' . $this->token]);
148151
$result = $this->auth->getUser($request, $this->response);
149152
$this->assertFalse($result);
150153
}
@@ -164,12 +167,12 @@ public function testQueryDatasourceFalse()
164167
$token = JWT::encode($expected, Security::getSalt());
165168
$this->auth->setConfig('queryDatasource', false);
166169

167-
$request = new ServerRequest('posts/index');
170+
$request = new ServerRequest();
168171
$request = $request->withEnv('HTTP_AUTHORIZATION', 'Bearer ' . $token);
169172
$result = $this->auth->getUser($request, $this->response);
170173
$this->assertEquals($expected, $result);
171174

172-
$request = new ServerRequest('posts/index?token=' . $token);
175+
$request = new ServerRequest(['url' => 'posts/index?token=' . $token]);
173176
$result = $this->auth->getUser($request, $this->response);
174177
$this->assertEquals($expected, $result);
175178
}
@@ -183,48 +186,12 @@ public function testWithValidTokenButNoUserInDb()
183186
{
184187
$token = JWT::encode(['id' => 4], Security::getSalt());
185188

186-
$request = new ServerRequest('posts/index');
189+
$request = new ServerRequest();
187190
$request = $request->withEnv('HTTP_AUTHORIZATION', 'Bearer ' . $token);
188191
$result = $this->auth->getUser($request, $this->response);
189192
$this->assertFalse($result);
190193

191-
$request = new ServerRequest('posts/index?token=' . $token);
192-
$result = $this->auth->getUser($request, $this->response);
193-
$this->assertFalse($result);
194-
}
195-
196-
/**
197-
* test contain.
198-
*
199-
* @return void
200-
*/
201-
public function testFindUserWithContain()
202-
{
203-
$request = new ServerRequest('posts/index');
204-
205-
$expected = [
206-
'id' => 1,
207-
'group_id' => 1,
208-
'user_name' => 'admad',
209-
'email' => '[email protected]',
210-
'created' => new Time('2014-03-17 01:18:23'),
211-
'updated' => new Time('2014-03-17 01:20:31'),
212-
'group' => [
213-
'id' => 1,
214-
'title' => 'admin',
215-
],
216-
];
217-
$request = $request->withEnv('HTTP_AUTHORIZATION', 'Bearer ' . $this->token);
218-
219-
$this->auth->setConfig('contain', ['Groups']);
220-
$table = TableRegistry::get('Users');
221-
$table->belongsTo('Groups');
222-
223-
$result = $this->auth->getUser($request, $this->response);
224-
$this->assertEquals($expected, $result);
225-
226-
$this->expectException('UnexpectedValueException');
227-
$request = $request->withEnv('HTTP_AUTHORIZATION', 'Bearer foobar');
194+
$request = new ServerRequest(['url' => 'posts/index?token=' . $token]);
228195
$result = $this->auth->getUser($request, $this->response);
229196
$this->assertFalse($result);
230197
}
@@ -242,15 +209,15 @@ public function testAuthenticated()
242209
/**
243210
* test that with debug off for invalid token exception from JWT::decode()
244211
* is re-thrown.
245-
*
246-
* @expectedException DomainException
247212
*/
248213
public function testExceptionForInvalidToken()
249214
{
250-
$request = new ServerRequest('posts/index');
215+
$this->expectException(DomainException::class);
216+
217+
$request = new ServerRequest();
251218
$request = $request->withEnv('HTTP_AUTHORIZATION', 'Bearer this.is.invalid');
252219

253-
$result = $this->auth->getUser($request, $this->response);
220+
$this->auth->getUser($request, $this->response);
254221
}
255222

256223
/**
@@ -289,7 +256,7 @@ public function testUnauthenticatedNoException()
289256
public function testWithInvalidToken()
290257
{
291258
Configure::write('debug', false);
292-
$request = new ServerRequest('posts/index');
259+
$request = new ServerRequest();
293260

294261
$request = $request->withEnv('HTTP_AUTHORIZATION', 'Bearer this.is.invalid');
295262
$result = $this->auth->getUser($request, $this->response);
@@ -312,12 +279,12 @@ public function testCustomKey()
312279
$payload = ['sub' => 100];
313280
$token = Jwt::encode($payload, $key);
314281

315-
$request = new ServerRequest('posts/index');
282+
$request = new ServerRequest();
316283
$request = $request->withEnv('HTTP_AUTHORIZATION', 'Bearer ' . $token);
317284
$result = $auth->getUser($request, $this->response);
318285
$this->assertEquals($payload, $result);
319286

320-
$request = new ServerRequest('posts/index?token=' . $token);
287+
$request = new ServerRequest(['url' => '/posts/index?token=' . $token]);
321288
$result = $auth->getUser($request, $this->response);
322289
$this->assertEquals($payload, $result);
323290
}
@@ -329,7 +296,7 @@ public function testCustomKey()
329296
*/
330297
public function testAuthenticateCookie()
331298
{
332-
$request = new ServerRequest('posts/index');
299+
$request = new ServerRequest();
333300

334301
$this->auth = new JwtAuthenticate($this->Registry, [
335302
'userModel' => 'Users',
@@ -350,7 +317,7 @@ public function testAuthenticateCookie()
350317

351318
$request = new ServerRequest([
352319
'url' => 'posts/index',
353-
'cookies' => ['jwt' => $this->token]
320+
'cookies' => ['jwt' => $this->token],
354321
]);
355322

356323
$result = $this->auth->getUser($request, $this->response);
@@ -359,15 +326,15 @@ public function testAuthenticateCookie()
359326
$this->auth->setConfig('cookie', 'tokenname');
360327
$request = new ServerRequest([
361328
'url' => 'posts/index',
362-
'cookies' => ['tokenname' => $this->token]
329+
'cookies' => ['tokenname' => $this->token],
363330
]);
364331

365332
$result = $this->auth->getUser($request, $this->response);
366333
$this->assertEquals($expected, $result);
367334

368335
$request = new ServerRequest([
369336
'url' => 'posts/index',
370-
'cookies' => ['wrongtoken' => $this->token]
337+
'cookies' => ['wrongtoken' => $this->token],
371338
]);
372339

373340
$result = $this->auth->getUser($request, $this->response);

tests/bootstrap.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?php
2-
use Cake\Core\Plugin;
2+
declare(strict_types=1);
33

44
/*
55
* Test suite bootstrap
@@ -22,5 +22,3 @@
2222
unset($findRoot);
2323
chdir($root);
2424
require $root . '/vendor/cakephp/cakephp/tests/bootstrap.php';
25-
26-
Plugin::load('ADmad/JwtAuth', ['path' => dirname(dirname(__FILE__)) . DS]);

0 commit comments

Comments
 (0)