refactor(security): add typed authentication-success event
Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
This commit is contained in:
@@ -13,6 +13,7 @@ use KTXC\Service\TenantFirewallRuleService;
|
||||
use KTXC\Service\TenantFirewallStatusService;
|
||||
use KTXC\Security\Event\AccessDeniedEvent;
|
||||
use KTXC\Security\Event\AuthenticationFailedEvent;
|
||||
use KTXC\Security\Event\AuthenticationSucceededEvent;
|
||||
use KTXC\Security\Event\BruteForceDetectedEvent;
|
||||
use KTXC\Security\Event\RateLimitExceededEvent;
|
||||
use KTXC\Security\Event\SuspiciousActivityEvent;
|
||||
@@ -47,7 +48,7 @@ class Module extends ModuleInstanceAbstract implements ModuleConsoleInterface, M
|
||||
);
|
||||
|
||||
foreach ([
|
||||
SecurityEvent::AUTH_SUCCESS,
|
||||
AuthenticationSucceededEvent::class,
|
||||
AccessDeniedEvent::class,
|
||||
BruteForceDetectedEvent::class,
|
||||
RateLimitExceededEvent::class,
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace KTXC\Security\Event;
|
||||
|
||||
use KTXF\Event\Event;
|
||||
|
||||
final class AuthenticationSucceededEvent extends Event implements SecurityRequestEventInterface
|
||||
{
|
||||
public function __construct(
|
||||
private readonly string $ipAddress,
|
||||
private readonly string $userId,
|
||||
?string $tenantId = null,
|
||||
private readonly ?string $deviceFingerprint = null,
|
||||
) {
|
||||
if ($ipAddress === '') {
|
||||
throw new \InvalidArgumentException('Successful authentication requires an IP address.');
|
||||
}
|
||||
if ($userId === '') {
|
||||
throw new \InvalidArgumentException('Successful authentication requires a user ID.');
|
||||
}
|
||||
|
||||
parent::__construct(
|
||||
self::class,
|
||||
['userId' => $userId],
|
||||
$tenantId,
|
||||
);
|
||||
}
|
||||
|
||||
public function getIpAddress(): string
|
||||
{
|
||||
return $this->ipAddress;
|
||||
}
|
||||
|
||||
public function getDeviceFingerprint(): ?string
|
||||
{
|
||||
return $this->deviceFingerprint;
|
||||
}
|
||||
|
||||
public function getUserAgent(): ?string
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public function getRequestPath(): ?string
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public function getRequestMethod(): ?string
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public function getUserId(): string
|
||||
{
|
||||
return $this->userId;
|
||||
}
|
||||
|
||||
public function getReason(): ?string
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public function getSeverity(): int
|
||||
{
|
||||
return SecurityEvent::SEVERITY_INFO;
|
||||
}
|
||||
}
|
||||
@@ -12,7 +12,6 @@ use KTXF\Event\Event;
|
||||
final class SecurityEvent extends Event implements SecurityRequestEventInterface
|
||||
{
|
||||
// Event names
|
||||
public const AUTH_SUCCESS = 'security.auth.success';
|
||||
public const AUTH_LOGOUT = 'security.auth.logout';
|
||||
public const TOKEN_REFRESH = 'security.token.refresh';
|
||||
public const TOKEN_REVOKED = 'security.token.revoked';
|
||||
@@ -90,32 +89,12 @@ final class SecurityEvent extends Event implements SecurityRequestEventInterface
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an authentication success event
|
||||
*/
|
||||
public static function authSuccess(
|
||||
string $ipAddress,
|
||||
?string $deviceFingerprint = null,
|
||||
?string $userId = null,
|
||||
?string $tenantId = null,
|
||||
): self {
|
||||
return self::create(
|
||||
self::AUTH_SUCCESS,
|
||||
$ipAddress,
|
||||
$deviceFingerprint,
|
||||
['userId' => $userId],
|
||||
tenantId: $tenantId,
|
||||
userId: $userId,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get default severity for event types
|
||||
*/
|
||||
private static function getSeverityForEvent(string $eventName): int
|
||||
{
|
||||
return match ($eventName) {
|
||||
self::AUTH_SUCCESS,
|
||||
self::ACCESS_GRANTED,
|
||||
self::TOKEN_REFRESH => self::SEVERITY_INFO,
|
||||
|
||||
|
||||
@@ -12,6 +12,7 @@ use KTXC\Stores\FirewallStore;
|
||||
use KTXC\Context\TenantContextInterface;
|
||||
use KTXC\Security\Event\AccessDeniedEvent;
|
||||
use KTXC\Security\Event\AuthenticationFailedEvent;
|
||||
use KTXC\Security\Event\AuthenticationSucceededEvent;
|
||||
use KTXC\Security\Event\BruteForceDetectedEvent;
|
||||
use KTXC\Security\Event\RateLimitExceededEvent;
|
||||
use KTXC\Security\Event\SuspiciousActivityEvent;
|
||||
@@ -283,7 +284,7 @@ class FirewallService
|
||||
{
|
||||
return match ($eventName) {
|
||||
AuthenticationFailedEvent::class => FirewallLogObject::EVENT_AUTH_FAILURE,
|
||||
SecurityEvent::AUTH_SUCCESS => FirewallLogObject::EVENT_ACCESS_CHECK,
|
||||
AuthenticationSucceededEvent::class => FirewallLogObject::EVENT_ACCESS_CHECK,
|
||||
BruteForceDetectedEvent::class => FirewallLogObject::EVENT_BRUTE_FORCE,
|
||||
RateLimitExceededEvent::class => FirewallLogObject::EVENT_RATE_LIMIT,
|
||||
AccessDeniedEvent::class => FirewallLogObject::EVENT_RULE_MATCH,
|
||||
@@ -304,7 +305,7 @@ class FirewallService
|
||||
private function mapEventToResult(SecurityEventInterface $event): string
|
||||
{
|
||||
return match ($event->getName()) {
|
||||
SecurityEvent::AUTH_SUCCESS,
|
||||
AuthenticationSucceededEvent::class,
|
||||
SecurityEvent::ACCESS_GRANTED => FirewallLogObject::RESULT_ALLOWED,
|
||||
SecurityEvent::FIREWALL_RULE_CREATED,
|
||||
SecurityEvent::FIREWALL_RULE_EXTENDED,
|
||||
|
||||
Reference in New Issue
Block a user