[Security] Add SimpleForm authentication
This commit is contained in:
parent
642c610189
commit
1fe2ed64d1
@ -0,0 +1,93 @@
|
||||
<?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\Bundle\SecurityBundle\DependencyInjection\Security\Factory;
|
||||
|
||||
use Symfony\Component\Config\Definition\Builder\NodeDefinition;
|
||||
use Symfony\Component\DependencyInjection\DefinitionDecorator;
|
||||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||
use Symfony\Component\DependencyInjection\Reference;
|
||||
|
||||
/**
|
||||
* @author Jordi Boggiano <j.boggiano@seld.be>
|
||||
*/
|
||||
class SimpleFormFactory extends FormLoginFactory
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
|
||||
$this->addOption('authenticator', null);
|
||||
}
|
||||
|
||||
public function getKey()
|
||||
{
|
||||
return 'simple-form';
|
||||
}
|
||||
|
||||
public function addConfiguration(NodeDefinition $node)
|
||||
{
|
||||
parent::addConfiguration($node);
|
||||
|
||||
$node->children()
|
||||
->scalarNode('authenticator')->cannotBeEmpty()->end()
|
||||
->end();
|
||||
}
|
||||
|
||||
protected function getListenerId()
|
||||
{
|
||||
return 'security.authentication.listener.simple_form';
|
||||
}
|
||||
|
||||
protected function createAuthProvider(ContainerBuilder $container, $id, $config, $userProviderId)
|
||||
{
|
||||
$provider = 'security.authentication.provider.simple_form.'.$id;
|
||||
$container
|
||||
->setDefinition($provider, new DefinitionDecorator('security.authentication.provider.simple'))
|
||||
->replaceArgument(0, new Reference($config['authenticator']))
|
||||
->replaceArgument(1, new Reference($userProviderId))
|
||||
->replaceArgument(2, $id)
|
||||
;
|
||||
|
||||
return $provider;
|
||||
}
|
||||
|
||||
protected function createListener($container, $id, $config, $userProvider)
|
||||
{
|
||||
$listenerId = parent::createListener($container, $id, $config, $userProvider);
|
||||
|
||||
if (!isset($config['csrf_provider'])) {
|
||||
$container
|
||||
->getDefinition($listenerId)
|
||||
->addArgument(null)
|
||||
;
|
||||
}
|
||||
$container
|
||||
->getDefinition($listenerId)
|
||||
->addArgument(new Reference($config['authenticator']))
|
||||
;
|
||||
|
||||
return $listenerId;
|
||||
}
|
||||
|
||||
protected function createEntryPoint($container, $id, $config, $defaultEntryPoint)
|
||||
{
|
||||
$entryPointId = 'security.authentication.form_entry_point.'.$id;
|
||||
$container
|
||||
->setDefinition($entryPointId, new DefinitionDecorator('security.authentication.form_entry_point'))
|
||||
->addArgument(new Reference('security.http_utils'))
|
||||
->addArgument($config['login_path'])
|
||||
->addArgument($config['use_forward'])
|
||||
;
|
||||
|
||||
return $entryPointId;
|
||||
}
|
||||
}
|
@ -12,6 +12,8 @@
|
||||
<parameter key="security.authentication.form_entry_point.class">Symfony\Component\Security\Http\EntryPoint\FormAuthenticationEntryPoint</parameter>
|
||||
<parameter key="security.authentication.listener.form.class">Symfony\Component\Security\Http\Firewall\UsernamePasswordFormAuthenticationListener</parameter>
|
||||
|
||||
<parameter key="security.authentication.listener.simple_form.class">Symfony\Component\Security\Http\Firewall\SimpleFormAuthenticationListener</parameter>
|
||||
|
||||
<parameter key="security.authentication.listener.basic.class">Symfony\Component\Security\Http\Firewall\BasicAuthenticationListener</parameter>
|
||||
<parameter key="security.authentication.basic_entry_point.class">Symfony\Component\Security\Http\EntryPoint\BasicAuthenticationEntryPoint</parameter>
|
||||
|
||||
@ -35,6 +37,7 @@
|
||||
<parameter key="security.context_listener.class">Symfony\Component\Security\Http\Firewall\ContextListener</parameter>
|
||||
|
||||
<parameter key="security.authentication.provider.dao.class">Symfony\Component\Security\Core\Authentication\Provider\DaoAuthenticationProvider</parameter>
|
||||
<parameter key="security.authentication.provider.simple.class">Symfony\Component\Security\Core\Authentication\Provider\SimpleAuthenticationProvider</parameter>
|
||||
<parameter key="security.authentication.provider.pre_authenticated.class">Symfony\Component\Security\Core\Authentication\Provider\PreAuthenticatedAuthenticationProvider</parameter>
|
||||
|
||||
<parameter key="security.authentication.provider.anonymous.class">Symfony\Component\Security\Core\Authentication\Provider\AnonymousAuthenticationProvider</parameter>
|
||||
@ -133,6 +136,12 @@
|
||||
abstract="true">
|
||||
</service>
|
||||
|
||||
<service id="security.authentication.listener.simple_form"
|
||||
class="%security.authentication.listener.simple_form.class%"
|
||||
parent="security.authentication.listener.abstract"
|
||||
abstract="true">
|
||||
</service>
|
||||
|
||||
<service id="security.authentication.listener.x509" class="%security.authentication.listener.x509.class%" public="false" abstract="true">
|
||||
<tag name="monolog.logger" channel="security" />
|
||||
<argument type="service" id="security.context" />
|
||||
@ -170,6 +179,12 @@
|
||||
<argument>%security.authentication.hide_user_not_found%</argument>
|
||||
</service>
|
||||
|
||||
<service id="security.authentication.provider.simple" class="%security.authentication.provider.simple.class%" abstract="true" public="false">
|
||||
<argument /> <!-- Simple Authenticator -->
|
||||
<argument /> <!-- User Provider -->
|
||||
<argument /> <!-- Provider-shared Key -->
|
||||
</service>
|
||||
|
||||
<service id="security.authentication.provider.pre_authenticated" class="%security.authentication.provider.pre_authenticated.class%" abstract="true" public="false">
|
||||
<argument /> <!-- User Provider -->
|
||||
<argument type="service" id="security.user_checker" />
|
||||
|
@ -19,6 +19,7 @@ use Symfony\Bundle\SecurityBundle\DependencyInjection\Security\Factory\HttpBasic
|
||||
use Symfony\Bundle\SecurityBundle\DependencyInjection\Security\Factory\HttpDigestFactory;
|
||||
use Symfony\Bundle\SecurityBundle\DependencyInjection\Security\Factory\RememberMeFactory;
|
||||
use Symfony\Bundle\SecurityBundle\DependencyInjection\Security\Factory\X509Factory;
|
||||
use Symfony\Bundle\SecurityBundle\DependencyInjection\Security\Factory\SimpleFormFactory;
|
||||
use Symfony\Bundle\SecurityBundle\DependencyInjection\Security\UserProvider\InMemoryFactory;
|
||||
|
||||
/**
|
||||
@ -38,6 +39,7 @@ class SecurityBundle extends Bundle
|
||||
$extension->addSecurityListenerFactory(new HttpDigestFactory());
|
||||
$extension->addSecurityListenerFactory(new RememberMeFactory());
|
||||
$extension->addSecurityListenerFactory(new X509Factory());
|
||||
$extension->addSecurityListenerFactory(new SimpleFormFactory());
|
||||
|
||||
$extension->addUserProviderFactory(new InMemoryFactory());
|
||||
$container->addCompilerPass(new AddSecurityVotersPass());
|
||||
|
@ -0,0 +1,56 @@
|
||||
<?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\Provider;
|
||||
|
||||
use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface;
|
||||
use Symfony\Component\Security\Core\User\UserProviderInterface;
|
||||
use Symfony\Component\Security\Core\User\UserCheckerInterface;
|
||||
use Symfony\Component\Security\Core\User\UserInterface;
|
||||
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
|
||||
use Symfony\Component\Security\Core\Exception\AuthenticationServiceException;
|
||||
use Symfony\Component\Security\Core\Exception\BadCredentialsException;
|
||||
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
|
||||
use Symfony\Component\Security\Core\Authentication\SimpleAuthenticatorInterface;
|
||||
use Symfony\Component\Security\Core\Exception\AuthenticationException;
|
||||
|
||||
/**
|
||||
* @author Jordi Boggiano <j.boggiano@seld.be>
|
||||
*/
|
||||
class SimpleAuthenticationProvider implements AuthenticationProviderInterface
|
||||
{
|
||||
private $simpleAuthenticator;
|
||||
private $userProvider;
|
||||
private $providerKey;
|
||||
|
||||
public function __construct(SimpleAuthenticatorInterface $simpleAuthenticator, UserProviderInterface $userProvider, $providerKey)
|
||||
{
|
||||
$this->simpleAuthenticator = $simpleAuthenticator;
|
||||
$this->userProvider = $userProvider;
|
||||
$this->providerKey = $providerKey;
|
||||
}
|
||||
|
||||
public function authenticate(TokenInterface $token)
|
||||
{
|
||||
$authToken = $this->simpleAuthenticator->authenticate($token, $this->userProvider, $this->providerKey);
|
||||
|
||||
if ($authToken instanceof TokenInterface) {
|
||||
return $authToken;
|
||||
}
|
||||
|
||||
throw new AuthenticationException('Simple authenticator failed to return an authenticated token.');
|
||||
}
|
||||
|
||||
public function supports(TokenInterface $token)
|
||||
{
|
||||
return $this->simpleAuthenticator->supports($token, $this->providerKey);
|
||||
}
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
<?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\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;
|
||||
|
||||
/**
|
||||
* @author Jordi Boggiano <j.boggiano@seld.be>
|
||||
*/
|
||||
interface SimpleAuthenticatorInterface
|
||||
{
|
||||
public function authenticate(TokenInterface $token, UserProviderInterface $userProvider, $providerKey);
|
||||
|
||||
public function supports(TokenInterface $token, $providerKey);
|
||||
|
||||
public function handleAuthenticationFailure(GetResponseEvent $event, AuthenticationException $exception);
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
<?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\Security\Core\Authentication\Token\TokenInterface;
|
||||
use Symfony\Component\Security\Core\Exception\AuthenticationException;
|
||||
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
|
||||
/**
|
||||
* @author Jordi Boggiano <j.boggiano@seld.be>
|
||||
*/
|
||||
interface SimpleFormAuthenticatorInterface extends SimpleAuthenticatorInterface
|
||||
{
|
||||
public function createToken(Request $request, $username, $password, $providerKey);
|
||||
}
|
@ -0,0 +1,116 @@
|
||||
<?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\EventDispatcher\EventDispatcherInterface;
|
||||
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
|
||||
use Symfony\Component\HttpKernel\Log\LoggerInterface;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\Security\Http\Authentication\AuthenticationFailureHandlerInterface;
|
||||
use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface;
|
||||
use Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface;
|
||||
use Symfony\Component\Form\Extension\Csrf\CsrfProvider\CsrfProviderInterface;
|
||||
use Symfony\Component\Security\Core\Authentication\SimpleFormAuthenticatorInterface;
|
||||
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
|
||||
use Symfony\Component\Security\Core\Exception\AuthenticationException;
|
||||
use Symfony\Component\Security\Core\Exception\SessionUnavailableException;
|
||||
use Symfony\Component\Security\Core\SecurityContextInterface;
|
||||
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
|
||||
use Symfony\Component\Security\Http\HttpUtils;
|
||||
use Symfony\Component\Security\Http\RememberMe\RememberMeServicesInterface;
|
||||
use Symfony\Component\Security\Http\SecurityEvents;
|
||||
use Symfony\Component\Security\Http\Session\SessionAuthenticationStrategyInterface;
|
||||
|
||||
/**
|
||||
* @author Jordi Boggiano <j.boggiano@seld.be>
|
||||
*/
|
||||
class SimpleFormAuthenticationListener extends AbstractAuthenticationListener
|
||||
{
|
||||
private $simpleAuthenticator;
|
||||
private $csrfProvider;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param SecurityContextInterface $securityContext A SecurityContext instance
|
||||
* @param AuthenticationManagerInterface $authenticationManager An AuthenticationManagerInterface instance
|
||||
* @param SessionAuthenticationStrategyInterface $sessionStrategy
|
||||
* @param HttpUtils $httpUtils An HttpUtilsInterface instance
|
||||
* @param string $providerKey
|
||||
* @param AuthenticationSuccessHandlerInterface $successHandler
|
||||
* @param AuthenticationFailureHandlerInterface $failureHandler
|
||||
* @param array $options An array of options for the processing of a
|
||||
* successful, or failed authentication attempt
|
||||
* @param LoggerInterface $logger A LoggerInterface instance
|
||||
* @param EventDispatcherInterface $dispatcher An EventDispatcherInterface instance
|
||||
* @param SimpleFormAuthenticatorInterface $simpleAuthenticator A SimpleFormAuthenticatorInterface instance
|
||||
* @param CsrfProviderInterface $csrfProvider A CsrfProviderInterface instance
|
||||
*/
|
||||
public function __construct(SecurityContextInterface $securityContext, AuthenticationManagerInterface $authenticationManager, SessionAuthenticationStrategyInterface $sessionStrategy, HttpUtils $httpUtils, $providerKey, AuthenticationSuccessHandlerInterface $successHandler, AuthenticationFailureHandlerInterface $failureHandler, array $options = array(), LoggerInterface $logger = null, EventDispatcherInterface $dispatcher = null, CsrfProviderInterface $csrfProvider = null, SimpleFormAuthenticatorInterface $simpleAuthenticator = null)
|
||||
{
|
||||
if (!$simpleAuthenticator) {
|
||||
throw new \InvalidArgumentException('Missing simple authenticator');
|
||||
}
|
||||
|
||||
$this->simpleAuthenticator = $simpleAuthenticator;
|
||||
$this->csrfProvider = $csrfProvider;
|
||||
|
||||
$options = array_merge(array(
|
||||
'username_parameter' => '_username',
|
||||
'password_parameter' => '_password',
|
||||
'csrf_parameter' => '_csrf_token',
|
||||
'intention' => 'authenticate',
|
||||
'post_only' => true,
|
||||
), $options);
|
||||
parent::__construct($securityContext, $authenticationManager, $sessionStrategy, $httpUtils, $providerKey, $successHandler, $failureHandler, $options, $logger, $dispatcher);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function requiresAuthentication(Request $request)
|
||||
{
|
||||
if ($this->options['post_only'] && !$request->isMethod('POST')) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return parent::requiresAuthentication($request);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function attemptAuthentication(Request $request)
|
||||
{
|
||||
if (null !== $this->csrfProvider) {
|
||||
$csrfToken = $request->get($this->options['csrf_parameter'], null, true);
|
||||
|
||||
if (false === $this->csrfProvider->isCsrfTokenValid($this->options['intention'], $csrfToken)) {
|
||||
throw new InvalidCsrfTokenException('Invalid CSRF token.');
|
||||
}
|
||||
}
|
||||
|
||||
if ($this->options['post_only']) {
|
||||
$username = trim($request->request->get($this->options['username_parameter'], null, true));
|
||||
$password = $request->request->get($this->options['password_parameter'], null, true);
|
||||
} else {
|
||||
$username = trim($request->get($this->options['username_parameter'], null, true));
|
||||
$password = $request->get($this->options['password_parameter'], null, true);
|
||||
}
|
||||
|
||||
$request->getSession()->set(SecurityContextInterface::LAST_USERNAME, $username);
|
||||
|
||||
$token = $this->simpleAuthenticator->createToken($request, $username, $password, $this->providerKey);
|
||||
|
||||
return $this->authenticationManager->authenticate($token);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user