diff options
5 files changed, 120 insertions, 10 deletions
diff --git a/Core/Authentication/Provider/SimpleAuthenticationProvider.php b/Core/Authentication/Provider/SimpleAuthenticationProvider.php index 72b82cb..8f8cceb 100644 --- a/Core/Authentication/Provider/SimpleAuthenticationProvider.php +++ b/Core/Authentication/Provider/SimpleAuthenticationProvider.php @@ -40,7 +40,7 @@ class SimpleAuthenticationProvider implements AuthenticationProviderInterface public function authenticate(TokenInterface $token) { - $authToken = $this->simpleAuthenticator->authenticate($token, $this->userProvider, $this->providerKey); + $authToken = $this->simpleAuthenticator->authenticateToken($token, $this->userProvider, $this->providerKey); if ($authToken instanceof TokenInterface) { return $authToken; @@ -51,6 +51,6 @@ class SimpleAuthenticationProvider implements AuthenticationProviderInterface public function supports(TokenInterface $token) { - return $this->simpleAuthenticator->supports($token, $this->providerKey); + return $this->simpleAuthenticator->supportsToken($token, $this->providerKey); } } diff --git a/Core/Authentication/SimpleAuthenticatorInterface.php b/Core/Authentication/SimpleAuthenticatorInterface.php index 81f761f..fbbaa37 100644 --- a/Core/Authentication/SimpleAuthenticatorInterface.php +++ b/Core/Authentication/SimpleAuthenticatorInterface.php @@ -13,18 +13,19 @@ namespace Symfony\Component\Security\Core\Authentication; use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; use Symfony\Component\Security\Core\Exception\AuthenticationException; -use Symfony\Component\HttpKernel\Event\GetResponseEvent; -use Symfony\Component\HttpFoundation\Request; use Symfony\Component\Security\Core\User\UserProviderInterface; +use Symfony\Component\HttpFoundation\Request; /** * @author Jordi Boggiano <j.boggiano@seld.be> */ interface SimpleAuthenticatorInterface { - public function authenticate(TokenInterface $token, UserProviderInterface $userProvider, $providerKey); + public function authenticateToken(TokenInterface $token, UserProviderInterface $userProvider, $providerKey); + + public function supportsToken(TokenInterface $token, $providerKey); - public function supports(TokenInterface $token, $providerKey); + public function onAuthenticationFailure(Request $request, AuthenticationException $exception); - public function handleAuthenticationFailure(GetResponseEvent $event, AuthenticationException $exception); + public function onAuthenticationSuccess(Request $request, TokenInterface $token); } diff --git a/Core/Authentication/SimpleFormAuthenticatorInterface.php b/Core/Authentication/SimpleFormAuthenticatorInterface.php index 79fdb1c..95ee881 100644 --- a/Core/Authentication/SimpleFormAuthenticatorInterface.php +++ b/Core/Authentication/SimpleFormAuthenticatorInterface.php @@ -11,9 +11,6 @@ namespace Symfony\Component\Security\Core\Authentication; -use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; -use Symfony\Component\Security\Core\Exception\AuthenticationException; -use Symfony\Component\HttpKernel\Event\GetResponseEvent; use Symfony\Component\HttpFoundation\Request; /** diff --git a/Core/Authentication/SimpleTokenAuthenticatorInterface.php b/Core/Authentication/SimpleTokenAuthenticatorInterface.php new file mode 100644 index 0000000..a611754 --- /dev/null +++ b/Core/Authentication/SimpleTokenAuthenticatorInterface.php @@ -0,0 +1,22 @@ +<?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\Core\Authentication; + +use Symfony\Component\HttpFoundation\Request; + +/** + * @author Jordi Boggiano <j.boggiano@seld.be> + */ +interface SimpleTokenAuthenticatorInterface extends SimpleAuthenticatorInterface +{ + public function createToken(Request $request, $providerKey); +} diff --git a/Http/Firewall/SimpleTokenAuthenticationListener.php b/Http/Firewall/SimpleTokenAuthenticationListener.php new file mode 100644 index 0000000..cda535b --- /dev/null +++ b/Http/Firewall/SimpleTokenAuthenticationListener.php @@ -0,0 +1,90 @@ +<?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\Firewall; + +use Symfony\Component\Security\Core\SecurityContextInterface; +use Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface; +use Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface; +use Psr\Log\LoggerInterface; +use Symfony\Component\HttpKernel\Event\GetResponseEvent; +use Symfony\Component\Security\Core\Authentication\SimpleTokenAuthenticatorInterface; +use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; +use Symfony\Component\Security\Core\Exception\AuthenticationException; + +/** + * SimpleTokenListener implements simple proxying to an authenticator. + * + * @author Jordi Boggiano <j.boggiano@seld.be> + */ +class SimpleTokenAuthenticationListener implements ListenerInterface +{ + private $securityContext; + private $authenticationManager; + private $providerKey; + private $simpleAuthenticator; + private $logger; + + /** + * Constructor. + * + * @param SecurityContextInterface $securityContext A SecurityContext instance + * @param AuthenticationManagerInterface $authenticationManager An AuthenticationManagerInterface instance + * @param string $providerKey + * @param SimpleTokenAuthenticatorInterface $simpleAuthenticator A SimpleTokenAuthenticatorInterface instance + * @param LoggerInterface $logger A LoggerInterface instance + */ + public function __construct(SecurityContextInterface $securityContext, AuthenticationManagerInterface $authenticationManager, $providerKey, SimpleTokenAuthenticatorInterface $simpleAuthenticator, LoggerInterface $logger = null) + { + if (empty($providerKey)) { + throw new \InvalidArgumentException('$providerKey must not be empty.'); + } + + $this->securityContext = $securityContext; + $this->authenticationManager = $authenticationManager; + $this->providerKey = $providerKey; + $this->simpleAuthenticator = $simpleAuthenticator; + $this->logger = $logger; + } + + /** + * Handles basic authentication. + * + * @param GetResponseEvent $event A GetResponseEvent instance + */ + public function handle(GetResponseEvent $event) + { + $request = $event->getRequest(); + + if (null !== $this->logger) { + $this->logger->info(sprintf('Attempting simple token authorization %s', $this->providerKey)); + } + + + try { + $token = $this->simpleAuthenticator->createToken($request, $this->providerKey); + $token = $this->authenticationManager->authenticate($token); + $this->securityContext->setToken($token); + + } catch (AuthenticationException $failed) { + $this->securityContext->setToken(null); + + if (null !== $this->logger) { + $this->logger->info(sprintf('Authentication request failed: %s', $failed->getMessage())); + } + + // TODO call failure handler + return; + } + + // TODO call success handler + } +} |