|
| 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\Oidc; |
| 13 | + |
| 14 | +use Jose\Component\Checker; |
| 15 | +use Jose\Component\Checker\ClaimCheckerManager; |
| 16 | +use Jose\Component\Core\Algorithm; |
| 17 | +use Jose\Component\Core\AlgorithmManager; |
| 18 | +use Jose\Component\Core\JWK; |
| 19 | +use Jose\Component\Signature\JWSTokenSupport; |
| 20 | +use Jose\Component\Signature\JWSVerifier; |
| 21 | +use Jose\Component\Signature\Serializer\CompactSerializer; |
| 22 | +use Jose\Component\Signature\Serializer\JWSSerializerManager; |
| 23 | +use Psr\Log\LoggerInterface; |
| 24 | +use Symfony\Component\Clock\NativeClock; |
| 25 | +use Symfony\Component\Security\Core\Exception\BadCredentialsException; |
| 26 | +use Symfony\Component\Security\Http\AccessToken\AccessTokenHandlerInterface; |
| 27 | +use Symfony\Component\Security\Http\AccessToken\Oidc\Exception\InvalidSignatureException; |
| 28 | +use Symfony\Component\Security\Http\AccessToken\Oidc\Exception\MissingClaimException; |
| 29 | +use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge; |
| 30 | + |
| 31 | +/** |
| 32 | + * The token handler decodes and validates the token, and retrieves the user identifier from it. |
| 33 | + * |
| 34 | + * @experimental |
| 35 | + */ |
| 36 | +final class OidcTokenHandler implements AccessTokenHandlerInterface |
| 37 | +{ |
| 38 | + use OidcTrait; |
| 39 | + |
| 40 | + public function __construct( |
| 41 | + private Algorithm $signatureAlgorithm, |
| 42 | + private JWK $jwk, |
| 43 | + private ?LoggerInterface $logger = null, |
| 44 | + private string $claim = 'sub', |
| 45 | + private ?string $audience = null |
| 46 | + ) { |
| 47 | + } |
| 48 | + |
| 49 | + public function getUserBadgeFrom(string $accessToken): UserBadge |
| 50 | + { |
| 51 | + if (!class_exists(JWSVerifier::class) || !class_exists(Checker\HeaderCheckerManager::class)) { |
| 52 | + throw new \LogicException('You cannot use the "oidc" token handler since "web-token/jwt-signature" and "web-token/jwt-checker" are not installed. Try running "composer require web-token/jwt-signature web-token/jwt-checker".'); |
| 53 | + } |
| 54 | + |
| 55 | + try { |
| 56 | + // Decode the token |
| 57 | + $jwsVerifier = new JWSVerifier(new AlgorithmManager([$this->signatureAlgorithm])); |
| 58 | + $serializerManager = new JWSSerializerManager([new CompactSerializer()]); |
| 59 | + $jws = $serializerManager->unserialize($accessToken); |
| 60 | + $claims = json_decode($jws->getPayload(), true); |
| 61 | + |
| 62 | + // Verify the signature |
| 63 | + if (!$jwsVerifier->verifyWithKey($jws, $this->jwk, 0)) { |
| 64 | + throw new InvalidSignatureException(); |
| 65 | + } |
| 66 | + |
| 67 | + // Verify the headers |
| 68 | + $headerCheckerManager = new Checker\HeaderCheckerManager([ |
| 69 | + new Checker\AlgorithmChecker([$this->signatureAlgorithm->name()]), |
| 70 | + ], [ |
| 71 | + new JWSTokenSupport(), |
| 72 | + ]); |
| 73 | + // if this check fails, an InvalidHeaderException is thrown |
| 74 | + $headerCheckerManager->check($jws, 0); |
| 75 | + |
| 76 | + // Verify the claims |
| 77 | + $clock = class_exists(NativeClock::class) ? new NativeClock() : null; |
| 78 | + $checkers = [ |
| 79 | + new Checker\IssuedAtChecker(0, false, $clock), |
| 80 | + new Checker\NotBeforeChecker(0, false, $clock), |
| 81 | + new Checker\ExpirationTimeChecker(0, false, $clock), |
| 82 | + ]; |
| 83 | + if ($this->audience) { |
| 84 | + $checkers[] = new Checker\AudienceChecker($this->audience); |
| 85 | + } |
| 86 | + $claimCheckerManager = new ClaimCheckerManager($checkers); |
| 87 | + // if this check fails, an InvalidClaimException is thrown |
| 88 | + $claimCheckerManager->check($claims); |
| 89 | + |
| 90 | + if (empty($claims[$this->claim])) { |
| 91 | + throw new MissingClaimException(sprintf('"%s" claim not found.', $this->claim)); |
| 92 | + } |
| 93 | + |
| 94 | + // UserLoader argument can be overridden by a UserProvider on AccessTokenAuthenticator::authenticate |
| 95 | + return new UserBadge($claims[$this->claim], fn () => $this->createUser($claims), $claims); |
| 96 | + } catch (\Throwable $e) { |
| 97 | + $this->logger?->error('An error while decoding and validating the token.', [ |
| 98 | + 'error' => $e->getMessage(), |
| 99 | + 'trace' => $e->getTraceAsString(), |
| 100 | + ]); |
| 101 | + |
| 102 | + throw new BadCredentialsException('Invalid credentials.', $e->getCode(), $e); |
| 103 | + } |
| 104 | + } |
| 105 | +} |
0 commit comments