src/App/Security/Authentication/Provider/OAuthProvider.php line 29

Open in your IDE?
  1. <?php
  2. namespace App\Security\Authentication\Provider;
  3. use App\Exception\Security\InvalidAccessTokenException;
  4. use App\Security\Authentication\Token\OAuthToken;
  5. use App\Service\Security\AuthServer;
  6. use Aqarmap\Bundle\UserBundle\Entity\User;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Symfony\Component\Security\Core\Authentication\Provider\AuthenticationProviderInterface;
  9. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  10. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  11. use Symfony\Component\Security\Core\User\UserProviderInterface;
  12. class OAuthProvider implements AuthenticationProviderInterface
  13. {
  14.     private AuthServer $authServer;
  15.     private EntityManagerInterface $entityManager;
  16.     public function __construct(UserProviderInterface $userProviderAuthServer $authServerEntityManagerInterface $entityManager)
  17.     {
  18.         $this->authServer $authServer;
  19.         $this->entityManager $entityManager;
  20.     }
  21.     /**
  22.      * @throws InvalidAccessTokenException
  23.      */
  24.     public function authenticate(TokenInterface $token): ?TokenInterface
  25.     {
  26.         if (!$this->supports($token)) {
  27.             return null;
  28.         }
  29.         try {
  30.             $tokenInfo $this->authServer->getTokenInfo($token->getToken());
  31.             if (!$tokenInfo['oauth_client_id'] ?? null) {
  32.                 throw new AuthenticationException('OAuth2 authentication failed. Client identifier not found.');
  33.             }
  34.             $oauthRoles $tokenInfo['roles'] ?? [];
  35.             $authenticateToken = new OAuthToken($tokenInfo['roles'] ?? []);
  36.             if ($authUerIdentifier $tokenInfo['user']['id'] ?? null) {
  37.                 $userRepo $this->entityManager->getRepository(User::class);
  38.                 if ($user $userRepo->findOneBy(['authIdentifier' => $authUerIdentifier])) {
  39.                     $authenticateToken = new OAuthToken(array_merge($oauthRoles$user->getRoles()));
  40.                     $authenticateToken->setUser($user);
  41.                 } elseif ($user $userRepo->findOneBy(['emailCanonical' => $tokenInfo['user']['email'] ?? null])) {
  42.                     $authenticateToken = new OAuthToken(array_merge($oauthRoles$user->getRoles()));
  43.                     $authenticateToken->setUser($user);
  44.                 } else {
  45.                     // @todo: Signup the user if doesn't exists?
  46.                     throw new AuthenticationException(sprintf('Could not find user profile with identifier "%s".'$authUerIdentifier));
  47.                 }
  48.             }
  49.             $authenticateToken->setAuthenticated(true);
  50.             $authenticateToken->setToken($token->getToken());
  51.             return $authenticateToken;
  52.         } catch (InvalidAccessTokenException $exception) {
  53.             throw $exception;
  54.         } catch (\Exception $exception) {
  55.             throw new AuthenticationException(sprintf('OAuth2 authentication failed. (%s)'$exception->getMessage()), 0$exception);
  56.         }
  57.     }
  58.     public function supports(TokenInterface $token): bool
  59.     {
  60.         return $token instanceof OAuthToken;
  61.     }
  62. }