2022-10-03 23:09:50 +02:00
|
|
|
<?php
|
2023-06-11 18:59:07 +02:00
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
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/>.
|
|
|
|
*/
|
2022-10-03 23:09:50 +02:00
|
|
|
namespace App\Controller;
|
|
|
|
|
2023-02-21 00:29:50 +01:00
|
|
|
use App\Entity\UserSystem\User;
|
2022-10-04 00:08:58 +02:00
|
|
|
use App\Entity\UserSystem\WebauthnKey;
|
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
2022-10-03 23:09:50 +02:00
|
|
|
use Jbtronics\TFAWebauthn\Services\TFAWebauthnRegistrationHelper;
|
2023-02-21 00:29:50 +01:00
|
|
|
use RuntimeException;
|
2022-10-03 23:09:50 +02:00
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
|
|
use Symfony\Component\Routing\Annotation\Route;
|
|
|
|
|
2022-10-04 00:08:58 +02:00
|
|
|
use function Symfony\Component\Translation\t;
|
|
|
|
|
2022-10-03 23:09:50 +02:00
|
|
|
class WebauthnKeyRegistrationController extends AbstractController
|
|
|
|
{
|
2023-06-11 14:15:46 +02:00
|
|
|
public function __construct(private readonly bool $demo_mode)
|
2023-02-21 00:29:50 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-05-28 01:21:05 +02:00
|
|
|
#[Route(path: '/webauthn/register', name: 'webauthn_register')]
|
2022-10-04 00:08:58 +02:00
|
|
|
public function register(Request $request, TFAWebauthnRegistrationHelper $registrationHelper, EntityManagerInterface $em)
|
2022-10-03 23:09:50 +02:00
|
|
|
{
|
2022-11-05 23:49:53 +01:00
|
|
|
//When user change its settings, he should be logged in fully.
|
|
|
|
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
|
2022-10-03 23:09:50 +02:00
|
|
|
|
2023-02-21 00:29:50 +01:00
|
|
|
if ($this->demo_mode) {
|
|
|
|
throw new RuntimeException('You can not do 2FA things in demo mode');
|
|
|
|
}
|
|
|
|
|
|
|
|
$user = $this->getUser();
|
|
|
|
|
|
|
|
if (!$user instanceof User) {
|
|
|
|
throw new RuntimeException('This controller only works only for Part-DB User objects!');
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($user->isSamlUser()) {
|
|
|
|
throw new RuntimeException('You can not remove U2F keys from SAML users!');
|
|
|
|
}
|
|
|
|
|
2022-10-03 23:09:50 +02:00
|
|
|
//If form was submitted, check the auth response
|
|
|
|
if ($request->getMethod() === 'POST') {
|
|
|
|
$webauthnResponse = $request->request->get('_auth_code');
|
|
|
|
|
|
|
|
//Retrieve other data from the form, that you want to store with the key
|
|
|
|
$keyName = $request->request->get('keyName');
|
2022-10-04 00:08:58 +02:00
|
|
|
if (empty($keyName)) {
|
|
|
|
$keyName = 'Key ' . date('Y-m-d H:i:s');
|
|
|
|
}
|
2022-10-03 23:09:50 +02:00
|
|
|
|
|
|
|
//Check the response
|
2022-10-04 00:08:58 +02:00
|
|
|
try {
|
|
|
|
$new_key = $registrationHelper->checkRegistrationResponse($webauthnResponse);
|
2023-06-11 14:15:46 +02:00
|
|
|
} catch (\Exception) {
|
2022-10-04 00:08:58 +02:00
|
|
|
$this->addFlash('error', t('tfa_u2f.add_key.registration_error'));
|
|
|
|
return $this->redirectToRoute('webauthn_register');
|
|
|
|
}
|
|
|
|
|
2023-06-13 20:24:54 +02:00
|
|
|
$user = $this->getUser();
|
|
|
|
if (!$user instanceof User) {
|
|
|
|
throw new RuntimeException('This controller only works only for Part-DB User objects!');
|
|
|
|
}
|
|
|
|
|
2022-10-04 00:08:58 +02:00
|
|
|
$keyEntity = WebauthnKey::fromRegistration($new_key);
|
|
|
|
$keyEntity->setName($keyName);
|
2023-06-13 20:24:54 +02:00
|
|
|
$keyEntity->setUser($user);
|
2022-10-04 00:08:58 +02:00
|
|
|
|
|
|
|
$em->persist($keyEntity);
|
|
|
|
$em->flush();
|
2022-10-03 23:09:50 +02:00
|
|
|
|
|
|
|
|
|
|
|
$this->addFlash('success', 'Key registered successfully');
|
2022-10-04 00:08:58 +02:00
|
|
|
return $this->redirectToRoute('user_settings');
|
2022-10-03 23:09:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return $this->render(
|
2023-02-04 22:59:43 +01:00
|
|
|
'security/webauthn/webauthn_register.html.twig',
|
2022-10-03 23:09:50 +02:00
|
|
|
[
|
|
|
|
'registrationRequest' => $registrationHelper->generateRegistrationRequestAsJSON(),
|
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
2023-06-11 18:59:07 +02:00
|
|
|
}
|