src/Security/GoogleAuthenticator.php line 23

Open in your IDE?
  1. <?php
  2. namespace App\Security;
  3. use App\Entity\Club;
  4. use App\Entity\ClubManager;
  5. use App\Entity\User;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use KnpU\OAuth2ClientBundle\Client\ClientRegistry;
  8. use KnpU\OAuth2ClientBundle\Security\Authenticator\OAuth2Authenticator;
  9. use League\OAuth2\Client\Provider\GoogleUser;
  10. use Symfony\Component\HttpFoundation\RedirectResponse;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\Routing\RouterInterface;
  14. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  15. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  16. use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
  17. use Symfony\Component\Security\Http\Authenticator\Passport\Passport;
  18. use Symfony\Component\Security\Http\Authenticator\Passport\SelfValidatingPassport;
  19. use Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface;
  20. class GoogleAuthenticator extends OAuth2Authenticator implements AuthenticationEntrypointInterface
  21. {
  22.     private $clientRegistry;
  23.     private $entityManager;
  24.     private $router;
  25.     public function __construct(ClientRegistry $clientRegistryEntityManagerInterface $entityManagerRouterInterface $router)
  26.     {
  27.         $this->clientRegistry $clientRegistry;
  28.         $this->entityManager $entityManager;
  29.         $this->router $router;
  30.     }
  31.     public function supports(Request $request): ?bool
  32.     {
  33.         // continue ONLY if the current ROUTE matches the check ROUTE
  34.         return $request->attributes->get('_route') === 'connect_google_check';
  35.     }
  36.     public function authenticate(Request $request): Passport
  37.     {
  38.         $client $this->clientRegistry->getClient('google');
  39.         $accessToken $this->fetchAccessToken($client);
  40.         return new SelfValidatingPassport(
  41.             new UserBadge($accessToken->getToken(), function() use ($accessToken$client) {
  42.                 /** @var GoogleUser $googleUser */
  43.                 $googleUser $client->fetchUserFromToken($accessToken);
  44.                 $email $googleUser->getEmail();
  45.                 $existingUser $this->entityManager->getRepository(User::class)->findOneBy(['googleId' => $googleUser->getId()]);
  46.                 if ($existingUser) {
  47.                     return $existingUser;
  48.                 }
  49.                 $user $this->entityManager->getRepository(User::class)->findOneBy(['email' => $email]);
  50.                 if ($user){
  51.                     $user->setGoogleId($googleUser->getId());
  52.                     $this->entityManager->persist($user);
  53.                     $this->entityManager->flush();
  54.                 }
  55.                 return $user;
  56.             })
  57.         );
  58.     }
  59.     public function onAuthenticationSuccess(Request $requestTokenInterface $tokenstring $firewallName): ?Response
  60.     {
  61.         $targetUrl $this->router->generate('home');
  62.         return new RedirectResponse($targetUrl);
  63.     }
  64.     public function onAuthenticationFailure(Request $requestAuthenticationException $exception): ?Response
  65.     {
  66.         $targetUrl $this->router->generate('home');
  67.         return new RedirectResponse($targetUrl);
  68.     }
  69.     /**
  70.      * Called when authentication is needed, but it's not sent.
  71.      * This redirects to the 'login'.
  72.      */
  73.     public function start(Request $requestAuthenticationException $authException null): Response
  74.     {
  75.         return new RedirectResponse(
  76.             '/login',
  77.             Response::HTTP_TEMPORARY_REDIRECT
  78.         );
  79.     }
  80. }