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, ); } /** * 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, ); } /** * Create a rate limit exceeded event */ public static function rateLimitExceeded( string $ipAddress, int $requestCount, int $windowSeconds, ?string $endpoint = null, ?string $tenantId = null, ): self { return self::create( self::RATE_LIMIT_EXCEEDED, $ipAddress, data: [ 'requestCount' => $requestCount, 'windowSeconds' => $windowSeconds, 'endpoint' => $endpoint, ], tenantId: $tenantId, requestPath: $endpoint, reason: sprintf('%d requests in %d seconds', $requestCount, $windowSeconds), ); } /** * 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, self::AUTH_LOGOUT, self::TOKEN_REVOKED => self::SEVERITY_WARNING, self::RATE_LIMIT_EXCEEDED, self::SUSPICIOUS_ACTIVITY => self::SEVERITY_ERROR, self::IP_BLOCKED, self::DEVICE_BLOCKED => self::SEVERITY_CRITICAL, 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; } }