Skip to content

Commit a7f2223

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 80c9ace + c40cea0 commit a7f2223

File tree

7 files changed

+83
-0
lines changed

7 files changed

+83
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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\Bundle\SecurityBundle\DependencyInjection\Security\Factory;
13+
14+
use Symfony\Component\Config\Definition\Builder\NodeDefinition;
15+
16+
use Symfony\Component\DependencyInjection\DefinitionDecorator;
17+
18+
use Symfony\Component\DependencyInjection\ContainerBuilder;
19+
use Symfony\Component\DependencyInjection\Reference;
20+
21+
/**
22+
* RemoteUserFactory creates services for REMOTE_USER based authentication.
23+
*
24+
* @author Fabien Potencier <[email protected]>
25+
* @author Maxime Douailin <[email protected]>
26+
*/
27+
class RemoteUserFactory implements SecurityFactoryInterface
28+
{
29+
public function create(ContainerBuilder $container, $id, $config, $userProvider, $defaultEntryPoint)
30+
{
31+
$providerId = 'security.authentication.provider.pre_authenticated.'.$id;
32+
$container
33+
->setDefinition($providerId, new DefinitionDecorator('security.authentication.provider.pre_authenticated'))
34+
->replaceArgument(0, new Reference($userProvider))
35+
->addArgument($id)
36+
;
37+
38+
$listenerId = 'security.authentication.listener.remote_user.'.$id;
39+
$listener = $container->setDefinition($listenerId, new DefinitionDecorator('security.authentication.listener.remote_user'));
40+
$listener->replaceArgument(2, $id);
41+
$listener->replaceArgument(3, $config['user']);
42+
43+
return array($providerId, $listenerId, $defaultEntryPoint);
44+
}
45+
46+
public function getPosition()
47+
{
48+
return 'pre_auth';
49+
}
50+
51+
public function getKey()
52+
{
53+
return 'remote-user';
54+
}
55+
56+
public function addConfiguration(NodeDefinition $node)
57+
{
58+
$node
59+
->children()
60+
->scalarNode('provider')->end()
61+
->scalarNode('user')->defaultValue('REMOTE_USER')->end()
62+
->end()
63+
;
64+
}
65+
}

Resources/config/security_listeners.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424

2525
<parameter key="security.authentication.listener.x509.class">Symfony\Component\Security\Http\Firewall\X509AuthenticationListener</parameter>
2626

27+
<parameter key="security.authentication.listener.remote_user.class">Symfony\Component\Security\Http\Firewall\RemoteUserAuthenticationListener</parameter>
28+
2729
<parameter key="security.authentication.listener.anonymous.class">Symfony\Component\Security\Http\Firewall\AnonymousAuthenticationListener</parameter>
2830

2931
<parameter key="security.authentication.switchuser_listener.class">Symfony\Component\Security\Http\Firewall\SwitchUserListener</parameter>
@@ -173,6 +175,16 @@
173175
<argument type="service" id="event_dispatcher" on-invalid="null"/>
174176
</service>
175177

178+
<service id="security.authentication.listener.remote_user" class="%security.authentication.listener.remote_user.class%" public="false" abstract="true">
179+
<tag name="monolog.logger" channel="security" />
180+
<argument type="service" id="security.context" />
181+
<argument type="service" id="security.authentication.manager" />
182+
<argument /> <!-- Provider-shared Key -->
183+
<argument /> <!-- REMOTE_USER server env var -->
184+
<argument type="service" id="logger" on-invalid="null" />
185+
<argument type="service" id="event_dispatcher" on-invalid="null"/>
186+
</service>
187+
176188
<service id="security.authentication.listener.basic" class="%security.authentication.listener.basic.class%" public="false" abstract="true">
177189
<tag name="monolog.logger" channel="security" />
178190
<argument type="service" id="security.context" />

SecurityBundle.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Symfony\Bundle\SecurityBundle\DependencyInjection\Security\Factory\HttpDigestFactory;
2020
use Symfony\Bundle\SecurityBundle\DependencyInjection\Security\Factory\RememberMeFactory;
2121
use Symfony\Bundle\SecurityBundle\DependencyInjection\Security\Factory\X509Factory;
22+
use Symfony\Bundle\SecurityBundle\DependencyInjection\Security\Factory\RemoteUserFactory;
2223
use Symfony\Bundle\SecurityBundle\DependencyInjection\Security\Factory\SimplePreAuthenticationFactory;
2324
use Symfony\Bundle\SecurityBundle\DependencyInjection\Security\Factory\SimpleFormFactory;
2425
use Symfony\Bundle\SecurityBundle\DependencyInjection\Security\UserProvider\InMemoryFactory;
@@ -40,6 +41,7 @@ public function build(ContainerBuilder $container)
4041
$extension->addSecurityListenerFactory(new HttpDigestFactory());
4142
$extension->addSecurityListenerFactory(new RememberMeFactory());
4243
$extension->addSecurityListenerFactory(new X509Factory());
44+
$extension->addSecurityListenerFactory(new RemoteUserFactory());
4345
$extension->addSecurityListenerFactory(new SimplePreAuthenticationFactory());
4446
$extension->addSecurityListenerFactory(new SimpleFormFactory());
4547

Tests/DependencyInjection/CompleteConfigurationTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ public function testFirewalls()
7878
'security.channel_listener',
7979
'security.logout_listener.secure',
8080
'security.authentication.listener.x509.secure',
81+
'security.authentication.listener.remote_user.secure',
8182
'security.authentication.listener.form.secure',
8283
'security.authentication.listener.basic.secure',
8384
'security.authentication.listener.digest.secure',

Tests/DependencyInjection/Fixtures/php/container1.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
'anonymous' => true,
7070
'switch_user' => true,
7171
'x509' => true,
72+
'remote_user' => true,
7273
'logout' => true,
7374
'remember_me' => array('key' => 'TheKey'),
7475
),

Tests/DependencyInjection/Fixtures/xml/container1.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
<anonymous />
5555
<switch-user />
5656
<x509 />
57+
<remote-user />
5758
<logout />
5859
<remember-me key="TheyKey"/>
5960
</firewall>

Tests/DependencyInjection/Fixtures/yml/container1.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ security:
5252
anonymous: true
5353
switch_user: true
5454
x509: true
55+
remote_user: true
5556
logout: true
5657
remember_me:
5758
key: TheKey

0 commit comments

Comments
 (0)