1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Yii\RateLimiter; |
6
|
|
|
|
7
|
|
|
use Psr\Http\Message\ResponseFactoryInterface; |
8
|
|
|
use Psr\Http\Message\ResponseInterface; |
9
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
10
|
|
|
use Psr\Http\Server\MiddlewareInterface; |
11
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
12
|
|
|
use Yiisoft\Http\Status; |
13
|
|
|
use Yiisoft\Yii\RateLimiter\Policy\LimitPerIp; |
14
|
|
|
use Yiisoft\Yii\RateLimiter\Policy\LimitPolicyInterface; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* RateLimiter helps to prevent abuse by limiting the number of requests that could be me made consequentially. |
18
|
|
|
* |
19
|
|
|
* For example, you may want to limit the API usage of each user to be at most 100 API calls within a period of 10 |
20
|
|
|
* minutes. If too many requests are received from a user within the stated period of the time, a response with status |
21
|
|
|
* code 429 (meaning "Too Many Requests") should be returned. |
22
|
|
|
* |
23
|
|
|
* @psalm-type CounterIdCallback = callable(ServerRequestInterface):string |
24
|
|
|
*/ |
25
|
|
|
final class LimitRequestsMiddleware implements MiddlewareInterface |
26
|
|
|
{ |
27
|
|
|
private CounterInterface $counter; |
28
|
|
|
|
29
|
|
|
private ResponseFactoryInterface $responseFactory; |
30
|
|
|
|
31
|
|
|
private LimitPolicyInterface $limitingPolicy; |
32
|
|
|
|
33
|
|
|
public function __construct( |
34
|
|
|
CounterInterface $counter, |
35
|
|
|
ResponseFactoryInterface $responseFactory, |
36
|
|
|
?LimitPolicyInterface $limitingPolicy = null |
37
|
|
|
) { |
38
|
|
|
$this->counter = $counter; |
39
|
|
|
$this->responseFactory = $responseFactory; |
40
|
|
|
$this->limitingPolicy = $limitingPolicy ?: new LimitPerIp(); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
44
|
|
|
{ |
45
|
|
|
$state = $this->counter->hit($this->limitingPolicy->fingerprint($request)); |
46
|
|
|
|
47
|
|
|
if ($state->isLimitReached()) { |
48
|
|
|
$response = $this->createErrorResponse(); |
49
|
|
|
} else { |
50
|
|
|
$response = $handler->handle($request); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return $this->addHeaders($response, $state); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
private function createErrorResponse(): ResponseInterface |
57
|
|
|
{ |
58
|
|
|
$response = $this->responseFactory->createResponse(Status::TOO_MANY_REQUESTS); |
59
|
|
|
$response->getBody()->write(Status::TEXTS[Status::TOO_MANY_REQUESTS]); |
60
|
|
|
|
61
|
|
|
return $response; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
private function addHeaders(ResponseInterface $response, CounterState $result): ResponseInterface |
65
|
|
|
{ |
66
|
|
|
return $response |
67
|
|
|
->withHeader('X-Rate-Limit-Limit', (string)$result->getLimit()) |
68
|
|
|
->withHeader('X-Rate-Limit-Remaining', (string)$result->getRemaining()) |
69
|
|
|
->withHeader('X-Rate-Limit-Reset', (string)$result->getResetTime()); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|