Skip to content

Commit d9cd41c

Browse files
[Security] make TokenInterface::getUser() nullable to tell about unauthenticated tokens
1 parent 53215e2 commit d9cd41c

File tree

18 files changed

+57
-66
lines changed

18 files changed

+57
-66
lines changed

UPGRADE-5.4.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,8 @@ Security
112112
behavior when using `enable_authenticator_manager: true`)
113113
* Deprecate not setting the `$exceptionOnNoToken` argument of `AccessListener` to `false`
114114
(this is the default behavior when using `enable_authenticator_manager: true`)
115-
* Deprecate `TokenInterface:isAuthenticated()` and `setAuthenticated()` methods without replacement.
116-
Security tokens won't have an "authenticated" flag anymore, so they will always be considered authenticated
115+
* Deprecate `TokenInterface:isAuthenticated()` and `setAuthenticated()` methods,
116+
return `null` from `getUser()` instead when a token is not authenticated
117117
* Deprecate `DeauthenticatedEvent`, use `TokenDeauthenticatedEvent` instead
118118
* Deprecate `CookieClearingLogoutHandler`, `SessionLogoutHandler` and `CsrfTokenClearingLogoutHandler`.
119119
Use `CookieClearingLogoutListener`, `SessionLogoutListener` and `CsrfTokenClearingLogoutListener` instead

UPGRADE-6.0.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -359,8 +359,8 @@ Security
359359
`UsernamePasswordFormAuthenticationListener`, `UsernamePasswordJsonAuthenticationListener` and `X509AuthenticationListener`
360360
from security-http, use the new authenticator system instead
361361
* Remove the Guard component, use the new authenticator system instead
362-
* Remove `TokenInterface:isAuthenticated()` and `setAuthenticated()` methods without replacement.
363-
Security tokens won't have an "authenticated" flag anymore, so they will always be considered authenticated
362+
* Remove `TokenInterface:isAuthenticated()` and `setAuthenticated()`,
363+
return `null` from `getUser()` instead when a token is not authenticated
364364
* Remove `DeauthenticatedEvent`, use `TokenDeauthenticatedEvent` instead
365365
* Remove `CookieClearingLogoutHandler`, `SessionLogoutHandler` and `CsrfTokenClearingLogoutHandler`.
366366
Use `CookieClearingLogoutListener`, `SessionLogoutListener` and `CsrfTokenClearingLogoutListener` instead

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' => method_exists($token, 'isAuthenticated') ? $token->isAuthenticated(false) : true, // @deprecated since Symfony 5.4, always true in 6.0
45+
'authenticated' => method_exists($token, 'isAuthenticated') ? $token->isAuthenticated(false) : (bool) $token->getUser(),
4646
'roles' => $token->getRoleNames(),
4747
];
4848

src/Symfony/Bridge/Twig/AppVariable.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\HttpFoundation\Session\Session;
1717
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
1818
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
19+
use Symfony\Component\Security\Core\User\UserInterface;
1920

2021
/**
2122
* Exposes some Symfony parameters and services as an "app" global variable.
@@ -68,7 +69,7 @@ public function getToken()
6869
/**
6970
* Returns the current user.
7071
*
71-
* @return object|null
72+
* @return UserInterface|null
7273
*
7374
* @see TokenInterface::getUser()
7475
*/
@@ -84,7 +85,7 @@ public function getUser()
8485

8586
$user = $token->getUser();
8687

87-
// @deprecated since 5.4, $user will always be a UserInterface instance
88+
// @deprecated since Symfony 5.4, $user will always be a UserInterface instance
8889
return \is_object($user) ? $user : null;
8990
}
9091

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

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

128128
$this->data = [
129129
'enabled' => true,
130-
'authenticated' => method_exists($token, 'isAuthenticated') ? $token->isAuthenticated(false) : true,
130+
'authenticated' => method_exists($token, 'isAuthenticated') ? $token->isAuthenticated(false) : (bool) $token->getUser(),
131131
'impersonated' => null !== $impersonatorUser,
132132
'impersonator_user' => $impersonatorUser,
133133
'impersonation_exit_path' => null,

src/Symfony/Component/Security/Core/Authentication/AuthenticationProviderManager.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public function authenticate(TokenInterface $token)
111111
}
112112

113113
// @deprecated since Symfony 5.3
114-
if ($user = $result->getUser() instanceof UserInterface && !method_exists($result->getUser(), 'getUserIdentifier')) {
114+
if ($result->getUser() instanceof UserInterface && !method_exists($result->getUser(), 'getUserIdentifier')) {
115115
trigger_deprecation('symfony/security-core', '5.3', 'Not implementing method "getUserIdentifier(): string" in user class "%s" is deprecated. This method will replace "getUsername()" in Symfony 6.0.', get_debug_type($result->getUser()));
116116
}
117117

src/Symfony/Component/Security/Core/Authentication/AuthenticationTrustResolver.php

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
namespace Symfony\Component\Security\Core\Authentication;
1313

1414
use Symfony\Component\Security\Core\Authentication\Token\AnonymousToken;
15-
use Symfony\Component\Security\Core\Authentication\Token\NullToken;
1615
use Symfony\Component\Security\Core\Authentication\Token\RememberMeToken;
1716
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
1817

@@ -25,9 +24,9 @@ class AuthenticationTrustResolver implements AuthenticationTrustResolverInterfac
2524
{
2625
public function isAuthenticated(TokenInterface $token = null): bool
2726
{
28-
return null !== $token && !$token instanceof NullToken
27+
return $token && $token->getUser()
2928
// @deprecated since Symfony 5.4, TokenInterface::isAuthenticated() and AnonymousToken no longer exists in 6.0
30-
&& !$token instanceof AnonymousToken && $token->isAuthenticated(false);
29+
&& !$token instanceof AnonymousToken && (!method_exists($token, 'isAuthenticated') || $token->isAuthenticated(false));
3130
}
3231

3332
/**
@@ -39,34 +38,22 @@ public function isAnonymous(TokenInterface $token = null/*, $deprecation = true*
3938
trigger_deprecation('symfony/security-core', '5.4', 'The "%s()" method is deprecated, use "isAuthenticated()" or "isFullFledged()" if you want to check if the request is (fully) authenticated.', __METHOD__);
4039
}
4140

42-
if (null === $token) {
43-
return false;
44-
}
45-
46-
return $token instanceof AnonymousToken || $token instanceof NullToken;
41+
return $token && !$this->isAuthenticated($token);
4742
}
4843

4944
/**
5045
* {@inheritdoc}
5146
*/
5247
public function isRememberMe(TokenInterface $token = null)
5348
{
54-
if (null === $token) {
55-
return false;
56-
}
57-
58-
return $token instanceof RememberMeToken;
49+
return $token && $token instanceof RememberMeToken;
5950
}
6051

6152
/**
6253
* {@inheritdoc}
6354
*/
6455
public function isFullFledged(TokenInterface $token = null)
6556
{
66-
if (null === $token) {
67-
return false;
68-
}
69-
70-
return !$this->isAnonymous($token, false) && !$this->isRememberMe($token);
57+
return $token && !$this->isAnonymous($token, false) && !$this->isRememberMe($token);
7158
}
7259
}

src/Symfony/Component/Security/Core/Authentication/Token/AbstractToken.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ public function setUser($user)
141141
public function isAuthenticated()
142142
{
143143
if (1 > \func_num_args() || func_get_arg(0)) {
144-
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__);
144+
trigger_deprecation('symfony/security-core', '5.4', 'Method "%s()" is deprecated, return null from "getUser()" instead when a token is not authenticated.', __METHOD__);
145145
}
146146

147147
return $this->authenticated;
@@ -153,7 +153,7 @@ public function isAuthenticated()
153153
public function setAuthenticated(bool $authenticated)
154154
{
155155
if (2 > \func_num_args() || func_get_arg(1)) {
156-
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__);
156+
trigger_deprecation('symfony/security-core', '5.4', 'Method "%s()" is deprecated', __METHOD__);
157157
}
158158

159159
$this->authenticated = $authenticated;

src/Symfony/Component/Security/Core/Authentication/Token/NullToken.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public function getUserIdentifier(): string
5959
public function isAuthenticated()
6060
{
6161
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__);
62+
trigger_deprecation('symfony/security-core', '5.4', 'Method "%s()" is deprecated, return null from "getUser()" instead when a token is not authenticated.', __METHOD__);
6363
}
6464

6565
return true;

src/Symfony/Component/Security/Core/Authentication/Token/TokenInterface.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public function getCredentials();
5151
/**
5252
* Returns a user representation.
5353
*
54-
* @return UserInterface
54+
* @return UserInterface|null
5555
*
5656
* @see AbstractToken::setUser()
5757
*/
@@ -71,14 +71,14 @@ public function setUser($user);
7171
*
7272
* @return bool true if the token has been authenticated, false otherwise
7373
*
74-
* @deprecated since Symfony 5.4. In 6.0, security tokens will always be considered authenticated
74+
* @deprecated since Symfony 5.4, return null from "getUser()" instead when a token is not authenticated
7575
*/
7676
public function isAuthenticated();
7777

7878
/**
7979
* Sets the authenticated flag.
8080
*
81-
* @deprecated since Symfony 5.4. In 6.0, security tokens will always be considered authenticated
81+
* @deprecated since Symfony 5.4
8282
*/
8383
public function setAuthenticated(bool $isAuthenticated);
8484

0 commit comments

Comments
 (0)