|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.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 | + |
| 12 | +namespace Symfony\Component\Security\Http\AccessToken\OAuth2; |
| 13 | + |
| 14 | +use Psr\Log\LoggerInterface; |
| 15 | +use Symfony\Component\Security\Core\Exception\AuthenticationException; |
| 16 | +use Symfony\Component\Security\Core\Exception\BadCredentialsException; |
| 17 | +use Symfony\Component\Security\Core\User\OAuth2User; |
| 18 | +use Symfony\Component\Security\Http\AccessToken\AccessTokenHandlerInterface; |
| 19 | +use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge; |
| 20 | +use Symfony\Contracts\HttpClient\HttpClientInterface; |
| 21 | + |
| 22 | +use function Symfony\Component\String\u; |
| 23 | + |
| 24 | +/** |
| 25 | + * The token handler validates the token on the authorization server and the Introspection Endpoint. |
| 26 | + * |
| 27 | + * @see https://tools.ietf.org/html/rfc7662 |
| 28 | + * |
| 29 | + * @internal |
| 30 | + */ |
| 31 | +final class Oauth2TokenHandler implements AccessTokenHandlerInterface |
| 32 | +{ |
| 33 | + public function __construct( |
| 34 | + private readonly HttpClientInterface $client, |
| 35 | + private readonly ?LoggerInterface $logger = null, |
| 36 | + ) { |
| 37 | + } |
| 38 | + |
| 39 | + public function getUserBadgeFrom(string $accessToken): UserBadge |
| 40 | + { |
| 41 | + try { |
| 42 | + // Call the Authorization server to retrieve the resource owner details |
| 43 | + // If the token is invalid or expired, the Authorization server will return an error |
| 44 | + $claims = $this->client->request('POST', '', [ |
| 45 | + 'body' => [ |
| 46 | + 'token' => $accessToken, |
| 47 | + 'token_type_hint' => 'access_token', |
| 48 | + ], |
| 49 | + ])->toArray(); |
| 50 | + |
| 51 | + $sub = $claims['sub'] ?? null; |
| 52 | + $username = $claims['username'] ?? null; |
| 53 | + if (!$sub && !$username) { |
| 54 | + throw new BadCredentialsException('"sub" and "username" claims not found on the authorization server response. At least one is required.'); |
| 55 | + } |
| 56 | + $active = $claims['active'] ?? false; |
| 57 | + if (!$active) { |
| 58 | + throw new BadCredentialsException('The claim "active" was not found on the authorization server response or is set to false.'); |
| 59 | + } |
| 60 | + |
| 61 | + return new UserBadge($sub ?? $username, fn () => $this->createUser($claims), $claims); |
| 62 | + } catch (AuthenticationException $e) { |
| 63 | + $this->logger?->error('An error occurred on the authorization server.', [ |
| 64 | + 'error' => $e->getMessage(), |
| 65 | + 'trace' => $e->getTraceAsString(), |
| 66 | + ]); |
| 67 | + |
| 68 | + throw new BadCredentialsException('Invalid credentials.', $e->getCode(), $e); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + private function createUser(array $claims): OAuth2User |
| 73 | + { |
| 74 | + if (!\function_exists(\Symfony\Component\String\u::class)) { |
| 75 | + throw new \LogicException('You cannot use the "OAuth2TokenHandler" since the String component is not installed. Try running "composer require symfony/string".'); |
| 76 | + } |
| 77 | + |
| 78 | + foreach ($claims as $claim => $value) { |
| 79 | + unset($claims[$claim]); |
| 80 | + if ('' === $value || null === $value) { |
| 81 | + continue; |
| 82 | + } |
| 83 | + $claims[u($claim)->camel()->toString()] = $value; |
| 84 | + } |
| 85 | + |
| 86 | + if ('' !== ($claims['updatedAt'] ?? '')) { |
| 87 | + $claims['updatedAt'] = (new \DateTimeImmutable())->setTimestamp($claims['updatedAt']); |
| 88 | + } |
| 89 | + |
| 90 | + if ('' !== ($claims['emailVerified'] ?? '')) { |
| 91 | + $claims['emailVerified'] = (bool) $claims['emailVerified']; |
| 92 | + } |
| 93 | + |
| 94 | + if ('' !== ($claims['phoneNumberVerified'] ?? '')) { |
| 95 | + $claims['phoneNumberVerified'] = (bool) $claims['phoneNumberVerified']; |
| 96 | + } |
| 97 | + |
| 98 | + return new OAuth2User(...$claims); |
| 99 | + } |
| 100 | +} |
0 commit comments