Skip to content

Commit

Permalink
migrate session after remember me authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
xabbuh authored and fabpot committed Nov 23, 2015
1 parent 3dc2244 commit f88e600
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
Expand Up @@ -20,6 +20,7 @@
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
use Symfony\Component\Security\Http\SecurityEvents;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Security\Http\Session\SessionAuthenticationStrategy;

/**
* RememberMeListener implements authentication capabilities via a cookie.
Expand All @@ -33,6 +34,7 @@ class RememberMeListener implements ListenerInterface
private $authenticationManager;
private $logger;
private $dispatcher;
private $sessionStrategy;

/**
* Constructor.
Expand All @@ -50,6 +52,7 @@ public function __construct(SecurityContextInterface $securityContext, RememberM
$this->authenticationManager = $authenticationManager;
$this->logger = $logger;
$this->dispatcher = $dispatcher;
$this->sessionStrategy = new SessionAuthenticationStrategy(SessionAuthenticationStrategy::MIGRATE);
}

/**
Expand All @@ -70,6 +73,11 @@ public function handle(GetResponseEvent $event)

try {
$token = $this->authenticationManager->authenticate($token);

if ($request->hasSession() && $request->getSession()->isStarted()) {
$this->sessionStrategy->onAuthentication($request, $token);
}

$this->securityContext->setToken($token);

if (null !== $this->dispatcher) {
Expand Down
Expand Up @@ -138,6 +138,69 @@ public function testOnCoreSecurity()
$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()
{
return $this->getMock('Symfony\Component\HttpKernel\Event\GetResponseEvent', array(), array(), '', false);
Expand Down

0 comments on commit f88e600

Please sign in to comment.