Lines
100.00%
35 / 35
Functions and Methods
100.00%
7 / 7
Classes and Traits
100.00%
1 / 1
| Name | Lines | Functions and Methods | CRAP | Classes and Traits | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| GoogleAuthenticator | 100.00% | 35 / 35 | 100.00% | 7 / 7 | 9 | 100.00% | 1 / 1 | |||
| __construct | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| supports | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| authenticate | 100.00% | 8 / 8 | 100.00% | 1 / 1 | 1 | |||||
| getGoogleClient | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | |||||
| getUser | 100.00% | 11 / 11 | 100.00% | 1 / 1 | 2 | |||||
| onAuthenticationSuccess | 100.00% | 6 / 6 | 100.00% | 1 / 1 | 2 | |||||
| onAuthenticationFailure | 100.00% | 7 / 7 | 100.00% | 1 / 1 | 1 | |||||
| 1 | <?php | |
| 2 | ||
| 3 | namespace App\Security; | |
| 4 | ||
| 5 | use App\Entity\User; | |
| 6 | use App\Repository\UserRepository; | |
| 7 | use Doctrine\ORM\EntityManagerInterface; | |
| 8 | use KnpU\OAuth2ClientBundle\Client\ClientRegistry; | |
| 9 | use KnpU\OAuth2ClientBundle\Client\OAuth2ClientInterface; | |
| 10 | use League\OAuth2\Client\Provider\GoogleUser; | |
| 11 | use Symfony\Component\HttpFoundation\RedirectResponse; | |
| 12 | use Symfony\Component\HttpFoundation\Request; | |
| 13 | use Symfony\Component\HttpFoundation\Session\Session; | |
| 14 | use Symfony\Component\Routing\Generator\UrlGeneratorInterface; | |
| 15 | use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; | |
| 16 | use Symfony\Component\Security\Core\Exception\AuthenticationException; | |
| 17 | use Symfony\Component\Security\Http\Authenticator\AbstractAuthenticator; | |
| 18 | use Symfony\Component\Security\Http\Authenticator\Passport\Badge\RememberMeBadge; | |
| 19 | use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge; | |
| 20 | use Symfony\Component\Security\Http\Authenticator\Passport\Passport; | |
| 21 | use Symfony\Component\Security\Http\Authenticator\Passport\SelfValidatingPassport; | |
| 22 | use Symfony\Component\Security\Http\Util\TargetPathTrait; | |
| 23 | ||
| 24 | class GoogleAuthenticator extends AbstractAuthenticator | |
| 25 | { | |
| 26 | use TargetPathTrait; | |
| 27 | ||
| 28 | public function __construct( | |
| 29 | private readonly ClientRegistry $clientRegistry, | |
| 30 | private readonly EntityManagerInterface $entityManager, | |
| 31 | private readonly UserRepository $userRepository, | |
| 32 | private readonly UrlGeneratorInterface $urlGenerator, | |
| 33 | ) { | |
| 34 | } | |
| 35 | ||
| 36 | #[\Override] | |
| 37 | public function supports(Request $request): bool | |
| 38 | { | |
| 39 | return '/connect/google/check' === $request->getPathInfo(); | |
| 40 | } | |
| 41 | ||
| 42 | /** | |
| 43 | * @throws \League\OAuth2\Client\Provider\Exception\IdentityProviderException | |
| 44 | */ | |
| 45 | #[\Override] | |
| 46 | public function authenticate(Request $request): Passport | |
| 47 | { | |
| 48 | $token = $this->getGoogleClient()->getAccessToken(); | |
| 49 | ||
| 50 | /** @var GoogleUser $googleUser */ | |
| 51 | $googleUser = $this->getGoogleClient() | |
| 52 | ->fetchUserFromToken($token); | |
| 53 | ||
| 54 | $user = $this->getUser($googleUser); | |
| 55 | ||
| 56 | return new SelfValidatingPassport( | |
| 57 | new UserBadge($user->getUserIdentifier()), | |
| 58 | [new RememberMeBadge()], | |
| 59 | ); | |
| 60 | } | |
| 61 | ||
| 62 | private function getGoogleClient(): OAuth2ClientInterface | |
| 63 | { | |
| 64 | return $this->clientRegistry->getClient('google'); | |
| 65 | } | |
| 66 | ||
| 67 | private function getUser(GoogleUser $googleUser): User | |
| 68 | { | |
| 69 | $user = $this->userRepository->findOneBy( | |
| 70 | ['googleId' => $googleUser->getId()] | |
| 71 | ); | |
| 72 | ||
| 73 | if (null !== $user) { | |
| 74 | return $user; | |
| 75 | } | |
| 76 | ||
| 77 | // Link to existing account with same email, or register a new user | |
| 78 | $user = $this->userRepository->findOneBy(['identifier' => $googleUser->getEmail()]) | |
| 79 | ?? new User()->setIdentifier((string) $googleUser->getEmail()); | |
| 80 | ||
| 81 | $user->setGoogleId((string) $googleUser->getId()); | |
| 82 | ||
| 83 | $this->entityManager->persist($user); | |
| 84 | $this->entityManager->flush(); | |
| 85 | ||
| 86 | return $user; | |
| 87 | } | |
| 88 | ||
| 89 | #[\Override] | |
| 90 | public function onAuthenticationSuccess( | |
| 91 | Request $request, | |
| 92 | TokenInterface $token, | |
| 93 | string $firewallName | |
| 94 | ): RedirectResponse { | |
| 95 | if ($targetPath = $this->getTargetPath( | |
| 96 | $request->getSession(), | |
| 97 | $firewallName | |
| 98 | ) | |
| 99 | ) { | |
| 100 | return new RedirectResponse($targetPath); | |
| 101 | } | |
| 102 | ||
| 103 | return new RedirectResponse($this->urlGenerator->generate('app_default')); | |
| 104 | } | |
| 105 | ||
| 106 | #[\Override] | |
| 107 | public function onAuthenticationFailure( | |
| 108 | Request $request, | |
| 109 | AuthenticationException $exception, | |
| 110 | ): RedirectResponse { | |
| 111 | $message = \strtr( | |
| 112 | $exception->getMessageKey(), | |
| 113 | $exception->getMessageData() | |
| 114 | ); | |
| 115 | ||
| 116 | /** | |
| 117 | * @var Session $session | |
| 118 | */ | |
| 119 | $session = $request->getSession(); | |
| 120 | $session->getFlashBag()->add('danger', $message); | |
| 121 | ||
| 122 | return new RedirectResponse($this->urlGenerator->generate('app_login')); | |
| 123 | } | |
| 124 | } |