|
| 1 | +<?php |
| 2 | +/* |
| 3 | + * (c) Minh Vuong <vuongxuongminh@gmail.com> |
| 4 | + * |
| 5 | + * This source file is subject to the MIT license that is bundled |
| 6 | + * with this source code in the file LICENSE. |
| 7 | + */ |
| 8 | + |
| 9 | +declare(strict_types=1); |
| 10 | + |
| 11 | +namespace Istio\Symfony\JWTAuthentication\Authenticator; |
| 12 | + |
| 13 | +use Istio\Symfony\JWTAuthentication\User\JWTPayloadAwareUserProviderInterface; |
| 14 | +use Nyholm\Psr7\Factory\Psr17Factory; |
| 15 | +use Psr\Http\Message\ServerRequestInterface; |
| 16 | +use Symfony\Bridge\PsrHttpMessage\Factory\PsrHttpFactory; |
| 17 | +use Symfony\Component\HttpFoundation\Request; |
| 18 | +use Symfony\Component\HttpFoundation\Response; |
| 19 | +use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
| 20 | +use Symfony\Component\Security\Core\Exception\AuthenticationException; |
| 21 | +use Symfony\Component\Security\Core\User\UserProviderInterface; |
| 22 | +use Symfony\Component\Security\Http\Authenticator\AbstractAuthenticator; |
| 23 | +use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge; |
| 24 | +use Symfony\Component\Security\Http\Authenticator\Passport\PassportInterface; |
| 25 | +use Symfony\Component\Security\Http\Authenticator\Passport\SelfValidatingPassport; |
| 26 | +use Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface; |
| 27 | + |
| 28 | +final class Authenticator extends AbstractAuthenticator implements AuthenticationEntryPointInterface |
| 29 | +{ |
| 30 | + public function __construct( |
| 31 | + private iterable $userIdentifierClaimMappings, |
| 32 | + private UserProviderInterface $userProvider |
| 33 | + ) { |
| 34 | + } |
| 35 | + |
| 36 | + public function supports(Request $request): ?bool |
| 37 | + { |
| 38 | + $psr7Request = $this->normalizeRequest($request); |
| 39 | + |
| 40 | + foreach ($this->userIdentifierClaimMappings as $mapping) { |
| 41 | + /** @var UserIdentifierClaimMapping $mapping */ |
| 42 | + $payload = $mapping->extractor()->extract($psr7Request); |
| 43 | + |
| 44 | + if (null !== $payload && false !== is_string($payload[$mapping->userIdentifierClaim()] ?? null)) { |
| 45 | + $request->attributes->set( |
| 46 | + '_user_identifier_and_payload', |
| 47 | + [ |
| 48 | + $mapping->userIdentifierClaim(), |
| 49 | + $payload, |
| 50 | + ] |
| 51 | + ); |
| 52 | + |
| 53 | + return true; |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + return false; |
| 58 | + } |
| 59 | + |
| 60 | + public function authenticate(Request $request): PassportInterface |
| 61 | + { |
| 62 | + [$userIdentifierClaim, $payload] = $request->attributes->get('_user_identifier_and_payload'); |
| 63 | + $request->attributes->remove('_user_identifier_and_payload'); |
| 64 | + $userBadge = new UserBadge($payload[$userIdentifierClaim], $this->makeUserLoader($payload)); |
| 65 | + $passport = new SelfValidatingPassport($userBadge); |
| 66 | + $passport->setAttribute('_payload', $payload); |
| 67 | + |
| 68 | + return $passport; |
| 69 | + } |
| 70 | + |
| 71 | + private function makeUserLoader(array $payload): callable |
| 72 | + { |
| 73 | + return function (string $userIdentifier) use ($payload) { |
| 74 | + if ($this->userProvider instanceof JWTPayloadAwareUserProviderInterface) { |
| 75 | + return $this->userProvider->loadUserByIdentifier($userIdentifier, $payload); |
| 76 | + } |
| 77 | + |
| 78 | + return $this->userProvider->loadUserByIdentifier($userIdentifier); |
| 79 | + }; |
| 80 | + } |
| 81 | + |
| 82 | + public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $firewallName): ?Response |
| 83 | + { |
| 84 | + return null; |
| 85 | + } |
| 86 | + |
| 87 | + public function onAuthenticationFailure(Request $request, AuthenticationException $exception): ?Response |
| 88 | + { |
| 89 | + throw $exception; |
| 90 | + } |
| 91 | + |
| 92 | + public function createAuthenticatedToken(PassportInterface $passport, string $firewallName): TokenInterface |
| 93 | + { |
| 94 | + /** @var SelfValidatingPassport $passport */ |
| 95 | + $payload = $passport->getAttribute('_payload'); |
| 96 | + $token = parent::createAuthenticatedToken($passport, $firewallName); |
| 97 | + $token->setAttribute('jwt_payload', $payload); |
| 98 | + |
| 99 | + return $token; |
| 100 | + } |
| 101 | + |
| 102 | + public function start(Request $request, AuthenticationException $authException = null) |
| 103 | + { |
| 104 | + return new Response('Istio JWT in request\'s missing or invalid.', Response::HTTP_UNAUTHORIZED); |
| 105 | + } |
| 106 | + |
| 107 | + private function normalizeRequest(Request $request): ServerRequestInterface |
| 108 | + { |
| 109 | + $psr17Factory = new Psr17Factory(); |
| 110 | + $psrHttpFactory = new PsrHttpFactory($psr17Factory, $psr17Factory, $psr17Factory, $psr17Factory); |
| 111 | + |
| 112 | + return $psrHttpFactory->createRequest($request); |
| 113 | + } |
| 114 | +} |
0 commit comments