forked from jaytaph/RateLimitBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRateLimitAnnotationListener.php
More file actions
166 lines (134 loc) · 5.79 KB
/
RateLimitAnnotationListener.php
File metadata and controls
166 lines (134 loc) · 5.79 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
<?php
namespace Noxlogic\RateLimitBundle\EventListener;
use Noxlogic\RateLimitBundle\Annotation\RateLimit;
use Noxlogic\RateLimitBundle\Events\CheckedRateLimitEvent;
use Noxlogic\RateLimitBundle\Events\GenerateKeyEvent;
use Noxlogic\RateLimitBundle\Events\RateLimitEvents;
use Noxlogic\RateLimitBundle\Exception\RateLimitExceptionInterface;
use Noxlogic\RateLimitBundle\LimitProcessorInterface;
use Noxlogic\RateLimitBundle\Service\RateLimitService;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;
class RateLimitAnnotationListener extends BaseListener
{
/**
* @var eventDispatcherInterface
*/
protected $eventDispatcher;
/**
* @var \Noxlogic\RateLimitBundle\Service\RateLimitService
*/
protected $rateLimitService;
/**
* @var LimitProcessorInterface
*/
protected $limitProcessor;
/**
* @param EventDispatcherInterface $eventDispatcher
* @param RateLimitService $rateLimitService
* @param LimitProcessorInterface $limitProcessor
*/
public function __construct(
EventDispatcherInterface $eventDispatcher,
RateLimitService $rateLimitService,
LimitProcessorInterface $limitProcessor
) {
$this->eventDispatcher = $eventDispatcher;
$this->rateLimitService = $rateLimitService;
$this->limitProcessor = $limitProcessor;
}
/**
* @param FilterControllerEvent $event
*/
public function onKernelController(FilterControllerEvent $event)
{
// Skip if the bundle isn't enabled (for instance in test environment)
if( ! $this->getParameter('enabled', true)) {
return;
}
// Skip if we aren't the main request
if ($event->getRequestType() != HttpKernelInterface::MASTER_REQUEST) {
return;
}
$rateLimit = $this->limitProcessor->getRateLimit($event->getRequest());
// Another treatment before applying RateLimit ?
$checkedRateLimitEvent = new CheckedRateLimitEvent($event->getRequest(), $rateLimit);
$this->eventDispatcher->dispatch(RateLimitEvents::CHECKED_RATE_LIMIT, $checkedRateLimitEvent);
$rateLimit = $checkedRateLimitEvent->getRateLimit();
// No matching annotation found
if (! $rateLimit) {
return;
}
$key = $this->getKey($rateLimit, $event);
$rateLimitInfo = $this->rateLimitService->getRateLimitInfo($key, $rateLimit);
if (!$rateLimitInfo) {
// @codeCoverageIgnoreStart
return;
// @codeCoverageIgnoreEnd
}
// Store the current rating info in the request attributes
$request = $event->getRequest();
$request->attributes->set('rate_limit_info', $rateLimitInfo);
// When we exceeded our limit, return a custom error response
if ($rateLimitInfo->isExceeded()) {
// Throw an exception if configured.
if ($this->getParameter('rate_response_exception')) {
$class = $this->getParameter('rate_response_exception');
$e = new $class($this->getParameter('rate_response_message'), $this->getParameter('rate_response_code'));
if ($e instanceof RateLimitExceptionInterface) {
$e->setPayload($rateLimit->getPayload());
}
throw $e;
}
$message = $this->getParameter('rate_response_message');
$code = $this->getParameter('rate_response_code');
$event->setController(function () use ($message, $code) {
// @codeCoverageIgnoreStart
return new Response($message, $code);
// @codeCoverageIgnoreEnd
});
$event->stopPropagation();
}
}
/**
* @param Request $request
* @param RateLimit[] $annotations
* @return RateLimit|null
*
* @deprecated since 1.15, use the "\Noxlogic\RateLimitBundle\LimitProcessorInterface::getRateLimit()" method instead.
*/
protected function findBestMethodMatch(Request $request, array $annotations)
{
@trigger_error(sprintf('The "%s()" method is deprecated since version 1.15, use the "\Noxlogic\RateLimitBundle\LimitProcessorInterface::getRateLimit()" method instead.', __METHOD__), E_USER_DEPRECATED);
// Empty array, check the path limits
if (count($annotations) == 0) {
return $this->limitProcessor->getRateLimit($request);
}
$best_match = null;
foreach ($annotations as $annotation) {
// cast methods to array, even method holds a string
$methods = is_array($annotation->getMethods()) ? $annotation->getMethods() : array($annotation->getMethods());
if (in_array($request->getMethod(), $methods)) {
$best_match = $annotation;
}
// Only match "default" annotation when we don't have a best match
if (count($annotation->getMethods()) == 0 && $best_match == null) {
$best_match = $annotation;
}
}
return $best_match;
}
private function getKey(RateLimit $rateLimit, FilterControllerEvent $event)
{
// Let listeners manipulate the key
$request = $event->getRequest();
$keyEvent = new GenerateKeyEvent($request, '', $rateLimit->getPayload());
$keyEvent->addToKey(join('.', $rateLimit->getMethods()));
$keyEvent->addToKey($this->limitProcessor->getRateLimitAlias($request, $event->getController()));
$this->eventDispatcher->dispatch(RateLimitEvents::GENERATE_KEY, $keyEvent);
return $keyEvent->getKey();
}
}