2020-04-03 18:27:47 +02:00
|
|
|
<?php
|
2022-11-29 21:21:26 +01:00
|
|
|
/*
|
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
|
|
|
*
|
|
|
|
* Copyright (C) 2019 - 2022 Jan Böhmer (https://github.com/jbtronics)
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as published
|
|
|
|
* by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2020-04-10 13:05:08 +02:00
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2020-04-03 18:27:47 +02:00
|
|
|
/**
|
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
|
|
|
*
|
2022-11-29 22:28:53 +01:00
|
|
|
* Copyright (C) 2019 - 2022 Jan Böhmer (https://github.com/jbtronics)
|
2020-04-03 18:27:47 +02:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as published
|
|
|
|
* by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2020-04-09 17:24:16 +02:00
|
|
|
namespace App\EventSubscriber\LogSystem;
|
2020-04-03 18:27:47 +02:00
|
|
|
|
|
|
|
use App\Entity\LogSystem\SecurityEventLogEntry;
|
|
|
|
use App\Events\SecurityEvent;
|
|
|
|
use App\Events\SecurityEvents;
|
|
|
|
use App\Services\LogSystem\EventLogger;
|
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
|
|
|
use Symfony\Component\HttpFoundation\RequestStack;
|
|
|
|
|
2020-04-09 17:24:16 +02:00
|
|
|
/**
|
2023-04-15 23:14:53 +02:00
|
|
|
* This subscriber writes entries to log if a security related event happens (e.g. the user changes its password).
|
2020-04-09 17:24:16 +02:00
|
|
|
*/
|
2020-04-03 18:27:47 +02:00
|
|
|
final class SecurityEventLoggerSubscriber implements EventSubscriberInterface
|
|
|
|
{
|
2023-06-11 14:50:47 +02:00
|
|
|
public function __construct(private readonly RequestStack $requestStack, private readonly EventLogger $eventLogger, private readonly bool $gdpr_compliance)
|
2020-04-03 18:27:47 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-08-21 22:43:37 +02:00
|
|
|
public static function getSubscribedEvents(): array
|
2020-04-03 18:27:47 +02:00
|
|
|
{
|
|
|
|
return [
|
|
|
|
SecurityEvents::U2F_ADDED => 'u2f_added',
|
|
|
|
SecurityEvents::PASSWORD_CHANGED => 'password_changed',
|
|
|
|
SecurityEvents::TRUSTED_DEVICE_RESET => 'trusted_device_reset',
|
|
|
|
SecurityEvents::U2F_REMOVED => 'u2f_removed',
|
|
|
|
SecurityEvents::BACKUP_KEYS_RESET => 'backup_keys_reset',
|
|
|
|
SecurityEvents::PASSWORD_RESET => 'password_reset',
|
|
|
|
SecurityEvents::GOOGLE_DISABLED => 'google_disabled',
|
|
|
|
SecurityEvents::GOOGLE_ENABLED => 'google_enabled',
|
|
|
|
SecurityEvents::TFA_ADMIN_RESET => 'tfa_admin_reset',
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function tfa_admin_reset(SecurityEvent $event): void
|
|
|
|
{
|
|
|
|
$this->addLog(SecurityEvents::TFA_ADMIN_RESET, $event);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function google_enabled(SecurityEvent $event): void
|
|
|
|
{
|
|
|
|
$this->addLog(SecurityEvents::GOOGLE_ENABLED, $event);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function google_disabled(SecurityEvent $event): void
|
|
|
|
{
|
|
|
|
$this->addLog(SecurityEvents::GOOGLE_DISABLED, $event);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function password_reset(SecurityEvent $event): void
|
|
|
|
{
|
|
|
|
$this->addLog(SecurityEvents::PASSWORD_RESET, $event);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function backup_keys_reset(SecurityEvent $event): void
|
|
|
|
{
|
|
|
|
$this->addLog(SecurityEvents::BACKUP_KEYS_RESET, $event);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function u2f_removed(SecurityEvent $event): void
|
|
|
|
{
|
|
|
|
$this->addLog(SecurityEvents::U2F_REMOVED, $event);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function u2f_added(SecurityEvent $event): void
|
|
|
|
{
|
|
|
|
$this->addLog(SecurityEvents::U2F_ADDED, $event);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function password_changed(SecurityEvent $event): void
|
|
|
|
{
|
|
|
|
$this->addLog(SecurityEvents::PASSWORD_CHANGED, $event);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function trusted_device_reset(SecurityEvent $event): void
|
|
|
|
{
|
|
|
|
$this->addLog(SecurityEvents::TRUSTED_DEVICE_RESET, $event);
|
|
|
|
}
|
2020-04-10 13:05:08 +02:00
|
|
|
|
|
|
|
private function addLog(string $type, SecurityEvent $event): void
|
|
|
|
{
|
2023-06-11 14:50:47 +02:00
|
|
|
$anonymize = $this->gdpr_compliance;
|
2020-04-10 13:05:08 +02:00
|
|
|
|
|
|
|
$request = $this->requestStack->getCurrentRequest();
|
2023-06-11 14:15:46 +02:00
|
|
|
if ($request instanceof \Symfony\Component\HttpFoundation\Request) {
|
2020-04-10 13:05:08 +02:00
|
|
|
$ip = $request->getClientIp() ?? 'unknown';
|
|
|
|
} else {
|
|
|
|
$ip = 'Console';
|
2023-04-15 23:14:53 +02:00
|
|
|
//Don't try to apply IP filter rules to non-numeric string
|
2020-04-10 13:05:08 +02:00
|
|
|
$anonymize = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$log = new SecurityEventLogEntry($type, $ip, $anonymize);
|
|
|
|
$log->setTargetElement($event->getTargetUser());
|
|
|
|
$this->eventLogger->logAndFlush($log);
|
|
|
|
}
|
|
|
|
}
|