feat: Introduce typed authentication failure event
Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
This commit is contained in:
@@ -11,9 +11,10 @@ use KTXC\Service\SystemFirewallStatusService;
|
||||
use KTXC\Service\TenantFirewallLogService;
|
||||
use KTXC\Service\TenantFirewallRuleService;
|
||||
use KTXC\Service\TenantFirewallStatusService;
|
||||
use KTXC\Security\Event\AuthenticationFailedEvent;
|
||||
use KTXC\Security\Event\SecurityEvent;
|
||||
use KTXF\Event\DeliveryMode;
|
||||
use KTXF\Event\EventListenerRegistrarInterface;
|
||||
use KTXC\Security\Event\SecurityEvent;
|
||||
use KTXF\Module\ModuleBrowserInterface;
|
||||
use KTXF\Module\ModuleConsoleInterface;
|
||||
use KTXF\Module\ModuleInstanceAbstract;
|
||||
@@ -34,7 +35,7 @@ class Module extends ModuleInstanceAbstract implements ModuleConsoleInterface, M
|
||||
{
|
||||
$this->events->listen(
|
||||
'core',
|
||||
SecurityEvent::AUTH_FAILURE,
|
||||
AuthenticationFailedEvent::class,
|
||||
FirewallService::class,
|
||||
'handleAuthFailure',
|
||||
priority: 100,
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace KTXC\Security\Event;
|
||||
|
||||
use KTXF\Event\Event;
|
||||
|
||||
final class AuthenticationFailedEvent extends Event implements SecurityEventInterface
|
||||
{
|
||||
public function __construct(
|
||||
private readonly string $ipAddress,
|
||||
private readonly ?string $deviceFingerprint = null,
|
||||
private readonly ?string $userId = null,
|
||||
private readonly ?string $reason = null,
|
||||
?string $tenantId = null,
|
||||
?string $identityId = null,
|
||||
private readonly ?string $userAgent = null,
|
||||
private readonly ?string $requestPath = null,
|
||||
private readonly ?string $requestMethod = null,
|
||||
) {
|
||||
parent::__construct(
|
||||
self::class,
|
||||
['userId' => $userId, 'reason' => $reason],
|
||||
$tenantId,
|
||||
$identityId,
|
||||
);
|
||||
}
|
||||
|
||||
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 SecurityEvent::SEVERITY_WARNING;
|
||||
}
|
||||
}
|
||||
@@ -9,11 +9,10 @@ use KTXF\Event\Event;
|
||||
/**
|
||||
* Security-specific event for authentication and access control events
|
||||
*/
|
||||
final class SecurityEvent extends Event
|
||||
final class SecurityEvent extends Event implements SecurityEventInterface
|
||||
{
|
||||
// Event names
|
||||
public const AUTH_SUCCESS = 'security.auth.success';
|
||||
public const AUTH_FAILURE = 'security.auth.failure';
|
||||
public const AUTH_LOGOUT = 'security.auth.logout';
|
||||
public const TOKEN_REFRESH = 'security.token.refresh';
|
||||
public const TOKEN_REVOKED = 'security.token.revoked';
|
||||
@@ -96,35 +95,6 @@ final class SecurityEvent extends Event
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an authentication failure event
|
||||
*/
|
||||
public static function authFailure(
|
||||
string $ipAddress,
|
||||
?string $deviceFingerprint = null,
|
||||
?string $userId = null,
|
||||
?string $reason = null,
|
||||
?string $tenantId = null,
|
||||
?string $identityId = null,
|
||||
?string $userAgent = null,
|
||||
?string $requestPath = null,
|
||||
?string $requestMethod = null,
|
||||
): self {
|
||||
return self::create(
|
||||
self::AUTH_FAILURE,
|
||||
$ipAddress,
|
||||
$deviceFingerprint,
|
||||
['userId' => $userId, 'reason' => $reason],
|
||||
$tenantId,
|
||||
$identityId,
|
||||
$userAgent,
|
||||
$requestPath,
|
||||
$requestMethod,
|
||||
$userId,
|
||||
$reason,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an authentication success event
|
||||
*/
|
||||
@@ -219,7 +189,6 @@ final class SecurityEvent extends Event
|
||||
self::ACCESS_GRANTED,
|
||||
self::TOKEN_REFRESH => self::SEVERITY_INFO,
|
||||
|
||||
self::AUTH_FAILURE,
|
||||
self::ACCESS_DENIED,
|
||||
self::AUTH_LOGOUT,
|
||||
self::TOKEN_REVOKED => self::SEVERITY_WARNING,
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace KTXC\Security\Event;
|
||||
|
||||
interface SecurityEventInterface
|
||||
{
|
||||
public function getName(): string;
|
||||
|
||||
public function get(string $key, mixed $default = null): mixed;
|
||||
|
||||
public function getData(): array;
|
||||
|
||||
public function getEventId(): string;
|
||||
|
||||
public function getTenantId(): ?string;
|
||||
|
||||
public function getIdentityId(): ?string;
|
||||
|
||||
public function getIpAddress(): ?string;
|
||||
|
||||
public function getDeviceFingerprint(): ?string;
|
||||
|
||||
public function getUserAgent(): ?string;
|
||||
|
||||
public function getRequestPath(): ?string;
|
||||
|
||||
public function getRequestMethod(): ?string;
|
||||
|
||||
public function getUserId(): ?string;
|
||||
|
||||
public function getReason(): ?string;
|
||||
|
||||
public function getSeverity(): int;
|
||||
}
|
||||
@@ -9,8 +9,10 @@ use KTXC\Models\Firewall\FirewallRuleObject;
|
||||
use KTXC\Models\Firewall\FirewallLogObject;
|
||||
use KTXC\Stores\FirewallStore;
|
||||
use KTXC\Context\TenantContextInterface;
|
||||
use KTXF\Event\EventDispatcherInterface;
|
||||
use KTXC\Security\Event\AuthenticationFailedEvent;
|
||||
use KTXC\Security\Event\SecurityEvent;
|
||||
use KTXC\Security\Event\SecurityEventInterface;
|
||||
use KTXF\Event\EventDispatcherInterface;
|
||||
use KTXF\IpUtils;
|
||||
|
||||
/**
|
||||
@@ -130,7 +132,7 @@ class FirewallService
|
||||
/**
|
||||
* Handle authentication failure event
|
||||
*/
|
||||
public function handleAuthFailure(SecurityEvent $event): void
|
||||
public function handleAuthFailure(AuthenticationFailedEvent $event): void
|
||||
{
|
||||
$ipAddress = $event->getIpAddress();
|
||||
$tenantId = $event->getTenantId() ?? $this->tenantContext->identifier();
|
||||
@@ -225,7 +227,7 @@ class FirewallService
|
||||
/**
|
||||
* Log security event to firewall logs
|
||||
*/
|
||||
public function logSecurityEvent(SecurityEvent $event): void
|
||||
public function logSecurityEvent(SecurityEventInterface $event): void
|
||||
{
|
||||
$log = $this->securityLog($event);
|
||||
if ($log !== null) {
|
||||
@@ -233,7 +235,7 @@ class FirewallService
|
||||
}
|
||||
}
|
||||
|
||||
private function securityLog(SecurityEvent $event): ?FirewallLogObject
|
||||
private function securityLog(SecurityEventInterface $event): ?FirewallLogObject
|
||||
{
|
||||
$tenantId = $event->getTenantId() ?? $this->tenantContext->identifier();
|
||||
$ruleScope = $event->get('ruleScope');
|
||||
@@ -264,7 +266,7 @@ class FirewallService
|
||||
private function mapEventToLogType(string $eventName): string
|
||||
{
|
||||
return match ($eventName) {
|
||||
SecurityEvent::AUTH_FAILURE => FirewallLogObject::EVENT_AUTH_FAILURE,
|
||||
AuthenticationFailedEvent::class => FirewallLogObject::EVENT_AUTH_FAILURE,
|
||||
SecurityEvent::AUTH_SUCCESS => FirewallLogObject::EVENT_ACCESS_CHECK,
|
||||
SecurityEvent::BRUTE_FORCE_DETECTED => FirewallLogObject::EVENT_BRUTE_FORCE,
|
||||
SecurityEvent::RATE_LIMIT_EXCEEDED => FirewallLogObject::EVENT_RATE_LIMIT,
|
||||
@@ -283,7 +285,7 @@ class FirewallService
|
||||
/**
|
||||
* Map security event to result
|
||||
*/
|
||||
private function mapEventToResult(SecurityEvent $event): string
|
||||
private function mapEventToResult(SecurityEventInterface $event): string
|
||||
{
|
||||
return match ($event->getName()) {
|
||||
SecurityEvent::AUTH_SUCCESS,
|
||||
|
||||
Reference in New Issue
Block a user