Skip to content

Commit 33b8fbd

Browse files
committed
[Security] Deprecate TokenInterface::isAuthenticated() and setAuthenticated()
1 parent 479919d commit 33b8fbd

33 files changed

+228
-45
lines changed

UPGRADE-5.4.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,6 @@ Security
3030
behavior when using `enable_authenticator_manager: true`)
3131
* Deprecate not setting the 5th argument (`$exceptionOnNoToken`) of `AccessListener` to `false`
3232
(this is the default behavior when using `enable_authenticator_manager: true`)
33+
* Deprecate `TokenInterface:isAuthenticated()` and `setAuthenticated()` methods without replacement.
34+
Security tokens won't have an "authenticated" flag anymore, so they will always be considered authenticated
35+
* Deprecate `DeauthenticatedEvent`, use `TokenDeauthenticatedEvent` instead

UPGRADE-6.0.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,9 @@ Security
316316
`UsernamePasswordFormAuthenticationListener`, `UsernamePasswordJsonAuthenticationListener` and `X509AuthenticationListener`
317317
from security-http, use the new authenticator system instead
318318
* Remove the Guard component, use the new authenticator system instead
319+
* Remove `TokenInterface:isAuthenticated()` and `setAuthenticated()` methods without replacement.
320+
Security tokens won't have an "authenticated" flag anymore, so they will always be considered authenticated
321+
* Remove `DeauthenticatedEvent`, use `TokenDeauthenticatedEvent` instead
319322

320323
SecurityBundle
321324
--------------

src/Symfony/Bridge/Monolog/Processor/AbstractTokenProcessor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public function __invoke(array $record): array
4242

4343
if (null !== $token = $this->getToken()) {
4444
$record['extra'][$this->getKey()] = [
45-
'authenticated' => $token->isAuthenticated(),
45+
'authenticated' => $token->isAuthenticated(false), // @deprecated since Symfony 5.4, always true in 6.0
4646
'roles' => $token->getRoleNames(),
4747
];
4848

src/Symfony/Bridge/Monolog/Tests/Processor/TokenProcessorTest.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ public function testLegacyProcessor()
3939

4040
$this->assertArrayHasKey('token', $record['extra']);
4141
$this->assertEquals($token->getUsername(), $record['extra']['token']['username']);
42-
$this->assertEquals($token->isAuthenticated(), $record['extra']['token']['authenticated']);
4342
$this->assertEquals(['ROLE_USER'], $record['extra']['token']['roles']);
4443
}
4544

@@ -59,7 +58,6 @@ public function testProcessor()
5958

6059
$this->assertArrayHasKey('token', $record['extra']);
6160
$this->assertEquals($token->getUserIdentifier(), $record['extra']['token']['user_identifier']);
62-
$this->assertEquals($token->isAuthenticated(), $record['extra']['token']['authenticated']);
6361
$this->assertEquals(['ROLE_USER'], $record['extra']['token']['roles']);
6462
}
6563
}

src/Symfony/Bundle/FrameworkBundle/KernelBrowser.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public function loginUser(object $user, string $firewallContext = 'main'): self
123123
}
124124

125125
$token = new TestBrowserToken($user->getRoles(), $user, $firewallContext);
126-
$token->setAuthenticated(true);
126+
$token->setAuthenticated(true, false);
127127

128128
$container = $this->getContainer();
129129
$container->get('security.untracked_token_storage')->setToken($token);

src/Symfony/Bundle/SecurityBundle/DataCollector/SecurityDataCollector.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public function collect(Request $request, Response $response, \Throwable $except
123123

124124
$this->data = [
125125
'enabled' => true,
126-
'authenticated' => $token->isAuthenticated(),
126+
'authenticated' => $token->isAuthenticated(false),
127127
'impersonated' => null !== $impersonatorUser,
128128
'impersonator_user' => $impersonatorUser,
129129
'impersonation_exit_path' => null,

src/Symfony/Component/Security/Core/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)) {

src/Symfony/Component/Security/Core/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
/**

src/Symfony/Component/Security/Core/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.');

src/Symfony/Component/Security/Core/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

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy