8000 Automatic logout after changing password on another session by xaben · Pull Request #19033 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

Automatic logout after changing password on another session #19033

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
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ protected function che 8000 ckAuthentication(UserInterface $user, UsernamePasswordToke
protected function retrieveUser($username, UsernamePasswordToken $token)
{
$user = $token->getUser();
if ($user instanceof UserInterface) {
if ($user instanceof UserInterface && $token->isAuthenticated()) {
return $user;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,9 @@ public function setUser($user)

if ($changed) {
$this->setAuthenticated(false);
} else {
$this->user = $user;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Won't this break applications that rely on the current behaviour?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well as I stated at the top of my initial comment, this is the point that is actually the most problematic. And I need input frim somebody that knows really well the security component and it's usage.

This setter as it stands does too much on it's own, and should be refactored. Most likely separating the logic for marking a token as not authenticated and doing a check elsewhere before setting the token.

The logout fix will not work if we overwrite the user in the token with the refreshed user.


$this->user = $user;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ public function testRetrieveUserReturnsUserFromTokenOnReauthentication()
->method('getUser')
->will($this->returnValue($user))
;
$token->expects($this->once())
->method('isAuthenticated')
->will($this->returnValue(true))
;

$provider = new DaoAuthenticationProvider($userProvider, $this->getMock('Symfony\\Component\\Security\\Core\\User\\UserCheckerInterface'), 'key', $this->getMock('Symfony\\Component\\Security\\Core\\Encoder\\EncoderFactoryInterface'));
$reflection = new \ReflectionMethod($provider, 'retrieveUser');
Expand Down Expand Up @@ -260,7 +264,7 @@ public function testCheckAuthentication()

protected function getSupportedToken()
{
$mock = $this->getMock('Symfony\\Component\\Security\\Core\\Authentication\\Token\\UsernamePasswordToken', array('getCredentials', 'getUser', 'getProviderKey'), array(), '', false);
$mock = $this->getMock('Symfony\\Component\\Security\\Core\\Authentication\\Token\\UsernamePasswordToken', array('getCredentials', 'getUser', 'getProviderKey', 'isAuthenticated'), array(), '', false);
$mock
->expects($this->any())
->method('getProviderKey')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,11 @@ public function testGetUsername()
$token->setUser('fabien');
$this->assertEquals('fabien', $token->getUsername());

$token = $this->getToken(array('ROLE_FOO'));
$token->setUser(new TestUser('fabien'));
$this->assertEquals('fabien', $token->getUsername());

$token = $this->getToken(array('ROLE_FOO'));
$user = $this->getMock('Symfony\Component\Security\Core\User\UserInterface');
$user->expects($this->once())->method('getUsername')->will($this->returnValue('fabien'));
$token->setUser($user);
Expand Down
10 changes: 10 additions & 0 deletions src/Symfony/Component/Security/Http/Firewall/ExceptionListener.php
810D
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\Security\Http\Firewall;

use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\Exception\BadCredentialsException;
use Symfony\Component\Security\Http\Authorization\AccessDeniedHandlerInterface;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Security\Core\Authentication\AuthenticationTrustResolverInterface;
Expand Down Expand Up @@ -203,6 +204,15 @@ private function startAuthentication(Request $request, AuthenticationException $
}
}

if ($authException instanceof BadCredentialsException) {
// remove the security token to prevent infinite redirect loops
$this->tokenStorage->setToken(null);

if (null !== $this->logger) {
$this->logger->info('The security token was removed due to a BadCredentialsException.', array('exception' => $authException));
}
}

$response = $this->authenticationEntryPoint->start($request, $authException);

if (!$response instanceof Response) {
Expand Down
0