src/BundleExtensions/OAuth2/OAuthProvider.php line 18

Open in your IDE?
  1. <?php
  2. namespace App\BundleExtensions\OAuth2;
  3. use FOS\OAuthServerBundle\Security\Authentication\Provider\OAuthProvider as OAuthProviderBase;
  4. use FOS\OAuthServerBundle\Security\Authentication\Token\OAuthToken;
  5. use OAuth2\OAuth2;
  6. use OAuth2\OAuth2AuthenticateException;
  7. use OAuth2\OAuth2ServerException;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  10. use Symfony\Component\Security\Core\Exception\AccountStatusException;
  11. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  12. /*
  13.  * https://github.com/FriendsOfSymfony/FOSOAuthServerBundle/issues/688
  14.  */
  15. class OAuthProvider extends OAuthProviderBase
  16. {
  17.     /**
  18.      * @param OAuthToken&TokenInterface $token
  19.      *
  20.      * @return OAuthToken|null
  21.      */
  22.     public function authenticate(TokenInterface $token)
  23.     {
  24.         if (!$this->supports($token)) {
  25.             // note: since strict types in PHP 7, return; and return null; are not the same
  26.             // Symfony's interface says to "never return null", but return; is still technically null
  27.             // PHPStan treats return; as return (void);
  28.             return null;
  29.         }
  30.         try {
  31.             $tokenString $token->getToken();
  32.             // TODO: this is nasty, create a proper interface here
  33.             /** @var OAuthToken&TokenInterface&\OAuth2\Model\IOAuth2AccessToken $accessToken */
  34.             $accessToken $this->serverService->verifyAccessToken($tokenString);
  35.             $scope $accessToken->getScope();
  36.             $user $accessToken->getUser();
  37.             if (null === $user) {
  38.                 $user = new ClientCredentialsDummyUser();
  39.             }
  40.             if (null !== $user) {
  41.                 try {
  42.                     $this->userChecker->checkPreAuth($user);
  43.                 } catch (AccountStatusException $e) {
  44.                     throw new OAuth2AuthenticateException(Response::HTTP_UNAUTHORIZEDOAuth2::TOKEN_TYPE_BEARER$this->serverService->getVariable(OAuth2::CONFIG_WWW_REALM), 'access_denied'$e->getMessage());
  45.                 }
  46.                 $token->setUser($user);
  47.             }
  48.             $roles = (null !== $user) ? $user->getRoles() : [];
  49.             if (!empty($scope)) {
  50.                 foreach (explode(' '$scope) as $role) {
  51.                     $roles[] = 'ROLE_' mb_strtoupper($role);
  52.                 }
  53.             }
  54.             $roles array_unique($rolesSORT_REGULAR);
  55.             $token = new OAuthToken($roles);
  56.             $token->setAuthenticated(true);
  57.             $token->setToken($tokenString);
  58.             if (null !== $user) {
  59.                 try {
  60.                     $this->userChecker->checkPostAuth($user);
  61.                 } catch (AccountStatusException $e) {
  62.                     throw new OAuth2AuthenticateException(Response::HTTP_UNAUTHORIZEDOAuth2::TOKEN_TYPE_BEARER$this->serverService->getVariable(OAuth2::CONFIG_WWW_REALM), 'access_denied'$e->getMessage());
  63.                 }
  64.                 $token->setUser($user);
  65.             }
  66.             return $token;
  67.         } catch (OAuth2ServerException $e) {
  68.             throw new AuthenticationException('OAuth2 authentication failed'0$e);
  69.         }
  70.         throw new AuthenticationException('OAuth2 authentication failed');
  71.     }
  72. }