Skip to content

[Security] fixed issue where x509 authentication clears unrelated tokens #8528

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

Merged
merged 1 commit into from
Jul 20, 2013
Merged
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 @@ -186,7 +186,10 @@ private function onFailure(GetResponseEvent $event, Request $request, Authentica
$this->logger->info(sprintf('Authentication request failed: %s', $failed->getMessage()));
}

$this->securityContext->setToken(null);
$token = $this->securityContext->getToken();
if ($token instanceof UsernamePasswordToken && $this->providerKey === $token->getProviderKey()) {
$this->securityContext->setToken(null);
}

$response = $this->failureHandler->onAuthenticationFailure($request, $failed);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Security\Core\Exception\BadCredentialsException;

/**
* AbstractPreAuthenticatedListener is the base class for all listener that
Expand Down Expand Up @@ -59,7 +60,12 @@ final public function handle(GetResponseEvent $event)
$this->logger->debug(sprintf('Checking secure context token: %s', $this->securityContext->getToken()));
}

list($user, $credentials) = $this->getPreAuthenticatedData($request);
try {
list($user, $credentials) = $this->getPreAuthenticatedData($request);
} catch (BadCredentialsException $exception) {
$this->clearToken();
return;
}

if (null !== $token = $this->securityContext->getToken()) {
if ($token instanceof PreAuthenticatedToken && $this->providerKey == $token->getProviderKey() && $token->isAuthenticated() && $token->getUsername() === $user) {
Expand All @@ -84,6 +90,17 @@ final public function handle(GetResponseEvent $event)
$this->dispatcher->dispatch(SecurityEvents::INTERACTIVE_LOGIN, $loginEvent);
}
} catch (AuthenticationException $failed) {
$this->clearToken();
}
}

/**
* Clears a PreAuthenticatedToken for this provider (if present)
*/
protected function clearToken()
{
$token = $this->securityContext->getToken();
if ($token instanceof PreAuthenticatedToken && $this->providerKey === $token->getProviderKey()) {
$this->securityContext->setToken(null);

if (null !== $this->logger) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,10 @@ public function handle(GetResponseEvent $event)
$token = $this->authenticationManager->authenticate(new UsernamePasswordToken($username, $request->headers->get('PHP_AUTH_PW'), $this->providerKey));
$this->securityContext->setToken($token);
} catch (AuthenticationException $failed) {
$this->securityContext->setToken(null);
$token = $this->securityContext->getToken();
if ($token instanceof UsernamePasswordToken && $this->providerKey === $token->getProviderKey()) {
$this->securityContext->setToken(null);
}

if (null !== $this->logger) {
$this->logger->info(sprintf('Authentication request failed for user "%s": %s', $username, $failed->getMessage()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,10 @@ public function handle(GetResponseEvent $event)

private function fail(GetResponseEvent $event, Request $request, AuthenticationException $authException)
{
$this->securityContext->setToken(null);
$token = $this->securityContext->getToken();
if ($token instanceof UsernamePasswordToken && $this->providerKey === $token->getProviderKey()) {
$this->securityContext->setToken(null);
}

if (null !== $this->logger) {
$this->logger->info($authException);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,267 @@
<?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\Tests\Http\Firewall;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Authentication\Token\PreAuthenticatedToken;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use Symfony\Component\Security\Core\Exception\AuthenticationException;

class AbstractPreAuthenticatedListenerTest extends \PHPUnit_Framework_TestCase
{
protected function setUp()
{
if (!class_exists('Symfony\Component\EventDispatcher\EventDispatcher')) {
$this->markTestSkipped('The "EventDispatcher" component is not available');
}

if (!class_exists('Symfony\Component\HttpFoundation\Request')) {
$this->markTestSkipped('The "HttpFoundation" component is not available');
}

if (!class_exists('Symfony\Component\HttpKernel\HttpKernel')) {
$this->markTestSkipped('The "HttpKernel" component is not available');
}
}

public function testHandleWithValidValues()
{
$userCredentials = array('TheUser', 'TheCredentials');

$request = new Request(array(), array(), array(), array(), array(), array());

$token = $this->getMock('Symfony\Component\Security\Core\Authentication\Token\TokenInterface');

$context = $this->getMock('Symfony\Component\Security\Core\SecurityContextInterface');
$context
->expects($this->any())
->method('getToken')
->will($this->returnValue(null))
;
$context
->expects($this->once())
->method('setToken')
->with($this->equalTo($token))
;

$authenticationManager = $this->getMock('Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface');
$authenticationManager
->expects($this->once())
->method('authenticate')
->with($this->isInstanceOf('Symfony\Component\Security\Core\Authentication\Token\PreAuthenticatedToken'))
->will($this->returnValue($token))
;

$listener = $this->getMockForAbstractClass('Symfony\Component\Security\Http\Firewall\AbstractPreAuthenticatedListener', array(
$context,
$authenticationManager,
'TheProviderKey'
));
$listener
->expects($this->once())
->method('getPreAuthenticatedData')
->will($this->returnValue($userCredentials));

$event = $this->getMock('Symfony\Component\HttpKernel\Event\GetResponseEvent', array(), array(), '', false);
$event
->expects($this->any())
->method('getRequest')
->will($this->returnValue($request))
;

$listener->handle($event);
}

public function testHandleWhenAuthenticationFails()
{
$userCredentials = array('TheUser', 'TheCredentials');

$request = new Request(array(), array(), array(), array(), array(), array());

$context = $this->getMock('Symfony\Component\Security\Core\SecurityContextInterface');
$context
->expects($this->any())
->method('getToken')
->will($this->returnValue(null))
;
$context
->expects($this->never())
->method('setToken')
;

$exception = new AuthenticationException('Authentication failed.');
$authenticationManager = $this->getMock('Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface');
$authenticationManager
->expects($this->once())
->method('authenticate')
->with($this->isInstanceOf('Symfony\Component\Security\Core\Authentication\Token\PreAuthenticatedToken'))
->will($this->throwException($exception))
;

$listener = $this->getMockForAbstractClass('Symfony\Component\Security\Http\Firewall\AbstractPreAuthenticatedListener', array(
$context,
$authenticationManager,
'TheProviderKey'
));
$listener
->expects($this->once())
->method('getPreAuthenticatedData')
->will($this->returnValue($userCredentials));

$event = $this->getMock('Symfony\Component\HttpKernel\Event\GetResponseEvent', array(), array(), '', false);
$event
->expects($this->any())
->method('getRequest')
->will($this->returnValue($request))
;

$listener->handle($event);
}

public function testHandleWhenAuthenticationFailsWithDifferentToken()
{
$userCredentials = array('TheUser', 'TheCredentials');

$token = new UsernamePasswordToken('TheUsername', 'ThePassword', 'TheProviderKey', array('ROLE_FOO'));

$request = new Request(array(), array(), array(), array(), array(), array());

$context = $this->getMock('Symfony\Component\Security\Core\SecurityContextInterface');
$context
->expects($this->any())
->method('getToken')
->will($this->returnValue($token))
;
$context
->expects($this->never())
->method('setToken')
;

$exception = new AuthenticationException('Authentication failed.');
$authenticationManager = $this->getMock('Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface');
$authenticationManager
->expects($this->once())
->method('authenticate')
->with($this->isInstanceOf('Symfony\Component\Security\Core\Authentication\Token\PreAuthenticatedToken'))
->will($this->throwException($exception))
;

$listener = $this->getMockForAbstractClass('Symfony\Component\Security\Http\Firewall\AbstractPreAuthenticatedListener', array(
$context,
$authenticationManager,
'TheProviderKey'
));
$listener
->expects($this->once())
->method('getPreAuthenticatedData')
->will($this->returnValue($userCredentials));

$event = $this->getMock('Symfony\Component\HttpKernel\Event\GetResponseEvent', array(), array(), '', false);
$event
->expects($this->any())
->method('getRequest')
->will($this->returnValue($request))
;

$listener->handle($event);
}

public function testHandleWithASimilarAuthenticatedToken()
{
$userCredentials = array('TheUser', 'TheCredentials');

$request = new Request(array(), array(), array(), array(), array(), array());

$token = new PreAuthenticatedToken('TheUser', 'TheCredentials', 'TheProviderKey', array('ROLE_FOO'));

$context = $this->getMock('Symfony\Component\Security\Core\SecurityContextInterface');
$context
->expects($this->any())
->method('getToken')
->will($this->returnValue($token))
;

$authenticationManager = $this->getMock('Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface');
$authenticationManager
->expects($this->never())
->method('authenticate')
;

$listener = $this->getMockForAbstractClass('Symfony\Component\Security\Http\Firewall\AbstractPreAuthenticatedListener', array(
$context,
$authenticationManager,
'TheProviderKey'
));
$listener
->expects($this->once())
->method('getPreAuthenticatedData')
->will($this->returnValue($userCredentials));

$event = $this->getMock('Symfony\Component\HttpKernel\Event\GetResponseEvent', array(), array(), '', false);
$event
->expects($this->any())
->method('getRequest')
->will($this->returnValue($request))
;

$listener->handle($event);
}

public function testHandleWithAnInvalidSimilarToken()
{
$userCredentials = array('TheUser', 'TheCredentials');

$request = new Request(array(), array(), array(), array(), array(), array());

$token = new PreAuthenticatedToken('AnotherUser', 'TheCredentials', 'TheProviderKey', array('ROLE_FOO'));

$context = $this->getMock('Symfony\Component\Security\Core\SecurityContextInterface');
$context
->expects($this->any())
->method('getToken')
->will($this->returnValue($token))
;
$context
->expects($this->once())
->method('setToken')
->with($this->equalTo(null))
;

$exception = new AuthenticationException('Authentication failed.');
$authenticationManager = $this->getMock('Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface');
$authenticationManager
->expects($this->once())
->method('authenticate')
->with($this->isInstanceOf('Symfony\Component\Security\Core\Authentication\Token\PreAuthenticatedToken'))
->will($this->throwException($exception))
;

$listener = $this->getMockForAbstractClass('Symfony\Component\Security\Http\Firewall\AbstractPreAuthenticatedListener', array(
$context,
$authenticationManager,
'TheProviderKey'
));
$listener
->expects($this->once())
->method('getPreAuthenticatedData')
->will($this->returnValue($userCredentials));

$event = $this->getMock('Symfony\Component\HttpKernel\Event\GetResponseEvent', array(), array(), '', false);
$event
->expects($this->any())
->method('getRequest')
->will($this->returnValue($request))
;

$listener->handle($event);
}
}
Loading