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

Commit 3b2cb19

Browse files
mdouailinfabpot
authored andcommitted
[Security] Added a REMOTE_USER based listener to security firewalls
1 parent f70d342 commit 3b2cb19

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)