|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | +namespace In2code\Lux\Domain\Tracker; |
| 5 | + |
| 6 | +use In2code\Lux\Domain\Cache\RateLimiterCache; |
| 7 | +use In2code\Lux\Events\StopAnyProcessBeforePersistenceEvent; |
| 8 | +use In2code\Lux\Exception\RateLimitException; |
| 9 | +use In2code\Lux\Utility\ConfigurationUtility; |
| 10 | +use Psr\Log\LoggerInterface; |
| 11 | +use TYPO3\CMS\Core\Configuration\Exception\ExtensionConfigurationExtensionNotConfiguredException; |
| 12 | +use TYPO3\CMS\Core\Configuration\Exception\ExtensionConfigurationPathDoesNotExistException; |
| 13 | + |
| 14 | +class RateLimiter |
| 15 | +{ |
| 16 | + protected RateLimiterCache $cache; |
| 17 | + protected LoggerInterface $logger; |
| 18 | + |
| 19 | + public function __construct( |
| 20 | + RateLimiterCache $cache, |
| 21 | + LoggerInterface $logger |
| 22 | + ) { |
| 23 | + $this->cache = $cache; |
| 24 | + $this->logger = $logger; |
| 25 | + } |
| 26 | + |
| 27 | + public function __invoke(StopAnyProcessBeforePersistenceEvent $event) |
| 28 | + { |
| 29 | + if ($this->isRateLimitingEnabled()) { |
| 30 | + $fingerprint = $event->getFingerprint(); |
| 31 | + $fingerprintHash = $fingerprint->getValue(); |
| 32 | + |
| 33 | + if ($fingerprintHash !== '') { |
| 34 | + $currentCount = $this->cache->incrementAndGet($fingerprintHash); |
| 35 | + if ($currentCount > $this->getRateLimit()) { |
| 36 | + $this->logRateLimitExceeded($fingerprintHash, $currentCount, $this->getRateLimit()); |
| 37 | + throw new RateLimitException( |
| 38 | + 'Rate limit exceeded: ' . $currentCount . ' requests in last minute', |
| 39 | + 1768214806 |
| 40 | + ); |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + protected function isRateLimitingEnabled(): bool |
| 47 | + { |
| 48 | + try { |
| 49 | + return ConfigurationUtility::isRateLimitingEnabled(); |
| 50 | + } catch (ExtensionConfigurationExtensionNotConfiguredException | ExtensionConfigurationPathDoesNotExistException $exception) { |
| 51 | + return true; |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + protected function getRateLimit(): int |
| 56 | + { |
| 57 | + try { |
| 58 | + return ConfigurationUtility::getRateLimitRequestsPerMinute(); |
| 59 | + } catch (ExtensionConfigurationExtensionNotConfiguredException | ExtensionConfigurationPathDoesNotExistException $exception) { |
| 60 | + return ConfigurationUtility::RATE_LIMIT; |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + protected function logRateLimitExceeded(string $fingerprintHash, int $currentCount, int $limit): void |
| 65 | + { |
| 66 | + if (ConfigurationUtility::isExceptionLoggingActivated()) { |
| 67 | + $this->logger->warning('Rate limit exceeded', [ |
| 68 | + 'fingerprint' => $fingerprintHash, |
| 69 | + 'count' => $currentCount, |
| 70 | + 'limit' => $limit, |
| 71 | + 'component' => 'RateLimiter', |
| 72 | + ]); |
| 73 | + } |
| 74 | + } |
| 75 | +} |
0 commit comments