bakaphp /
phalcon-api
| 1 | <?php |
||||
| 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) {
|
||||
|
0 ignored issues
–
show
|
|||||
| 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); |
||||
|
0 ignored issues
–
show
It seems like
$request->getClientAddress() can also be of type boolean; however, parameter $userIp of Baka\Auth\Models\Sessions::check() does only seem to accept string, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 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 |
This check looks for imports that have been defined, but are not used in the scope.