Call AuthenticationManager in AnonymousAuthenticationListener
This commit is contained in:
parent
a0f10d3bbf
commit
78fa5e285b
@ -54,6 +54,7 @@
|
|||||||
<tag name="monolog.logger" channel="security" />
|
<tag name="monolog.logger" channel="security" />
|
||||||
<argument type="service" id="security.context" />
|
<argument type="service" id="security.context" />
|
||||||
<argument /> <!-- Key -->
|
<argument /> <!-- Key -->
|
||||||
|
<argument type="service" id="security.authentication.manager" />
|
||||||
<argument type="service" id="logger" on-invalid="null" />
|
<argument type="service" id="logger" on-invalid="null" />
|
||||||
</service>
|
</service>
|
||||||
|
|
||||||
|
@ -11,6 +11,8 @@
|
|||||||
|
|
||||||
namespace Symfony\Component\Security\Http\Firewall;
|
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 Symfony\Component\Security\Core\SecurityContextInterface;
|
||||||
use Psr\Log\LoggerInterface;
|
use Psr\Log\LoggerInterface;
|
||||||
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
|
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
|
||||||
@ -26,13 +28,15 @@ class AnonymousAuthenticationListener implements ListenerInterface
|
|||||||
{
|
{
|
||||||
private $context;
|
private $context;
|
||||||
private $key;
|
private $key;
|
||||||
|
private $authenticationManager;
|
||||||
private $logger;
|
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->context = $context;
|
||||||
$this->key = $key;
|
$this->key = $key;
|
||||||
$this->logger = $logger;
|
$this->authenticationManager = $authenticationManager;
|
||||||
|
$this->logger = $logger;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -46,10 +50,17 @@ class AnonymousAuthenticationListener implements ListenerInterface
|
|||||||
return;
|
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) {
|
if (null !== $this->logger) {
|
||||||
$this->logger->info('Populated SecurityContext with an anonymous Token');
|
$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()));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -11,6 +11,7 @@
|
|||||||
|
|
||||||
namespace Symfony\Component\Security\Http\Tests\Firewall;
|
namespace Symfony\Component\Security\Http\Tests\Firewall;
|
||||||
|
|
||||||
|
use Symfony\Component\Security\Core\Authentication\Token\AnonymousToken;
|
||||||
use Symfony\Component\Security\Http\Firewall\AnonymousAuthenticationListener;
|
use Symfony\Component\Security\Http\Firewall\AnonymousAuthenticationListener;
|
||||||
|
|
||||||
class AnonymousAuthenticationListenerTest extends \PHPUnit_Framework_TestCase
|
class AnonymousAuthenticationListenerTest extends \PHPUnit_Framework_TestCase
|
||||||
@ -28,7 +29,13 @@ class AnonymousAuthenticationListenerTest extends \PHPUnit_Framework_TestCase
|
|||||||
->method('setToken')
|
->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));
|
$listener->handle($this->getMock('Symfony\Component\HttpKernel\Event\GetResponseEvent', array(), array(), '', false));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -40,16 +47,27 @@ class AnonymousAuthenticationListenerTest extends \PHPUnit_Framework_TestCase
|
|||||||
->method('getToken')
|
->method('getToken')
|
||||||
->will($this->returnValue(null))
|
->will($this->returnValue(null))
|
||||||
;
|
;
|
||||||
|
|
||||||
|
$anonymousToken = new AnonymousToken('TheKey', 'anon.', array());
|
||||||
|
|
||||||
|
$authenticationManager = $this->getMock('Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface');
|
||||||
|
$authenticationManager
|
||||||
|
->expects($this->once())
|
||||||
|
->method('authenticate')
|
||||||
|
->with(self::logicalAnd(
|
||||||
|
$this->isInstanceOf('Symfony\Component\Security\Core\Authentication\Token\AnonymousToken'),
|
||||||
|
$this->attributeEqualTo('key', 'TheKey')
|
||||||
|
))
|
||||||
|
->will($this->returnValue($anonymousToken))
|
||||||
|
;
|
||||||
|
|
||||||
$context
|
$context
|
||||||
->expects($this->once())
|
->expects($this->once())
|
||||||
->method('setToken')
|
->method('setToken')
|
||||||
->with(self::logicalAnd(
|
->with($anonymousToken)
|
||||||
$this->isInstanceOf('Symfony\Component\Security\Core\Authentication\Token\AnonymousToken'),
|
|
||||||
$this->attributeEqualTo('key', 'TheKey')
|
|
||||||
))
|
|
||||||
;
|
;
|
||||||
|
|
||||||
$listener = new AnonymousAuthenticationListener($context, 'TheKey');
|
$listener = new AnonymousAuthenticationListener($context, 'TheKey', $authenticationManager);
|
||||||
$listener->handle($this->getMock('Symfony\Component\HttpKernel\Event\GetResponseEvent', array(), array(), '', false));
|
$listener->handle($this->getMock('Symfony\Component\HttpKernel\Event\GetResponseEvent', array(), array(), '', false));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -66,7 +84,9 @@ class AnonymousAuthenticationListenerTest extends \PHPUnit_Framework_TestCase
|
|||||||
->with('Populated SecurityContext with an anonymous Token')
|
->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));
|
$listener->handle($this->getMock('Symfony\Component\HttpKernel\Event\GetResponseEvent', array(), array(), '', false));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user