Part-DB.Part-DB-server/src/EventSubscriber/UserSystem/PasswordChangeNeededSubscriber.php

162 lines
5.7 KiB
PHP
Raw Normal View History

<?php
2020-02-22 18:14:36 +01: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-02-22 18:14:36 +01: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-01-05 15:46:58 +01:00
declare(strict_types=1);
namespace App\EventSubscriber\UserSystem;
2023-06-11 14:55:06 +02:00
use Symfony\Bundle\SecurityBundle\Security;
use App\Entity\UserSystem\Group;
use App\Entity\UserSystem\User;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Request;
2020-06-01 18:21:50 +02:00
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Security\Http\HttpUtils;
/**
* This event subscriber redirects a user to its settings page, when it needs to change its password or is enforced
* to setup a 2FA method (enforcement can be set per group).
* In this cases the user is unable to access sites other than the whitelisted (see ALLOWED_ROUTES).
*/
2020-01-05 22:49:00 +01:00
final class PasswordChangeNeededSubscriber implements EventSubscriberInterface
{
/**
* @var string[] The routes the user is allowed to access without being redirected.
2020-01-04 20:24:09 +01:00
* This should be only routes related to login/logout and user settings
*/
public const ALLOWED_ROUTES = [
'2fa_login',
'2fa_login_check',
'user_settings',
'club_base_register_u2f',
'logout',
];
2020-01-05 22:49:00 +01:00
/**
* @var string The route the user will redirected to, if he needs to change this password
*/
public const REDIRECT_TARGET = 'user_settings';
2023-06-11 14:55:06 +02:00
public function __construct(private readonly Security $security, private readonly HttpUtils $httpUtils)
{
}
/**
* This function is called when the kernel encounters a request.
* It checks if the user must change its password or add an 2FA mehtod and redirect it to the user settings page,
* if needed.
*/
2020-01-04 20:24:09 +01:00
public function redirectToSettingsIfNeeded(RequestEvent $event): void
{
$user = $this->security->getUser();
$request = $event->getRequest();
2021-10-02 20:41:14 +02:00
if (!$event->isMainRequest()) {
return;
}
2020-08-21 21:36:22 +02:00
if (!$user instanceof User) {
return;
}
//If the user is impersonated, we don't need to redirect him
if ($this->security->isGranted('IS_IMPERSONATOR')) {
return;
}
//Abort if we dont need to redirect the user.
if (!$user->isNeedPwChange() && !self::TFARedirectNeeded($user, $request)) {
return;
}
//Check for a whitelisted URL
foreach (static::ALLOWED_ROUTES as $route) {
//Dont do anything if we encounter an allowed route
if ($this->httpUtils->checkRequestPath($request, $route)) {
return;
}
}
/* Dont redirect tree endpoints, as this would cause trouble and creates multiple flash
warnigs for one page reload */
if (str_contains($request->getUri(), '/tree/')) {
return;
}
/** @var Session $session */
$session = $request->getSession();
$flashBag = $session->getFlashBag();
//Show appropriate message to user about the reason he was redirected
2020-01-04 20:24:09 +01:00
if ($user->isNeedPwChange()) {
$flashBag->add('warning', 'user.pw_change_needed.flash');
}
if (self::TFARedirectNeeded($user, $request)) {
2023-04-16 00:55:25 +02:00
$flashBag->add('warning', 'user.2fa_needed.flash');
}
$event->setResponse($this->httpUtils->createRedirectResponse($request, static::REDIRECT_TARGET));
}
/**
* Check if a redirect because of a missing 2FA method is needed.
* That is the case if the group of the user enforces 2FA, but the user has neither Google Authenticator nor an
* U2F key setup.
* The result is cached for some minutes in the session to prevent unnecessary database queries.
2020-01-04 20:24:09 +01:00
*
2020-08-21 21:36:22 +02:00
* @param User $user the user for which should be checked if it needs to be redirected
2020-01-04 20:24:09 +01:00
*
2020-08-21 21:36:22 +02:00
* @return bool true if the user needs to be redirected
*/
public static function TFARedirectNeeded(User $user, Request $request): bool
{
//Check if when we have checked the user the last time
$session = $request->getSession();
$last_check = $session->get('tfa_redirect_check', 0);
//If we have checked the user already in the last 10 minutes, we don't need to check it again
if ($last_check > time() - 600) {
return false;
}
//Otherwise we check the user again
$tfa_enabled = $user->isWebAuthnAuthenticatorEnabled() || $user->isGoogleAuthenticatorEnabled();
$result = $user->getGroup() instanceof Group && $user->getGroup()->isEnforce2FA() && !$tfa_enabled;
//If no redirect is needed, we set the last check time to now
if (!$result) {
$session->set('tfa_redirect_check', time());
}
return $result;
}
2020-08-21 22:43:37 +02:00
public static function getSubscribedEvents(): array
{
return [
KernelEvents::REQUEST => 'redirectToSettingsIfNeeded',
];
}
2020-01-04 20:24:09 +01:00
}