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.     /**
  36.      * @param SimpleAuthenticatorInterface          $authenticator  SimpleAuthenticatorInterface instance
  37.      * @param AuthenticationSuccessHandlerInterface $successHandler Default success handler
  38.      * @param AuthenticationFailureHandlerInterface $failureHandler Default failure handler
  39.      * @param LoggerInterface                       $logger         Optional logger
  40.      */
  41.     public function __construct(SimpleAuthenticatorInterface $authenticatorAuthenticationSuccessHandlerInterface $successHandlerAuthenticationFailureHandlerInterface $failureHandlerLoggerInterface $logger null)
  42.     {
  43.         $this->simpleAuthenticator $authenticator;
  44.         $this->successHandler $successHandler;
  45.         $this->failureHandler $failureHandler;
  46.         $this->logger $logger;
  47.     }
  48.     /**
  49.      * {@inheritdoc}
  50.      */
  51.     public function onAuthenticationSuccess(Request $requestTokenInterface $token)
  52.     {
  53.         if ($this->simpleAuthenticator instanceof AuthenticationSuccessHandlerInterface) {
  54.             if ($this->logger) {
  55.                 $this->logger->debug('Selected an authentication success handler.', ['handler' => \get_class($this->simpleAuthenticator)]);
  56.             }
  57.             $response $this->simpleAuthenticator->onAuthenticationSuccess($request$token);
  58.             if ($response instanceof Response) {
  59.                 return $response;
  60.             }
  61.             if (null !== $response) {
  62.                 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)));
  63.             }
  64.         }
  65.         if ($this->logger) {
  66.             $this->logger->debug('Fallback to the default authentication success handler.');
  67.         }
  68.         return $this->successHandler->onAuthenticationSuccess($request$token);
  69.     }
  70.     /**
  71.      * {@inheritdoc}
  72.      */
  73.     public function onAuthenticationFailure(Request $requestAuthenticationException $exception)
  74.     {
  75.         if ($this->simpleAuthenticator instanceof AuthenticationFailureHandlerInterface) {
  76.             if ($this->logger) {
  77.                 $this->logger->debug('Selected an authentication failure handler.', ['handler' => \get_class($this->simpleAuthenticator)]);
  78.             }
  79.             $response $this->simpleAuthenticator->onAuthenticationFailure($request$exception);
  80.             if ($response instanceof Response) {
  81.                 return $response;
  82.             }
  83.             if (null !== $response) {
  84.                 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)));
  85.             }
  86.         }
  87.         if ($this->logger) {
  88.             $this->logger->debug('Fallback to the default authentication failure handler.');
  89.         }
  90.         return $this->failureHandler->onAuthenticationFailure($request$exception);
  91.     }
  92. }