Skip to content

Commit 7741021

Browse files
committed
Fixed autoLogin() returning null
1 parent 106a460 commit 7741021

File tree

2 files changed

+21
-29
lines changed

2 files changed

+21
-29
lines changed

Authenticator/RememberMeAuthenticator.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
use Symfony\Component\Security\Core\Exception\AuthenticationException;
2020
use Symfony\Component\Security\Http\Authenticator\Passport\PassportInterface;
2121
use Symfony\Component\Security\Http\Authenticator\Passport\SelfValidatingPassport;
22-
use Symfony\Component\Security\Http\RememberMe\AbstractRememberMeServices;
2322
use Symfony\Component\Security\Http\RememberMe\RememberMeServicesInterface;
2423

2524
/**
@@ -57,21 +56,23 @@ public function supports(Request $request): ?bool
5756
return false;
5857
}
5958

60-
if (($cookie = $request->attributes->get(AbstractRememberMeServices::COOKIE_ATTR_NAME)) && null === $cookie->getValue()) {
59+
$token = $this->rememberMeServices->autoLogin($request);
60+
if (null === $token) {
6161
return false;
6262
}
6363

64-
if (isset($this->options['name']) && !$request->cookies->has($this->options['name'])) {
65-
return false;
66-
}
64+
$request->attributes->set('_remember_me_token', $token);
6765

6866
// the `null` return value indicates that this authenticator supports lazy firewalls
6967
return null;
7068
}
7169

7270
public function authenticate(Request $request): PassportInterface
7371
{
74-
$token = $this->rememberMeServices->autoLogin($request);
72+
$token = $request->attributes->get('_remember_me_token');
73+
if (null === $token) {
74+
throw new \LogicException('No remember me token is set.');
75+
}
7576

7677
return new SelfValidatingPassport($token->getUser());
7778
}

Tests/Authenticator/RememberMeAuthenticatorTest.php

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,12 @@
1212
namespace Symfony\Component\Security\Http\Tests\Authenticator;
1313

1414
use PHPUnit\Framework\TestCase;
15-
use Symfony\Component\HttpFoundation\Cookie;
1615
use Symfony\Component\HttpFoundation\Request;
1716
use Symfony\Component\Security\Core\Authentication\Token\RememberMeToken;
1817
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
1918
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
2019
use Symfony\Component\Security\Core\User\User;
2120
use Symfony\Component\Security\Http\Authenticator\RememberMeAuthenticator;
22-
use Symfony\Component\Security\Http\RememberMe\AbstractRememberMeServices;
2321
use Symfony\Component\Security\Http\RememberMe\RememberMeServicesInterface;
2422

2523
class RememberMeAuthenticatorTest extends TestCase
@@ -37,8 +35,6 @@ protected function setUp(): void
3735
'name' => '_remember_me_cookie',
3836
]);
3937
$this->request = new Request();
40-
$this->request->cookies->set('_remember_me_cookie', $val = $this->generateCookieValue());
41-
$this->request->attributes->set(AbstractRememberMeServices::COOKIE_ATTR_NAME, new Cookie('_remember_me_cookie', $val));
4238
}
4339

4440
public function testSupportsTokenStorageWithToken()
@@ -48,39 +44,34 @@ public function testSupportsTokenStorageWithToken()
4844
$this->assertFalse($this->authenticator->supports($this->request));
4945
}
5046

51-
public function testSupportsRequestWithoutAttribute()
47+
/**
48+
* @dataProvider provideSupportsData
49+
*/
50+
public function testSupports($autoLoginResult, $support)
5251
{
53-
$this->request->attributes->remove(AbstractRememberMeServices::COOKIE_ATTR_NAME);
52+
$this->rememberMeServices->expects($this->once())->method('autoLogin')->with($this->request)->willReturn($autoLoginResult);
5453

55-
$this->assertNull($this->authenticator->supports($this->request));
54+
$this->assertSame($support, $this->authenticator->supports($this->request));
5655
}
5756

58-
public function testSupportsRequestWithoutCookie()
57+
public function provideSupportsData()
5958
{
60-
$this->request->cookies->remove('_remember_me_cookie');
61-
62-
$this->assertFalse($this->authenticator->supports($this->request));
63-
}
64-
65-
public function testSupports()
66-
{
67-
$this->assertNull($this->authenticator->supports($this->request));
59+
yield [null, false];
60+
yield [$this->createMock(TokenInterface::class), null];
6861
}
6962

7063
public function testAuthenticate()
7164
{
72-
$this->rememberMeServices->expects($this->once())
73-
->method('autoLogin')
74-
->with($this->request)
75-
->willReturn(new RememberMeToken($user = new User('wouter', 'test'), 'main', 'secret'));
76-
65+
$this->request->attributes->set('_remember_me_token', new RememberMeToken($user = new User('wouter', 'test'), 'main', 'secret'));
7766
$passport = $this->authenticator->authenticate($this->request);
7867

7968
$this->assertSame($user, $passport->getUser());
8069
}
8170

82-
private function generateCookieValue()
71+
public function testAuthenticateWithoutToken()
8372
{
84-
return base64_encode(implode(AbstractRememberMeServices::COOKIE_DELIMITER, ['part1', 'part2']));
73+
$this->expectException(\LogicException::class);
74+
75+
$this->authenticator->authenticate($this->request);
8576
}
8677
}

0 commit comments

Comments
 (0)