8000 bug #26370 [Security] added userChecker to SimpleAuthenticationProvid… · symfony/symfony@7753282 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7753282

Browse files
bug #26370 [Security] added userChecker to SimpleAuthenticationProvider (i3or1s)
This PR was submitted for the 2.8 branch but it was squashed and merged into the 2.7 branch instead (closes #26370). Discussion ---------- [Security] added userChecker to SimpleAuthenticationProvider [Security] added userChecker to SimpleAuthenticationProvider [SecurityBundle] [DependencyInjection] updated SimpleFormFactory | Q | A | ------------- | --- | Branch? | 2.8 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #26314 | License | MIT | Doc PR | no Introduces user checker to the simple authentication provider. Commits ------- cb9c92d [Security] added userChecker to SimpleAuthenticationProvider
2 parents bd49884 + cb9c92d commit 7753282

File tree

3 files changed

+102
-4
lines changed

3 files changed

+102
-4
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ protected function createAuthProvider(ContainerBuilder $container, $id, $config,
5555
->replaceArgument(0, new Reference($config['authenticator']))
5656
->replaceArgument(1, new Reference($userProviderId))
5757
->replaceArgument(2, $id)
58+
->replaceArgument(3, new Reference('security.user_checker.'.$id))
5859
;
5960

6061
return $provider;

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

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

14+
use Symfony\Component\Security\Core\User\UserChecker;
15+
use Symfony\Component\Security\Core\User\UserCheckerInterface;
1416
use Symfony\Component\Security\Core\User\UserProviderInterface;
1517
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
1618
use Symfony\Component\Security\Core\Authentication\SimpleAuthenticatorInterface;
@@ -24,23 +26,29 @@ class SimpleAuthenticationProvider implements AuthenticationProviderInterface
2426
private $simpleAuthenticator;
2527
private $userProvider;
2628
private $providerKey;
29+
private $userChecker;
2730

28-
public function __construct(SimpleAuthenticatorInterface $simpleAuthenticator, UserProviderInterface $userProvider, $providerKey)
31+
public function __construct(SimpleAuthenticatorInterface $simpleAuthenticator, UserProviderInterface $userProvider, $providerKey, UserCheckerInterface $userChecker = null)
2932
{
3033
$this->simpleAuthenticator = $simpleAuthenticator;
3134
$this->userProvider = $userProvider;
3235
$this->providerKey = $providerKey;
36+
$this->userChecker = $userChecker ?: new UserChecker();
3337
}
3438

3539
public function authenticate(TokenInterface $token)
3640
{
3741
$authToken = $this->simpleAuthenticator->authenticateToken($token, $this->userProvider, $this->providerKey);
3842

39-
if ($authToken instanceof TokenInterface) {
40-
return $authToken;
43+
if (!$authToken instanceof TokenInterface) {
44+
throw new AuthenticationException('Simple authenticator failed to return an authenticated token.');
4145
}
4246

43-
throw new AuthenticationException('Simple authenticator failed to return an authenticated token.');
47+
$user = $authToken->getUser();
48+
$this->userChecker->checkPreAuth($user);
49+
$this->userChecker->checkPostAuth($user);
50+
51+
return $authToken;
4452
}
4553

4654
public function supports(TokenInterface $token)
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Security\Core\Tests\Authentication\Provider;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Security\Core\Exception\DisabledException;
16+
use Symfony\Component\Security\Core\Authentication\Provider\SimpleAuthenticationProvider;
17+
use Symfony\Component\Security\Core\Exception\LockedException;
18+
19+
class SimpleAuthenticationProviderTest extends TestCase
20+
{
21+
/**
22+
* @expectedException \Symfony\Component\Security\Core\Exception\DisabledException
23+
*/
24+
public function testAuthenticateWhenPreChecksFails()
25+
{
26+
$user = $this->getMockBuilder('Symfony\Component\Security\Core\User\UserInterface')->getMock();
27+
28+
$token = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')->getMock();
29+
$token->expects($this->any())
30+
->method('getUser')
31+
->will($this->returnValue($user));
32+
33+
$userChecker = $this->getMockBuilder('Symfony\Component\Security\Core\User\UserCheckerInterface')->getMock();
34+
$userChecker->expects($this->once())
35+
->method('checkPreAuth')
36+
->will($this->throwException(new DisabledException()));
37+
38+
$authenticator = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\SimpleAuthenticatorInterface')->getMock();
39+
$authenticator->expects($this->once())
40+
->method('authenticateToken')
41+
->will($this->returnValue($token));
42+
43+
$provider = $this->getProvider($authenticator, null, $userChecker);
44+
45+
$provider->authenticate($token);
46+
}
47+
48+
/**
49+
* @expectedException \Symfony\Component\Security\Core\Exception\LockedException
50+
*/
51+
public function testAuthenticateWhenPostChecksFails()
52+
{
53+
$user = $this->getMockBuilder('Symfony\Component\Security\Core\User\UserInterface')->getMock();
54+
55+
$token = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')->getMock();
56+
$token->expects($this->any())
57+
->method('getUser')
58+
->will($this->returnValue($user));
59+
60+
$userChecker = $this->getMockBuilder('Symfony\Component\Security\Core\User\UserCheckerInterface')->getMock();
61+
$userChecker->expects($this->once())
62+
->method('checkPostAuth')
63+
->will($this->throwException(new LockedException()));
64+
65+
$authenticator = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\SimpleAuthenticatorInterface')->getMock();
66+
$authenticator->expects($this->once())
67+
->method('authenticateToken')
68+
->will($this->returnValue($token));
69+
70+
$provider = $this->getProvider($authenticator, null, $userChecker);
71+
72+
$provider->authenticate($token);
73+
}
74+
75+
protected function getProvider($simpleAuthenticator = null, $userProvider = null, $userChecker = null, $key = 'test')
76+
{
77+
if (null === $userChecker) {
78+
$userChecker = $this->getMockBuilder('Symfony\Component\Security\Core\User\UserCheckerInterface')->getMock();
79+
}
80+
if (null === $simpleAuthenticator) {
81+
$simpleAuthenticator = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\SimpleAuthenticatorInterface')->getMock();
82+
}
83+
if (null === $userProvider) {
84+
$userProvider = $this->getMockBuilder('Symfony\Component\Security\Core\User\UserProviderInterface')->getMock();
85+
}
86+
87+
return new SimpleAuthenticationProvider($simpleAuthenticator, $userProvider, $key, $userChecker);
88+
}
89+
}

0 commit comments

Comments
 (0)
0