vendor/friendsofsymfony/oauth-server-bundle/Security/Authentication/Provider/OAuthProvider.php line 33

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4.  * This file is part of the FOSOAuthServerBundle package.
  5.  *
  6.  * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
  7.  *
  8.  * For the full copyright and license information, please view the LICENSE
  9.  * file that was distributed with this source code.
  10.  */
  11. namespace FOS\OAuthServerBundle\Security\Authentication\Provider;
  12. use FOS\OAuthServerBundle\Security\Authentication\Token\OAuthToken;
  13. use OAuth2\OAuth2;
  14. use OAuth2\OAuth2AuthenticateException;
  15. use OAuth2\OAuth2ServerException;
  16. use Symfony\Component\HttpFoundation\Response;
  17. use Symfony\Component\Security\Core\Authentication\Provider\AuthenticationProviderInterface;
  18. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  19. use Symfony\Component\Security\Core\Exception\AccountStatusException;
  20. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  21. use Symfony\Component\Security\Core\User\UserCheckerInterface;
  22. use Symfony\Component\Security\Core\User\UserProviderInterface;
  23. /**
  24.  * OAuthProvider class.
  25.  *
  26.  * @author  Arnaud Le Blanc <arnaud.lb@gmail.com>
  27.  */
  28. class OAuthProvider implements AuthenticationProviderInterface
  29. {
  30.     /**
  31.      * @var UserProviderInterface
  32.      */
  33.     protected $userProvider;
  34.     /**
  35.      * @var OAuth2
  36.      */
  37.     protected $serverService;
  38.     /**
  39.      * @var UserCheckerInterface
  40.      */
  41.     protected $userChecker;
  42.     /**
  43.      * @param UserProviderInterface $userProvider  the user provider
  44.      * @param OAuth2                $serverService the OAuth2 server service
  45.      * @param UserCheckerInterface  $userChecker   The Symfony User Checker for Pre and Post auth checks
  46.      */
  47.     public function __construct(UserProviderInterface $userProviderOAuth2 $serverServiceUserCheckerInterface $userChecker)
  48.     {
  49.         $this->userProvider $userProvider;
  50.         $this->serverService $serverService;
  51.         $this->userChecker $userChecker;
  52.     }
  53.     /**
  54.      * @param OAuthToken&TokenInterface $token
  55.      *
  56.      * @return OAuthToken|null
  57.      */
  58.     public function authenticate(TokenInterface $token)
  59.     {
  60.         if (!$this->supports($token)) {
  61.             // note: since strict types in PHP 7, return; and return null; are not the same
  62.             // Symfony's interface says to "never return null", but return; is still technically null
  63.             // PHPStan treats return; as return (void);
  64.             return null;
  65.         }
  66.         try {
  67.             $tokenString $token->getToken();
  68.             // TODO: this is nasty, create a proper interface here
  69.             /** @var OAuthToken&TokenInterface&\OAuth2\Model\IOAuth2AccessToken $accessToken */
  70.             $accessToken $this->serverService->verifyAccessToken($tokenString);
  71.             $scope $accessToken->getScope();
  72.             $user $accessToken->getUser();
  73.             if (null !== $user) {
  74.                 try {
  75.                     $this->userChecker->checkPreAuth($user);
  76.                 } catch (AccountStatusException $e) {
  77.                     throw new OAuth2AuthenticateException(Response::HTTP_UNAUTHORIZEDOAuth2::TOKEN_TYPE_BEARER$this->serverService->getVariable(OAuth2::CONFIG_WWW_REALM), 'access_denied'$e->getMessage());
  78.                 }
  79.                 $token->setUser($user);
  80.             }
  81.             $roles = (null !== $user) ? $user->getRoles() : [];
  82.             if (!empty($scope)) {
  83.                 foreach (explode(' '$scope) as $role) {
  84.                     $roles[] = 'ROLE_'.mb_strtoupper($role);
  85.                 }
  86.             }
  87.             $roles array_unique($rolesSORT_REGULAR);
  88.             $token = new OAuthToken($roles);
  89.             $token->setAuthenticated(true);
  90.             $token->setToken($tokenString);
  91.             if (null !== $user) {
  92.                 try {
  93.                     $this->userChecker->checkPostAuth($user);
  94.                 } catch (AccountStatusException $e) {
  95.                     throw new OAuth2AuthenticateException(Response::HTTP_UNAUTHORIZEDOAuth2::TOKEN_TYPE_BEARER$this->serverService->getVariable(OAuth2::CONFIG_WWW_REALM), 'access_denied'$e->getMessage());
  96.                 }
  97.                 $token->setUser($user);
  98.             }
  99.             return $token;
  100.         } catch (OAuth2ServerException $e) {
  101.             throw new AuthenticationException('OAuth2 authentication failed'0$e);
  102.         }
  103.         throw new AuthenticationException('OAuth2 authentication failed');
  104.     }
  105.     /**
  106.      * {@inheritdoc}
  107.      */
  108.     public function supports(TokenInterface $token)
  109.     {
  110.         return $token instanceof OAuthToken;
  111.     }
  112. }