Skip to content

Commit f7a11a1

Browse files
committed
[Security] Add simple_token auth method
1 parent 1fe2ed6 commit f7a11a1

File tree

9 files changed

+199
-10
lines changed

9 files changed

+199
-10
lines changed

src/Symfony/Bundle/SecurityBundle/DependencyInjection/Security/Factory/SimpleFormFactory.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ public function __construct()
2828
$this->addOption('authenticator', null);
2929
}
3030

31+
// TODO create proxies for success_handler/failure_handler that call the impl ones then the default ones by default if no response is returned
32+
3133
public function getKey()
3234
{
3335
return 'simple-form';
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\SecurityBundle\DependencyInjection\Security\Factory;
13+
14+
use Symfony\Component\Config\Definition\Builder\NodeDefinition;
15+
use Symfony\Component\DependencyInjection\DefinitionDecorator;
16+
use Symfony\Component\DependencyInjection\ContainerBuilder;
17+
use Symfony\Component\DependencyInjection\Reference;
18+
19+
/**
20+
* @author Jordi Boggiano <j.boggiano@seld.be>
21+
*/
22+
class SimpleTokenFactory implements SecurityFactoryInterface
23+
{
24+
public function getPosition()
25+
{
26+
return 'http';
27+
}
28+
29+
public function getKey()
30+
{
31+
return 'simple-token';
32+
}
33+
34+
// TODO add support for success_handler/failure_handler that call the impl ones
35+
36+
public function addConfiguration(NodeDefinition $node)
37+
{
38+
$node
39+
->children()
40+
->scalarNode('provider')->end()
41+
->scalarNode('authenticator')->cannotBeEmpty()->end()
42+
->end()
43+
;
44+
}
45+
46+
public function create(ContainerBuilder $container, $id, $config, $userProvider, $defaultEntryPoint)
47+
{
48+
$provider = 'security.authentication.provider.simple_form.'.$id;
49+
$container
50+
->setDefinition($provider, new DefinitionDecorator('security.authentication.provider.simple'))
51+
->replaceArgument(0, new Reference($config['authenticator']))
52+
->replaceArgument(1, new Reference($userProvider))
53+
->replaceArgument(2, $id)
54+
;
55+
56+
// listener
57+
$listenerId = 'security.authentication.listener.simple_token.'.$id;
58+
$listener = $container->setDefinition($listenerId, new DefinitionDecorator('security.authentication.listener.simple_token'));
59+
$listener->replaceArgument(2, $id);
60+
$listener->replaceArgument(3, new Reference($config['authenticator']));
61+
62+
return array($provider, $listenerId);
63+
}
64+
}

src/Symfony/Bundle/SecurityBundle/Resources/config/security_listeners.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
<parameter key="security.authentication.listener.simple_form.class">Symfony\Component\Security\Http\Firewall\SimpleFormAuthenticationListener</parameter>
1616

17+
<parameter key="security.authentication.listener.simple_token.class">Symfony\Component\Security\Http\Firewall\SimpleTokenAuthenticationListener</parameter>
18+
1719
<parameter key="security.authentication.listener.basic.class">Symfony\Component\Security\Http\Firewall\BasicAuthenticationListener</parameter>
1820
<parameter key="security.authentication.basic_entry_point.class">Symfony\Component\Security\Http\EntryPoint\BasicAuthenticationEntryPoint</parameter>
1921

@@ -142,6 +144,15 @@
142144
abstract="true">
143145
</service>
144146

147+
<service id="security.authentication.listener.simple_token" class="%security.authentication.listener.simple_token.class%" public="false" abstract="true">
148+
<tag name="monolog.logger" channel="security" />
149+
<argument type="service" id="security.context" />
150+
<argument type="service" id="security.authentication.manager" />
151+
<argument /> <!-- Provider-shared Key -->
152+
<argument /> <!-- Authenticator -->
153+
<argument type="service" id="logger" on-invalid="null" />
154+
</service>
155+
145156
<service id="security.authentication.listener.x509" class="%security.authentication.listener.x509.class%" public="false" abstract="true">
146157
<tag name="monolog.logger" channel="security" />
147158
<argument type="service" id="security.context" />

src/Symfony/Bundle/SecurityBundle/SecurityBundle.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Symfony\Bundle\SecurityBundle\DependencyInjection\Security\Factory\HttpDigestFactory;
2020
use Symfony\Bundle\SecurityBundle\DependencyInjection\Security\Factory\RememberMeFactory;
2121
use Symfony\Bundle\SecurityBundle\DependencyInjection\Security\Factory\X509Factory;
22+
use Symfony\Bundle\SecurityBundle\DependencyInjection\Security\Factory\SimpleTokenFactory;
2223
use Symfony\Bundle\SecurityBundle\DependencyInjection\Security\Factory\SimpleFormFactory;
2324
use Symfony\Bundle\SecurityBundle\DependencyInjection\Security\UserProvider\InMemoryFactory;
2425

@@ -39,6 +40,7 @@ public function build(ContainerBuilder $container)
3940
$extension->addSecurityListenerFactory(new HttpDigestFactory());
4041
$extension->addSecurityListenerFactory(new RememberMeFactory());
4142
$extension->addSecurityListenerFactory(new X509Factory());
43+
$extension->addSecurityListenerFactory(new SimpleTokenFactory());
4244
$extension->addSecurityListenerFactory(new SimpleFormFactory());
4345

4446
$extension->addUserProviderFactory(new InMemoryFactory());

src/Symfony/Component/Security/Core/Authentication/Provider/SimpleAuthenticationProvider.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function __construct(SimpleAuthenticatorInterface $simpleAuthenticator, U
4040

4141
public function authenticate(TokenInterface $token)
4242
{
43-
$authToken = $this->simpleAuthenticator->authenticate($token, $this->userProvider, $this->providerKey);
43+
$authToken = $this->simpleAuthenticator->authenticateToken($token, $this->userProvider, $this->providerKey);
4444

4545
if ($authToken instanceof TokenInterface) {
4646
return $authToken;
@@ -51,6 +51,6 @@ public function authenticate(TokenInterface $token)
5151

5252
public function supports(TokenInterface $token)
5353
{
54-
return $this->simpleAuthenticator->supports($token, $this->providerKey);
54+
return $this->simpleAuthenticator->supportsToken($token, $this->providerKey);
5555
}
5656
}

src/Symfony/Component/Security/Core/Authentication/SimpleAuthenticatorInterface.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,19 @@
1313

1414
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
1515
use Symfony\Component\Security\Core\Exception\AuthenticationException;
16-
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
17-
use Symfony\Component\HttpFoundation\Request;
1816
use Symfony\Component\Security\Core\User\UserProviderInterface;
17+
use Symfony\Component\HttpFoundation\Request;
1918

2019
/**
2120
* @author Jordi Boggiano <j.boggiano@seld.be>
2221
*/
2322
interface SimpleAuthenticatorInterface
2423
{
25-
public function authenticate(TokenInterface $token, UserProviderInterface $userProvider, $providerKey);
24+
public function authenticateToken(TokenInterface $token, UserProviderInterface $userProvider, $providerKey);
25+
26+
public function supportsToken(TokenInterface $token, $providerKey);
2627

27-
public function supports(TokenInterface $token, $providerKey);
28+
public function onAuthenticationFailure(Request $request, AuthenticationException $exception);
2829

29-
public function handleAuthenticationFailure(GetResponseEvent $event, AuthenticationException $exception);
30+
public function onAuthenticationSuccess(Request $request, TokenInterface $token);
3031
}

src/Symfony/Component/Security/Core/Authentication/SimpleFormAuthenticatorInterface.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@
1111

1212
namespace Symfony\Component\Security\Core\Authentication;
1313

14-
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
15-
use Symfony\Component\Security\Core\Exception\AuthenticationException;
16-
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
1714
use Symfony\Component\HttpFoundation\Request;
1815

1916
/**
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Security\Core\Authentication;
13+
14+
use Symfony\Component\HttpFoundation\Request;
15+
16+
/**
17+
* @author Jordi Boggiano <j.boggiano@seld.be>
18+
*/
19+
interface SimpleTokenAuthenticatorInterface extends SimpleAuthenticatorInterface
20+
{
21+
public function createToken(Request $request, $providerKey);
22+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Security\Http\Firewall;
13+
14+
use Symfony\Component\Security\Core\SecurityContextInterface;
15+
use Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface;
16+
use Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface;
17+
use Psr\Log\LoggerInterface;
18+
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
19+
use Symfony\Component\Security\Core\Authentication\SimpleTokenAuthenticatorInterface;
20+
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
21+
use Symfony\Component\Security\Core\Exception\AuthenticationException;
22+
23+
/**
24+
* SimpleTokenListener implements simple proxying to an authenticator.
25+
*
26+
* @author Jordi Boggiano <j.boggiano@seld.be>
27+
*/
28+
class SimpleTokenAuthenticationListener implements ListenerInterface
29+
{
30+
private $securityContext;
31+
private $authenticationManager;
32+
private $providerKey;
33+
private $simpleAuthenticator;
34+
private $logger;
35+
36+
/**
37+
* Constructor.
38+
*
39+
* @param SecurityContextInterface $securityContext A SecurityContext instance
40+
* @param AuthenticationManagerInterface $authenticationManager An AuthenticationManagerInterface instance
41+
* @param string $providerKey
42+
* @param SimpleTokenAuthenticatorInterface $simpleAuthenticator A SimpleTokenAuthenticatorInterface instance
43+
* @param LoggerInterface $logger A LoggerInterface instance
44+
*/
45+
public function __construct(SecurityContextInterface $securityContext, AuthenticationManagerInterface $authenticationManager, $providerKey, SimpleTokenAuthenticatorInterface $simpleAuthenticator, LoggerInterface $logger = null)
46+
{
47+
if (empty($providerKey)) {
48+
throw new \InvalidArgumentException('$providerKey must not be empty.');
49+
}
50+
51+
$this->securityContext = $securityContext;
52+
$this->authenticationManager = $authenticationManager;
53+
$this->providerKey = $providerKey;
54+
$this->simpleAuthenticator = $simpleAuthenticator;
55+
$this->logger = $logger;
56+
}
57+
58+
/**
59+
* Handles basic authentication.
60+
*
61+
* @param GetResponseEvent $event A GetResponseEvent instance
62+
*/
63+
public function handle(GetResponseEvent $event)
64+
{
65+
$request = $event->getRequest();
66+
67+
if (null !== $this->logger) {
68+
$this->logger->info(sprintf('Attempting simple token authorization %s', $this->providerKey));
69+
}
70+
71+
72+
try {
73+
$token = $this->simpleAuthenticator->createToken($request, $this->providerKey);
74+
$token = $this->authenticationManager->authenticate($token);
75+
$this->securityContext->setToken($token);
76+
77+
} catch (AuthenticationException $failed) {
78+
$this->securityContext->setToken(null);
79+
80+
if (null !== $this->logger) {
81+
$this->logger->info(sprintf('Authentication request failed: %s', $failed->getMessage()));
82+
}
83+
84+
// TODO call failure handler
85+
return;
86+
}
87+
88+
// TODO call success handler
89+
}
90+
}

0 commit comments

Comments
 (0)