Skip to content

Commit 7b1e5b7

Browse files
committed
add running test, fix undefined id on get ID
1 parent 7159410 commit 7b1e5b7

File tree

4 files changed

+61
-40
lines changed

4 files changed

+61
-40
lines changed

README.md

+23-9
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ $ composer update
2525
### Configuration - Loading the config service
2626

2727
in config.ini or in any config file
28-
```ini
28+
```ini
2929
[jwtAuth]
3030

3131
; JWT Secret Key
@@ -65,7 +65,7 @@ $di = new FactoryDefault();
6565

6666
/**
6767
* IMPORTANT:
68-
* You must set "config" service that will load the configuration file.
68+
* You must set "config" service that will load the configuration file.
6969
*/
7070
$config = new ConfigIni( APP_PATH . "app/config/config.ini");
7171
$di->set(
@@ -137,11 +137,11 @@ $auth->onUnauthorized(function($authMicro, $app) {
137137
$response = $app["response"];
138138
$response->setStatusCode(401, 'Unauthorized');
139139
$response->setContentType("application/json");
140-
140+
141141
// to get the error messages
142142
$response->setContent(json_encode([$authMicro->getMessages()[0]]));
143143
$response->send();
144-
144+
145145
// return false to stop the execution
146146
return false;
147147
});
@@ -152,18 +152,18 @@ If you want an additional checking on the authentication, like intentionally exp
152152
$auth->onCheck(function($auth) {
153153
// to get the payload
154154
$data = $auth->data();
155-
155+
156156
if($data['iat'] <= strtotime('-1 day')) ) {
157157
// return false to invalidate the authentication
158158
return false;
159159
}
160-
160+
161161
});
162162
```
163163

164164
### The Auth service
165165

166-
You can access the middleware by calling the "auth" service.
166+
You can access the middleware by calling the "auth" service.
167167
```php
168168
print_r( $app['auth']->data() );
169169

@@ -181,8 +181,8 @@ AuthMicro::$diName = 'jwtAuth';
181181

182182
In your controller or route handler
183183
```php
184-
$payload = [
185-
'sub' => $user->id,
184+
$payload = [
185+
'sub' => $user->id,
186186
'email' => $user->email,
187187
'username' => $user->username,
188188
'role' => 'admin',
@@ -213,3 +213,17 @@ Dmkit\Phalcon\Auth\TokenGetter\TokenGetter.php and its adapters - does the parsi
213213
### JWT
214214
Phalcon JWT Auth uses the Firebase JWT library. To learn more about it and JSON Web Tokens in general, visit: https://github.com/firebase/php-jwt
215215
https://jwt.io/introduction/
216+
217+
### Tests
218+
Install PHPUnit https://phpunit.de/getting-started.html
219+
```php
220+
$ phpunit --configuration phpunit.xml.dist
221+
PHPUnit 5.6.5 by Sebastian Bergmann and contributors.
222+
223+
......["missing token"].["members option"].["members put"].["members put"].["Expired token"].["members post"].... 15 / 15 (100%)
224+
225+
Time: 73 ms, Memory: 10.00MB
226+
227+
OK (15 tests, 27 assertions)
228+
229+
```

src/Phalcon/Auth/Adapter.php

+9-9
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ abstract class Adapter implements AdapterInterface
1616

1717
// window time for jwt to expire
1818
protected $leeway;
19-
19+
2020
// supported algs are on JWT::$supported_algs
2121
protected $algo = 'HS256';
2222

@@ -28,7 +28,7 @@ abstract class Adapter implements AdapterInterface
2828
* @param int $mins
2929
*
3030
* @return int
31-
*/
31+
*/
3232
public function minToSec(int $mins)
3333
{
3434
return (60 * $mins);
@@ -39,8 +39,8 @@ public function minToSec(int $mins)
3939
*
4040
* @param int $mins
4141
*
42-
*/
43-
public function setLeeway(int $mins)
42+
*/
43+
public function setLeeway(int $mins)
4444
{
4545
$this->leeway = $this->minToSec($mins);
4646
}
@@ -51,7 +51,7 @@ public function setLeeway(int $mins)
5151
*
5252
* @param int $mins
5353
*
54-
*/
54+
*/
5555
public function setAlgo(string $alg) {
5656
$this->algo = $alg;
5757
}
@@ -63,7 +63,7 @@ public function setAlgo(string $alg) {
6363
* @param string $key
6464
*
6565
* @return array
66-
*/
66+
*/
6767
protected function decode($token, $key)
6868
{
6969
try {
@@ -72,7 +72,7 @@ protected function decode($token, $key)
7272
}
7373

7474
$payload = (array) JWT::decode($token, $key, [$this->algo]);
75-
75+
7676
return $payload;
7777

7878
} catch(\Exception $e) {
@@ -104,7 +104,7 @@ protected function encode($payload, $key)
104104
* @param string $msg
105105
*
106106
*/
107-
public function appendMessage(string $msg)
107+
public function appendMessage(string $msg)
108108
{
109109
$this->errorMsgs[] = $msg;
110110
}
@@ -126,7 +126,7 @@ public function getMessages()
126126
*/
127127
public function id()
128128
{
129-
return $this->payload['sub'] ?? $this->payload['id'];
129+
return $this->payload['sub'] ?? $this->payload['id'] ?? NULL;
130130
}
131131

132132
/**

tests/Phalcon/AuthTest.php

+8-1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@ public function testMake()
4242
$this->assertEquals($this->jwt, $token);
4343
}
4444

45+
public function testWithEmptyAuth()
46+
{
47+
$auth = new Auth;
48+
$auth->id();
49+
$this->assertEquals(NULL, $auth->id());
50+
}
51+
4552
public function testCheckSuccess()
4653
{
4754
$response = $this->createMock(RequestInterface::class);
@@ -113,5 +120,5 @@ public function testCheckFail()
113120

114121
$this->assertEquals($expected_errors, $auth->getMessages());
115122
}
116-
123+
117124
}

tests/Phalcon/MiddlewareMicroTest.php

+21-21
Original file line numberDiff line numberDiff line change
@@ -43,40 +43,40 @@ public function setUp()
4343
$this->middleware = new AuthMicro($this->app);
4444

4545
$app = $this->app;
46-
47-
$this->app->get('/', function() use($app) {
46+
47+
$this->app->get('/', function() use($app) {
4848
$response = $app["response"];
4949
$response->setStatusCode(200);
5050
$response->setContentType("application/json");
5151
$response->setContent(json_encode(['index get']));
5252
$response->send();
5353
});
5454

55-
$this->app->get('/members', function() use($app) {
55+
$this->app->get('/members', function() use($app) {
5656
$response = $app["response"];
5757
$response->setStatusCode(200);
5858
$response->setContentType("application/json");
5959
$response->setContent(json_encode(['members get']));
6060
$response->send();
6161
});
6262

63-
$this->app->post('/members', function() use($app) {
63+
$this->app->post('/members', function() use($app) {
6464
$response = $app["response"];
6565
$response->setStatusCode(200);
6666
$response->setContentType("application/json");
6767
$response->setContent(json_encode(['members post']));
6868
$response->send();
6969
});
7070

71-
$this->app->put('/members', function() use($app) {
71+
$this->app->put('/members', function() use($app) {
7272
$response = $app["response"];
7373
$response->setStatusCode(200);
7474
$response->setContentType("application/json");
7575
$response->setContent(json_encode(['members put']));
7676
$response->send();
7777
});
7878

79-
$this->app->options('/members', function() use($app) {
79+
$this->app->options('/members', function() use($app) {
8080
$response = $app["response"];
8181
$response->setStatusCode(204);
8282
$response->setContentType("application/json");
@@ -90,11 +90,11 @@ public function testLookForTokenFail()
9090
{
9191
// override for testing
9292
$_SERVER['REQUEST_URI'] = '/members';
93-
93+
9494
// call this on test methods instead
9595
$this->app->handle('/members');
9696

97-
$this->assertEquals('401 Unauthorized', $this->app['response']->getStatusCode());
97+
$this->assertEquals(401, $this->app['response']->getStatusCode());
9898
$this->assertEquals('["missing token"]', $this->app['response']->getContent());
9999
}
100100

@@ -105,22 +105,22 @@ public function testIgnoreOptionMethod()
105105
$_SERVER["REQUEST_METHOD"] = "OPTIONS";
106106

107107
$this->middleware->setIgnoreOptionsMethod();
108-
108+
109109
// call this on test methods instead
110110
$this->app->handle('/members');
111111

112-
$this->assertEquals('204 No Content', $this->app['response']->getStatusCode());
112+
$this->assertEquals(204, $this->app['response']->getStatusCode());
113113
}
114114

115115
public function testIgnoreUri()
116116
{
117117
$_SERVER['REQUEST_URI'] = '/members';
118118
$_SERVER["REQUEST_METHOD"] = "PUT";
119-
119+
120120
// call this on test methods instead
121121
$this->app->handle('/members');
122122

123-
$this->assertEquals('200 OK', $this->app['response']->getStatusCode());
123+
$this->assertEquals(200, $this->app['response']->getStatusCode());
124124
$this->assertEquals('["members put"]', $this->app['response']->getContent());
125125
}
126126

@@ -133,12 +133,12 @@ public function testIgnoreUriWithToken()
133133

134134
$jwt = JWT::encode($payload, $this->config['secretKey']);
135135

136-
$_GET['token'] = $jwt;
137-
136+
$_GET['_token'] = $jwt;
137+
138138
// call this on test methods instead
139139
$this->app->handle('/members');
140140

141-
$this->assertEquals('200 OK', $this->app['response']->getStatusCode());
141+
$this->assertEquals(200, $this->app['response']->getStatusCode());
142142
$this->assertEquals('["members put"]', $this->app['response']->getContent());
143143
$this->assertEquals($payload['sub'], $this->app['auth']->id());
144144
}
@@ -153,12 +153,12 @@ public function testPassedExpiredToken()
153153
$payload['exp'] = -20;
154154
$jwt = JWT::encode($payload, $this->config['secretKey']);
155155

156-
$_GET['token'] = $jwt;
157-
156+
$_GET['_token'] = $jwt;
157+
158158
// call this on test methods instead
159159
$this->app->handle('/members');
160160

161-
$this->assertEquals('401 Unauthorized', $this->app['response']->getStatusCode());
161+
$this->assertEquals(401, $this->app['response']->getStatusCode());
162162
$this->assertEquals('["Expired token"]', $this->app['response']->getContent());
163163
}
164164

@@ -171,12 +171,12 @@ public function testPasssedValidToken()
171171
// let's expired the token
172172
$jwt = JWT::encode($payload, $this->config['secretKey']);
173173

174-
$_GET['token'] = $jwt;
175-
174+
$_GET['_token'] = $jwt;
175+
176176
// call this on test methods instead
177177
$this->app->handle('/members');
178178

179-
$this->assertEquals('200 OK', $this->app['response']->getStatusCode());
179+
$this->assertEquals(200, $this->app['response']->getStatusCode());
180180
$this->assertEquals('["members post"]', $this->app['response']->getContent());
181181

182182
// make sure data is correct

0 commit comments

Comments
 (0)