Skip to content
This repository was archived by the owner on May 31, 2024. It is now read-only.

Commit ddbd3ca

Browse files
committed
feature #10698 [Security] Added a REMOTE_USER based listener to security firewalls (Maxime Douailin)
This PR was squashed before being merged into the 2.6-dev branch (closes #10698). Discussion ---------- [Security] Added a REMOTE_USER based listener to security firewalls | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | / | License | MIT | Doc PR | symfony/symfony-docs#3912 TODO - [x] submit changes to the documentation I've seen myself implementing a few times a REMOTE_USER based authentication listener, as a large part of security modules for Apache (Kerberos, CAS, and more) are providing the username via an environment variable. So I thought this could benefit the whole community if directly included in the framework. It is very similar to the X509AuthenticationListener, and basing the RemoteUserAuthenticationListener on the AbstractPreAuthenticatedListener is relevant and very convenient. Using the X509AuthenticationListener could be possible, but it is confusing to use it directly when your authentication is not certificate based. Please let me know if I need to update anything. Regards Commits ------- a2872f2 [Security] Added a REMOTE_USER based listener to security firewalls
2 parents 18d45ff + 3b2cb19 commit ddbd3ca

File tree

2 files changed

+140
-0
lines changed

2 files changed

+140
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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\Http\Firewall;
13+
14+
use Symfony\Component\Security\Core\SecurityContextInterface;
15+
use Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface;
16+
use Psr\Log\LoggerInterface;
17+
use Symfony\Component\HttpFoundation\Request;
18+
use Symfony\Component\Security\Core\Exception\BadCredentialsException;
19+
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
20+
21+
/**
22+
* REMOTE_USER authentication listener.
23+
*
24+
* @author Fabien Potencier <fabien@symfony.com>
25+
* @author Maxime Douailin <maxime.douailin@gmail.com>
26+
*/
27+
class RemoteUserAuthenticationListener extends AbstractPreAuthenticatedListener
28+
{
29+
private $userKey;
30+
31+
public function __construct(SecurityContextInterface $securityContext, AuthenticationManagerInterface $authenticationManager, $providerKey, $userKey = 'REMOTE_USER', LoggerInterface $logger = null, EventDispatcherInterface $dispatcher = null)
32+
{
33+
parent::__construct($securityContext, $authenticationManager, $providerKey, $logger, $dispatcher);
34+
35+
$this->userKey = $userKey;
36+
}
37+
38+
/**
39+
* {@inheritdoc}
40+
*/
41+
protected function getPreAuthenticatedData(Request $request)
42+
{
43+
if (!$request->server->has($this->userKey)) {
44+
throw new BadCredentialsException(sprintf('User key was not found: %s', $this->userKey));
45+
}
46+
47+
return array($request->server->get($this->userKey), null);
48+
}
49+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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\Http\Tests\Firewall;
13+
14+
use Symfony\Component\HttpFoundation\Request;
15+
use Symfony\Component\Security\Http\Firewall\RemoteUserAuthenticationListener;
16+
17+
class RemoteUserAuthenticationListenerTest extends \PHPUnit_Framework_TestCase
18+
{
19+
public function testGetPreAuthenticatedData()
20+
{
21+
$serverVars = array(
22+
'REMOTE_USER' => 'TheUser'
23+
);
24+
25+
$request = new Request(array(), array(), array(), array(), array(), $serverVars);
26+
27+
$context = $this->getMock('Symfony\Component\Security\Core\SecurityContextInterface');
28+
29+
$authenticationManager = $this->getMock('Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface');
30+
31+
$listener = new RemoteUserAuthenticationListener(
32+
$context,
33+
$authenticationManager,
34+
'TheProviderKey'
35+
);
36+
37+
$method = new \ReflectionMethod($listener, 'getPreAuthenticatedData');
38+
$method->setAccessible(true);
39+
40+
$result = $method->invokeArgs($listener, array($request));
41+
$this->assertSame($result, array('TheUser', null));
42+
}
43+
44+
/**
45+
* @expectedException \Symfony\Component\Security\Core\Exception\BadCredentialsException
46+
*/
47+
public function testGetPreAuthenticatedDataNoUser()
48+
{
49+
$request = new Request(array(), array(), array(), array(), array(), array());
50+
51+
$context = $this->getMock('Symfony\Component\Security\Core\SecurityContextInterface');
52+
53+
$authenticationManager = $this->getMock('Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface');
54+
55+
$listener = new RemoteUserAuthenticationListener(
56+
$context,
57+
$authenticationManager,
58+
'TheProviderKey'
59+
);
60+
61+
$method = new \ReflectionMethod($listener, 'getPreAuthenticatedData');
62+
$method->setAccessible(true);
63+
64+
$result = $method->invokeArgs($listener, array($request));
65+
}
66+
67+
public function testGetPreAuthenticatedDataWithDifferentKeys()
68+
{
69+
$userCredentials = array('TheUser', null);
70+
71+
$request = new Request(array(), array(), array(), array(), array(), array(
72+
'TheUserKey' => 'TheUser'
73+
));
74+
$context = $this->getMock('Symfony\Component\Security\Core\SecurityContextInterface');
75+
76+
$authenticationManager = $this->getMock('Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface');
77+
78+
$listener = new RemoteUserAuthenticationListener(
79+
$context,
80+
$authenticationManager,
81+
'TheProviderKey',
82+
'TheUserKey'
83+
);
84+
85+
$method = new \ReflectionMethod($listener, 'getPreAuthenticatedData');
86+
$method->setAccessible(true);
87+
88+
$result = $method->invokeArgs($listener, array($request));
89+
$this->assertSame($result, $userCredentials);
90+
}
91+
}

0 commit comments

Comments
 (0)