Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bb06f61

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

File tree

16 files changed

+49
-48
lines changed

16 files changed

+49
-48
lines changed

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: 2 additions & 1 deletion
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
*/

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: 3 additions & 8 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,11 +38,7 @@ 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 !$this->isAuthenticated($token);
4742
}
4843

4944
/**

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

Lines changed: 1 addition & 1 deletion
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. In version 6.0, security tokens won\'t have an "authenticated" flag anymore and will always be considered authenticated when they hold a user.', __METHOD__);
145145
}
146146

147147
return $this->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. In version 6.0, security tokens won\'t have an "authenticated" flag anymore and will always be considered authenticated when they hold a user.', __METHOD__);
6363
}
6464

6565
return true;

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

Lines changed: 2 additions & 2 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,7 +71,7 @@ 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. In 6.0, security tokens will always be considered authenticated when they hold a user
7575
*/
7676
public function isAuthenticated();
7777

src/Symfony/Component/Security/Core/Authorization/AuthorizationChecker.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,9 @@ public function __construct(TokenStorageInterface $tokenStorage, /*AccessDecisio
6767
*/
6868
final public function isGranted($attribute, $subject = null): bool
6969
{
70-
if (null === ($token = $this->tokenStorage->getToken())) {
70+
$token = $this->tokenStorage->getToken();
71+
72+
if (!$token || !$token->getUser()) {
7173
if ($this->exceptionOnNoToken) {
7274
throw new AuthenticationCredentialsNotFoundException('The token storage contains no authentication token. One possible reason may be that there is no firewall configured for this URL.');
7375
}
@@ -78,7 +80,7 @@ final public function isGranted($attribute, $subject = null): bool
7880
// @deprecated since Symfony 5.4
7981
if ($this->alwaysAuthenticate || !$authenticated = $token->isAuthenticated(false)) {
8082
if (!($authenticated ?? true)) {
81-
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.');
83+
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 when they hold a user.');
8284
}
8385
$this->tokenStorage->setToken($token = $this->authenticationManager->authenticate($token));
8486
}

src/Symfony/Component/Security/Core/Authorization/Voter/AuthenticatedVoter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public function vote(TokenInterface $token, $subject, array $attributes)
9696
if (self::IS_AUTHENTICATED === $attribute
9797
&& (method_exists($this->authenticationTrustResolver, 'isAuthenticated')
9898
? $this->authenticationTrustResolver->isAuthenticated($token)
99-
: (null !== $token && !$token instanceof NullToken))) {
99+
: ($token && $token->getUser()))) {
100100
return VoterInterface::ACCESS_GRANTED;
101101
}
102102

0 commit comments

Comments
 (0)