Skip to content

Commit a3d092f

Browse files
committed
[Security] Deprecate TokenInterface::isAuthenticated() and setAuthenticated()
1 parent a78b850 commit a3d092f

16 files changed

+110
-19
lines changed

Authentication/Token/AbstractToken.php

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,12 @@ public function setUser($user)
9999
throw new \InvalidArgumentException('$user must be an instanceof UserInterface, an object implementing a __toString method, or a primitive string.');
100100
}
101101

102-
if (null === $this->user) {
102+
// @deprecated since Symfony 5.4, remove the whole block if/elseif/else block in 6.0
103+
if (1 < \func_num_args() && !func_get_arg(1)) {
104+
// ContextListener checks if the user has changed on its own and calls `setAuthenticated()` subsequently,
105+
// avoid doing the same checks twice
106+
$changed = false;
107+
} elseif (null === $this->user) {
103108
$changed = false;
104109
} elseif ($this->user instanceof UserInterface) {
105110
if (!$user instanceof UserInterface) {
@@ -113,18 +118,25 @@ public function setUser($user)
113118
$changed = (string) $this->user !== (string) $user;
114119
}
115120

121+
// @deprecated since Symfony 5.4
116122
if ($changed) {
117-
$this->setAuthenticated(false);
123+
$this->setAuthenticated(false, false);
118124
}
119125

120126
$this->user = $user;
121127
}
122128

123129
/**
124130
* {@inheritdoc}
131+
*
132+
* @deprecated since Symfony 5.4
125133
*/
126134
public function isAuthenticated()
127135
{
136+
if (1 > \func_num_args() || func_get_arg(0)) {
137+
trigger_deprecation('symfony/security-core', '5.4', 'Method "%s()" is deprecated. In version 6.0, security tokens won\'t have an "authenticated" flag anymore and will always be considered authenticated.', __METHOD__);
138+
}
139+
128140
return $this->authenticated;
129141
}
130142

@@ -133,6 +145,10 @@ public function isAuthenticated()
133145
*/
134146
public function setAuthenticated(bool $authenticated)
135147
{
148+
if (2 > \func_num_args() || func_get_arg(1)) {
149+
trigger_deprecation('symfony/security-core', '5.4', 'Method "%s()" is deprecated. In version 6.0, security tokens won\'t have an "authenticated" state anymore and will always be considered as authenticated.', __METHOD__);
150+
}
151+
136152
$this->authenticated = $authenticated;
137153
}
138154

@@ -275,6 +291,9 @@ final public function unserialize($serialized)
275291
$this->__unserialize(\is_array($serialized) ? $serialized : unserialize($serialized));
276292
}
277293

294+
/**
295+
* @deprecated since Symfony 5.4
296+
*/
278297
private function hasUserChanged(UserInterface $user): bool
279298
{
280299
if (!($this->user instanceof UserInterface)) {

Authentication/Token/AnonymousToken.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ public function __construct(string $secret, $user, array $roles = [])
3333

3434
$this->secret = $secret;
3535
$this->setUser($user);
36-
$this->setAuthenticated(true);
36+
// @deprecated since Symfony 5.4
37+
$this->setAuthenticated(true, false);
3738
}
3839

3940
/**

Authentication/Token/NullToken.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,21 @@ public function getUserIdentifier(): string
5353
return '';
5454
}
5555

56+
/**
57+
* @deprecated since Symfony 5.4
58+
*/
5659
public function isAuthenticated()
5760
{
61+
if (0 === \func_num_args() || func_get_arg(0)) {
62+
trigger_deprecation('symfony/security-core', '5.4', 'Method "%s()" is deprecated. In version 6.0, security tokens won\'t have an "authenticated" flag anymore and will always be considered authenticated.', __METHOD__);
63+
}
64+
5865
return true;
5966
}
6067

68+
/**
69+
* @deprecated since Symfony 5.4
70+
*/
6171
public function setAuthenticated(bool $isAuthenticated)
6272
{
6373
throw new \BadMethodCallException('Cannot change authentication state of NullToken.');

Authentication/Token/PreAuthenticatedToken.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function __construct($user, $credentials, string $firewallName, array $ro
4141
$this->firewallName = $firewallName;
4242

4343
if ($roles) {
44-
$this->setAuthenticated(true);
44+
$this->setAuthenticated(true, false);
4545
}
4646
}
4747

Authentication/Token/RememberMeToken.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function __construct(UserInterface $user, string $firewallName, string $s
4444
$this->secret = $secret;
4545

4646
$this->setUser($user);
47-
parent::setAuthenticated(true);
47+
parent::setAuthenticated(true, false);
4848
}
4949

5050
/**
@@ -56,7 +56,7 @@ public function setAuthenticated(bool $authenticated)
5656
throw new \LogicException('You cannot set this token to authenticated after creation.');
5757
}
5858

59-
parent::setAuthenticated(false);
59+
parent::setAuthenticated(false, false);
6060
}
6161

6262
/**

Authentication/Token/TokenInterface.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,15 @@ public function setUser($user);
7171
* Returns whether the user is authenticated or not.
7272
*
7373
* @return bool true if the token has been authenticated, false otherwise
74+
*
75+
* @deprecated since Symfony 5.4. In 6.0, security tokens will always be considered authenticated
7476
*/
7577
public function isAuthenticated();
7678

7779
/**
7880
* Sets the authenticated flag.
81+
*
82+
* @deprecated since Symfony 5.4. In 6.0, security tokens will always be considered authenticated
7983
*/
8084
public function setAuthenticated(bool $isAuthenticated);
8185

Authentication/Token/UsernamePasswordToken.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public function __construct($user, $credentials, string $firewallName, array $ro
4242
$this->credentials = $credentials;
4343
$this->firewallName = $firewallName;
4444

45-
parent::setAuthenticated(\count($roles) > 0);
45+
parent::setAuthenticated(\count($roles) > 0, false);
4646
}
4747

4848
/**
@@ -54,7 +54,7 @@ public function setAuthenticated(bool $isAuthenticated)
5454
throw new \LogicException('Cannot set this token to trusted after instantiation.');
5555
}
5656

57-
parent::setAuthenticated(false);
57+
parent::setAuthenticated(false, false);
5858
}
5959

6060
/**

Authorization/AuthorizationChecker.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,12 @@ final public function isGranted($attribute, $subject = null): bool
6262

6363
$token = new NullToken();
6464
} else {
65-
if ($this->alwaysAuthenticate || !$token->isAuthenticated()) {
65+
$authenticated = true;
66+
// @deprecated since Symfony 5.4
67+
if ($this->alwaysAuthenticate || !$authenticated = $token->isAuthenticated(false)) {
68+
if (!($authenticated ?? true)) {
69+
trigger_deprecation('symfony/core', '5.4', 'Returning false from "%s()" is deprecated and won\'t have any effect in Symfony 6.0 as security tokens will always be considered authenticated.');
70+
}
6671
$this->tokenStorage->setToken($token = $this->authenticationManager->authenticate($token));
6772
}
6873
}

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ CHANGELOG
66

77
* Deprecate setting the 4th argument (`$alwaysAuthenticate`) to `true` and not setting the
88
5th argument (`$exceptionOnNoToken`) to `false` of `AuthorizationChecker`
9+
* Deprecate methods `TokenInterface::isAuthenticated()` and `setAuthenticated`,
10+
tokens will always be considered authenticated in 6.0
911

1012
5.3
1113
---

Tests/Authentication/Token/AbstractTokenTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public function getUsername()
4141

4242
public function getRoles()
4343
{
44+
return [];
4445
}
4546

4647
public function getPassword()
@@ -104,6 +105,9 @@ public function testConstructor()
104105
$this->assertEquals(['ROLE_FOO'], $token->getRoleNames());
105106
}
106107

108+
/**
109+
* @group legacy
110+
*/
107111
public function testAuthenticatedFlag()
108112
{
109113
$token = new ConcreteToken();
@@ -158,6 +162,7 @@ public function getUsers()
158162
}
159163

160164
/**
165+
* @group legacy
161166
* @dataProvider getUserChanges
162167
*/
163168
public function testSetUserSetsAuthenticatedToFalseWhenUserChanges($firstUser, $secondUser)
@@ -190,6 +195,7 @@ public function getUserChanges()
190195
}
191196

192197
/**
198+
* @group legacy
193199
* @dataProvider getUsers
194200
*/
195201
public function testSetUserDoesNotSetAuthenticatedToFalseWhenUserDoesNotChange($user)
@@ -205,6 +211,9 @@ public function testSetUserDoesNotSetAuthenticatedToFalseWhenUserDoesNotChange($
205211
$this->assertTrue($token->isAuthenticated());
206212
}
207213

214+
/**
215+
* @group legacy
216+
*/
208217
public function testIsUserChangedWhenSerializing()
209218
{
210219
$token = new ConcreteToken(['ROLE_ADMIN']);

0 commit comments

Comments
 (0)