SimpleAuthenticationHandler.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. /**
  18. * Class to proxy authentication success/failure handlers.
  19. *
  20. * Events are sent to the SimpleAuthenticatorInterface if it implements
  21. * the right interface, otherwise (or if it fails to return a Response)
  22. * the default handlers are triggered.
  23. *
  24. * @author Jordi Boggiano <j.boggiano@seld.be>
  25. */
  26. class SimpleAuthenticationHandler implements AuthenticationFailureHandlerInterface, AuthenticationSuccessHandlerInterface
  27. {
  28. protected $successHandler;
  29. protected $failureHandler;
  30. protected $simpleAuthenticator;
  31. protected $logger;
  32. /**
  33. * @param SimpleAuthenticatorInterface $authenticator SimpleAuthenticatorInterface instance
  34. * @param AuthenticationSuccessHandlerInterface $successHandler Default success handler
  35. * @param AuthenticationFailureHandlerInterface $failureHandler Default failure handler
  36. * @param LoggerInterface $logger Optional logger
  37. */
  38. public function __construct(SimpleAuthenticatorInterface $authenticator, AuthenticationSuccessHandlerInterface $successHandler, AuthenticationFailureHandlerInterface $failureHandler, LoggerInterface $logger = null)
  39. {
  40. $this->simpleAuthenticator = $authenticator;
  41. $this->successHandler = $successHandler;
  42. $this->failureHandler = $failureHandler;
  43. $this->logger = $logger;
  44. }
  45. /**
  46. * {@inheritdoc}
  47. */
  48. public function onAuthenticationSuccess(Request $request, TokenInterface $token)
  49. {
  50. if ($this->simpleAuthenticator instanceof AuthenticationSuccessHandlerInterface) {
  51. if ($this->logger) {
  52. $this->logger->debug('Selected an authentication success handler.', array('handler' => \get_class($this->simpleAuthenticator)));
  53. }
  54. $response = $this->simpleAuthenticator->onAuthenticationSuccess($request, $token);
  55. if ($response instanceof Response) {
  56. return $response;
  57. }
  58. if (null !== $response) {
  59. 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)));
  60. }
  61. }
  62. if ($this->logger) {
  63. $this->logger->debug('Fallback to the default authentication success handler.');
  64. }
  65. return $this->successHandler->onAuthenticationSuccess($request, $token);
  66. }
  67. /**
  68. * {@inheritdoc}
  69. */
  70. public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
  71. {
  72. if ($this->simpleAuthenticator instanceof AuthenticationFailureHandlerInterface) {
  73. if ($this->logger) {
  74. $this->logger->debug('Selected an authentication failure handler.', array('handler' => \get_class($this->simpleAuthenticator)));
  75. }
  76. $response = $this->simpleAuthenticator->onAuthenticationFailure($request, $exception);
  77. if ($response instanceof Response) {
  78. return $response;
  79. }
  80. if (null !== $response) {
  81. 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)));
  82. }
  83. }
  84. if ($this->logger) {
  85. $this->logger->debug('Fallback to the default authentication failure handler.');
  86. }
  87. return $this->failureHandler->onAuthenticationFailure($request, $exception);
  88. }
  89. }