Skip to content

Commit cc396ff

Browse files
committed
feature #10694 [Security] Call AuthenticationManager in AnonymousAuthenticationListener (Kacper Gunia)
This PR was merged into the 2.6-dev branch. Discussion ---------- [Security] Call AuthenticationManager in AnonymousAuthenticationListener | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | yes | Deprecations? | no | Tests pass? | yes | Fixed tickets | #10651 | License | MIT | Doc PR | - Commits ------- 78fa5e2 Call AuthenticationManager in AnonymousAuthenticationListener
2 parents 499c1dd + 78fa5e2 commit cc396ff

File tree

3 files changed

+46
-14
lines changed

3 files changed

+46
-14
lines changed

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

+1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
<tag name="monolog.logger" channel="security" />
5757
<argument type="service" id="security.context" />
5858
<argument /> <!-- Key -->
59+
<argument type="service" id="security.authentication.manager" />
5960
<argument type="service" id="logger" on-invalid="null" />
6061
</service>
6162

src/Symfony/Component/Security/Http/Firewall/AnonymousAuthenticationListener.php

+18-7
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Symfony\Component\Security\Http\Firewall;
1313

14+
use Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface;
15+
use Symfony\Component\Security\Core\Exception\AuthenticationException;
1416
use Symfony\Component\Security\Core\SecurityContextInterface;
1517
use Psr\Log\LoggerInterface;
1618
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
@@ -26,13 +28,15 @@ class AnonymousAuthenticationListener implements ListenerInterface
2628
{
2729
private $context;
2830
private $key;
31+
private $authenticationManager;
2932
private $logger;
3033

31-
public function __construct(SecurityContextInterface $context, $key, LoggerInterface $logger = null)
34+
public function __construct(SecurityContextInterface $context, $key, AuthenticationManagerInterface $authenticationManager, LoggerInterface $logger = null)
3235
{
33-
$this->context = $context;
34-
$this->key = $key;
35-
$this->logger = $logger;
36+
$this->context = $context;
37+
$this->key = $key;
38+
$this->authenticationManager = $authenticationManager;
39+
$this->logger = $logger;
3640
}
3741

3842
/**
@@ -46,10 +50,17 @@ public function handle(GetResponseEvent $event)
4650
return;
4751
}
4852

49-
$this->context->setToken(new AnonymousToken($this->key, 'anon.', array()));
53+
try {
54+
$token = $this->authenticationManager->authenticate(new AnonymousToken($this->key, 'anon.', array()));
55+
$this->context->setToken($token);
5056

51-
if (null !== $this->logger) {
52-
$this->logger->info('Populated SecurityContext with an anonymous Token');
57+
if (null !== $this->logger) {
58+
$this->logger->info('Populated SecurityContext with an anonymous Token');
59+
}
60+
} catch (AuthenticationException $failed) {
61+
if (null !== $this->logger) {
62+
$this->logger->info(sprintf('Anonymous authentication failed: %s', $failed->getMessage()));
63+
}
5364
}
5465
}
5566
}

src/Symfony/Component/Security/Http/Tests/Firewall/AnonymousAuthenticationListenerTest.php

+27-7
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\Security\Http\Tests\Firewall;
1313

14+
use Symfony\Component\Security\Core\Authentication\Token\AnonymousToken;
1415
use Symfony\Component\Security\Http\Firewall\AnonymousAuthenticationListener;
1516

1617
class AnonymousAuthenticationListenerTest extends \PHPUnit_Framework_TestCase
@@ -28,7 +29,13 @@ public function testHandleWithContextHavingAToken()
2829
->method('setToken')
2930
;
3031

31-
$listener = new AnonymousAuthenticationListener($context, 'TheKey');
32+
$authenticationManager = $this->getMock('Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface');
33+
$authenticationManager
34+
->expects($this->never())
35+
->method('authenticate')
36+
;
37+
38+
$listener = new AnonymousAuthenticationListener($context, 'TheKey', $authenticationManager);
3239
$listener->handle($this->getMock('Symfony\Component\HttpKernel\Event\GetResponseEvent', array(), array(), '', false));
3340
}
3441

@@ -40,16 +47,27 @@ public function testHandleWithContextHavingNoToken()
4047
->method('getToken')
4148
->will($this->returnValue(null))
4249
;
43-
$context
50+
51+
$anonymousToken = new AnonymousToken('TheKey', 'anon.', array());
52+
53+
$authenticationManager = $this->getMock('Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface');
54+
$authenticationManager
4455
->expects($this->once())
45-
->method('setToken')
56+
->method('authenticate')
4657
->with(self::logicalAnd(
47-
$this->isInstanceOf('Symfony\Component\Security\Core\Authentication\Token\AnonymousToken'),
48-
$this->attributeEqualTo('key', 'TheKey')
58+
$this->isInstanceOf('Symfony\Component\Security\Core\Authentication\Token\AnonymousToken'),
59+
$this->attributeEqualTo('key', 'TheKey')
4960
))
61+
->will($this->returnValue($anonymousToken))
5062
;
5163

52-
$listener = new AnonymousAuthenticationListener($context, 'TheKey');
64+
$context
65+
->expects($this->once())
66+
->method('setToken')
67+
->with($anonymousToken)
68+
;
69+
70+
$listener = new AnonymousAuthenticationListener($context, 'TheKey', $authenticationManager);
5371
$listener->handle($this->getMock('Symfony\Component\HttpKernel\Event\GetResponseEvent', array(), array(), '', false));
5472
}
5573

@@ -66,7 +84,9 @@ public function testHandledEventIsLogged()
6684
->with('Populated SecurityContext with an anonymous Token')
6785
;
6886

69-
$listener = new AnonymousAuthenticationListener($context, 'TheKey', $logger);
87+
$authenticationManager = $this->getMock('Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface');
88+
89+
$listener = new AnonymousAuthenticationListener($context, 'TheKey', $authenticationManager, $logger);
7090
$listener->handle($this->getMock('Symfony\Component\HttpKernel\Event\GetResponseEvent', array(), array(), '', false));
7191
}
7292
}

0 commit comments

Comments
 (0)