|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Handling authentication for clients in the same network. |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
namespace Graviton\SecurityBundle\Authentication\Strategies; |
|
7
|
|
|
|
|
8
|
|
|
use Graviton\TestBundle\Test\WebTestCase; |
|
9
|
|
|
use Symfony\Bundle\FrameworkBundle\Client; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Class SameSubnetStrategyTest |
|
13
|
|
|
* |
|
14
|
|
|
* @author List of contributors <https://github.com/libgraviton/graviton/graphs/contributors> |
|
15
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GNU Public License |
|
16
|
|
|
* @link http://swisscom.ch |
|
17
|
|
|
*/ |
|
18
|
|
|
class SameSubnetStrategyTest extends WebTestCase |
|
19
|
|
|
{ |
|
20
|
|
|
protected $strategy; |
|
21
|
|
|
/** @var Client */ |
|
22
|
|
|
protected $client; |
|
23
|
|
|
protected $propertyKey; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* UnitTest Starts this on reach test |
|
27
|
|
|
* @return void |
|
28
|
|
|
*/ |
|
29
|
|
View Code Duplication |
public function setUp() |
|
30
|
|
|
{ |
|
31
|
|
|
parent::setUp(); |
|
32
|
|
|
|
|
33
|
|
|
$this->client = static::createClient(); |
|
34
|
|
|
$this->propertyKey = $this->client->getKernel() |
|
35
|
|
|
->getContainer() |
|
36
|
|
|
->getParameter('graviton.security.authentication.strategy.subnet.key'); |
|
37
|
|
|
$this->strategy = new SameSubnetStrategy( |
|
38
|
|
|
$this->propertyKey |
|
39
|
|
|
); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @covers \Graviton\SecurityBundle\Authentication\Strategies\SameSubnetStrategy::apply |
|
44
|
|
|
* @covers \Graviton\SecurityBundle\Authentication\Strategies\AbstractHttpStrategy::extractFieldInfo |
|
45
|
|
|
* @covers \Graviton\SecurityBundle\Authentication\Strategies\AbstractHttpStrategy::validateField |
|
46
|
|
|
* |
|
47
|
|
|
* @return void |
|
48
|
|
|
*/ |
|
49
|
|
|
public function testApplyHeader() |
|
50
|
|
|
{ |
|
51
|
|
|
$client = static::createClient([], ['HTTP_X-GRAVITON-AUTHENTICATION' => 'test-user-name']); |
|
52
|
|
|
$this->strategy->setSubnetIp('127.0.0.0/7'); |
|
53
|
|
|
$client->request('GET', '/'); |
|
54
|
|
|
|
|
55
|
|
|
$this->assertSame('test-user-name', $this->strategy->apply($client->getRequest())); |
|
|
|
|
|
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @covers \Graviton\SecurityBundle\Authentication\Strategies\SameSubnetStrategy::apply |
|
60
|
|
|
* @covers \Graviton\SecurityBundle\Authentication\Strategies\AbstractHttpStrategy::extractFieldInfo |
|
61
|
|
|
* @covers \Graviton\SecurityBundle\Authentication\Strategies\AbstractHttpStrategy::validateField |
|
62
|
|
|
* |
|
63
|
|
|
* @return void |
|
64
|
|
|
*/ |
|
65
|
|
|
public function testApplyHeaderReturnEmpty() |
|
66
|
|
|
{ |
|
67
|
|
|
$options = ['HTTP_X-GRAVITON-AUTHENTICATION' => 'test-user-name', 'REMOTE_ADDR' => '126.0.0.1']; |
|
68
|
|
|
$client = static::createClient([], $options); |
|
69
|
|
|
$client->request('GET', '/'); |
|
70
|
|
|
|
|
71
|
|
|
$this->assertSame('', $this->strategy->apply($client->getRequest())); |
|
|
|
|
|
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: