|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace CodedMonkey\Dirigent\Security; |
| 4 | + |
| 5 | +use CodedMonkey\Dirigent\Doctrine\Entity\User; |
| 6 | +use Doctrine\ORM\EntityManagerInterface; |
| 7 | +use KnpU\OAuth2ClientBundle\Client\ClientRegistry; |
| 8 | +use KnpU\OAuth2ClientBundle\Security\Authenticator\OAuth2Authenticator; |
| 9 | +use Symfony\Component\HttpFoundation\RedirectResponse; |
| 10 | +use Symfony\Component\HttpFoundation\Request; |
| 11 | +use Symfony\Component\HttpFoundation\Response; |
| 12 | +use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
| 13 | +use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
| 14 | +use Symfony\Component\Security\Core\Exception\AuthenticationException; |
| 15 | +use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge; |
| 16 | +use Symfony\Component\Security\Http\Authenticator\Passport\Passport; |
| 17 | +use Symfony\Component\Security\Http\Authenticator\Passport\SelfValidatingPassport; |
| 18 | + |
| 19 | +class OauthAuthenticator extends OAuth2Authenticator |
| 20 | +{ |
| 21 | + public function __construct( |
| 22 | + private readonly ClientRegistry $clientRegistry, |
| 23 | + private readonly EntityManagerInterface $entityManager, |
| 24 | + private readonly UrlGeneratorInterface $router, |
| 25 | + ) { |
| 26 | + } |
| 27 | + |
| 28 | + public function supports(Request $request): ?bool |
| 29 | + { |
| 30 | + return 'oauth_check' === $request->attributes->get('_route'); |
| 31 | + } |
| 32 | + |
| 33 | + public function authenticate(Request $request): Passport |
| 34 | + { |
| 35 | + $providerName = $request->attributes->get('provider'); |
| 36 | + $client = $this->clientRegistry->getClient($providerName); |
| 37 | + $accessToken = $this->fetchAccessToken($client); |
| 38 | + |
| 39 | + return new SelfValidatingPassport( |
| 40 | + new UserBadge($accessToken->getToken(), function () use ($accessToken, $client, $providerName) { |
| 41 | + $remoteUser = $client->fetchUserFromToken($accessToken); |
| 42 | + $data = $remoteUser->toArray(); |
| 43 | + |
| 44 | + $user = $this->entityManager->getRepository(User::class)->findOneBy([ |
| 45 | + 'oauthProvider' => $providerName, |
| 46 | + 'oauthSub' => $data['sub'], |
| 47 | + ]); |
| 48 | + |
| 49 | + if (!$user) { |
| 50 | + $user = new User(); |
| 51 | + $user->setOauthProvider($providerName); |
| 52 | + $user->setOauthSub($data['sub']); |
| 53 | + } |
| 54 | + |
| 55 | + $user->setUsername($data['username']); |
| 56 | + $user->setName($data['name']); |
| 57 | + $user->setEmail($data['email']); |
| 58 | + |
| 59 | + $this->entityManager->persist($user); |
| 60 | + $this->entityManager->flush(); |
| 61 | + |
| 62 | + return $user; |
| 63 | + }) |
| 64 | + ); |
| 65 | + } |
| 66 | + |
| 67 | + public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $firewallName): ?Response |
| 68 | + { |
| 69 | + return new RedirectResponse($this->router->generate('dashboard')); |
| 70 | + } |
| 71 | + |
| 72 | + public function onAuthenticationFailure(Request $request, AuthenticationException $exception): ?Response |
| 73 | + { |
| 74 | + // todo provide user with failure message if it doesn't leak any security details |
| 75 | + return new RedirectResponse($this->router->generate('dashboard_login')); |
| 76 | + } |
| 77 | +} |
0 commit comments