Skip to content

[Security] Call AuthenticationManager in AnonymousAuthenticationListener #10694

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 25, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
<tag name="monolog.logger" channel="security" />
<argument type="service" id="security.context" />
<argument /> <!-- Key -->
<argument type="service" id="security.authentication.manager" />
<argument type="service" id="logger" on-invalid="null" />
</service>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace Symfony\Component\Security\Http\Firewall;

use Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Core\SecurityContextInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
Expand All @@ -26,13 +28,15 @@ class AnonymousAuthenticationListener implements ListenerInterface
{
private $context;
private $key;
private $authenticationManager;
private $logger;

public function __construct(SecurityContextInterface $context, $key, LoggerInterface $logger = null)
public function __construct(SecurityContextInterface $context, $key, AuthenticationManagerInterface $authenticationManager, LoggerInterface $logger = null)
{
$this->context = $context;
$this->key = $key;
$this->logger = $logger;
$this->context = $context;
$this->key = $key;
$this->authenticationManager = $authenticationManager;
$this->logger = $logger;
}

/**
Expand All @@ -46,10 +50,17 @@ public function handle(GetResponseEvent $event)
return;
}

$this->context->setToken(new AnonymousToken($this->key, 'anon.', array()));
try {
$token = $this->authenticationManager->authenticate(new AnonymousToken($this->key, 'anon.', array()));
$this->context->setToken($token);

if (null !== $this->logger) {
$this->logger->info('Populated SecurityContext with an anonymous Token');
if (null !== $this->logger) {
$this->logger->info('Populated SecurityContext with an anonymous Token');
}
} catch (AuthenticationException $failed) {
if (null !== $this->logger) {
$this->logger->info(sprintf('Anonymous authentication failed: %s', $failed->getMessage()));
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

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

use Symfony\Component\Security\Core\Authentication\Token\AnonymousToken;
use Symfony\Component\Security\Http\Firewall\AnonymousAuthenticationListener;

class AnonymousAuthenticationListenerTest extends \PHPUnit_Framework_TestCase
Expand All @@ -28,7 +29,13 @@ public function testHandleWithContextHavingAToken()
->method('setToken')
;

$listener = new AnonymousAuthenticationListener($context, 'TheKey');
$authenticationManager = $this->getMock('Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface');
$authenticationManager
->expects($this->never())
->method('authenticate')
;

$listener = new AnonymousAuthenticationListener($context, 'TheKey', $authenticationManager);
$listener->handle($this->getMock('Symfony\Component\HttpKernel\Event\GetResponseEvent', array(), array(), '', false));
}

Expand All @@ -40,16 +47,27 @@ public function testHandleWithContextHavingNoToken()
->method('getToken')
->will($this->returnValue(null))
;
$context

$anonymousToken = new AnonymousToken('TheKey', 'anon.', array());

$authenticationManager = $this->getMock('Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface');
$authenticationManager
->expects($this->once())
->method('setToken')
->method('authenticate')
->with(self::logicalAnd(
$this->isInstanceOf('Symfony\Component\Security\Core\Authentication\Token\AnonymousToken'),
$this->attributeEqualTo('key', 'TheKey')
$this->isInstanceOf('Symfony\Component\Security\Core\Authentication\Token\AnonymousToken'),
$this->attributeEqualTo('key', 'TheKey')
))
->will($this->returnValue($anonymousToken))
;

$listener = new AnonymousAuthenticationListener($context, 'TheKey');
$context
->expects($this->once())
->method('setToken')
->with($anonymousToken)
;

$listener = new AnonymousAuthenticationListener($context, 'TheKey', $authenticationManager);
$listener->handle($this->getMock('Symfony\Component\HttpKernel\Event\GetResponseEvent', array(), array(), '', false));
}

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

$listener = new AnonymousAuthenticationListener($context, 'TheKey', $logger);
$authenticationManager = $this->getMock('Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface');

$listener = new AnonymousAuthenticationListener($context, 'TheKey', $authenticationManager, $logger);
$listener->handle($this->getMock('Symfony\Component\HttpKernel\Event\GetResponseEvent', array(), array(), '', false));
}
}