forked from NeoRazorX/facturascripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApiController.php
More file actions
222 lines (187 loc) · 7.02 KB
/
ApiController.php
File metadata and controls
222 lines (187 loc) · 7.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<?php
/**
* This file is part of FacturaScripts
* Copyright (C) 2017-2024 Carlos Garcia Gomez <carlos@facturascripts.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace FacturaScripts\Core\Template;
use FacturaScripts\Core\Base\DataBase\DataBaseWhere;
use FacturaScripts\Core\Cache;
use FacturaScripts\Core\Contract\ControllerInterface;
use FacturaScripts\Core\KernelException;
use FacturaScripts\Core\Request;
use FacturaScripts\Core\Response;
use FacturaScripts\Core\Session;
use FacturaScripts\Core\Tools;
use FacturaScripts\Dinamic\Model\ApiAccess;
use FacturaScripts\Dinamic\Model\ApiKey;
abstract class ApiController implements ControllerInterface
{
const API_VERSION = 3;
const INCIDENT_EXPIRATION_TIME = 600;
const IP_LIST = 'api-ip-list';
const MAX_INCIDENT_COUNT = 5;
/** @var ApiKey */
protected $apiKey;
/** @var Request */
protected $request;
/** @var Response */
protected $response;
/** @var string */
protected $url;
abstract protected function runResource(): void;
public function __construct(string $className, string $url = '')
{
$this->request = Request::createFromGlobals();
$this->response = new Response();
$this->url = $url;
Session::set('uri', $url);
}
public function getPageData(): array
{
return [];
}
public function run(): void
{
// si no hay constante api_key y la api está desactivada, no se puede acceder
if (null === Tools::config('api_key') && false == Tools::settings('default', 'enable_api', false)) {
throw new KernelException('DisabledApi', Tools::lang()->trans('api-disabled'));
}
if ($this->request->headers->get('REQUEST_METHOD') == 'OPTIONS') {
$this->response->headers->set('Access-Control-Allow-Origin', '*');
$this->response->headers->set('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, PATCH, OPTIONS');
$allowHeaders = $this->request->headers->get('HTTP_ACCESS_CONTROL_REQUEST_HEADERS');
$this->response->headers->set('Access-Control-Allow-Headers', $allowHeaders);
$this->response->headers->set('Content-Type', 'application/json');
$this->response->send();
return;
}
// comprobamos si la IP está bloqueada
if ($this->clientHasManyIncidents()) {
throw new KernelException('IpBannedOnApi', Tools::lang()->trans('ip-banned'));
}
// comprobamos el token
$altToken = $this->request->headers->get('Token', '');
$token = $this->request->headers->get('X-Auth-Token', $altToken);
if (false === $this->validateApiToken($token)) {
$this->saveIncident();
throw new KernelException('InvalidApiToken', Tools::lang()->trans('auth-token-invalid'));
}
// comprobamos los permisos
$resource = $this->getUriParam(2);
if (false === $this->isAllowed($resource)) {
$this->saveIncident();
throw new KernelException('ForbiddenApiEndpoint', Tools::lang()->trans('forbidden'));
}
// comprobamos la versión de la api
$version = $this->getUriParam(1);
if (empty($version)) {
throw new KernelException('MissingApiVersion', Tools::lang()->trans('api-version-not-found'));
}
if ($version != self::API_VERSION) {
throw new KernelException('InvalidApiVersion', Tools::lang()->trans('api-version-invalid'));
}
$this->response->headers->set('Access-Control-Allow-Origin', '*');
$this->response->headers->set('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, PATCH, OPTIONS');
$this->response->headers->set('Content-Type', 'application/json');
$this->runResource();
$this->response->send();
}
private function clientHasManyIncidents(): bool
{
// get ip count on the list
$currentIp = Session::getClientIp();
$ipCount = 0;
foreach ($this->getIpList() as $item) {
if ($item['ip'] === $currentIp) {
$ipCount++;
}
}
return $ipCount >= self::MAX_INCIDENT_COUNT;
}
private function getIpList(): array
{
$ipList = Cache::get(self::IP_LIST);
if (false === is_array($ipList)) {
return [];
}
// remove expired items
$newList = [];
foreach ($ipList as $item) {
if (time() - $item['time'] < self::INCIDENT_EXPIRATION_TIME) {
$newList[] = $item;
}
}
return $newList;
}
protected function getUriParam(string $num): string
{
$params = explode('/', substr($this->url, 1));
return $params[$num] ?? '';
}
private function isAllowed(string $resource): bool
{
if ($resource === '' || $this->apiKey->fullaccess) {
return true;
}
$apiAccess = new ApiAccess();
$where = [
new DataBaseWhere('idapikey', $this->apiKey->id),
new DataBaseWhere('resource', $resource)
];
if ($apiAccess->loadFromCode('', $where)) {
switch ($this->request->method()) {
case 'DELETE':
return $apiAccess->allowdelete;
case 'GET':
return $apiAccess->allowget;
case 'PATCH':
case 'PUT':
return $apiAccess->allowput;
case 'POST':
return $apiAccess->allowpost;
}
}
return false;
}
private function saveIncident(): void
{
// add the current IP to the list
$ipList = $this->getIpList();
$ipList[] = [
'ip' => Session::getClientIp(),
'time' => time()
];
// save the list in cache
Cache::set(self::IP_LIST, $ipList);
}
private function validateApiToken(string $token): bool
{
$this->apiKey = new ApiKey();
if (empty($token)) {
return false;
}
if ($token === Tools::config('api_key')) {
$this->apiKey->apikey = Tools::config('api_key');
$this->apiKey->fullaccess = true;
return true;
}
$where = [
new DataBaseWhere('apikey', $token),
new DataBaseWhere('enabled', true)
];
return $this->apiKey->loadFromCode('', $where);
}
}