Skip to content

Commit 23b77e8

Browse files
committed
Merge pull request #26 from ADmad/analysis-qMEBvq
Applied fixes from StyleCI
2 parents 9331bd0 + dbf90b6 commit 23b77e8

File tree

5 files changed

+43
-39
lines changed

5 files changed

+43
-39
lines changed

src/Auth/JwtAuthenticate.php

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
use Cake\Core\Configure;
77
use Cake\Network\Request;
88
use Cake\Network\Response;
9-
use Cake\ORM\TableRegistry;
109
use Cake\Utility\Security;
1110
use Exception;
1211
use Firebase\JWT\JWT;
@@ -28,28 +27,28 @@
2827
*
2928
* @copyright 2015 ADmad
3029
* @license MIT
30+
*
3131
* @see http://jwt.io
3232
* @see http://tools.ietf.org/html/draft-ietf-oauth-json-web-token
3333
*/
3434
class JwtAuthenticate extends BaseAuthenticate
3535
{
36-
3736
/**
38-
* Parsed token
37+
* Parsed token.
3938
*
4039
* @var string|null
4140
*/
4241
protected $_token;
4342

4443
/**
45-
* Payload data
44+
* Payload data.
4645
*
4746
* @var object|null
4847
*/
4948
protected $_payload;
5049

5150
/**
52-
* Exception
51+
* Exception.
5352
*
5453
* @var \Exception
5554
*/
@@ -104,6 +103,7 @@ public function __construct(ComponentRegistry $registry, $config)
104103
*
105104
* @param \Cake\Network\Request $request The request object.
106105
* @param \Cake\Network\Response $response Response object.
106+
*
107107
* @return bool|array User record array or false on failure.
108108
*/
109109
public function authenticate(Request $request, Response $response)
@@ -115,6 +115,7 @@ public function authenticate(Request $request, Response $response)
115115
* Get user record based on info available in JWT.
116116
*
117117
* @param \Cake\Network\Request $request Request object.
118+
*
118119
* @return bool|array User record array or false on failure.
119120
*/
120121
public function getUser(Request $request)
@@ -135,13 +136,15 @@ public function getUser(Request $request)
135136
}
136137

137138
unset($user[$this->_config['fields']['password']]);
139+
138140
return $user;
139141
}
140142

141143
/**
142-
* Get payload data
144+
* Get payload data.
143145
*
144146
* @param \Cake\Network\Request|null $request Request instance or null
147+
*
145148
* @return object|null Payload object on success, null on failurec
146149
*/
147150
public function getPayload($request = null)
@@ -164,6 +167,7 @@ public function getPayload($request = null)
164167
* Get token from header or query string.
165168
*
166169
* @param \Cake\Network\Request|null $request Request object.
170+
*
167171
* @return string|null Token string if found else null.
168172
*/
169173
public function getToken($request = null)
@@ -190,12 +194,14 @@ public function getToken($request = null)
190194
* Decode JWT token.
191195
*
192196
* @param string $token JWT token to decode.
197+
*
193198
* @return object|null The JWT's payload as a PHP object, null on failure.
194199
*/
195200
protected function _decode($token)
196201
{
197202
try {
198203
$payload = JWT::decode($token, Security::salt(), $this->_config['allowedAlgs']);
204+
199205
return $payload;
200206
} catch (Exception $e) {
201207
if (Configure::read('debug')) {
@@ -212,9 +218,11 @@ protected function _decode($token)
212218
*
213219
* @param \Cake\Network\Request $request A request object.
214220
* @param \Cake\Network\Response $response A response object.
215-
* @return void
221+
*
216222
* @throws \Cake\Network\Exception\UnauthorizedException Or any other
217223
* configured exception.
224+
*
225+
* @return void
218226
*/
219227
public function unauthenticated(Request $request, Response $response)
220228
{

tests/Fixture/GroupsFixture.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,23 @@
44
use Cake\TestSuite\Fixture\TestFixture;
55

66
/**
7-
* Class GroupsFixture
8-
*
7+
* Class GroupsFixture.
98
*/
109
class GroupsFixture extends TestFixture
1110
{
12-
1311
/**
14-
* fields property
12+
* fields property.
1513
*
1614
* @var array
1715
*/
1816
public $fields = [
1917
'id' => ['type' => 'integer'],
2018
'title' => ['type' => 'string'],
21-
'_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
19+
'_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]],
2220
];
2321

2422
/**
25-
* records property
23+
* records property.
2624
*
2725
* @var array
2826
*/

tests/Fixture/UsersFixture.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
class UsersFixture extends TestFixture
77
{
88
/**
9-
* fields property
9+
* fields property.
1010
*
1111
* @var array
1212
*/
@@ -18,29 +18,29 @@ class UsersFixture extends TestFixture
1818
'password' => ['type' => 'string', 'null' => false],
1919
'created' => 'datetime',
2020
'updated' => 'datetime',
21-
'_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
21+
'_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]],
2222
];
2323

2424
/**
25-
* records property
25+
* records property.
2626
*
2727
* @var array
2828
*/
2929
public $records = [
3030
[
3131
'group_id' => 1, 'user_name' => 'admad',
3232
'email' => 'admad@example.com', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
33-
'created' => '2014-03-17 01:18:23', 'updated' => '2014-03-17 01:20:31'
33+
'created' => '2014-03-17 01:18:23', 'updated' => '2014-03-17 01:20:31',
3434
],
3535
[
3636
'group_id' => 2, 'user_name' => 'mark',
3737
'email' => 'mark@example.com', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
38-
'created' => '2014-03-17 01:16:23', 'updated' => '2014-03-17 01:18:31'
38+
'created' => '2014-03-17 01:16:23', 'updated' => '2014-03-17 01:18:31',
3939
],
4040
[
4141
'group_id' => 2, 'user_name' => 'jose',
4242
'email' => 'jose@example.com', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99',
43-
'created' => '2014-03-17 01:20:23', 'updated' => '2014-03-17 01:22:31'
43+
'created' => '2014-03-17 01:20:23', 'updated' => '2014-03-17 01:22:31',
4444
],
4545
];
4646
}

tests/TestCase/Auth/JwtAuthenticateTest.php

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
namespace ADmad\JwtAuth\Auth\Test\TestCase\Auth;
33

44
use ADmad\JwtAuth\Auth\JwtAuthenticate;
5-
use Cake\Controller\Component\AuthComponent;
6-
use Cake\Controller\Controller;
75
use Cake\Core\Configure;
86
use Cake\I18n\Time;
97
use Cake\Network\Request;
@@ -14,7 +12,7 @@
1412
use Firebase\JWT\JWT;
1513

1614
/**
17-
* Test case for JwtAuthentication
15+
* Test case for JwtAuthentication.
1816
*/
1917
class JwtAuthenticateTest extends TestCase
2018
{
@@ -24,7 +22,7 @@ class JwtAuthenticateTest extends TestCase
2422
];
2523

2624
/**
27-
* setup
25+
* setup.
2826
*
2927
* @return void
3028
*/
@@ -36,7 +34,7 @@ public function setUp()
3634

3735
$this->Registry = $this->getMock('Cake\Controller\ComponentRegistry');
3836
$this->auth = new JwtAuthenticate($this->Registry, [
39-
'userModel' => 'Users'
37+
'userModel' => 'Users',
4038
]);
4139

4240
$this->token = JWT::encode(['sub' => 1], Security::salt());
@@ -45,7 +43,7 @@ public function setUp()
4543
}
4644

4745
/**
48-
* test authenticate token as query parameter
46+
* test authenticate token as query parameter.
4947
*
5048
* @return void
5149
*/
@@ -62,7 +60,7 @@ public function testAuthenticateTokenParameter()
6260
'user_name' => 'admad',
6361
'email' => 'admad@example.com',
6462
'created' => new Time('2014-03-17 01:18:23'),
65-
'updated' => new Time('2014-03-17 01:20:31')
63+
'updated' => new Time('2014-03-17 01:20:31'),
6664
];
6765
$request = new Request('posts/index?token=' . $this->token);
6866
$result = $this->auth->getUser($request, $this->response);
@@ -75,7 +73,7 @@ public function testAuthenticateTokenParameter()
7573
}
7674

7775
/**
78-
* test authenticate token as request header
76+
* test authenticate token as request header.
7977
*
8078
* @return void
8179
*/
@@ -89,7 +87,7 @@ public function testAuthenticateTokenHeader()
8987
'user_name' => 'admad',
9088
'email' => 'admad@example.com',
9189
'created' => new Time('2014-03-17 01:18:23'),
92-
'updated' => new Time('2014-03-17 01:20:31')
90+
'updated' => new Time('2014-03-17 01:20:31'),
9391
];
9492
$request->env('HTTP_AUTHORIZATION', 'Bearer ' . $this->token);
9593
$result = $this->auth->getUser($request, $this->response);
@@ -113,7 +111,7 @@ public function testQueryDatasourceFalse()
113111
$expected = [
114112
'id' => 99,
115113
'username' => 'ADmad',
116-
'group' => ['name' => 'admin']
114+
'group' => ['name' => 'admin'],
117115
];
118116
$request->env(
119117
'HTTP_AUTHORIZATION',
@@ -125,7 +123,7 @@ public function testQueryDatasourceFalse()
125123
}
126124

127125
/**
128-
* test for valid token but no matching user found in db
126+
* test for valid token but no matching user found in db.
129127
*
130128
* @return void
131129
*/
@@ -140,7 +138,7 @@ public function testWithValidTokenButNoUserInDb()
140138
}
141139

142140
/**
143-
* test contain
141+
* test contain.
144142
*
145143
* @return void
146144
*/
@@ -157,8 +155,8 @@ public function testFindUserWithContain()
157155
'updated' => new Time('2014-03-17 01:20:31'),
158156
'group' => [
159157
'id' => 1,
160-
'title' => 'admin'
161-
]
158+
'title' => 'admin',
159+
],
162160
];
163161
$request->env('HTTP_AUTHORIZATION', 'Bearer ' . $this->token);
164162

@@ -176,7 +174,7 @@ public function testFindUserWithContain()
176174
}
177175

178176
/**
179-
* Test that authenticated() always returns false
177+
* Test that authenticated() always returns false.
180178
*
181179
* @return void
182180
*/
@@ -187,7 +185,7 @@ public function testAuthenticated()
187185

188186
/**
189187
* test that with debug off for invalid token exception from JWT::decode()
190-
* is re-thrown
188+
* is re-thrown.
191189
*
192190
* @expectedException DomainException
193191
*/
@@ -213,7 +211,7 @@ public function testUnauthenticated()
213211

214212
/**
215213
* test unauthenticated() doesn't throw exception is config `unauthenticatedException`
216-
* is set to falsey value
214+
* is set to falsey value.
217215
*/
218216
public function testUnauthenticatedNoException()
219217
{
@@ -223,7 +221,7 @@ public function testUnauthenticatedNoException()
223221

224222
/**
225223
* test that getUser() returns false instead of throwing exception with
226-
* invalid token when debug is off
224+
* invalid token when debug is off.
227225
*
228226
* @return void
229227
*/

tests/bootstrap.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
use Cake\Core\Plugin;
33

4-
/**
4+
/*
55
* Test suite bootstrap
66
*
77
* This function is used to find the location of CakePHP whether CakePHP
@@ -16,7 +16,7 @@
1616
return $root;
1717
}
1818
} while ($root !== $lastRoot);
19-
throw new Exception("Cannot find the root of the application, unable to run tests");
19+
throw new Exception('Cannot find the root of the application, unable to run tests');
2020
};
2121
$root = $findRoot(__FILE__);
2222
unset($findRoot);

0 commit comments

Comments
 (0)