|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/* |
| 6 | + * This file is part of the FOSOAuthServerBundle package. |
| 7 | + * |
| 8 | + * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/> |
| 9 | + * |
| 10 | + * For the full copyright and license information, please view the LICENSE |
| 11 | + * file that was distributed with this source code. |
| 12 | + */ |
| 13 | + |
| 14 | +namespace FOS\OAuthServerBundle\Security\Authentication\Authenticator; |
| 15 | + |
| 16 | +use FOS\OAuthServerBundle\Security\Authentication\Passport\OAuthCredentials; |
| 17 | +use FOS\OAuthServerBundle\Security\Authentication\Token\OAuthToken; |
| 18 | +use OAuth2\OAuth2; |
| 19 | +use OAuth2\OAuth2AuthenticateException; |
| 20 | +use OAuth2\OAuth2ServerException; |
| 21 | +use Symfony\Component\HttpFoundation\Request; |
| 22 | +use Symfony\Component\HttpFoundation\Response; |
| 23 | +use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; |
| 24 | +use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
| 25 | +use Symfony\Component\Security\Core\Exception\AccountStatusException; |
| 26 | +use Symfony\Component\Security\Core\Exception\AuthenticationException; |
| 27 | +use Symfony\Component\Security\Core\User\UserCheckerInterface; |
| 28 | +use Symfony\Component\Security\Core\User\UserProviderInterface; |
| 29 | +use Symfony\Component\Security\Http\Authenticator\AuthenticatorInterface; |
| 30 | +use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge; |
| 31 | +use Symfony\Component\Security\Http\Authenticator\Passport\Passport; |
| 32 | +use Symfony\Component\Security\Http\Authenticator\Passport\PassportInterface; |
| 33 | +use Symfony\Component\Security\Http\Authenticator\Passport\UserPassportInterface; |
| 34 | + |
| 35 | +/** |
| 36 | + * OAuthAuthenticator class. |
| 37 | + * |
| 38 | + * @author Israel J. Carberry <[email protected]> |
| 39 | + */ |
| 40 | +class OAuthAuthenticator implements AuthenticatorInterface |
| 41 | +{ |
| 42 | + /** |
| 43 | + * @var OAuth2 |
| 44 | + */ |
| 45 | + protected $serverService; |
| 46 | + |
| 47 | + /** |
| 48 | + * @var TokenStorageInterface |
| 49 | + */ |
| 50 | + private $tokenStorage; |
| 51 | + |
| 52 | + /** |
| 53 | + * @var UserCheckerInterface |
| 54 | + */ |
| 55 | + protected $userChecker; |
| 56 | + |
| 57 | + /** |
| 58 | + * @var UserProviderInterface |
| 59 | + */ |
| 60 | + protected $userProvider; |
| 61 | + |
| 62 | + public function __construct( |
| 63 | + OAuth2 $serverService, |
| 64 | + TokenStorageInterface $tokenStorage, |
| 65 | + UserCheckerInterface $userChecker, |
| 66 | + UserProviderInterface $userProvider |
| 67 | + ) { |
| 68 | + $this->serverService = $serverService; |
| 69 | + $this->tokenStorage = $tokenStorage; |
| 70 | + $this->userChecker = $userChecker; |
| 71 | + $this->userProvider = $userProvider; |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * {@inheritdoc} |
| 76 | + */ |
| 77 | + public function authenticate(Request $request): UserPassportInterface |
| 78 | + { |
| 79 | + try { |
| 80 | + $token = $this->tokenStorage->getToken(); |
| 81 | + $tokenString = $token->getToken(); |
| 82 | + |
| 83 | + $accessToken = $this->serverService->verifyAccessToken($tokenString); |
| 84 | + |
| 85 | + /** @var \Symfony\Component\Security\Core\User\UserInterface **/ |
| 86 | + $user = $accessToken->getUser(); |
| 87 | + |
| 88 | + if (null === $user) { |
| 89 | + throw new AuthenticationException('OAuth2 authentication failed'); |
| 90 | + } |
| 91 | + |
| 92 | + // check the user |
| 93 | + try { |
| 94 | + $this->userChecker->checkPreAuth($user); |
| 95 | + } catch (AccountStatusException $e) { |
| 96 | + throw new OAuth2AuthenticateException( |
| 97 | + Response::HTTP_UNAUTHORIZED, |
| 98 | + OAuth2::TOKEN_TYPE_BEARER, |
| 99 | + $this->serverService->getVariable(OAuth2::CONFIG_WWW_REALM), |
| 100 | + 'access_denied', |
| 101 | + $e->getMessage() |
| 102 | + ); |
| 103 | + } |
| 104 | + |
| 105 | + return new Passport( |
| 106 | + new UserBadge($user->getUsername()), |
| 107 | + new OAuthCredentials($tokenString, $accessToken->getScope()) |
| 108 | + ); |
| 109 | + } catch (OAuth2ServerException $e) { |
| 110 | + throw new AuthenticationException('OAuth2 authentication failed', 0, $e); |
| 111 | + } |
| 112 | + |
| 113 | + // this should never be reached |
| 114 | + throw new AuthenticationException('OAuth2 authentication failed'); |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * {@inheritdoc} |
| 119 | + */ |
| 120 | + public function createAuthenticatedToken(PassportInterface $passport, string $firewallName): TokenInterface |
| 121 | + { |
| 122 | + try { |
| 123 | + // expect the badges in the passport from authenticate method above |
| 124 | + if (!$passport->hasBadge(OAuthCredentials::class) |
| 125 | + || !$passport->hasBadge(UserBadge::class) |
| 126 | + ) { |
| 127 | + throw new OAuth2AuthenticateException( |
| 128 | + Response::HTTP_UNAUTHORIZED, |
| 129 | + OAuth2::TOKEN_TYPE_BEARER, |
| 130 | + $this->serverService->getVariable(OAuth2::CONFIG_WWW_REALM), |
| 131 | + 'access_denied', |
| 132 | + 'Unexpected credentials type.' |
| 133 | + ); |
| 134 | + } |
| 135 | + |
| 136 | + // get the passport badges |
| 137 | + $credentials = $passport->getBadge(OAuthCredentials::class); |
| 138 | + $user = $this->userProvider->loadUserByUsername( |
| 139 | + $passport->getBadge(UserBadge::class)->getUserIdentifier() |
| 140 | + ); |
| 141 | + |
| 142 | + // check the user |
| 143 | + try { |
| 144 | + $this->userChecker->checkPostAuth($user); |
| 145 | + } catch (AccountStatusException $e) { |
| 146 | + throw new OAuth2AuthenticateException( |
| 147 | + Response::HTTP_UNAUTHORIZED, |
| 148 | + OAuth2::TOKEN_TYPE_BEARER, |
| 149 | + $this->serverService->getVariable(OAuth2::CONFIG_WWW_REALM), |
| 150 | + 'access_denied', |
| 151 | + $e->getMessage() |
| 152 | + ); |
| 153 | + } |
| 154 | + } catch (OAuth2ServerException $e) { |
| 155 | + throw new AuthenticationException('OAuth2 authentication failed', 0, $e); |
| 156 | + } |
| 157 | + |
| 158 | + $token = new OAuthToken($credentials->getRoles($user)); |
| 159 | + $token->setAuthenticated(true); |
| 160 | + $token->setToken($credentials->getTokenString()); |
| 161 | + $token->setUser($user); |
| 162 | + |
| 163 | + $credentials->markResolved(); |
| 164 | + |
| 165 | + return $token; |
| 166 | + } |
| 167 | + |
| 168 | + /** |
| 169 | + * {@inheritdoc} |
| 170 | + */ |
| 171 | + public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $firewallName): ?Response |
| 172 | + { |
| 173 | + return null; |
| 174 | + } |
| 175 | + |
| 176 | + /** |
| 177 | + * {@inheritdoc} |
| 178 | + */ |
| 179 | + public function onAuthenticationFailure(Request $request, AuthenticationException $exception): ?Response |
| 180 | + { |
| 181 | + $responseException = new OAuth2AuthenticateException( |
| 182 | + Response::HTTP_UNAUTHORIZED, |
| 183 | + OAuth2::TOKEN_TYPE_BEARER, |
| 184 | + $this->serverService->getVariable(OAuth2::CONFIG_WWW_REALM), |
| 185 | + 'access_denied', |
| 186 | + $exception->getMessage() |
| 187 | + ); |
| 188 | + |
| 189 | + return $responseException->getHttpResponse(); |
| 190 | + } |
| 191 | + |
| 192 | + /** |
| 193 | + * {@inheritdoc} |
| 194 | + */ |
| 195 | + public function supports(Request $request): ?bool |
| 196 | + { |
| 197 | + return $this->tokenStorage->getToken() instanceof OAuthToken; |
| 198 | + } |
| 199 | +} |
0 commit comments