1 | <?php |
||
0 ignored issues
–
show
Coding Style
introduced
by
![]() |
|||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace Gewaer\Middleware; |
||
6 | |||
7 | use Gewaer\Exception\ModelException; |
||
8 | use Phalcon\Mvc\Micro; |
||
9 | use Phalcon\Mvc\Micro\MiddlewareInterface; |
||
10 | use Baka\Auth\Models\Sessions; |
||
11 | use Gewaer\Models\Users; |
||
12 | use Phalcon\Http\Request; |
||
13 | use Exception; |
||
14 | |||
15 | /** |
||
16 | * Class TokenValidationMiddleware |
||
17 | * |
||
18 | * @package Gewaer\Middleware |
||
19 | */ |
||
20 | class TokenValidationMiddleware implements MiddlewareInterface |
||
21 | { |
||
22 | /** |
||
23 | * @param Micro $api |
||
24 | * |
||
25 | * @return bool |
||
26 | * @throws ModelException |
||
27 | */ |
||
28 | public function call(Micro $api) |
||
29 | { |
||
30 | $config = $api->getService('config'); |
||
31 | |||
32 | $auth = $api->getService('auth'); |
||
33 | // to get the payload |
||
34 | $data = $auth->data(); |
||
35 | |||
36 | $api->getDI()->setShared( |
||
37 | 'userData', |
||
38 | function () use ($config, $data) { |
||
39 | $session = new Sessions(); |
||
40 | $request = new Request(); |
||
41 | |||
42 | if (!empty($data) && !empty($data['sessionId'])) { |
||
43 | //user |
||
44 | if (!$user = Users::getByEmail($data['email'])) { |
||
45 | throw new Exception('User not found'); |
||
46 | } |
||
47 | |||
48 | return $session->check($user, $data['sessionId'], $request->getClientAddress(), 1); |
||
49 | } else { |
||
50 | throw new Exception('User not found'); |
||
51 | } |
||
52 | } |
||
53 | ); |
||
54 | |||
55 | if (!empty($data) && $data['iat'] <= strtotime('-10 seconds')) { |
||
56 | // return false to invalidate the authentication |
||
57 | //throw new Exception("Old Request"); |
||
58 | } |
||
59 | |||
60 | return true; |
||
61 | } |
||
62 | } |
||
63 |