Skip to content

Commit fa20fe1

Browse files
author
Robin Chalas
committed
[Security] Skip user checks if not implementing UserInterface
1 parent e775871 commit fa20fe1

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

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

+6
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\Security\Core\User\UserChecker;
1515
use Symfony\Component\Security\Core\User\UserCheckerInterface;
16+
use Symfony\Component\Security\Core\User\UserInterface;
1617
use Symfony\Component\Security\Core\User\UserProviderInterface;
1718
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
1819
use Symfony\Component\Security\Core\Authentication\SimpleAuthenticatorInterface;
@@ -45,6 +46,11 @@ public function authenticate(TokenInterface $token)
4546
}
4647

4748
$user = $authToken->getUser();
49+
50+
if (!$user instanceof UserInterface) {
51+
return $authToken;
52+
}
53+
4854
$this->userChecker->checkPreAuth($user);
4955
$this->userChecker->checkPostAuth($user);
5056

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

+15
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\Security\Core\Exception\DisabledException;
1616
use Symfony\Component\Security\Core\Authentication\Provider\SimpleAuthenticationProvider;
1717
use Symfony\Component\Security\Core\Exception\LockedException;
18+
use Symfony\Component\Security\Core\User\UserChecker;
1819

1920
class SimpleAuthenticationProviderTest extends TestCase
2021
{
@@ -72,6 +73,20 @@ public function testAuthenticateWhenPostChecksFails()
7273
$provider->authenticate($token);
7374
}
7475

76+
public function testAuthenticateSkipsUserChecksForAnonymousTokens()
77+
{
78+
$token = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')->getMock();
79+
$token->expects($this->any())
80+
->method('getUser')
81+
->will($this->returnValue('string-user'));
82+
$authenticator = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\SimpleAuthenticatorInterface')->getMock();
83+
$authenticator->expects($this->once())
84+
->method('authenticateToken')
85+
->will($this->returnValue($token));
86+
87+
$this->assertSame($token, $this->getProvider($authenticator, null, new UserChecker())->authenticate($token));
88+
}
89+
7590
protected function getProvider($simpleAuthenticator = null, $userProvider = null, $userChecker = null, $key = 'test')
7691
{
7792
if (null === $userChecker) {

0 commit comments

Comments
 (0)