84eb0e2c21
Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
105 lines
2.4 KiB
PHP
105 lines
2.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace KTXC\Security\Event;
|
|
|
|
use KTXF\Event\Event;
|
|
|
|
final class RateLimitExceededEvent extends Event implements SecurityRequestEventInterface
|
|
{
|
|
private readonly string $reason;
|
|
|
|
public function __construct(
|
|
private readonly string $ipAddress,
|
|
private readonly int $requestCount,
|
|
private readonly int $windowSeconds,
|
|
private readonly ?string $endpoint = null,
|
|
?string $tenantId = null,
|
|
) {
|
|
if ($ipAddress === '') {
|
|
throw new \InvalidArgumentException('Rate-limit detection requires an IP address.');
|
|
}
|
|
if ($requestCount < 1) {
|
|
throw new \InvalidArgumentException('Rate-limit detection requires at least one request.');
|
|
}
|
|
if ($windowSeconds < 1) {
|
|
throw new \InvalidArgumentException('Rate-limit detection requires a positive window.');
|
|
}
|
|
if ($endpoint === '') {
|
|
throw new \InvalidArgumentException('A supplied rate-limit endpoint cannot be empty.');
|
|
}
|
|
|
|
$this->reason = sprintf(
|
|
'%d requests in %d seconds',
|
|
$requestCount,
|
|
$windowSeconds,
|
|
);
|
|
|
|
parent::__construct(
|
|
self::class,
|
|
[
|
|
'requestCount' => $requestCount,
|
|
'windowSeconds' => $windowSeconds,
|
|
'endpoint' => $endpoint,
|
|
],
|
|
$tenantId,
|
|
);
|
|
}
|
|
|
|
public function getIpAddress(): string
|
|
{
|
|
return $this->ipAddress;
|
|
}
|
|
|
|
public function getRequestCount(): int
|
|
{
|
|
return $this->requestCount;
|
|
}
|
|
|
|
public function getWindowSeconds(): int
|
|
{
|
|
return $this->windowSeconds;
|
|
}
|
|
|
|
public function getEndpoint(): ?string
|
|
{
|
|
return $this->endpoint;
|
|
}
|
|
|
|
public function getDeviceFingerprint(): ?string
|
|
{
|
|
return null;
|
|
}
|
|
|
|
public function getUserAgent(): ?string
|
|
{
|
|
return null;
|
|
}
|
|
|
|
public function getRequestPath(): ?string
|
|
{
|
|
return $this->endpoint;
|
|
}
|
|
|
|
public function getRequestMethod(): ?string
|
|
{
|
|
return null;
|
|
}
|
|
|
|
public function getUserId(): ?string
|
|
{
|
|
return null;
|
|
}
|
|
|
|
public function getReason(): string
|
|
{
|
|
return $this->reason;
|
|
}
|
|
|
|
public function getSeverity(): SecurityEventSeverity
|
|
{
|
|
return SecurityEventSeverity::ERROR;
|
|
}
|
|
}
|