8000 [Security] added userChecker to SimpleAuthenticationProvider · symfony/symfony@476861e · GitHub
[go: up one dir, main page]

Skip to content

Commit 476861e

Browse files
author
Boris Vujicic
committed
[Security] added userChecker to SimpleAuthenticationProvider
test for userChecker postAuth and preAuth
1 parent 3cc426e commit 476861e

File tree

2 files changed

+93
-3
lines changed

2 files changed

+93
-3
lines changed

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,13 @@ public function authenticate(TokenInterface $token)
4040
{
4141
$authToken = $this->simpleAuthenticator->authenticateToken($token, $this->userProvider, $this->providerKey);
4242

43-
if (!($authToken instanceof TokenInterface)) {
43+
if (!$authToken instanceof TokenInterface) {
4444
throw new AuthenticationException('Simple authenticator failed to return an authenticated token.');
4545
}
4646

47-
$this->userChecker->checkPreAuth($authToken->getUser());
48-
$this->userChecker->checkPostAuth($authToken->getUser());
47+
$user = $authToken->getUser();
48+
$this->userChecker->checkPreAuth($user);
49+
$this->userChecker->checkPostAuth($user);
4950

5051
return $authToken;
5152
}
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