|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* check if reading from cookie works |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
namespace Graviton\SecurityBundle\Authentication\Strategies; |
|
7
|
|
|
|
|
8
|
|
|
use Graviton\TestBundle\Test\WebTestCase; |
|
9
|
|
|
use Symfony\Component\BrowserKit\Cookie; |
|
10
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Class CookieFieldStrategyTest |
|
14
|
|
|
* |
|
15
|
|
|
* @author List of contributors <https://github.com/libgraviton/graviton/graphs/contributors> |
|
16
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GNU Public License |
|
17
|
|
|
* @link http://swisscom.ch |
|
18
|
|
|
*/ |
|
19
|
|
|
class CookieFieldStrategyTest extends WebTestCase |
|
20
|
|
|
{ |
|
21
|
|
|
protected $strategy; |
|
22
|
|
|
protected $client; |
|
23
|
|
|
protected $propertyKey; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* UnitTest Starts this on reach test |
|
27
|
|
|
* @return void |
|
28
|
|
|
*/ |
|
29
|
|
|
public function setUp() |
|
30
|
|
|
{ |
|
31
|
|
|
parent::setUp(); |
|
32
|
|
|
|
|
33
|
|
|
$this->client = static::createClient(); |
|
34
|
|
|
$this->propertyKey = $this->client->getKernel() |
|
35
|
|
|
->getContainer()->getParameter('graviton.security.authentication.strategy_key'); |
|
36
|
|
|
$this->strategy = new CookieFieldStrategy( |
|
37
|
|
|
$this->propertyKey |
|
38
|
|
|
); |
|
39
|
|
|
|
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @covers \Graviton\SecurityBundle\Authentication\Strategies\CookieFieldStrategy::apply |
|
44
|
|
|
* @covers \Graviton\SecurityBundle\Authentication\Strategies\AbstractHttpStrategy::extractFieldInfo |
|
45
|
|
|
* @covers \Graviton\SecurityBundle\Authentication\Strategies\AbstractHttpStrategy::validateField |
|
46
|
|
|
* |
|
47
|
|
|
* @dataProvider stringProvider |
|
48
|
|
|
* |
|
49
|
|
|
* @param string $fieldValue value to check |
|
50
|
|
|
* |
|
51
|
|
|
* @return void |
|
52
|
|
|
*/ |
|
53
|
|
|
public function testApply($fieldValue) |
|
54
|
|
|
{ |
|
55
|
|
|
$cookie = new Cookie( |
|
56
|
|
|
$this->propertyKey, |
|
57
|
|
|
$fieldValue, |
|
58
|
|
|
time() + 3600 * 24 * 7, |
|
59
|
|
|
'/', |
|
60
|
|
|
null, |
|
61
|
|
|
false, |
|
62
|
|
|
false |
|
63
|
|
|
); |
|
64
|
|
|
$this->client->getCookieJar()->set($cookie); |
|
65
|
|
|
$this->client->request( |
|
66
|
|
|
'GET', //method |
|
67
|
|
|
'/', //uri |
|
68
|
|
|
array(), //parameters |
|
69
|
|
|
array(), //files |
|
70
|
|
|
array() //server |
|
71
|
|
|
); |
|
72
|
|
|
|
|
73
|
|
|
$this->assertSame($fieldValue, $this->strategy->apply($this->client->getRequest())); |
|
|
|
|
|
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @return array<string> |
|
78
|
|
|
*/ |
|
79
|
|
View Code Duplication |
public function stringProvider() |
|
|
|
|
|
|
80
|
|
|
{ |
|
81
|
|
|
return array( |
|
82
|
|
|
'plain string, no special chars' => array('exampleAuthenticationHeader'), |
|
83
|
|
|
'string with special chars' => array("$-_.+!*'(),{}|\\^~[]`<>#%;/?:@&=."), |
|
84
|
|
|
'string with octal chars' => array("a: \141, A: \101"), |
|
85
|
|
|
'string with hex chars' => array("a: \x61, A: \x41") |
|
86
|
|
|
); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Todo, find a way to have also to client id set in request stack. |
|
91
|
|
|
* |
|
92
|
|
|
* @covers \Graviton\SecurityBundle\Authentication\Strategies\CookieFieldStrategy::apply |
|
93
|
|
|
* @covers \Graviton\SecurityBundle\Authentication\Strategies\AbstractHttpStrategy::extractFieldInfo |
|
94
|
|
|
* @covers \Graviton\SecurityBundle\Authentication\Strategies\AbstractHttpStrategy::validateField |
|
95
|
|
|
* @covers \Graviton\SecurityBundle\Authentication\Strategies\AbstractHttpStrategy::setDynamicParameters |
|
96
|
|
|
* |
|
97
|
|
|
* @dataProvider stringExtractProvider |
|
98
|
|
|
* |
|
99
|
|
|
* @param string $fieldValue value to check |
|
100
|
|
|
* |
|
101
|
|
|
* @return void |
|
102
|
|
|
*/ |
|
103
|
|
|
public function testApplyExtract($fieldValue) |
|
104
|
|
|
{ |
|
105
|
|
|
$cookie = new Cookie( |
|
106
|
|
|
$this->propertyKey, |
|
107
|
|
|
$fieldValue, |
|
108
|
|
|
time() + 3600 * 24 * 7, |
|
109
|
|
|
'/', |
|
110
|
|
|
null, |
|
111
|
|
|
false, |
|
112
|
|
|
false |
|
113
|
|
|
); |
|
114
|
|
|
$this->client->getCookieJar()->set($cookie); |
|
115
|
|
|
$this->client->request( |
|
116
|
|
|
'GET', //method |
|
117
|
|
|
'/', //uri |
|
118
|
|
|
array(), //parameters |
|
119
|
|
|
array(), //files |
|
120
|
|
|
array() //server |
|
121
|
|
|
); |
|
122
|
|
|
|
|
123
|
|
|
$this->strategy->setDynamicParameters(new RequestStack(), 'username', false, false); |
|
|
|
|
|
|
124
|
|
|
$username = $this->strategy->apply($this->client->getRequest()); |
|
|
|
|
|
|
125
|
|
|
$this->assertSame('testUser', $username); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* @return array<string> |
|
130
|
|
|
*/ |
|
131
|
|
|
public function stringExtractProvider() |
|
132
|
|
|
{ |
|
133
|
|
|
return array( |
|
134
|
|
|
'testing extract username' => array("username=testUser,core_client_id=someId123"), |
|
135
|
|
|
'testing extract rev username' => array("core_client_id=someId123,username=testUser"), |
|
136
|
|
|
); |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: