vendor/symfony/security/Http/Authentication/SimpleAuthenticationHandler.php line 21

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Component\Security\Http\Authentication;
  11. use Psr\Log\LoggerInterface;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\Security\Core\Authentication\SimpleAuthenticatorInterface;
  15. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  16. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  17. @trigger_error(sprintf('The "%s" class is deprecated since Symfony 4.2, use Guard instead.'SimpleAuthenticationHandler::class), \E_USER_DEPRECATED);
  18. /**
  19.  * Class to proxy authentication success/failure handlers.
  20.  *
  21.  * Events are sent to the SimpleAuthenticatorInterface if it implements
  22.  * the right interface, otherwise (or if it fails to return a Response)
  23.  * the default handlers are triggered.
  24.  *
  25.  * @author Jordi Boggiano <j.boggiano@seld.be>
  26.  *
  27.  * @deprecated since Symfony 4.2, use Guard instead.
  28.  */
  29. class SimpleAuthenticationHandler implements AuthenticationFailureHandlerInterfaceAuthenticationSuccessHandlerInterface
  30. {
  31.     protected $successHandler;
  32.     protected $failureHandler;
  33.     protected $simpleAuthenticator;
  34.     protected $logger;
  35.     public function __construct(SimpleAuthenticatorInterface $authenticatorAuthenticationSuccessHandlerInterface $successHandlerAuthenticationFailureHandlerInterface $failureHandlerLoggerInterface $logger null)
  36.     {
  37.         $this->simpleAuthenticator $authenticator;
  38.         $this->successHandler $successHandler;
  39.         $this->failureHandler $failureHandler;
  40.         $this->logger $logger;
  41.     }
  42.     /**
  43.      * {@inheritdoc}
  44.      */
  45.     public function onAuthenticationSuccess(Request $requestTokenInterface $token)
  46.     {
  47.         if ($this->simpleAuthenticator instanceof AuthenticationSuccessHandlerInterface) {
  48.             if ($this->logger) {
  49.                 $this->logger->debug('Selected an authentication success handler.', ['handler' => \get_class($this->simpleAuthenticator)]);
  50.             }
  51.             $response $this->simpleAuthenticator->onAuthenticationSuccess($request$token);
  52.             if ($response instanceof Response) {
  53.                 return $response;
  54.             }
  55.             if (null !== $response) {
  56.                 throw new \UnexpectedValueException(sprintf('The "%s::onAuthenticationSuccess()" method must return null to use the default success handler, or a Response object.', \get_class($this->simpleAuthenticator)));
  57.             }
  58.         }
  59.         if ($this->logger) {
  60.             $this->logger->debug('Fallback to the default authentication success handler.');
  61.         }
  62.         return $this->successHandler->onAuthenticationSuccess($request$token);
  63.     }
  64.     /**
  65.      * {@inheritdoc}
  66.      */
  67.     public function onAuthenticationFailure(Request $requestAuthenticationException $exception)
  68.     {
  69.         if ($this->simpleAuthenticator instanceof AuthenticationFailureHandlerInterface) {
  70.             if ($this->logger) {
  71.                 $this->logger->debug('Selected an authentication failure handler.', ['handler' => \get_class($this->simpleAuthenticator)]);
  72.             }
  73.             $response $this->simpleAuthenticator->onAuthenticationFailure($request$exception);
  74.             if ($response instanceof Response) {
  75.                 return $response;
  76.             }
  77.             if (null !== $response) {
  78.                 throw new \UnexpectedValueException(sprintf('The "%s::onAuthenticationFailure()" method must return null to use the default failure handler, or a Response object.', \get_class($this->simpleAuthenticator)));
  79.             }
  80.         }
  81.         if ($this->logger) {
  82.             $this->logger->debug('Fallback to the default authentication failure handler.');
  83.         }
  84.         return $this->failureHandler->onAuthenticationFailure($request$exception);
  85.     }
  86. }