8000 [Security] Skip user checks for anonymous tokens · symfony/symfony@758860f · GitHub
[go: up one dir, main page]

Skip to content

Commit 758860f

Browse files
author
Robin Chalas
committed
[Security] Skip user checks for anonymous tokens
1 parent e775871 commit 758860f

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

src/Symfony/Component/Security/Core/Authentication/Provider/SimpleAuthenticationProvider.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\Security\Core\Authentication\Provider;
1313

14+
use Symfony\Component\Security\Core\Authentication\Token\AnonymousToken;
1415
use Symfony\Component\Security\Core\User\UserChecker;
1516
use Symfony\Component\Security\Core\User\UserCheckerInterface;
1617
use Symfony\Component\Security\Core\User\UserProviderInterface;
@@ -44,6 +45,10 @@ public function authenticate(TokenInterface $token)
4445
throw new AuthenticationException('Simple authenticator failed to return an authenticated token.');
4546
}
4647

48+
if ($authToken instanceof AnonymousToken) {
49+
return $authToken;
50+
}
51+
4752
$user = $authToken->getUser();
4853
$this->userChecker->checkPreAuth($user);
4954
$this->userChecker->checkPostAuth($user);

src/Symfony/Component/Security/Core/Tests/Authentication/Provider/SimpleAuthenticationProviderTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@
1212
namespace Symfony\Component\Security\Core\Tests\Authentication\Provider;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Security\Core\Authentication\Token\AnonymousToken;
1516
use Symfony\Component\Security\Core\Exception\DisabledException;
1617
use Symfony\Component\Security\Core\Authentication\Provider\SimpleAuthenticationProvider;
1718
use Symfony\Component\Security\Core\Exception\LockedException;
19+
use Symfony\Component\Security\Core\User\UserChecker;
1820

1921
class SimpleAuthenticationProviderTest extends TestCase
2022
{
@@ -72,6 +74,16 @@ public function testAuthenticateWhenPostChecksFails()
7274
$provider->authenticate($token);
7375
}
7476

77+
public function testAuthenticateSkipsUserChecksForAnonymousTokens()
78+
{
79+
$authenticator = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\SimpleAuthenticatorInterface')->getMock();
80+
$authenticator->expects($this->once())
81+
->method('authenticateToken')
82+
->will($this->returnValue($token = new AnonymousToken('dummy', 'anon.')));
83+
84+
$this->assertSame($token, $this->getProvider($authenticator, null, new UserChecker())->authenticate($token));
85+
}
86+
7587
protected function getProvider($simpleAuthenticator = null, $userProvider = null, $userChecker = null, $key = 'test')
7688
{
7789
if (null === $userChecker) {

0 commit comments

Comments
0