src/Controller/SecurityController.php line 26

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Scheb\TwoFactorBundle\Security\TwoFactor\Provider\Google\GoogleAuthenticatorInterface;
  4. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  5. use Symfony\Component\HttpFoundation\Response;
  6. use Symfony\Component\Routing\Annotation\Route;
  7. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  8. use Endroid\QrCode\Builder\Builder;
  9. use Endroid\QrCode\Encoding\Encoding;
  10. use Endroid\QrCode\ErrorCorrectionLevel\ErrorCorrectionLevelHigh;
  11. use Endroid\QrCode\RoundBlockSizeMode\RoundBlockSizeModeMargin;
  12. use Endroid\QrCode\Writer\PngWriter;
  13. class SecurityController extends AbstractController
  14. {
  15.     public function login(AuthenticationUtils $authenticationUtilsGoogleAuthenticatorInterface $googleAuthenticatorInterface): Response
  16.     {
  17.         // get the login error if there is one
  18.         $error $authenticationUtils->getLastAuthenticationError();
  19.         // last username entered by the user
  20.         $lastUsername $authenticationUtils->getLastUsername();
  21.         return $this->render('backend/login.html.twig', ['last_username' => $lastUsername'error' => $error]);
  22.     }
  23.     /**
  24.      * @Route("/2fa/inProgress", name="2fa_in_progress")
  25.      */
  26.     public function accessibleDuring2fa()
  27.     {
  28.         return new Response('This page is accessible during 2fa');
  29.     }    
  30.     /**
  31.      * @Route("/admin/2fa/qr", name="admin_2fa_qr")
  32.      */
  33.     public function qr2fa(GoogleAuthenticatorInterface $googleAuthenticatorInterface)
  34.     {
  35.         $user $this->getUser();
  36.         if($user->getGoogleAuthenticatorSecret() != null && $user->getGoogleAuthenticatorSecret() != ""){
  37.             return $this->redirectToRoute('admin_homepage');
  38.         }
  39.         else{
  40.             $entityManager $this->getDoctrine()->getManager();
  41.             $secret $googleAuthenticatorInterface->generateSecret();
  42.             $user->setGoogleAuthenticatorSecret($secret);
  43.             $entityManager->persist($user);
  44.             $entityManager->flush();   
  45.     
  46.             // GENERAR QR
  47.             $qrCodeContent $googleAuthenticatorInterface->getQRContent($user);
  48.     
  49.             $qrImage Builder::create()
  50.                 ->writer(new PngWriter())
  51.                 ->writerOptions([])
  52.                 ->data($qrCodeContent)
  53.                 ->encoding(new Encoding('UTF-8'))
  54.                 ->size(200)
  55.                 ->margin(0)
  56.                 ->build();
  57.                 
  58.             return $this->render('backend/2fa_qr.html.twig', ["qrImage" => $qrImage->getDataUri()]);
  59.         }
  60.         
  61.     }     
  62. }