Classes List

Symfony\Component\Security\Http\Authentication\SimpleAuthenticationHandler

 1 
 2 
 3 
 4 
 5 
 6 
 7 
 8 
 9 
 10 
 11 
 12 
 13 
 14 
 15 
 16 
 17 
 18 
 19 
 20 
 21 
 22 
 23 
 24 
 25 
 26 
 27 
 28 
 29 
 30 
 31 
 32 
 33 
 34 
 35 
 36 
 37 
 38 
 39 
 40 
 41 
 42 
 43 
 44 
 45 
 46 
 47 
 48 
 49 
 50 
 51 
 52 
 53 
 54 
 55 
 56 
 57 
 58 
 59 
 60 
 61 
 62 
 63 
 64 
 65 
 66 
 67 
 68 
 69 
 70 
 71 
 72 
 73 
 74 
 75 
 76 
 77 
 78 
 79 
 80 
 81 
 82 
 83 
 84 
 85 
 86 
 87 
 88 
 89 
 90 
 91 
 92 
 93 
 94 
 95 
 96 
 97 
 98 
 99 
 100 
 101 
 102 
 103 
 104 
 105 
<?php

/* * This file is part of the Symfony package. * * (c) Fabien Potencier <fabien@symfony.com> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */


namespace Symfony\Component\Security\Http\Authentication;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Psr\Log\LoggerInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authentication\SimpleAuthenticatorInterface;

/** * Class to proxy authentication success/failure handlers. * * Events are sent to the SimpleAuthenticatorInterface if it implements * the right interface, otherwise (or if it fails to return a Response) * the default handlers are triggered. * * @author Jordi Boggiano <j.boggiano@seld.be> */
class SimpleAuthenticationHandler implements AuthenticationFailureHandlerInterfaceAuthenticationSuccessHandlerInterface
{
    protected $successHandler;
    protected $failureHandler;
    protected $simpleAuthenticator;
    protected $logger;

    /** * @param SimpleAuthenticatorInterface $authenticator SimpleAuthenticatorInterface instance * @param AuthenticationSuccessHandlerInterface $successHandler Default success handler * @param AuthenticationFailureHandlerInterface $failureHandler Default failure handler * @param LoggerInterface $logger Optional logger */
    public function __construct(SimpleAuthenticatorInterface $authenticatorAuthenticationSuccessHandlerInterface $successHandlerAuthenticationFailureHandlerInterface $failureHandlerLoggerInterface $logger = null)
    {
        $this->simpleAuthenticator = $authenticator;
        $this->successHandler = $successHandler;
        $this->failureHandler = $failureHandler;
        $this->logger = $logger;
    }

    /** * {@inheritdoc} */
    public function onAuthenticationSuccess(Request $requestTokenInterface $token)
    {
        if ($this->simpleAuthenticator instanceof AuthenticationSuccessHandlerInterface) {
            if ($this->logger) {
                $this->logger->debug('Selected an authentication success handler.'array('handler' => get_class($this->simpleAuthenticator)));
            }

            $response = $this->simpleAuthenticator->onAuthenticationSuccess($request$token);
            if ($response instanceof Response) {
                return $response;
            }

            if (null !== $response) {
                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)));
            }
        }

        if ($this->logger) {
            $this->logger->debug('Fallback to the default authentication success handler.');
        }

        return $this->successHandler->onAuthenticationSuccess($request$token);
    }

    /** * {@inheritdoc} */
    public function onAuthenticationFailure(Request $requestAuthenticationException $exception)
    {
        if ($this->simpleAuthenticator instanceof AuthenticationFailureHandlerInterface) {
            if ($this->logger) {
                $this->logger->debug('Selected an authentication failure handler.'array('handler' => get_class($this->simpleAuthenticator)));
            }

            $response = $this->simpleAuthenticator->onAuthenticationFailure($request$exception);
            if ($response instanceof Response) {
                return $response;
            }

            if (null !== $response) {
                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)));
            }
        }

        if ($this->logger) {
            $this->logger->debug('Fallback to the default authentication failure handler.');
        }

        return $this->failureHandler->onAuthenticationFailure($request$exception);
    }
}