Skip to content

Commit 6f0246d

Browse files
committed
deprecate the FQCN properties of PersistentToken and RememberMeDetails
1 parent da1417b commit 6f0246d

File tree

5 files changed

+65
-18
lines changed

5 files changed

+65
-18
lines changed

Authentication/RememberMe/InMemoryTokenProvider.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ public function updateToken(string $series, #[\SensitiveParameter] string $token
4242
$this->tokens[$series]->getUserIdentifier(),
4343
$series,
4444
$tokenValue,
45-
$lastUsed
45+
$lastUsed,
46+
false
4647
);
4748
$this->tokens[$series] = $token;
4849
}

Authentication/RememberMe/PersistentToken.php

Lines changed: 54 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,61 @@
1818
*/
1919
final class PersistentToken implements PersistentTokenInterface
2020
{
21+
private ?string $class = null;
22+
private string $userIdentifier;
23+
private string $series;
24+
private string $tokenValue;
2125
private \DateTimeImmutable $lastUsed;
2226

27+
/**
28+
* @param string $userIdentifier
29+
* @param string $series
30+
* @param string $tokenValue
31+
* @param \DateTimeInterface $lastUsed
32+
*/
2333
public function __construct(
24-
private string $class,
25-
private string $userIdentifier,
26-
private string $series,
27-
#[\SensitiveParameter] private string $tokenValue,
28-
\DateTimeInterface $lastUsed,
34+
$userIdentifier,
35+
$series,
36+
#[\SensitiveParameter] $tokenValue,
37+
#[\SensitiveParameter] $lastUsed,
2938
) {
30-
if (!$class) {
31-
throw new \InvalidArgumentException('$class must not be empty.');
39+
if (\func_num_args() > 4) {
40+
if (\func_num_args() < 6 || func_get_arg(5)) {
41+
trigger_deprecation('symfony/security-core', '7.4', 'Passing a user FQCN to %s() is deprecated. The user class will be removed from the remember-me cookie in 8.0.', __CLASS__, __NAMESPACE__);
42+
}
43+
44+
if (!\is_string($userIdentifier)) {
45+
throw new \TypeError(\sprintf('Argument 1 passed to "%s()" must be a string, "%s" given.', __METHOD__, get_debug_type($userIdentifier)));
46+
}
47+
48+
$this->class = $userIdentifier;
49+
$userIdentifier = $series;
50+
$series = $tokenValue;
51+
$tokenValue = $lastUsed;
52+
53+
if (\func_num_args() <= 4) {
54+
throw new \TypeError(\sprintf('Argument 5 passed to "%s()" must be an instance of "%s", the argument is missing.', __METHOD__, \DateTimeInterface::class));
55+
}
56+
57+
$lastUsed = func_get_arg(4);
58+
}
59+
60+
if (!\is_string($userIdentifier)) {
61+
throw new \TypeError(\sprintf('The $userIdentifier argument passed to "%s()" must be a string, "%s" given.', __METHOD__, get_debug_type($userIdentifier)));
3262
}
63+
64+
if (!\is_string($series)) {
65+
throw new \TypeError(\sprintf('The $series argument passed to "%s()" must be a string, "%s" given.', __METHOD__, get_debug_type($series)));
66+
}
67+
68+
if (!\is_string($tokenValue)) {
69+
throw new \TypeError(\sprintf('The $tokenValue argument passed to "%s()" must be a string, "%s" given.', __METHOD__, get_debug_type($tokenValue)));
70+
}
71+
72+
if (!$lastUsed instanceof \DateTimeInterface) {
73+
throw new \TypeError(\sprintf('The $lastUsed argument passed to "%s()" must be an instance of "%s", "%s" given.', __METHOD__, \DateTimeInterface::class, get_debug_type($lastUsed)));
74+
}
75+
3376
if ('' === $userIdentifier) {
3477
throw new \InvalidArgumentException('$userIdentifier must not be empty.');
3578
}
@@ -40,6 +83,9 @@ public function __construct(
4083
throw new \InvalidArgumentException('$tokenValue must not be empty.');
4184
}
4285

86+
$this->userIdentifier = $userIdentifier;
87+
$this->series = $series;
88+
$this->tokenValue = $tokenValue;
4389
$this->lastUsed = \DateTimeImmutable::createFromInterface($lastUsed);
4490
}
4591

@@ -52,7 +98,7 @@ public function getClass(bool $triggerDeprecation = true): string
5298
trigger_deprecation('symfony/security-core', '7.4', 'The "%s()" method is deprecated: the user class will be removed from the remember-me cookie in 8.0.', __METHOD__);
5399
}
54100

55-
return $this->class;
101+
return $this->class ?? '';
56102
}
57103

58104
public function getUserIdentifier(): string

Tests/Authentication/RememberMe/CacheTokenVerifierTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,22 @@ class CacheTokenVerifierTest extends TestCase
2121
public function testVerifyCurrentToken()
2222
{
2323
$verifier = new CacheTokenVerifier(new ArrayAdapter());
24-
$token = new PersistentToken('class', 'user', 'series1@special:chars=/', 'value', new \DateTimeImmutable());
24+
$token = new PersistentToken('class', 'user', 'series1@special:chars=/', 'value', new \DateTimeImmutable(), false);
2525
$this->assertTrue($verifier->verifyToken($token, 'value'));
2626
}
2727

2828
public function testVerifyFailsInvalidToken()
2929
{
3030
$verifier = new CacheTokenVerifier(new ArrayAdapter());
31-
$token = new PersistentToken('class', 'user', 'series1@special:chars=/', 'value', new \DateTimeImmutable());
31+
$token = new PersistentToken('class', 'user', 'series1@special:chars=/', 'value', new \DateTimeImmutable(), false);
3232
$this->assertFalse($verifier->verifyToken($token, 'wrong-value'));
3333
}
3434

3535
public function testVerifyOutdatedToken()
3636
{
3737
$verifier = new CacheTokenVerifier(new ArrayAdapter());
38-
$outdatedToken = new PersistentToken('class', 'user', 'series1@special:chars=/', 'value', new \DateTimeImmutable());
39-
$newToken = new PersistentToken('class', 'user', 'series1@special:chars=/', 'newvalue', new \DateTimeImmutable());
38+
$outdatedToken = new PersistentToken('class', 'user', 'series1@special:chars=/', 'value', new \DateTimeImmutable(), false);
39+
$newToken = new PersistentToken('class', 'user', 'series1@special:chars=/', 'newvalue', new \DateTimeImmutable(), false);
4040
$verifier->updateExistingToken($outdatedToken, 'newvalue', new \DateTimeImmutable());
4141
$this->assertTrue($verifier->verifyToken($newToken, 'value'));
4242
}

Tests/Authentication/RememberMe/InMemoryTokenProviderTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public function testCreateNewToken()
2222
{
2323
$provider = new InMemoryTokenProvider();
2424

25-
$token = new PersistentToken('foo', 'foo', 'foo', 'foo', new \DateTimeImmutable());
25+
$token = new PersistentToken('foo', 'foo', 'foo', 'foo', new \DateTimeImmutable(), false);
2626
$provider->createNewToken($token);
2727

2828
$this->assertSame($provider->loadTokenBySeries('foo'), $token);
@@ -38,7 +38,7 @@ public function testUpdateToken()
3838
{
3939
$provider = new InMemoryTokenProvider();
4040

41-
$token = new PersistentToken('foo', 'foo', 'foo', 'foo', new \DateTimeImmutable());
41+
$token = new PersistentToken('foo', 'foo', 'foo', 'foo', new \DateTimeImmutable(), false);
4242
$provider->createNewToken($token);
4343
$provider->updateToken('foo', 'newFoo', $lastUsed = new \DateTime());
4444
$token = $provider->loadTokenBySeries('foo');
@@ -51,7 +51,7 @@ public function testDeleteToken()
5151
{
5252
$provider = new InMemoryTokenProvider();
5353

54-
$token = new PersistentToken('foo', 'foo', 'foo', 'foo', new \DateTimeImmutable());
54+
$token = new PersistentToken('foo', 'foo', 'foo', 'foo', new \DateTimeImmutable(), false);
5555
$provider->createNewToken($token);
5656
$provider->deleteTokenBySeries('foo');
5757

Tests/Authentication/RememberMe/PersistentTokenTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class PersistentTokenTest extends TestCase
2121
public function testConstructor()
2222
{
2323
$lastUsed = new \DateTimeImmutable();
24-
$token = new PersistentToken('fooclass', 'fooname', 'fooseries', 'footokenvalue', $lastUsed);
24+
$token = new PersistentToken('fooname', 'fooseries', 'footokenvalue', $lastUsed);
2525

2626
$this->assertEquals('fooname', $token->getUserIdentifier());
2727
$this->assertEquals('fooseries', $token->getSeries());
@@ -32,7 +32,7 @@ public function testConstructor()
3232
public function testDateTime()
3333
{
3434
$lastUsed = new \DateTime();
35-
$token = new PersistentToken('fooclass', 'fooname', 'fooseries', 'footokenvalue', $lastUsed);
35+
$token = new PersistentToken('fooname', 'fooseries', 'footokenvalue', $lastUsed);
3636

3737
$this->assertEquals($lastUsed, $token->getLastUsed());
3838
}

0 commit comments

Comments
 (0)