Bouss /
boussimmo
| 1 | <?php |
||||
| 2 | |||||
| 3 | namespace App\Security; |
||||
| 4 | |||||
| 5 | use App\Entity\User; |
||||
| 6 | use App\Repository\UserRepository; |
||||
| 7 | use DateTime; |
||||
| 8 | use Doctrine\ORM\EntityManagerInterface; |
||||
| 9 | use KnpU\OAuth2ClientBundle\Client\ClientRegistry; |
||||
| 10 | use KnpU\OAuth2ClientBundle\Client\OAuth2ClientInterface; |
||||
| 11 | use KnpU\OAuth2ClientBundle\Client\Provider\GoogleClient; |
||||
| 12 | use KnpU\OAuth2ClientBundle\Security\Authenticator\SocialAuthenticator; |
||||
| 13 | use League\OAuth2\Client\Token\AccessToken; |
||||
| 14 | use Symfony\Component\HttpFoundation\RedirectResponse; |
||||
| 15 | use Symfony\Component\HttpFoundation\Request; |
||||
| 16 | use Symfony\Component\HttpFoundation\Response; |
||||
| 17 | use Symfony\Component\Routing\RouterInterface; |
||||
| 18 | use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
||||
| 19 | use Symfony\Component\Security\Core\Exception\AuthenticationException; |
||||
| 20 | use Symfony\Component\Security\Core\User\UserProviderInterface; |
||||
| 21 | |||||
| 22 | class GoogleAuthenticator extends SocialAuthenticator |
||||
| 23 | { |
||||
| 24 | 2 | public function __construct( |
|||
| 25 | private ClientRegistry $clientRegistry, |
||||
| 26 | private EntityManagerInterface $em, |
||||
| 27 | private RouterInterface $router, |
||||
| 28 | private UserRepository $userRepository |
||||
| 29 | 2 | ) {} |
|||
| 30 | |||||
| 31 | /** |
||||
| 32 | * {@inheritDoc} |
||||
| 33 | */ |
||||
| 34 | public function supports(Request $request): bool |
||||
| 35 | { |
||||
| 36 | return $request->attributes->get('_route') === 'connect_google_check'; |
||||
| 37 | } |
||||
| 38 | |||||
| 39 | /** |
||||
| 40 | * {@inheritDoc} |
||||
| 41 | */ |
||||
| 42 | public function getCredentials(Request $request) |
||||
| 43 | { |
||||
| 44 | return $this->fetchAccessToken($this->getGoogleClient()); |
||||
| 45 | } |
||||
| 46 | |||||
| 47 | /** |
||||
| 48 | * {@inheritDoc} |
||||
| 49 | */ |
||||
| 50 | 2 | public function getUser($credentials, UserProviderInterface $userProvider): User |
|||
| 51 | { |
||||
| 52 | /** @var AccessToken $accessToken */ |
||||
| 53 | 2 | $accessToken = $credentials; |
|||
| 54 | 2 | $googleUser = $this->getGoogleClient()->fetchUserFromToken($accessToken); |
|||
| 55 | |||||
| 56 | 2 | $user = $this->userRepository->findOneBy(['googleId' => $googleUser->getId()]); |
|||
| 57 | |||||
| 58 | 2 | if (null === $user) { |
|||
| 59 | 1 | $user = (new User()) |
|||
| 60 | 1 | ->setGoogleId($googleUser->getId()) |
|||
| 61 | 1 | ->setEmail($googleUser->getEmail()) |
|||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
| 62 | 1 | ->setAvatar($googleUser->getAvatar()) |
|||
|
0 ignored issues
–
show
The method
getAvatar() does not exist on League\OAuth2\Client\Pro...\ResourceOwnerInterface. It seems like you code against a sub-type of League\OAuth2\Client\Pro...\ResourceOwnerInterface such as League\OAuth2\Client\Provider\GoogleUser.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 63 | 1 | ->setRefreshToken($accessToken->getRefreshToken()); |
|||
| 64 | 1 | $this->em->persist($user); |
|||
| 65 | } else { |
||||
| 66 | 1 | $user->setRevokedAt(null); |
|||
| 67 | 1 | if (null !== $refreshToken = $accessToken->getRefreshToken()) { |
|||
|
0 ignored issues
–
show
|
|||||
| 68 | $user->setRefreshToken($refreshToken); |
||||
| 69 | } |
||||
| 70 | } |
||||
| 71 | |||||
| 72 | $user |
||||
| 73 | 2 | ->setAccessToken($accessToken) |
|||
| 74 | 2 | ->setAccessTokenCreatedAt(new DateTime('@' . time())) |
|||
| 75 | 2 | ->setAccessTokenExpiresAt(new DateTime('@' . $accessToken->getExpires())); |
|||
| 76 | |||||
| 77 | 2 | $this->em->flush(); |
|||
| 78 | |||||
| 79 | 2 | return $user; |
|||
| 80 | } |
||||
| 81 | |||||
| 82 | /** |
||||
| 83 | * {@inheritDoc} |
||||
| 84 | */ |
||||
| 85 | public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey): ?Response |
||||
| 86 | { |
||||
| 87 | return new RedirectResponse($this->router->generate('default_index')); |
||||
| 88 | } |
||||
| 89 | |||||
| 90 | /** |
||||
| 91 | * {@inheritDoc} |
||||
| 92 | */ |
||||
| 93 | public function onAuthenticationFailure(Request $request, AuthenticationException $exception): ?Response |
||||
| 94 | { |
||||
| 95 | $message = strtr($exception->getMessageKey(), $exception->getMessageData()); |
||||
| 96 | |||||
| 97 | return new Response($message, Response::HTTP_FORBIDDEN); |
||||
| 98 | } |
||||
| 99 | |||||
| 100 | /** |
||||
| 101 | * {@inheritDoc} |
||||
| 102 | */ |
||||
| 103 | public function start(Request $request, AuthenticationException $authException = null): RedirectResponse|Response |
||||
| 104 | { |
||||
| 105 | return new RedirectResponse('/connect/', Response::HTTP_TEMPORARY_REDIRECT); |
||||
| 106 | } |
||||
| 107 | |||||
| 108 | /** |
||||
| 109 | * @return GoogleClient |
||||
| 110 | */ |
||||
| 111 | 2 | private function getGoogleClient(): OAuth2ClientInterface |
|||
| 112 | { |
||||
| 113 | 2 | return $this->clientRegistry->getClient('google'); |
|||
| 114 | } |
||||
| 115 | } |
||||
| 116 |