|
| 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\Authenticator\Debug; |
| 13 | + |
| 14 | +use Symfony\Component\HttpFoundation\Request; |
| 15 | +use Symfony\Component\HttpFoundation\Response; |
| 16 | +use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
| 17 | +use Symfony\Component\Security\Core\Exception\AuthenticationException; |
| 18 | +use Symfony\Component\Security\Http\Authenticator\AuthenticatorInterface; |
| 19 | +use Symfony\Component\Security\Http\Authenticator\InteractiveAuthenticatorInterface; |
| 20 | +use Symfony\Component\Security\Http\Authenticator\Passport\Passport; |
| 21 | +use Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface; |
| 22 | +use Symfony\Component\Security\Http\EntryPoint\Exception\NotAnEntryPointException; |
| 23 | +use Symfony\Component\VarDumper\Caster\ClassStub; |
| 24 | + |
| 25 | +/** |
| 26 | + * Collects info about an authenticator for debugging purposes. |
| 27 | + * |
| 28 | + * @author Robin Chalas <robin.chalas@gmail.com> |
| 29 | + */ |
| 30 | +final class TraceableAuthenticator implements AuthenticatorInterface, InteractiveAuthenticatorInterface, AuthenticationEntryPointInterface |
| 31 | +{ |
| 32 | + private $authenticator; |
| 33 | + private $passport; |
| 34 | + private $duration; |
| 35 | + private $stub; |
| 36 | + |
| 37 | + public function __construct(AuthenticatorInterface $authenticator) |
| 38 | + { |
| 39 | + $this->authenticator = $authenticator; |
| 40 | + } |
| 41 | + |
| 42 | + public function getInfo(): array |
| 43 | + { |
| 44 | + return [ |
| 45 | + 'supports' => true, |
| 46 | + 'passport' => $this->passport, |
| 47 | + 'duration' => $this->duration, |
| 48 | + 'stub' => $this->stub ?? $this->stub = class_exists(ClassStub::class) ? new ClassStub(\get_class($this->authenticator)) : \get_class($this->authenticator), |
| 49 | + ]; |
| 50 | + } |
| 51 | + |
| 52 | + public function supports(Request $request): ?bool |
| 53 | + { |
| 54 | + return $this->authenticator->supports($request); |
| 55 | + } |
| 56 | + |
| 57 | + public function authenticate(Request $request): Passport |
| 58 | + { |
| 59 | + $startTime = microtime(true); |
| 60 | + $this->passport = $this->authenticator->authenticate($request); |
| 61 | + $this->duration = microtime(true) - $startTime; |
| 62 | + |
| 63 | + return $this->passport; |
| 64 | + } |
| 65 | + |
| 66 | + public function createToken(Passport $passport, string $firewallName): TokenInterface |
| 67 | + { |
| 68 | + return method_exists($this->authenticator, 'createToken') ? $this->authenticator->createToken($passport, $firewallName) : $this->authenticator->createAuthenticatedToken($passport, $firewallName); |
| 69 | + } |
| 70 | + |
| 71 | + public function createAuthenticatedToken(Passport $passport, string $firewallName): TokenInterface |
| 72 | + { |
| 73 | + return $this->authenticator->createAuthenticatedToken($passport, $firewallName); |
| 74 | + } |
| 75 | + |
| 76 | + public function onAuthenticationSuccess(Request $request, TokenInterface $token, string $firewallName): ?Response |
| 77 | + { |
| 78 | + return $this->authenticator->onAuthenticationSuccess($request, $token, $firewallName); |
| 79 | + } |
| 80 | + |
| 81 | + public function onAuthenticationFailure(Request $request, AuthenticationException $exception): ?Response |
| 82 | + { |
| 83 | + return $this->authenticator->onAuthenticationFailure($request, $exception); |
| 84 | + } |
| 85 | + |
| 86 | + public function start(Request $request, AuthenticationException $authException = null): Response |
| 87 | + { |
| 88 | + if (!$this->authenticator instanceof AuthenticationEntryPointInterface) { |
| 89 | + throw new NotAnEntryPointException(); |
| 90 | + } |
| 91 | + |
| 92 | + return $this->authenticator->start($request, $authException); |
| 93 | + } |
| 94 | + |
| 95 | + public function isInteractive(): bool |
| 96 | + { |
| 97 | + return $this->authenticator instanceof InteractiveAuthenticatorInterface && $this->authenticator->isInteractive(); |
| 98 | + } |
| 99 | + |
| 100 | + public function __call($method, $args) |
| 101 | + { |
| 102 | + return $this->authenticator->{$method}(...$args); |
| 103 | + } |
| 104 | +} |
0 commit comments