|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Copyright (C) 2016 SURFnet. |
|
4
|
|
|
* |
|
5
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
6
|
|
|
* it under the terms of the GNU Affero General Public License as |
|
7
|
|
|
* published by the Free Software Foundation, either version 3 of the |
|
8
|
|
|
* License, or (at your option) any later version. |
|
9
|
|
|
* |
|
10
|
|
|
* This program is distributed in the hope that it will be useful, |
|
11
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13
|
|
|
* GNU Affero General Public License for more details. |
|
14
|
|
|
* |
|
15
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
16
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
namespace SURFnet\VPN\Server\Api; |
|
20
|
|
|
|
|
21
|
|
|
use Base32\Base32; |
|
22
|
|
|
use Otp\Otp; |
|
23
|
|
|
use PDO; |
|
24
|
|
|
use PHPUnit_Framework_TestCase; |
|
25
|
|
|
use SURFnet\VPN\Common\Config; |
|
26
|
|
|
use SURFnet\VPN\Common\Http\BasicAuthenticationHook; |
|
27
|
|
|
use SURFnet\VPN\Common\Http\Request; |
|
28
|
|
|
use SURFnet\VPN\Common\Http\Service; |
|
29
|
|
|
use SURFnet\VPN\Server\Acl\Provider\StaticProvider; |
|
30
|
|
|
use SURFnet\VPN\Server\Storage; |
|
31
|
|
|
|
|
32
|
|
|
class ConnectionsModuleTest extends PHPUnit_Framework_TestCase |
|
33
|
|
|
{ |
|
34
|
|
|
/** @var \SURFnet\VPN\Common\Http\Service */ |
|
35
|
|
|
private $service; |
|
36
|
|
|
|
|
37
|
|
View Code Duplication |
public function setUp() |
|
|
|
|
|
|
38
|
|
|
{ |
|
39
|
|
|
$random = $this->getMockBuilder('SURFnet\VPN\Common\RandomInterface')->getMock(); |
|
40
|
|
|
$random->method('get')->will($this->onConsecutiveCalls('random_1', 'random_2')); |
|
41
|
|
|
|
|
42
|
|
|
$storage = new Storage( |
|
43
|
|
|
new PDO('sqlite::memory:'), |
|
44
|
|
|
$random |
|
45
|
|
|
); |
|
46
|
|
|
$storage->init(); |
|
47
|
|
|
$storage->addCertificate('foo', '12345678901234567890123456789012', '12345678901234567890123456789012', 12345678, 23456789); |
|
48
|
|
|
$storage->setTotpSecret('foo', 'CN2XAL23SIFTDFXZ'); |
|
49
|
|
|
$storage->clientConnect('internet', '12345678901234567890123456789012', '10.10.10.10', 'fd00:4242:4242:4242::', 12345678); |
|
50
|
|
|
|
|
51
|
|
|
$config = Config::fromFile(sprintf('%s/data/config.yaml', __DIR__)); |
|
52
|
|
|
|
|
53
|
|
|
$groupProviders = [ |
|
54
|
|
|
new StaticProvider( |
|
55
|
|
|
new Config( |
|
56
|
|
|
$config->v('groupProviders', 'StaticProvider') |
|
57
|
|
|
) |
|
58
|
|
|
), |
|
59
|
|
|
]; |
|
60
|
|
|
|
|
61
|
|
|
$this->service = new Service(); |
|
62
|
|
|
$this->service->addModule( |
|
63
|
|
|
new ConnectionsModule( |
|
64
|
|
|
$config, |
|
65
|
|
|
$storage, |
|
66
|
|
|
$groupProviders |
|
67
|
|
|
) |
|
68
|
|
|
); |
|
69
|
|
|
|
|
70
|
|
|
$bearerAuthentication = new BasicAuthenticationHook( |
|
71
|
|
|
[ |
|
72
|
|
|
'vpn-server-node' => 'aabbcc', |
|
73
|
|
|
] |
|
74
|
|
|
); |
|
75
|
|
|
|
|
76
|
|
|
$this->service->addBeforeHook('auth', $bearerAuthentication); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
View Code Duplication |
public function testConnect() |
|
|
|
|
|
|
80
|
|
|
{ |
|
81
|
|
|
$this->assertTrue( |
|
82
|
|
|
$this->makeRequest( |
|
83
|
|
|
['vpn-server-node', 'aabbcc'], |
|
84
|
|
|
'POST', |
|
85
|
|
|
'connect', |
|
86
|
|
|
[], |
|
87
|
|
|
[ |
|
88
|
|
|
'profile_id' => 'internet', |
|
89
|
|
|
'common_name' => '12345678901234567890123456789012', |
|
90
|
|
|
'ip4' => '10.10.10.10', |
|
91
|
|
|
'ip6' => 'fd00:4242:4242:4242::', |
|
92
|
|
|
'connected_at' => 12345678, |
|
93
|
|
|
] |
|
94
|
|
|
) |
|
95
|
|
|
); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
View Code Duplication |
public function testConnectInAcl() |
|
|
|
|
|
|
99
|
|
|
{ |
|
100
|
|
|
$this->assertTrue( |
|
101
|
|
|
$this->makeRequest( |
|
102
|
|
|
['vpn-server-node', 'aabbcc'], |
|
103
|
|
|
'POST', |
|
104
|
|
|
'connect', |
|
105
|
|
|
[], |
|
106
|
|
|
[ |
|
107
|
|
|
'profile_id' => 'acl', |
|
108
|
|
|
'common_name' => '12345678901234567890123456789012', |
|
109
|
|
|
'ip4' => '10.10.10.10', |
|
110
|
|
|
'ip6' => 'fd00:4242:4242:4242::', |
|
111
|
|
|
'connected_at' => 12345678, |
|
112
|
|
|
] |
|
113
|
|
|
) |
|
114
|
|
|
); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
View Code Duplication |
public function testConnectNotInAcl() |
|
|
|
|
|
|
118
|
|
|
{ |
|
119
|
|
|
$this->assertSame( |
|
120
|
|
|
[ |
|
121
|
|
|
'ok' => false, |
|
122
|
|
|
'error' => 'user not in ACL', |
|
123
|
|
|
], |
|
124
|
|
|
$this->makeRequest( |
|
125
|
|
|
['vpn-server-node', 'aabbcc'], |
|
126
|
|
|
'POST', |
|
127
|
|
|
'connect', |
|
128
|
|
|
[], |
|
129
|
|
|
[ |
|
130
|
|
|
'profile_id' => 'acl2', |
|
131
|
|
|
'common_name' => '12345678901234567890123456789012', |
|
132
|
|
|
'ip4' => '10.10.10.10', |
|
133
|
|
|
'ip6' => 'fd00:4242:4242:4242::', |
|
134
|
|
|
'connected_at' => 12345678, |
|
135
|
|
|
] |
|
136
|
|
|
) |
|
137
|
|
|
); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
View Code Duplication |
public function testDisconnect() |
|
|
|
|
|
|
141
|
|
|
{ |
|
142
|
|
|
$this->assertTrue( |
|
143
|
|
|
$this->makeRequest( |
|
144
|
|
|
['vpn-server-node', 'aabbcc'], |
|
145
|
|
|
'POST', |
|
146
|
|
|
'disconnect', |
|
147
|
|
|
[], |
|
148
|
|
|
[ |
|
149
|
|
|
'profile_id' => 'internet', |
|
150
|
|
|
'common_name' => '12345678901234567890123456789012', |
|
151
|
|
|
'ip4' => '10.10.10.10', |
|
152
|
|
|
'ip6' => 'fd00:4242:4242:4242::', |
|
153
|
|
|
'connected_at' => 12345678, |
|
154
|
|
|
'disconnected_at' => 23456789, |
|
155
|
|
|
'bytes_transferred' => 2222222, |
|
156
|
|
|
] |
|
157
|
|
|
) |
|
158
|
|
|
); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
public function testVerifyOtp() |
|
162
|
|
|
{ |
|
163
|
|
|
$otp = new Otp(); |
|
164
|
|
|
$totpSecret = 'CN2XAL23SIFTDFXZ'; |
|
165
|
|
|
$totpKey = $otp->totp(Base32::decode($totpSecret)); |
|
166
|
|
|
|
|
167
|
|
|
$this->assertTrue( |
|
168
|
|
|
$this->makeRequest( |
|
169
|
|
|
['vpn-server-node', 'aabbcc'], |
|
170
|
|
|
'POST', |
|
171
|
|
|
'verify_otp', |
|
172
|
|
|
[], |
|
173
|
|
|
[ |
|
174
|
|
|
'common_name' => '12345678901234567890123456789012', |
|
175
|
|
|
'otp_type' => 'totp', |
|
176
|
|
|
'totp_key' => $totpKey, |
|
177
|
|
|
] |
|
178
|
|
|
) |
|
179
|
|
|
); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
View Code Duplication |
private function makeRequest(array $basicAuth, $requestMethod, $pathInfo, array $getData = [], array $postData = []) |
|
|
|
|
|
|
183
|
|
|
{ |
|
184
|
|
|
$response = $this->service->run( |
|
185
|
|
|
new Request( |
|
186
|
|
|
[ |
|
187
|
|
|
'SERVER_PORT' => 80, |
|
188
|
|
|
'SERVER_NAME' => 'vpn.example', |
|
189
|
|
|
'REQUEST_METHOD' => $requestMethod, |
|
190
|
|
|
'PATH_INFO' => sprintf('/%s', $pathInfo), |
|
191
|
|
|
'REQUEST_URI' => sprintf('/%s', $pathInfo), |
|
192
|
|
|
'PHP_AUTH_USER' => $basicAuth[0], |
|
193
|
|
|
'PHP_AUTH_PW' => $basicAuth[1], |
|
194
|
|
|
], |
|
195
|
|
|
$getData, |
|
196
|
|
|
$postData |
|
197
|
|
|
) |
|
198
|
|
|
); |
|
199
|
|
|
|
|
200
|
|
|
$responseArray = json_decode($response->getBody(), true)[$pathInfo]; |
|
201
|
|
|
if ($responseArray['ok']) { |
|
202
|
|
|
if (array_key_exists('data', $responseArray)) { |
|
203
|
|
|
return $responseArray['data']; |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
return true; |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
// in case of errors... |
|
210
|
|
|
return $responseArray; |
|
211
|
|
|
} |
|
212
|
|
|
} |
|
213
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.