Part-DB.Part-DB-server/src/EventSubscriber/U2FRegistrationSubscriber.php

118 lines
4.2 KiB
PHP
Raw Normal View History

2019-12-29 13:35:30 +01:00
<?php
2020-02-22 18:14:36 +01:00
/**
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2020 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-01-05 15:46:58 +01:00
declare(strict_types=1);
2019-12-31 17:58:01 +01:00
/**
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 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 General Public License
* as published by the Free Software Foundation; either version 2
* 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
2019-12-29 13:35:30 +01:00
namespace App\EventSubscriber;
use App\Entity\UserSystem\U2FKey;
2020-02-01 19:42:28 +01:00
use App\Entity\UserSystem\User;
use App\Events\SecurityEvent;
use App\Events\SecurityEvents;
2019-12-29 13:35:30 +01:00
use Doctrine\ORM\EntityManagerInterface;
use R\U2FTwoFactorBundle\Event\RegisterEvent;
use Symfony\Component\EventDispatcher\EventDispatcher;
2019-12-29 13:35:30 +01:00
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
2019-12-29 13:35:30 +01:00
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
2019-12-29 13:35:30 +01:00
2020-01-05 22:49:00 +01:00
final class U2FRegistrationSubscriber implements EventSubscriberInterface
2019-12-29 13:35:30 +01:00
{
2020-02-01 16:17:20 +01:00
private $em;
2019-12-29 13:35:30 +01:00
2020-02-01 16:17:20 +01:00
private $demo_mode;
private $flashBag;
2020-01-05 22:49:00 +01:00
/**
* @var UrlGeneratorInterface
*/
2020-01-05 15:46:58 +01:00
private $router;
/** @var EventDispatcher */
private $eventDispatcher;
public function __construct(UrlGeneratorInterface $router, EntityManagerInterface $entityManager, FlashBagInterface $flashBag, EventDispatcherInterface $eventDispatcher, bool $demo_mode)
2019-12-29 13:35:30 +01:00
{
$this->router = $router;
$this->em = $entityManager;
$this->demo_mode = $demo_mode;
$this->flashBag = $flashBag;
$this->eventDispatcher = $eventDispatcher;
2019-12-29 13:35:30 +01:00
}
public static function getSubscribedEvents(): array
{
2020-01-04 20:24:09 +01:00
return [
2019-12-29 13:35:30 +01:00
'r_u2f_two_factor.register' => 'onRegister',
2020-01-04 20:24:09 +01:00
];
2019-12-29 13:35:30 +01:00
}
public function onRegister(RegisterEvent $event): void
{
//Skip adding of U2F key on demo mode
2020-01-05 15:46:58 +01:00
if (! $this->demo_mode) {
$user = $event->getUser();
2020-03-15 13:56:31 +01:00
if (! $user instanceof User) {
throw new \InvalidArgumentException('Only User objects can be registered for U2F!');
2020-02-01 19:42:28 +01:00
}
$registration = $event->getRegistration();
$newKey = new U2FKey();
$newKey->fromRegistrationData($registration);
$newKey->setUser($user);
$newKey->setName($event->getKeyName());
2019-12-29 13:35:30 +01:00
// persist the new key
$this->em->persist($newKey);
$this->em->flush();
$this->flashBag->add('success', 'tfa_u2f.key_added_successful');
$security_event = new SecurityEvent($user);
$this->eventDispatcher->dispatch($security_event, SecurityEvents::U2F_ADDED);
}
2019-12-29 13:35:30 +01:00
// generate new response, here we redirect the user to the fos user
// profile
$response = new RedirectResponse($this->router->generate('user_settings'));
$event->setResponse($response);
}
2020-01-04 20:24:09 +01:00
}