-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathApiPresenter.php
251 lines (219 loc) · 8.99 KB
/
ApiPresenter.php
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
<?php
declare(strict_types=1);
namespace Tomaj\NetteApi\Presenters;
use Nette\Application\IPresenter;
use Nette\Application\IResponse;
use Nette\Application\Request;
use Nette\Application\Responses\JsonResponse;
use Nette\DI\Container;
use Nette\Http\Response;
use Throwable;
use Tomaj\NetteApi\Api;
use Tomaj\NetteApi\ApiDecider;
use Tomaj\NetteApi\Authorization\ApiAuthorizationInterface;
use Tomaj\NetteApi\Logger\ApiLoggerInterface;
use Tomaj\NetteApi\Misc\IpDetectorInterface;
use Tomaj\NetteApi\Output\OutputInterface;
use Tomaj\NetteApi\Params\ParamsProcessor;
use Tomaj\NetteApi\RateLimit\RateLimitInterface;
use Tomaj\NetteApi\Response\JsonApiResponse;
use Tracy\Debugger;
final class ApiPresenter implements IPresenter
{
/** @var ApiDecider @inject */
public $apiDecider;
/** @var Response @inject */
public $response;
/** @var Container @inject */
public $context;
/**
* CORS header settings
*
* Available values:
* 'auto' - send back header Access-Control-Allow-Origin with domain that made request
* '*' - send header with '*' - this will workf fine if you dont need to send cookies via ajax calls to api
* with jquery $.ajax with xhrFields: { withCredentials: true } settings
* 'off' - will not send any CORS header
* other - any other value will be send in Access-Control-Allow-Origin header
*
* @var string
*/
protected $corsHeader = '*';
/**
* Set cors header
*
* See description to property $corsHeader for valid inputs
*
* @param string $corsHeader
*/
public function setCorsHeader(string $corsHeader): void
{
$this->corsHeader = $corsHeader;
}
public function run(Request $request): IResponse
{
$start = microtime(true);
$this->sendCorsHeaders();
$api = $this->getApi($request);
$handler = $api->getHandler();
$authorization = $api->getAuthorization();
$rateLimit = $api->getRateLimit();
$authResponse = $this->checkAuth($authorization);
if ($authResponse !== null) {
return $authResponse;
}
$rateLimitResponse = $this->checkRateLimit($rateLimit);
if ($rateLimitResponse !== null) {
return $rateLimitResponse;
}
$paramsProcessor = new ParamsProcessor($handler->params());
if ($paramsProcessor->isError()) {
$this->response->setCode(Response::S400_BAD_REQUEST);
if (!Debugger::$productionMode) {
$response = new JsonResponse(['status' => 'error', 'message' => 'wrong input', 'detail' => $paramsProcessor->getErrors()]);
} else {
$response = new JsonResponse(['status' => 'error', 'message' => 'wrong input']);
}
return $response;
}
$params = $paramsProcessor->getValues();
try {
$response = $handler->handle($params);
$outputValid = count($handler->outputs()) === 0; // back compatibility for handlers with no outputs defined
$outputValidatorErrors = [];
foreach ($handler->outputs() as $output) {
if (!$output instanceof OutputInterface) {
$outputValidatorErrors[] = ["Output does not implement OutputInterface"];
continue;
}
$validationResult = $output->validate($response);
if ($validationResult->isOk()) {
$outputValid = true;
break;
}
$outputValidatorErrors[] = $validationResult->getErrors();
}
if (!$outputValid) {
Debugger::log($outputValidatorErrors, Debugger::ERROR);
if (!Debugger::$productionMode) {
$response = new JsonApiResponse(Response::S500_INTERNAL_SERVER_ERROR, ['status' => 'error', 'message' => 'Internal server error', 'details' => $outputValidatorErrors]);
}
}
$code = $response->getCode();
} catch (Throwable $exception) {
if (!Debugger::$productionMode) {
$response = new JsonApiResponse(Response::S500_INTERNAL_SERVER_ERROR, ['status' => 'error', 'message' => 'Internal server error', 'detail' => $exception->getMessage()]);
} else {
$response = new JsonApiResponse(Response::S500_INTERNAL_SERVER_ERROR, ['status' => 'error', 'message' => 'Internal server error']);
}
$code = $response->getCode();
Debugger::log($exception, Debugger::EXCEPTION);
}
$end = microtime(true);
if ($this->context->findByType(ApiLoggerInterface::class)) {
/** @var ApiLoggerInterface $apiLogger */
$apiLogger = $this->context->getByType(ApiLoggerInterface::class);
$this->logRequest($request, $apiLogger, $code, $end - $start);
}
// output to nette
$this->response->setCode($code);
return $response;
}
private function getApi(Request $request): Api
{
return $this->apiDecider->getApi(
$request->getMethod(),
(int) $request->getParameter('version'),
$request->getParameter('package'),
$request->getParameter('apiAction')
);
}
private function checkAuth(ApiAuthorizationInterface $authorization): ?IResponse
{
if (!$authorization->authorized()) {
$this->response->setCode(Response::S403_FORBIDDEN);
return new JsonResponse(['status' => 'error', 'message' => $authorization->getErrorMessage()]);
}
return null;
}
private function checkRateLimit(RateLimitInterface $rateLimit): ?IResponse
{
$rateLimitResponse = $rateLimit->check();
if (!$rateLimitResponse) {
return null;
}
$limit = $rateLimitResponse->getLimit();
$remaining = $rateLimitResponse->getRemaining();
$retryAfter = $rateLimitResponse->getRetryAfter();
$this->response->addHeader('X-RateLimit-Limit', (string)$limit);
$this->response->addHeader('X-RateLimit-Remaining', (string)$remaining);
if ($remaining === 0) {
$this->response->setCode(Response::S429_TOO_MANY_REQUESTS);
$this->response->addHeader('Retry-After', (string)$retryAfter);
return $rateLimitResponse->getErrorResponse() ?: new JsonResponse(['status' => 'error', 'message' => 'Too many requests. Retry after ' . $retryAfter . ' seconds.']);
}
return null;
}
private function logRequest(Request $request, ApiLoggerInterface $logger, int $code, float $elapsed): void
{
$headers = [];
if (function_exists('getallheaders')) {
$headers = getallheaders();
} else {
foreach ($_SERVER as $name => $value) {
if (substr($name, 0, 5) === 'HTTP_') {
$key = str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))));
$headers[$key] = $value;
}
}
}
$requestHeaders = '';
foreach ($headers as $key => $value) {
$requestHeaders .= "$key: $value\n";
}
$ipDetector = $this->context->getByType(IpDetectorInterface::class);
$logger->log(
$code,
$request->getMethod(),
$requestHeaders,
(string) filter_input(INPUT_SERVER, 'REQUEST_URI'),
$ipDetector ? $ipDetector->getRequestIp() : '',
(string) filter_input(INPUT_SERVER, 'HTTP_USER_AGENT'),
(int) ($elapsed) * 1000
);
}
protected function sendCorsHeaders(): void
{
$this->response->addHeader('Access-Control-Allow-Methods', 'POST, DELETE, PUT, GET, OPTIONS');
if ($this->corsHeader === 'auto') {
$domain = $this->getRequestDomain();
if ($domain !== null) {
$this->response->addHeader('Access-Control-Allow-Origin', $domain);
$this->response->addHeader('Access-Control-Allow-Credentials', 'true');
}
return;
}
if ($this->corsHeader === '*') {
$this->response->addHeader('Access-Control-Allow-Origin', '*');
return;
}
if ($this->corsHeader !== 'off') {
$this->response->addHeader('Access-Control-Allow-Origin', $this->corsHeader);
}
}
private function getRequestDomain(): ?string
{
if (!filter_input(INPUT_SERVER, 'HTTP_REFERER')) {
return null;
}
$refererParsedUrl = parse_url(filter_input(INPUT_SERVER, 'HTTP_REFERER'));
if (!(isset($refererParsedUrl['scheme']) && isset($refererParsedUrl['host']))) {
return null;
}
$url = $refererParsedUrl['scheme'] . '://' . $refererParsedUrl['host'];
if (isset($refererParsedUrl['port']) && $refererParsedUrl['port'] !== 80) {
$url .= ':' . $refererParsedUrl['port'];
}
return $url;
}
}