Skip to content

Commit 0ce1d81

Browse files
Merge branch '4.3' into 4.4
* 4.3: [Messenger] add tests to FailedMessagesShowCommand Fix the translation commands when a template contains a syntax error [Security] Fix clearing remember-me cookie after deauthentication [Validator] Update Slovenian translations [Config][ReflectionClassResource] Handle parameters with undefined constant as their default values fix dumping number-like string parameters Fix CI [Console] Fix autocomplete multibyte input support [Config] don't break on virtual stack frames in ClassExistenceResource more robust initialization from request
2 parents 7629834 + 75e96df commit 0ce1d81

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

Firewall/ContextListener.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
use Symfony\Component\Security\Core\User\UserInterface;
3333
use Symfony\Component\Security\Core\User\UserProviderInterface;
3434
use Symfony\Component\Security\Http\Event\DeauthenticatedEvent;
35+
use Symfony\Component\Security\Http\RememberMe\RememberMeServicesInterface;
3536

3637
/**
3738
* ContextListener manages the SecurityContext persistence through a session.
@@ -52,6 +53,7 @@ class ContextListener extends AbstractListener implements ListenerInterface
5253
private $dispatcher;
5354
private $registered;
5455
private $trustResolver;
56+
private $rememberMeServices;
5557
private $sessionTrackerEnabler;
5658

5759
/**
@@ -136,6 +138,10 @@ public function authenticate(RequestEvent $event)
136138

137139
if ($token instanceof TokenInterface) {
138140
$token = $this->refreshUser($token);
141+
142+
if (!$token && $this->rememberMeServices) {
143+
$this->rememberMeServices->loginFail($request);
144+
}
139145
} elseif (null !== $token) {
140146
if (null !== $this->logger) {
141147
$this->logger->warning('Expected a security token from the session, got something else.', ['key' => $this->sessionKey, 'received' => $token]);
@@ -315,4 +321,9 @@ public static function handleUnserializeCallback($class)
315321
{
316322
throw new \ErrorException('Class not found: '.$class, 0x37313bc);
317323
}
324+
325+
public function setRememberMeServices(RememberMeServicesInterface $rememberMeServices)
326+
{
327+
$this->rememberMeServices = $rememberMeServices;
328+
}
318329
}

Tests/Firewall/ContextListenerTest.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
use Symfony\Component\Security\Core\User\UserProviderInterface;
3737
use Symfony\Component\Security\Http\Event\DeauthenticatedEvent;
3838
use Symfony\Component\Security\Http\Firewall\ContextListener;
39+
use Symfony\Component\Security\Http\RememberMe\RememberMeServicesInterface;
3940
use Symfony\Contracts\Service\ServiceLocatorTrait;
4041

4142
class ContextListenerTest extends TestCase
@@ -262,10 +263,23 @@ public function testIfTokenIsNotDeauthenticated()
262263
$tokenStorage = new TokenStorage();
263264
$badRefreshedUser = new User('foobar', 'baz');
264265
$goodRefreshedUser = new User('foobar', 'bar');
265-
$tokenStorage = $this->handleEventWithPreviousSession([new SupportingUserProvider($badRefreshedUser), new SupportingUserProvider($goodRefreshedUser)], $goodRefreshedUser, true);
266+
$tokenStorage = $this->handleEventWithPreviousSession([new SupportingUserProvider($badRefreshedUser), new SupportingUserProvider($goodRefreshedUser)], $goodRefreshedUser);
266267
$this->assertSame($goodRefreshedUser, $tokenStorage->getToken()->getUser());
267268
}
268269

270+
public function testRememberMeGetsCanceledIfTokenIsDeauthenticated()
271+
{
272+
$tokenStorage = new TokenStorage();
273+
$refreshedUser = new User('foobar', 'baz');
274+
275+
$rememberMeServices = $this->createMock(RememberMeServicesInterface::class);
276+
$rememberMeServices->expects($this->once())->method('loginFail');
277+
278+
$tokenStorage = $this->handleEventWithPreviousSession([new NotSupportingUserProvider(), new SupportingUserProvider($refreshedUser)], null, $rememberMeServices);
279+
280+
$this->assertNull($tokenStorage->getToken());
281+
}
282+
269283
public function testTryAllUserProvidersUntilASupportingUserProviderIsFound()
270284
{
271285
$refreshedUser = new User('foobar', 'baz');
@@ -374,7 +388,7 @@ protected function runSessionOnKernelResponse($newToken, $original = null)
374388
return $session;
375389
}
376390

377-
private function handleEventWithPreviousSession($userProviders, UserInterface $user = null)
391+
private function handleEventWithPreviousSession($userProviders, UserInterface $user = null, RememberMeServicesInterface $rememberMeServices = null)
378392
{
379393
$user = $user ?: new User('foo', 'bar');
380394
$session = new Session(new MockArraySessionStorage());
@@ -399,6 +413,10 @@ private function handleEventWithPreviousSession($userProviders, UserInterface $u
399413
}
400414

401415
$listener = new ContextListener($tokenStorage, $userProviders, 'context_key', null, null, null, $sessionTrackerEnabler);
416+
417+
if ($rememberMeServices) {
418+
$listener->setRememberMeServices($rememberMeServices);
419+
}
402420
$listener(new RequestEvent($this->getMockBuilder(HttpKernelInterface::class)->getMock(), $request, HttpKernelInterface::MASTER_REQUEST));
403421

404422
if (null !== $usageIndex) {

0 commit comments

Comments
 (0)