8000 bug #26788 [Security] Load the user before pre/post auth checks when … · symfony/symfony@1605684 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1605684

Browse files
bug #26788 [Security] Load the user before pre/post auth checks when needed (chalasr)
This PR was merged into the 2.8 branch. Discussion ---------- [Security] Load the user before pre/post auth checks when needed | Q | A | ------------- | --- | Branch? | 2.8 | Bug fix? | yes | New feature? | n/a | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #26775 | License | MIT | Doc PR | n/a Commits ------- c318306 [Security] Load the user before pre/post auth checks when needed
2 parents 3c54c4a + c318306 commit 1605684

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,11 @@
1111

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

14+
use Symfony\Component\Security\Core\Exception\AuthenticationServiceException;
15+
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
1416
use Symfony\Component\Security\Core\User\UserChecker;
1517
use Symfony\Component\Security\Core\User\UserCheckerInterface;
18+
use Symfony\Component\Security\Core\User\UserInterface;
1619
use Symfony\Component\Security\Core\User\UserProviderInterface;
1720
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
18 8000 21
use Symfony\Component\Security\Core\Authentication\SimpleAuthenticatorInterface;
@@ -45,6 +48,24 @@ public function authenticate(TokenInterface $token)
4548
}
4649

4750
$user = $authToken->getUser();
51+
52+
if (!$user instanceof UserInterface) {
53+
try {
54+
$user = $this->userProvider->loadUserByUsername($user);
55+
56+
if (!$user instanceof UserInterface) {
57+
throw new AuthenticationServiceException('The user provider must return a UserInterface object.');
58+
}
59+
} catch (UsernameNotFoundException $e) {
60+
$e->setUsername($user);
61+
throw $e;
62+
} catch (\Exception $e) {
63+
$e = new AuthenticationServiceException($e->getMessage(), 0, $e);
64+
$e->setToken($token);
65+
throw $e;
66+
}
67+
}
68+
4869
$this->userChecker->checkPreAuth($user);
4970
$this->userChecker->checkPostAuth($user);
5071

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

Lines changed: 49 additions & 0 deletions
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\Exception\UsernameNotFoundException;
1819

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

76+
public function testAuthenticateFromString()
77+
{
78+
$user = $this->getMockBuilder('Symfony\Component\Security\Core\User\UserInterface')->getMock();
79+
80+
$token = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')->getMock();
81+
$token->expects($this->any())
82+
->method('getUser')
83+
->will($this->returnValue('foo'));
84+
85+
$authenticator = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\SimpleAuthenticatorInterface')->getMock();
86+
$authenticator->expects($this->once())
87+
->method('authenticateToken')
88+
->will($this->returnValue($token));
89+
90+
$userProvider = $this->getMockBuilder('Symfony\Component\Security\Core\User\UserProviderInterface')->getMock();
91+
$userProvider->expects($this->once())
92+
->method('loadUserByUsername')
93+
->willReturn($this->getMockBuilder('Symfony\Component\Security\Core\User\UserInterface')->getMock());
94+
$provider = $this->getProvider($authenticator, $userProvider);
95+
96+
$this->assertSame($token, $provider->authenticate($token));
97+
}
98+
99+
/**
100+
* @expectedException \Symfony\Component\Security\Core\Exception\UsernameNotFoundException
101+
*/
102+
public function testUsernameNotFound()
103+
{
104+
$user = $this->getMockBuilder('Symfony\Component\Security\Core\User\UserInterface')->getMock();
105+
106+
$token = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')->getMock();
107+
$token->expects($this->any())
108+
->method('getUser')
109+
->will($this->returnValue('foo'));
110+
111+
$authenticator = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\SimpleAuthenticatorInterface')->getMock();
112+
$authenticator->expects($this->once())
113+
->method('authenticateToken')
114+
->will($this->returnValue($token));
115+
116+
$userProvider = $this->getMockBuilder('Symfony\Component\Security\Core\User\UserProviderInterface')->getMock();
117+
$userProvider->expects($this->once())
118+
->method('loadUserByUsername')
119+
->willThrowException(new UsernameNotFoundException());
120+
121+
$this->getProvider($authenticator, $userProvider)->authenticate($token);
122+
}
123+
75124
protected function getProvider($simpleAuthenticator = null, $userProvider = null, $userChecker = null, $key = 'test')
76125
{
77126
if (null === $userChecker) {

0 commit comments

Comments
 (0)
0