|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* MikoPBX - free phone system for small business |
|
4
|
|
|
* Copyright (C) 2017-2020 Alexey Portnov and Nikolay Beketov |
|
5
|
|
|
* |
|
6
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
7
|
|
|
* it under the terms of the GNU General Public License as published by |
|
8
|
|
|
* the Free Software Foundation; either version 3 of the License, or |
|
9
|
|
|
* (at your option) any later version. |
|
10
|
|
|
* |
|
11
|
|
|
* This program is distributed in the hope that it will be useful, |
|
12
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14
|
|
|
* GNU General Public License for more details. |
|
15
|
|
|
* |
|
16
|
|
|
* You should have received a copy of the GNU General Public License along with this program. |
|
17
|
|
|
* If not, see <https://www.gnu.org/licenses/>. |
|
18
|
|
|
*/ |
|
19
|
|
|
|
|
20
|
|
|
declare(strict_types=1); |
|
21
|
|
|
|
|
22
|
|
|
namespace MikoPBX\PBXCoreREST\Middleware; |
|
23
|
|
|
|
|
24
|
|
|
use MikoPBX\Common\Providers\LoggerAuthProvider; |
|
25
|
|
|
use MikoPBX\Common\Providers\PBXConfModulesProvider; |
|
26
|
|
|
use MikoPBX\Modules\Config\RestAPIConfigInterface; |
|
27
|
|
|
use MikoPBX\PBXCoreREST\Http\Request; |
|
28
|
|
|
use MikoPBX\PBXCoreREST\Http\Response; |
|
29
|
|
|
use MikoPBX\PBXCoreREST\Providers\RequestProvider; |
|
30
|
|
|
use MikoPBX\PBXCoreREST\Providers\ResponseProvider; |
|
31
|
|
|
use MikoPBX\PBXCoreREST\Traits\ResponseTrait; |
|
32
|
|
|
use Phalcon\Mvc\Micro; |
|
33
|
|
|
use Phalcon\Mvc\Micro\MiddlewareInterface; |
|
34
|
|
|
|
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Class AuthenticationMiddleware |
|
38
|
|
|
* |
|
39
|
|
|
*/ |
|
40
|
|
|
class AuthenticationMiddleware implements MiddlewareInterface |
|
41
|
|
|
{ |
|
42
|
|
|
use ResponseTrait; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Call me |
|
46
|
|
|
* |
|
47
|
|
|
* @param Micro $api |
|
48
|
|
|
* |
|
49
|
|
|
* @return bool |
|
50
|
|
|
*/ |
|
51
|
|
|
public function call(Micro $api) |
|
52
|
|
|
{ |
|
53
|
|
|
/** @var Request $request */ |
|
54
|
|
|
$request = $api->getService(RequestProvider::SERVICE_NAME); |
|
55
|
|
|
/** @var Response $response */ |
|
56
|
|
|
$response = $api->getService(ResponseProvider::SERVICE_NAME); |
|
57
|
|
|
|
|
58
|
|
|
if ( |
|
59
|
|
|
true !== $request->isLocalHostRequest() |
|
60
|
|
|
&& true !== $request->isDebugModeEnabled() |
|
61
|
|
|
&& true !== $request->isAuthorizedSessionRequest() |
|
62
|
|
|
&& true !== $request->thisIsModuleNoAuthRequest($api) |
|
63
|
|
|
) { |
|
64
|
|
|
$loggerAuth = $api->getService(LoggerAuthProvider::SERVICE_NAME); |
|
65
|
|
|
$loggerAuth->warning("From: {$request->getClientAddress(true)} UserAgent:{$request->getUserAgent()} Cause: Wrong password"); |
|
66
|
|
|
$this->halt( |
|
67
|
|
|
$api, |
|
68
|
|
|
$response::UNAUTHORIZED, |
|
69
|
|
|
'You are not authenticated ' |
|
70
|
|
|
); |
|
71
|
|
|
return false; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
if (true !== $request->isAllowedAction($api)) { |
|
75
|
|
|
$this->halt( |
|
76
|
|
|
$api, |
|
77
|
|
|
$response::FORBIDDEN, |
|
78
|
|
|
'The route is not allowed' |
|
79
|
|
|
); |
|
80
|
|
|
return false; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
|
|
84
|
|
|
return true; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
} |