refactor(security): replace generic event with severity enum

Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
This commit is contained in:
2026-08-05 23:59:49 -04:00
parent 3f9c2500d9
commit 84eb0e2c21
27 changed files with 80 additions and 247 deletions
-1
View File
@@ -23,7 +23,6 @@ use KTXC\Security\Event\FirewallRuleRemovedEvent;
use KTXC\Security\Event\FirewallSettingsUpdatedEvent;
use KTXC\Security\Event\RateLimitExceededEvent;
use KTXC\Security\Event\SuspiciousActivityEvent;
use KTXC\Security\Event\SecurityEvent;
use KTXF\Event\DeliveryMode;
use KTXF\Event\EventListenerRegistrarInterface;
use KTXF\Module\ModuleBrowserInterface;
@@ -81,8 +81,8 @@ final class AccessDeniedEvent extends Event implements SecurityRequestEventInter
return $this->reason;
}
public function getSeverity(): int
public function getSeverity(): SecurityEventSeverity
{
return SecurityEvent::SEVERITY_WARNING;
return SecurityEventSeverity::WARNING;
}
}
@@ -32,8 +32,8 @@ final class AuthenticationFailedEvent extends Event implements SecurityEventInte
return $this->reason;
}
public function getSeverity(): int
public function getSeverity(): SecurityEventSeverity
{
return SecurityEvent::SEVERITY_WARNING;
return SecurityEventSeverity::WARNING;
}
}
@@ -63,8 +63,8 @@ final class AuthenticationSucceededEvent extends Event implements SecurityReques
return null;
}
public function getSeverity(): int
public function getSeverity(): SecurityEventSeverity
{
return SecurityEvent::SEVERITY_INFO;
return SecurityEventSeverity::INFO;
}
}
@@ -84,8 +84,8 @@ final class BruteForceDetectedEvent extends Event implements SecurityRequestEven
return $this->reason;
}
public function getSeverity(): int
public function getSeverity(): SecurityEventSeverity
{
return SecurityEvent::SEVERITY_CRITICAL;
return SecurityEventSeverity::CRITICAL;
}
}
@@ -59,8 +59,8 @@ final class DeviceBlockedEvent extends Event implements SecurityRequestEventInte
return $this->reason;
}
public function getSeverity(): int
public function getSeverity(): SecurityEventSeverity
{
return SecurityEvent::SEVERITY_CRITICAL;
return SecurityEventSeverity::CRITICAL;
}
}
+2 -2
View File
@@ -8,7 +8,7 @@ use KTXF\Event\Event;
abstract class FirewallIpEvent extends Event implements SecurityRequestEventInterface
{
protected const SEVERITY = SecurityEvent::SEVERITY_INFO;
protected const SecurityEventSeverity SEVERITY = SecurityEventSeverity::INFO;
final public function __construct(
private readonly string $ipAddress,
@@ -61,7 +61,7 @@ abstract class FirewallIpEvent extends Event implements SecurityRequestEventInte
return $this->reason;
}
public function getSeverity(): int
public function getSeverity(): SecurityEventSeverity
{
return static::SEVERITY;
}
@@ -151,8 +151,8 @@ abstract class FirewallRuleEvent extends Event implements SecurityEventInterface
return $this->reason;
}
public function getSeverity(): int
public function getSeverity(): SecurityEventSeverity
{
return SecurityEvent::SEVERITY_INFO;
return SecurityEventSeverity::INFO;
}
}
@@ -69,8 +69,8 @@ final class FirewallSettingsUpdatedEvent extends Event implements SecurityEventI
return $this->changeReason;
}
public function getSeverity(): int
public function getSeverity(): SecurityEventSeverity
{
return SecurityEvent::SEVERITY_INFO;
return SecurityEventSeverity::INFO;
}
}
+1 -1
View File
@@ -6,5 +6,5 @@ namespace KTXC\Security\Event;
final class IpBlockedEvent extends FirewallIpEvent
{
protected const SEVERITY = SecurityEvent::SEVERITY_CRITICAL;
protected const SecurityEventSeverity SEVERITY = SecurityEventSeverity::CRITICAL;
}
@@ -97,8 +97,8 @@ final class RateLimitExceededEvent extends Event implements SecurityRequestEvent
return $this->reason;
}
public function getSeverity(): int
public function getSeverity(): SecurityEventSeverity
{
return SecurityEvent::SEVERITY_ERROR;
return SecurityEventSeverity::ERROR;
}
}
-141
View File
@@ -1,141 +0,0 @@
<?php
declare(strict_types=1);
namespace KTXC\Security\Event;
use KTXF\Event\Event;
/**
* Security-specific event for authentication and access control events
*/
final class SecurityEvent extends Event implements SecurityRequestEventInterface
{
// Event names
public const AUTH_LOGOUT = 'security.auth.logout';
public const TOKEN_REFRESH = 'security.token.refresh';
public const TOKEN_REVOKED = 'security.token.revoked';
public const ACCESS_GRANTED = 'security.access.granted';
// Severity levels
public const SEVERITY_DEBUG = 0;
public const SEVERITY_INFO = 1;
public const SEVERITY_WARNING = 2;
public const SEVERITY_ERROR = 3;
public const SEVERITY_CRITICAL = 4;
private readonly int $severity;
public function __construct(
string $name,
array $data = [],
?string $tenantId = null,
?string $identityId = null,
private readonly ?string $ipAddress = null,
private readonly ?string $deviceFingerprint = null,
private readonly ?string $userAgent = null,
private readonly ?string $requestPath = null,
private readonly ?string $requestMethod = null,
private readonly ?string $userId = null,
private readonly ?string $reason = null,
?int $severity = null,
) {
parent::__construct($name, $data, $tenantId, $identityId);
$this->severity = $severity ?? self::getSeverityForEvent($name);
}
/**
* Create a security event with common parameters
*/
public static function create(
string $name,
?string $ipAddress = null,
?string $deviceFingerprint = null,
array $data = [],
?string $tenantId = null,
?string $identityId = null,
?string $userAgent = null,
?string $requestPath = null,
?string $requestMethod = null,
?string $userId = null,
?string $reason = null,
?int $severity = null,
): self {
return new self(
$name,
$data,
$tenantId,
$identityId,
$ipAddress,
$deviceFingerprint,
$userAgent,
$requestPath,
$requestMethod,
$userId,
$reason,
$severity,
);
}
/**
* Get default severity for event types
*/
private static function getSeverityForEvent(string $eventName): int
{
return match ($eventName) {
self::ACCESS_GRANTED,
self::TOKEN_REFRESH => self::SEVERITY_INFO,
self::AUTH_LOGOUT,
self::TOKEN_REVOKED => self::SEVERITY_WARNING,
default => self::SEVERITY_INFO,
};
}
// Getters and setters
public function getIpAddress(): ?string
{
return $this->ipAddress;
}
public function getDeviceFingerprint(): ?string
{
return $this->deviceFingerprint;
}
public function getUserAgent(): ?string
{
return $this->userAgent;
}
public function getRequestPath(): ?string
{
return $this->requestPath;
}
public function getRequestMethod(): ?string
{
return $this->requestMethod;
}
public function getUserId(): ?string
{
return $this->userId;
}
public function getReason(): ?string
{
return $this->reason;
}
public function getSeverity(): int
{
return $this->severity;
}
}
@@ -22,5 +22,5 @@ interface SecurityEventInterface
public function getReason(): ?string;
public function getSeverity(): int;
public function getSeverity(): SecurityEventSeverity;
}
@@ -0,0 +1,14 @@
<?php
declare(strict_types=1);
namespace KTXC\Security\Event;
enum SecurityEventSeverity: int
{
case DEBUG = 0;
case INFO = 1;
case WARNING = 2;
case ERROR = 3;
case CRITICAL = 4;
}
@@ -93,8 +93,8 @@ final class SuspiciousActivityEvent extends Event implements SecurityRequestEven
return $this->reason;
}
public function getSeverity(): int
public function getSeverity(): SecurityEventSeverity
{
return SecurityEvent::SEVERITY_ERROR;
return SecurityEventSeverity::ERROR;
}
}
+1 -3
View File
@@ -22,7 +22,6 @@ use KTXC\Security\Event\FirewallRuleRemovedEvent;
use KTXC\Security\Event\FirewallSettingsUpdatedEvent;
use KTXC\Security\Event\RateLimitExceededEvent;
use KTXC\Security\Event\SuspiciousActivityEvent;
use KTXC\Security\Event\SecurityEvent;
use KTXC\Security\Event\SecurityEventInterface;
use KTXC\Security\Event\SecurityRequestEventInterface;
use KTXF\Event\EventDispatcherInterface;
@@ -311,8 +310,7 @@ class FirewallService
private function mapEventToResult(SecurityEventInterface $event): string
{
return match ($event->getName()) {
AuthenticationSucceededEvent::class,
SecurityEvent::ACCESS_GRANTED => FirewallLogObject::RESULT_ALLOWED,
AuthenticationSucceededEvent::class => FirewallLogObject::RESULT_ALLOWED,
FirewallRuleCreatedEvent::class,
FirewallRuleExtendedEvent::class,
FirewallRuleEnabledEvent::class,