diff options
author | Jordi Boggiano <j.boggiano@seld.be> | 2013-04-12 13:49:16 +0200 |
---|---|---|
committer | Jordi Boggiano <j.boggiano@seld.be> | 2013-05-08 15:02:39 +0200 |
commit | 3804723c24767f5122a5446cab74db241a7e66bf (patch) | |
tree | 5ef264737aadde056a44a16c97e0df02deef4661 /Http | |
parent | d202ab7471546fe14449117b6c568e286a21ef91 (diff) | |
download | symfony-security-3804723c24767f5122a5446cab74db241a7e66bf.zip symfony-security-3804723c24767f5122a5446cab74db241a7e66bf.tar.gz symfony-security-3804723c24767f5122a5446cab74db241a7e66bf.tar.bz2 |
[Security] Add simple_token auth method
Diffstat (limited to 'Http')
-rw-r--r-- | Http/Firewall/SimpleTokenAuthenticationListener.php | 90 |
1 files changed, 90 insertions, 0 deletions
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 + } +} |