-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Security] added userChecker to SimpleAuthenticationProvider #26370
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
c5bd0bd
[Security] added userChecker to SimpleAuthenticationProvider
63d9dce
[Security] added userChecker to SimpleAuthenticationProvider
i3or1s 3cc426e
[Security] added userChecker to SimpleAuthenticationProvider
i3or1s 476861e
[Security] added userChecker to SimpleAuthenticationProvider
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev
Previous commit
[Security] added userChecker to SimpleAuthenticationProvider
test for userChecker postAuth and preAuth
- Loading branch information
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
...omponent/Security/Core/Tests/Authentication/Provider/SimpleAuthenticationProviderTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Security\Core\Tests\Authentication\Provider; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\Security\Core\Exception\DisabledException; | ||
use Symfony\Component\Security\Core\Authentication\Provider\SimpleAuthenticationProvider; | ||
use Symfony\Component\Security\Core\Exception\LockedException; | ||
|
||
class SimpleAuthenticationProviderTest extends TestCase | ||
{ | ||
/** | ||
* @expectedException \Symfony\Component\Security\Core\Exception\DisabledException | ||
*/ | ||
public function testAuthenticateWhenPreChecksFails() | ||
{ | ||
$user = $this->getMockBuilder('Symfony\Component\Security\Core\User\UserInterface')->getMock(); | ||
|
||
$token = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')->getMock(); | ||
$token->expects($this->any()) | ||
->method('getUser') | ||
->will($this->returnValue($user)); | ||
|
||
$userChecker = $this->getMockBuilder('Symfony\Component\Security\Core\User\UserCheckerInterface')->getMock(); | ||
$userChecker->expects($this->once()) | ||
->method('checkPreAuth') | ||
->will($this->throwException(new DisabledException())); | ||
|
||
$authenticator = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\SimpleAuthenticatorInterface')->getMock(); | ||
$authenticator->expects($this->once()) | ||
->method('authenticateToken') | ||
->will($this->returnValue($token)); | ||
|
||
$provider = $this->getProvider($authenticator, null, $userChecker); | ||
|
||
$provider->authenticate($token); | ||
} | ||
|
||
/** | ||
* @expectedException \Symfony\Component\Security\Core\Exception\LockedException | ||
*/ | ||
public function testAuthenticateWhenPostChecksFails() | ||
{ | ||
$user = $this->getMockBuilder('Symfony\Component\Security\Core\User\UserInterface')->getMock(); | ||
|
||
$token = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')->getMock(); | ||
$token->expects($this->any()) | ||
->method('getUser') | ||
->will($this->returnValue($user)); | ||
|
||
$userChecker = $this->getMockBuilder('Symfony\Component\Security\Core\User\UserCheckerInterface')->getMock(); | ||
$userChecker->expects($this->once()) | ||
->method('checkPostAuth') | ||
->will($this->throwException(new LockedException())); | ||
|
||
$authenticator = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\SimpleAuthenticatorInterface')->getMock(); | ||
$authenticator->expects($this->once()) | ||
->method('authenticateToken') | ||
->will($this->returnValue($token)); | ||
|
||
$provider = $this->getProvider($authenticator, null, $userChecker); | ||
|
||
$provider->authenticate($token); | ||
} | ||
|
||
protected function getProvider($simpleAuthenticator = null, $userProvider = null, $userChecker = null, $key = 'test') | ||
{ | ||
if (null === $userChecker) { | ||
$userChecker = $this->getMockBuilder('Symfony\Component\Security\Core\User\UserCheckerInterface')->getMock(); | ||
} | ||
if (null === $simpleAuthenticator) { | ||
$simpleAuthenticator = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\SimpleAuthenticatorInterface')->getMock(); | ||
} | ||
if (null === $userProvider) { | ||
$userProvider = $this->getMockBuilder('Symfony\Component\Security\Core\User\UserProviderInterface')->getMock(); | ||
} | ||
|
||
return new SimpleAuthenticationProvider($simpleAuthenticator, $userProvider, $key, $userChecker); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While I understand the need to have the user here, it would be loaded twice now
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@iltar maybe then skip the pre auth check since it can be done during authentication of the token.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if you do
$this->userChecker->checkPreAuth($authToken->getUser())
? I know it's not exactly correct flow wise, but it beats fetching the user twice 🤔There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@iltar I was thinking about it but it sounds strange to me to pre authenticate authenticated token.
Changin interface to exclude the userProvider would cause BC breaks.
You are correct about twice loading the user and simple authenticator could do the pre-check if needed.
Maybe best is to remove the
checkPreAuth
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While it might seem strange, guard does it before/after the credentials check:
symfony/src/Symfony/Component/Security/Guard/Provider/GuardAuthenticationProvider.php
Lines 124 to 128 in a8dc953
I don't see another option to be honest, leaving it out would be as not having it at all.