Skip to content

Commit 941b45e

Browse files
xabbuhfabpot
authored andcommitted
migrate session after remember me authentication
1 parent 4feb527 commit 941b45e

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
2121
use Symfony\Component\Security\Http\SecurityEvents;
2222
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
23+
use Symfony\Component\Security\Http\Session\SessionAuthenticationStrategy;
2324

2425
/**
2526
* RememberMeListener implements authentication capabilities via a cookie.
@@ -34,6 +35,7 @@ class RememberMeListener implements ListenerInterface
3435
private $logger;
3536
private $dispatcher;
3637
private $catchExceptions = true;
38+
private $sessionStrategy;
3739

3840
/**
3941
* Constructor.
@@ -53,6 +55,7 @@ public function __construct(SecurityContextInterface $securityContext, RememberM
5355
$this->logger = $logger;
5456
$this->dispatcher = $dispatcher;
5557
$this->catchExceptions = $catchExceptions;
58+
$this->sessionStrategy = new SessionAuthenticationStrategy(SessionAuthenticationStrategy::MIGRATE);
5659
}
5760

5861
/**
@@ -73,6 +76,11 @@ public function handle(GetResponseEvent $event)
7376

7477
try {
7578
$token = $this->authenticationManager->authenticate($token);
79+
80+
if ($request->hasSession() && $request->getSession()->isStarted()) {
81+
$this->sessionStrategy->onAuthentication($request, $token);
82+
}
83+
7684
$this->securityContext->setToken($token);
7785

7886
if (null !== $this->dispatcher) {

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

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,60 @@ public function testOnCoreSecurity()
181181
$listener->handle($event);
182182
}
183183

184+
public function testSessionStrategy()
185+
{
186+
list($listener, $tokenStorage, $service, $manager) = $this->getListener(false, true, true);
187+
$tokenStorage
188+
->expects($this->once())
189+
->method('getToken')
190+
->will($this->returnValue(null))
191+
;
192+
$token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface');
193+
$service
194+
->expects($this->once())
195+
->method('autoLogin')
196+
->will($this->returnValue($token))
197+
;
198+
$tokenStorage
199+
->expects($this->once())
200+
->method('setToken')
201+
->with($this->equalTo($token))
202+
;
203+
$manager
204+
->expects($this->once())
205+
->method('authenticate')
206+
->will($this->returnValue($token))
207+
;
208+
$session = $this->getMock('\Symfony\Component\HttpFoundation\Session\SessionInterface');
209+
$session
210+
->expects($this->once())
211+
->method('isStarted')
212+
->will($this->returnValue(true))
213+
;
214+
$session
215+
->expects($this->once())
216+
->method('migrate')
217+
;
218+
$request = $this->getMock('\Symfony\Component\HttpFoundation\Request');
219+
$request
220+
->expects($this->any())
221+
->method('hasSession')
222+
->will($this->returnValue(true))
223+
;
224+
$request
225+
->expects($this->any())
226+
->method('getSession')
227+
->will($this->returnValue($session))
228+
;
229+
$event = $this->getGetResponseEvent();
230+
$event
231+
->expects($this->once())
232+
->method('getRequest')
233+
->will($this->returnValue($request))
234+
;
235+
$listener->handle($event);
236+
}
237+
184238
public function testOnCoreSecurityInteractiveLoginEventIsDispatchedIfDispatcherIsPresent()
185239
{
186240
list($listener, $context, $service, $manager, , $dispatcher) = $this->getListener(true);

0 commit comments

Comments
 (0)