8000 Merge branch '2.8' into 3.4 · symfony/symfony@5d189e1 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5d189e1

Browse files
Merge branch '2.8' into 3.4
* 2.8: [Security] Load the user before pre/post auth checks when needed [SecurityBundle] Add test for simple authentication config [SecurityBundle] Add missing argument to security.authentication.provider.simple [Finder] fix tests
2 parents 0e67060 + 2c7556f commit 5d189e1

File tree

8 files changed

+113
-0
lines changed

8 files changed

+113
-0
lines changed

src/Symfony/Bundle/SecurityBundle/DependencyInjection/Security/Factory/SimplePreAuthenticationFactory.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public function create(ContainerBuilder $container, $id, $config, $userProvider,
4949
->replaceArgument(0, new Reference($config['authenticator']))
5050
->replaceArgument(1, new Reference($userProvider))
5151
->replaceArgument(2, $id)
52+
->replaceArgument(3, new Reference('security.user_checker.'.$id))
5253
;
5354

5455
// listener

src/Symfony/Bundle/SecurityBundle/Resources/config/security_listeners.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@
210210
<argument /> <!-- Simple Authenticator -->
211211
<argument /> <!-- User Provider -->
212212
<argument /> <!-- Provider-shared Key -->
213+
<argument>null</argument> <!-- UserChecker -->
213214
</service>
214215

215216
<service id="security.authentication.provider.pre_authenticated" class="Symfony\Component\Security\Core\Authentication\Provider\PreAuthenticatedAuthenticationProvider" abstract="true">

src/Symfony/Bundle/SecurityBundle/Tests/DependencyInjection/CompleteConfigurationTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,23 @@ public function testFirewalls()
162162
),
163163
null,
164164
),
165+
array(
166+
'simple_auth',
167+
'security.user_checker',
168+
null,
169+
true,
170+
false,
171+
'security.user.provider.concrete.default',
172+
'simple_auth',
173+
'security.authentication.form_entry_point.simple_auth',
174+
null,
175+
null,
176+
array(
177+
'simple_form',
178+
'anonymous',
179+
),
180+
null,
181+
),
165182
), $configs);
166183

167184
$this->assertEquals(array(
@@ -192,6 +209,13 @@ public function testFirewalls()
192209
'security.authentication.listener.anonymous.with_user_checker',
193210
'security.access_listener',
194211
),
212+
array(
213+
'security.channel_listener',
214+
'security.context_listener.2',
215+
'security.authentication.listener.simple_form.simple_auth',
216+
'security.authentication.listener.anonymous.simple_auth',
217+
'security.access_listener',
218+
),
195219
), $listeners);
196220

197221
$this->assertFalse($container->hasAlias('Symfony\Component\Security\Core\User\UserCheckerInterface', 'No user checker alias is registered when custom user checker services are registered'));

src/Symfony/Bundle/SecurityBundle/Tests/DependencyInjection/Fixtures/php/container1.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,12 @@
9090
'http_basic' => true,
9191
'logout_on_user_change' => true,
9292
),
93+
'simple_auth' => array(
94+
'provider' => 'default',
95+
'anonymous' => true,
96+
'simple_form' => array('authenticator' => 'simple_authenticator'),
97+
'logout_on_user_change' => true,
98+
),
9399
),
94100

95101
'access_control' => array(

src/Symfony/Bundle/SecurityBundle/Tests/DependencyInjection/Fixtures/xml/container1.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@
6868
<user-checker>app.user_checker</user-checker>
6969
</firewall>
7070

71+
<firewall name="simple_auth" logout-on-user-change="true" provider="default">
72+
<anonymous />
73+
<simple-form authenticator="simple_authenticator" />
74+
</firewall>
75+
7176
<role id="ROLE_ADMIN">ROLE_USER</role>
7277
<role id="ROLE_SUPER_ADMIN">ROLE_USER,ROLE_ADMIN,ROLE_ALLOWED_TO_SWITCH</role>
7378
<role id="ROLE_REMOTE">ROLE_USER,ROLE_ADMIN</role>

src/Symfony/Bundle/SecurityBundle/Tests/DependencyInjection/Fixtures/yml/container1.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,12 @@ security:
7373
user_checker: app.user_checker
7474
logout_on_user_change: true
7575

76+
simple_auth:
77+
provider: default
78+
anonymous: ~
79+
simple_form: { authenticator: simple_authenticator }
80+
logout_on_user_change: true
81+
7682
role_hierarchy:
7783
ROLE_ADMIN: ROLE_USER
7884
ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]

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;
1821
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