summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFabien Potencier <fabien.potencier@gmail.com>2013-07-20 09:42:41 +0200
committerFabien Potencier <fabien.potencier@gmail.com>2013-07-20 09:42:41 +0200
commit710089319e7e5fa16c5b5654e17da5c5a0e02fe4 (patch)
treeb9ee3eb667ca868e0d795a42532ef79176c88287
parent794336a0c44bba2d62cd9bedcf90a4b039ed5bdd (diff)
parentfe2f6100bba95f9bc77ab93bfc0dcda064cfa780 (diff)
downloadsymfony-security-710089319e7e5fa16c5b5654e17da5c5a0e02fe4.zip
symfony-security-710089319e7e5fa16c5b5654e17da5c5a0e02fe4.tar.gz
symfony-security-710089319e7e5fa16c5b5654e17da5c5a0e02fe4.tar.bz2
Merge branch '2.3'
* 2.3: [PropertyAccess] added moves to pluralMap [Security] fixed issue where authentication listeners clear unrelated tokens added greek translation [DependencyInjection] Add exception for service name not dumpable in PHP bumped Symfony version to 2.3.3-DEV fix issue #8499 modelChoiceList call getPrimaryKey on a non object updated VERSION for 2.3.2 updated CHANGELOG for 2.3.2 [DependencyInjection] Add exception for service name not dumpable in PHP fixed typo bumped Symfony version to 2.2.5 updated VERSION for 2.2.4 update CONTRIBUTORS for 2.2.4 updated CHANGELOG for 2.2.4 Fixed NativeSessionStorage:regenerate when does not exists removed extraneous whitespaces Conflicts: src/Symfony/Component/HttpKernel/Kernel.php
-rw-r--r--Http/Firewall/AbstractAuthenticationListener.php5
-rw-r--r--Http/Firewall/AbstractPreAuthenticatedListener.php19
-rw-r--r--Http/Firewall/BasicAuthenticationListener.php5
-rw-r--r--Http/Firewall/DigestAuthenticationListener.php5
-rw-r--r--Resources/translations/security.el.xlf71
-rw-r--r--Tests/Http/Firewall/AbstractPreAuthenticatedListenerTest.php267
-rw-r--r--Tests/Http/Firewall/BasicAuthenticationListenerTest.php56
-rw-r--r--Tests/Http/Firewall/X509AuthenticationListenerTest.php115
8 files changed, 537 insertions, 6 deletions
diff --git a/Http/Firewall/AbstractAuthenticationListener.php b/Http/Firewall/AbstractAuthenticationListener.php
index 4e71e99..92618e5 100644
--- a/Http/Firewall/AbstractAuthenticationListener.php
+++ b/Http/Firewall/AbstractAuthenticationListener.php
@@ -194,7 +194,10 @@ abstract class AbstractAuthenticationListener implements ListenerInterface
$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);
diff --git a/Http/Firewall/AbstractPreAuthenticatedListener.php b/Http/Firewall/AbstractPreAuthenticatedListener.php
index c6e47d0..28f6411 100644
--- a/Http/Firewall/AbstractPreAuthenticatedListener.php
+++ b/Http/Firewall/AbstractPreAuthenticatedListener.php
@@ -21,6 +21,7 @@ use Symfony\Component\HttpKernel\Event\GetResponseEvent;
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
@@ -59,7 +60,12 @@ abstract class AbstractPreAuthenticatedListener implements ListenerInterface
$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) {
@@ -84,6 +90,17 @@ abstract class AbstractPreAuthenticatedListener implements ListenerInterface
$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) {
diff --git a/Http/Firewall/BasicAuthenticationListener.php b/Http/Firewall/BasicAuthenticationListener.php
index 5b1c8b3..bfc4abc 100644
--- a/Http/Firewall/BasicAuthenticationListener.php
+++ b/Http/Firewall/BasicAuthenticationListener.php
@@ -74,7 +74,10 @@ class BasicAuthenticationListener implements ListenerInterface
$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()));
diff --git a/Http/Firewall/DigestAuthenticationListener.php b/Http/Firewall/DigestAuthenticationListener.php
index 7ab3dcf..ea85e77 100644
--- a/Http/Firewall/DigestAuthenticationListener.php
+++ b/Http/Firewall/DigestAuthenticationListener.php
@@ -124,7 +124,10 @@ class DigestAuthenticationListener implements ListenerInterface
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);
diff --git a/Resources/translations/security.el.xlf b/Resources/translations/security.el.xlf
new file mode 100644
index 0000000..07eabe7
--- /dev/null
+++ b/Resources/translations/security.el.xlf
@@ -0,0 +1,71 @@
+<?xml version="1.0"?>
+<xliff version="1.2" xmlns="urn:oasis:names:tc:xliff:document:1.2">
+ <file source-language="en" datatype="plaintext" original="file.ext">
+ <body>
+ <trans-unit id="1">
+ <source>An authentication exception occurred.</source>
+ <target>Συνέβη ένα σφάλμα πιστοποίησης.</target>
+ </trans-unit>
+ <trans-unit id="2">
+ <source>Authentication credentials could not be found.</source>
+ <target>Τα στοιχεία πιστοποίησης δε βρέθηκαν.</target>
+ </trans-unit>
+ <trans-unit id="3">
+ <source>Authentication request could not be processed due to a system problem.</source>
+ <target>Το αίτημα πιστοποίησης δε μπορεί να επεξεργαστεί λόγω σφάλματος του συστήματος.</target>
+ </trans-unit>
+ <trans-unit id="4">
+ <source>Invalid credentials.</source>
+ <target>Λανθασμένα στοιχεία σύνδεσης.</target>
+ </trans-unit>
+ <trans-unit id="5">
+ <source>Cookie has already been used by someone else.</source>
+ <target>Το Cookie έχει ήδη χρησιμοποιηθεί από κάποιον άλλο.</target>
+ </trans-unit>
+ <trans-unit id="6">
+ <source>Not privileged to request the resource.</source>
+ <target>Δεν είστε εξουσιοδοτημένος για πρόσβαση στο συγκεκριμένο περιεχόμενο.</target>
+ </trans-unit>
+ <trans-unit id="7">
+ <source>Invalid CSRF token.</source>
+ <target>Μη έγκυρο CSRF token.</target>
+ </trans-unit>
+ <trans-unit id="8">
+ <source>Digest nonce has expired.</source>
+ <target>Το digest nonce έχει λήξει.</target>
+ </trans-unit>
+ <trans-unit id="9">
+ <source>No authentication provider found to support the authentication token.</source>
+ <target>Δε βρέθηκε κάποιος πάροχος πιστοποίησης που να υποστηρίζει το token πιστοποίησης.</target>
+ </trans-unit>
+ <trans-unit id="10">
+ <source>No session available, it either timed out or cookies are not enabled.</source>
+ <target>Δεν υπάρχει ενεργή σύνοδος (session), είτε έχει λήξει ή τα cookies δεν είναι ενεργοποιημένα.</target>
+ </trans-unit>
+ <trans-unit id="11">
+ <source>No token could be found.</source>
+ <target>Δεν ήταν δυνατόν να βρεθεί κάποιο token.</target>
+ </trans-unit>
+ <trans-unit id="12">
+ <source>Username could not be found.</source>
+ <target>Το Username δε βρέθηκε.</target>
+ </trans-unit>
+ <trans-unit id="13">
+ <source>Account has expired.</source>
+ <target>Ο λογαριασμός έχει λήξει.</target>
+ </trans-unit>
+ <trans-unit id="14">
+ <source>Credentials have expired.</source>
+ <target>Τα στοιχεία σύνδεσης έχουν λήξει.</target>
+ </trans-unit>
+ <trans-unit id="15">
+ <source>Account is disabled.</source>
+ <target>Ο λογαριασμός είναι απενεργοποιημένος.</target>
+ </trans-unit>
+ <trans-unit id="16">
+ <source>Account is locked.</source>
+ <target>Ο λογαριασμός είναι κλειδωμένος.</target>
+ </trans-unit>
+ </body>
+ </file>
+</xliff>
diff --git a/Tests/Http/Firewall/AbstractPreAuthenticatedListenerTest.php b/Tests/Http/Firewall/AbstractPreAuthenticatedListenerTest.php
new file mode 100644
index 0000000..76721ec
--- /dev/null
+++ b/Tests/Http/Firewall/AbstractPreAuthenticatedListenerTest.php
@@ -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);
+ }
+}
diff --git a/Tests/Http/Firewall/BasicAuthenticationListenerTest.php b/Tests/Http/Firewall/BasicAuthenticationListenerTest.php
index 5870956..7616149 100644
--- a/Tests/Http/Firewall/BasicAuthenticationListenerTest.php
+++ b/Tests/Http/Firewall/BasicAuthenticationListenerTest.php
@@ -12,6 +12,7 @@
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\HttpFoundation\Response;
use Symfony\Component\Security\Http\Firewall\BasicAuthenticationListener;
@@ -96,9 +97,8 @@ class BasicAuthenticationListenerTest extends \PHPUnit_Framework_TestCase
->will($this->returnValue(null))
;
$context
- ->expects($this->once())
+ ->expects($this->never())
->method('setToken')
- ->with($this->equalTo(null))
;
$response = new Response();
@@ -209,4 +209,56 @@ class BasicAuthenticationListenerTest extends \PHPUnit_Framework_TestCase
$this->getMock('Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface')
);
}
+
+ public function testHandleWithADifferentAuthenticatedToken()
+ {
+ $request = new Request(array(), array(), array(), array(), array(), array(
+ 'PHP_AUTH_USER' => 'TheUsername',
+ 'PHP_AUTH_PW' => 'ThePassword'
+ ));
+
+ $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))
+ ;
+ $context
+ ->expects($this->never())
+ ->method('setToken')
+ ;
+
+ $response = new Response();
+
+ $authenticationEntryPoint = $this->getMock('Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface');
+ $authenticationEntryPoint
+ ->expects($this->any())
+ ->method('start')
+ ->with($this->equalTo($request), $this->isInstanceOf('Symfony\Component\Security\Core\Exception\AuthenticationException'))
+ ->will($this->returnValue($response))
+ ;
+
+ $listener = new BasicAuthenticationListener(
+ $context,
+ new AuthenticationProviderManager(array($this->getMock('Symfony\Component\Security\Core\Authentication\Provider\AuthenticationProviderInterface'))),
+ 'TheProviderKey',
+ $authenticationEntryPoint
+ );
+
+ $event = $this->getMock('Symfony\Component\HttpKernel\Event\GetResponseEvent', array(), array(), '', false);
+ $event
+ ->expects($this->any())
+ ->method('getRequest')
+ ->will($this->returnValue($request))
+ ;
+ $event
+ ->expects($this->once())
+ ->method('setResponse')
+ ->with($this->equalTo($response))
+ ;
+
+ $listener->handle($event);
+ }
}
diff --git a/Tests/Http/Firewall/X509AuthenticationListenerTest.php b/Tests/Http/Firewall/X509AuthenticationListenerTest.php
new file mode 100644
index 0000000..81ac0f7
--- /dev/null
+++ b/Tests/Http/Firewall/X509AuthenticationListenerTest.php
@@ -0,0 +1,115 @@
+<?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\Http\Firewall\X509AuthenticationListener;
+
+class X509AuthenticationListenerTest extends \PHPUnit_Framework_TestCase
+{
+ protected function setUp()
+ {
+ if (!class_exists('Symfony\Component\HttpFoundation\Request')) {
+ $this->markTestSkipped('The "HttpFoundation" component is not available');
+ }
+ }
+
+ /**
+ * @dataProvider dataProviderGetPreAuthenticatedData
+ */
+ public function testGetPreAuthenticatedData($user, $credentials)
+ {
+ $serverVars = array();
+ if ('' !== $user) {
+ $serverVars['SSL_CLIENT_S_DN_Email'] = $user;
+ }
+ if ('' !== $credentials) {
+ $serverVars['SSL_CLIENT_S_DN'] = $credentials;
+ }
+
+ $request = new Request(array(), array(), array(), array(), array(), $serverVars);
+
+ $context = $this->getMock('Symfony\Component\Security\Core\SecurityContextInterface');
+
+ $authenticationManager = $this->getMock('Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface');
+
+ $listener = new X509AuthenticationListener(
+ $context,
+ $authenticationManager,
+ 'TheProviderKey'
+ );
+
+ $method = new \ReflectionMethod($listener, 'getPreAuthenticatedData');
+ $method->setAccessible(true);
+
+ $result = $method->invokeArgs($listener, array($request));
+ $this->assertSame($result, array($user, $credentials));
+ }
+
+ public static function dataProviderGetPreAuthenticatedData()
+ {
+ return array(
+ 'validValues' => array('TheUser', 'TheCredentials'),
+ 'noCredentials' => array('TheUser', ''),
+ );
+ }
+
+ /**
+ * @expectedException Symfony\Component\Security\Core\Exception\BadCredentialsException
+ */
+ public function testGetPreAuthenticatedDataNoUser()
+ {
+ $request = new Request(array(), array(), array(), array(), array(), array());
+
+ $context = $this->getMock('Symfony\Component\Security\Core\SecurityContextInterface');
+
+ $authenticationManager = $this->getMock('Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface');
+
+ $listener = new X509AuthenticationListener(
+ $context,
+ $authenticationManager,
+ 'TheProviderKey'
+ );
+
+ $method = new \ReflectionMethod($listener, 'getPreAuthenticatedData');
+ $method->setAccessible(true);
+
+ $result = $method->invokeArgs($listener, array($request));
+ }
+
+ public function testGetPreAuthenticatedDataWithDifferentKeys()
+ {
+ $userCredentials = array('TheUser', 'TheCredentials');
+
+ $request = new Request(array(), array(), array(), array(), array(), array(
+ 'TheUserKey' => 'TheUser',
+ 'TheCredentialsKey' => 'TheCredentials'
+ ));
+ $context = $this->getMock('Symfony\Component\Security\Core\SecurityContextInterface');
+
+ $authenticationManager = $this->getMock('Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface');
+
+ $listener = new X509AuthenticationListener(
+ $context,
+ $authenticationManager,
+ 'TheProviderKey',
+ 'TheUserKey',
+ 'TheCredentialsKey'
+ );
+
+ $method = new \ReflectionMethod($listener, 'getPreAuthenticatedData');
+ $method->setAccessible(true);
+
+ $result = $method->invokeArgs($listener, array($request));
+ $this->assertSame($result, $userCredentials);
+ }
+}