migrate session after remember me authentication
This commit is contained in:
parent
3dc2244187
commit
f88e600833
|
@ -20,6 +20,7 @@ use Symfony\Component\Security\Http\RememberMe\RememberMeServicesInterface;
|
||||||
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
|
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
|
||||||
use Symfony\Component\Security\Http\SecurityEvents;
|
use Symfony\Component\Security\Http\SecurityEvents;
|
||||||
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
||||||
|
use Symfony\Component\Security\Http\Session\SessionAuthenticationStrategy;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* RememberMeListener implements authentication capabilities via a cookie.
|
* RememberMeListener implements authentication capabilities via a cookie.
|
||||||
|
@ -33,6 +34,7 @@ class RememberMeListener implements ListenerInterface
|
||||||
private $authenticationManager;
|
private $authenticationManager;
|
||||||
private $logger;
|
private $logger;
|
||||||
private $dispatcher;
|
private $dispatcher;
|
||||||
|
private $sessionStrategy;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor.
|
* Constructor.
|
||||||
|
@ -50,6 +52,7 @@ class RememberMeListener implements ListenerInterface
|
||||||
$this->authenticationManager = $authenticationManager;
|
$this->authenticationManager = $authenticationManager;
|
||||||
$this->logger = $logger;
|
$this->logger = $logger;
|
||||||
$this->dispatcher = $dispatcher;
|
$this->dispatcher = $dispatcher;
|
||||||
|
$this->sessionStrategy = new SessionAuthenticationStrategy(SessionAuthenticationStrategy::MIGRATE);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -70,6 +73,11 @@ class RememberMeListener implements ListenerInterface
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$token = $this->authenticationManager->authenticate($token);
|
$token = $this->authenticationManager->authenticate($token);
|
||||||
|
|
||||||
|
if ($request->hasSession() && $request->getSession()->isStarted()) {
|
||||||
|
$this->sessionStrategy->onAuthentication($request, $token);
|
||||||
|
}
|
||||||
|
|
||||||
$this->securityContext->setToken($token);
|
$this->securityContext->setToken($token);
|
||||||
|
|
||||||
if (null !== $this->dispatcher) {
|
if (null !== $this->dispatcher) {
|
||||||
|
|
|
@ -138,6 +138,69 @@ class RememberMeListenerTest extends \PHPUnit_Framework_TestCase
|
||||||
$listener->handle($event);
|
$listener->handle($event);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testSessionStrategy()
|
||||||
|
{
|
||||||
|
list($listener, $tokenStorage, $service, $manager) = $this->getListener(false, true, true);
|
||||||
|
|
||||||
|
$tokenStorage
|
||||||
|
->expects($this->once())
|
||||||
|
->method('getToken')
|
||||||
|
->will($this->returnValue(null))
|
||||||
|
;
|
||||||
|
|
||||||
|
$token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface');
|
||||||
|
$service
|
||||||
|
->expects($this->once())
|
||||||
|
->method('autoLogin')
|
||||||
|
->will($this->returnValue($token))
|
||||||
|
;
|
||||||
|
|
||||||
|
$tokenStorage
|
||||||
|
->expects($this->once())
|
||||||
|
->method('setToken')
|
||||||
|
->with($this->equalTo($token))
|
||||||
|
;
|
||||||
|
|
||||||
|
$manager
|
||||||
|
->expects($this->once())
|
||||||
|
->method('authenticate')
|
||||||
|
->will($this->returnValue($token))
|
||||||
|
;
|
||||||
|
|
||||||
|
$session = $this->getMock('\Symfony\Component\HttpFoundation\Session\SessionInterface');
|
||||||
|
$session
|
||||||
|
->expects($this->once())
|
||||||
|
->method('isStarted')
|
||||||
|
->will($this->returnValue(true))
|
||||||
|
;
|
||||||
|
$session
|
||||||
|
->expects($this->once())
|
||||||
|
->method('migrate')
|
||||||
|
;
|
||||||
|
|
||||||
|
$request = $this->getMock('\Symfony\Component\HttpFoundation\Request');
|
||||||
|
$request
|
||||||
|
->expects($this->any())
|
||||||
|
->method('hasSession')
|
||||||
|
->will($this->returnValue(true))
|
||||||
|
;
|
||||||
|
|
||||||
|
$request
|
||||||
|
->expects($this->any())
|
||||||
|
->method('getSession')
|
||||||
|
->will($this->returnValue($session))
|
||||||
|
;
|
||||||
|
|
||||||
|
$event = $this->getGetResponseEvent();
|
||||||
|
$event
|
||||||
|
->expects($this->once())
|
||||||
|
->method('getRequest')
|
||||||
|
->will($this->returnValue($request))
|
||||||
|
;
|
||||||
|
|
||||||
|
$listener->handle($event);
|
||||||
|
}
|
||||||
|
|
||||||
protected function getGetResponseEvent()
|
protected function getGetResponseEvent()
|
||||||
{
|
{
|
||||||
return $this->getMock('Symfony\Component\HttpKernel\Event\GetResponseEvent', array(), array(), '', false);
|
return $this->getMock('Symfony\Component\HttpKernel\Event\GetResponseEvent', array(), array(), '', false);
|
||||||
|
|
Reference in New Issue