src/Controller/SecurityController.php line 31

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Controller;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use Endroid\QrCode\Builder\Builder;
  6. use Endroid\QrCode\Encoding\Encoding;
  7. use Endroid\QrCode\ErrorCorrectionLevel\ErrorCorrectionLevelHigh;
  8. use Endroid\QrCode\RoundBlockSizeMode\RoundBlockSizeModeMargin;
  9. use Endroid\QrCode\Writer\PngWriter;
  10. use Scheb\TwoFactorBundle\Security\TwoFactor\Provider\Totp\TotpAuthenticatorInterface;
  11. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  12. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  13. use Symfony\Component\HttpFoundation\ParameterBag;
  14. use Symfony\Component\HttpFoundation\RedirectResponse;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Symfony\Component\HttpFoundation\Response;
  17. use Symfony\Component\HttpFoundation\Session\Session;
  18. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  19. use Symfony\Component\Routing\Annotation\Route;
  20. use Symfony\Component\Routing\RouterInterface;
  21. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  22. use Symfony\Component\Security\Core\Exception\CustomUserMessageAuthenticationException;
  23. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  24. class SecurityController extends AbstractController
  25. {
  26.     #[Route('/login'name'app_login')]
  27.     public function index(AuthenticationUtils $authenticationUtils): Response
  28.     {
  29.         // get the login error if there is one
  30.         $error $authenticationUtils->getLastAuthenticationError();
  31.         // last username entered by the user
  32.         $lastUsername $authenticationUtils->getLastUsername();
  33.         return $this->render('login/index.html.twig', [
  34.             'last_username' => $lastUsername,
  35.             'error'         => $error,
  36.         ]);
  37.     }
  38.     #[Route('/logout'name'app_logout'methods: ['GET'])]
  39.     public function logout()
  40.     {
  41.     }
  42.     #[Route('/authentication/2fa/enable'name'app_2fa_enable')]
  43.     #[IsGranted('ROLE_ADMIN')]
  44.     public function enable2fa(TotpAuthenticatorInterface $totpAuthenticator)
  45.     {
  46.         $user $this->getUser();
  47.         if (null === $user) {
  48.             throw new AuthenticationException('Access denied.');
  49.         }
  50.         if (!$user->isTotpAuthenticationEnabled()) {
  51.             $totpSecret $totpAuthenticator->generateSecret();
  52.             $user->setTotpSecret($totpSecret);
  53.         }
  54.         return $this->render('security/2fa_form.html.twig', [
  55.             'qrContent' => base64_encode($totpAuthenticator->getQRContent($user)),
  56.             'secret' => base64_encode($totpSecret),
  57.         ]);
  58.     }
  59.     #[Route('/authentication/2fa/save'name'app_2fa_save'methods: ['POST'])]
  60.     #[IsGranted('ROLE_ADMIN')]
  61.     public function save2fa(
  62.         Request $request,
  63.         TotpAuthenticatorInterface $totpAuthenticator,
  64.         EntityManagerInterface $entityManager,
  65.         RouterInterface $router,
  66.         SessionInterface $session,
  67.     ) {
  68.         $user $this->getUser();
  69.         if (null === $user) {
  70.             return new RedirectResponse($router->generate('app_login'));
  71.         }
  72.         $code $request->request->get('code');
  73.         $secret base64_decode($request->request->get('totp-secret'));
  74.         $user->setTotpSecret($secret);
  75.         if ($totpAuthenticator->checkCode($user$code)) {
  76.             $entityManager->flush();
  77.             $session->getFlashBag()->add('success''2FA Authorization Enabled');
  78.             return new RedirectResponse($router->generate('app_login'));
  79.         }
  80.         return new RedirectResponse($router->generate('app_2fa_enable'));
  81.     }
  82.     #[Route('/authentication/2fa/generate'name'app_2fa_generate')]
  83.     #[IsGranted('ROLE_ADMIN')]
  84.     public function generate2fa(Request $request)
  85.     {
  86.         $result Builder::create()
  87.             ->writer(new PngWriter())
  88.             ->writerOptions([])
  89.             ->data(base64_decode($request->get('qrContent')))
  90.             ->encoding(new Encoding('UTF-8'))
  91.             ->errorCorrectionLevel(new ErrorCorrectionLevelHigh())
  92.             ->size(300)
  93.             ->margin(0)
  94.             ->roundBlockSizeMode(new RoundBlockSizeModeMargin())
  95.             ->build();
  96.         return new Response($result->getString(), 200, ['Content-Type' => 'image/png']);
  97.     }
  98. }