|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* eduVPN - End-user friendly VPN. |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright: 2016-2017, The Commons Conservancy eduVPN Programme |
|
7
|
|
|
* SPDX-License-Identifier: AGPL-3.0+ |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace SURFnet\VPN\Server\Tests\Api; |
|
11
|
|
|
|
|
12
|
|
|
use DateTime; |
|
13
|
|
|
use Otp\Otp; |
|
14
|
|
|
use ParagonIE\ConstantTime\Encoding; |
|
15
|
|
|
use PDO; |
|
16
|
|
|
use PHPUnit_Framework_TestCase; |
|
17
|
|
|
use SURFnet\VPN\Common\Config; |
|
18
|
|
|
use SURFnet\VPN\Common\Http\BasicAuthenticationHook; |
|
19
|
|
|
use SURFnet\VPN\Common\Http\Request; |
|
20
|
|
|
use SURFnet\VPN\Common\Http\Service; |
|
21
|
|
|
use SURFnet\VPN\Server\Acl\Provider\StaticProvider; |
|
22
|
|
|
use SURFnet\VPN\Server\Api\ConnectionsModule; |
|
23
|
|
|
use SURFnet\VPN\Server\Storage; |
|
24
|
|
|
|
|
25
|
|
|
class ConnectionsModuleTest extends PHPUnit_Framework_TestCase |
|
26
|
|
|
{ |
|
27
|
|
|
/** @var \SURFnet\VPN\Common\Http\Service */ |
|
28
|
|
|
private $service; |
|
29
|
|
|
|
|
30
|
|
|
public function setUp() |
|
|
|
|
|
|
31
|
|
|
{ |
|
32
|
|
|
$storage = new Storage( |
|
33
|
|
|
new PDO( |
|
34
|
|
|
$GLOBALS['DB_DSN'], |
|
35
|
|
|
$GLOBALS['DB_USER'], |
|
36
|
|
|
$GLOBALS['DB_PASSWD'] |
|
37
|
|
|
), |
|
38
|
|
|
new DateTime() |
|
39
|
|
|
); |
|
40
|
|
|
$storage->init(); |
|
41
|
|
|
$storage->addCertificate('foo', '12345678901234567890123456789012', '12345678901234567890123456789012', new DateTime('@12345678'), new DateTime('@23456789')); |
|
42
|
|
|
$storage->setTotpSecret('foo', 'CN2XAL23SIFTDFXZ'); |
|
43
|
|
|
$storage->clientConnect('internet', '12345678901234567890123456789012', '10.10.10.10', 'fd00:4242:4242:4242::', new DateTime('@12345678')); |
|
44
|
|
|
|
|
45
|
|
|
$config = Config::fromFile(sprintf('%s/data/config.php', __DIR__)); |
|
46
|
|
|
|
|
47
|
|
|
$groupProviders = [ |
|
48
|
|
|
new StaticProvider( |
|
49
|
|
|
$config->getSection('groupProviders')->getSection('StaticProvider') |
|
50
|
|
|
), |
|
51
|
|
|
]; |
|
52
|
|
|
|
|
53
|
|
|
$this->service = new Service(); |
|
54
|
|
|
$this->service->addModule( |
|
55
|
|
|
new ConnectionsModule( |
|
56
|
|
|
$config, |
|
57
|
|
|
$storage, |
|
58
|
|
|
$groupProviders |
|
59
|
|
|
) |
|
60
|
|
|
); |
|
61
|
|
|
|
|
62
|
|
|
$bearerAuthentication = new BasicAuthenticationHook( |
|
63
|
|
|
[ |
|
64
|
|
|
'vpn-server-node' => 'aabbcc', |
|
65
|
|
|
] |
|
66
|
|
|
); |
|
67
|
|
|
|
|
68
|
|
|
$this->service->addBeforeHook('auth', $bearerAuthentication); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
View Code Duplication |
public function testConnect() |
|
|
|
|
|
|
72
|
|
|
{ |
|
73
|
|
|
$this->assertTrue( |
|
74
|
|
|
$this->makeRequest( |
|
75
|
|
|
['vpn-server-node', 'aabbcc'], |
|
76
|
|
|
'POST', |
|
77
|
|
|
'connect', |
|
78
|
|
|
[], |
|
79
|
|
|
[ |
|
80
|
|
|
'profile_id' => 'internet', |
|
81
|
|
|
'common_name' => '12345678901234567890123456789012', |
|
82
|
|
|
'ip4' => '10.10.10.10', |
|
83
|
|
|
'ip6' => 'fd00:4242:4242:4242::', |
|
84
|
|
|
'connected_at' => 12345678, |
|
85
|
|
|
] |
|
86
|
|
|
) |
|
87
|
|
|
); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
View Code Duplication |
public function testConnectInAcl() |
|
|
|
|
|
|
91
|
|
|
{ |
|
92
|
|
|
$this->assertTrue( |
|
93
|
|
|
$this->makeRequest( |
|
94
|
|
|
['vpn-server-node', 'aabbcc'], |
|
95
|
|
|
'POST', |
|
96
|
|
|
'connect', |
|
97
|
|
|
[], |
|
98
|
|
|
[ |
|
99
|
|
|
'profile_id' => 'acl', |
|
100
|
|
|
'common_name' => '12345678901234567890123456789012', |
|
101
|
|
|
'ip4' => '10.10.10.10', |
|
102
|
|
|
'ip6' => 'fd00:4242:4242:4242::', |
|
103
|
|
|
'connected_at' => 12345678, |
|
104
|
|
|
] |
|
105
|
|
|
) |
|
106
|
|
|
); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
View Code Duplication |
public function testConnectNotInAcl() |
|
|
|
|
|
|
110
|
|
|
{ |
|
111
|
|
|
$this->assertSame( |
|
112
|
|
|
[ |
|
113
|
|
|
'ok' => false, |
|
114
|
|
|
'error' => '[VPN] unable to connect, account not a member of required group', |
|
115
|
|
|
], |
|
116
|
|
|
$this->makeRequest( |
|
117
|
|
|
['vpn-server-node', 'aabbcc'], |
|
118
|
|
|
'POST', |
|
119
|
|
|
'connect', |
|
120
|
|
|
[], |
|
121
|
|
|
[ |
|
122
|
|
|
'profile_id' => 'acl2', |
|
123
|
|
|
'common_name' => '12345678901234567890123456789012', |
|
124
|
|
|
'ip4' => '10.10.10.10', |
|
125
|
|
|
'ip6' => 'fd00:4242:4242:4242::', |
|
126
|
|
|
'connected_at' => 12345678, |
|
127
|
|
|
] |
|
128
|
|
|
) |
|
129
|
|
|
); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
View Code Duplication |
public function testDisconnect() |
|
|
|
|
|
|
133
|
|
|
{ |
|
134
|
|
|
$this->assertTrue( |
|
135
|
|
|
$this->makeRequest( |
|
136
|
|
|
['vpn-server-node', 'aabbcc'], |
|
137
|
|
|
'POST', |
|
138
|
|
|
'disconnect', |
|
139
|
|
|
[], |
|
140
|
|
|
[ |
|
141
|
|
|
'profile_id' => 'internet', |
|
142
|
|
|
'common_name' => '12345678901234567890123456789012', |
|
143
|
|
|
'ip4' => '10.10.10.10', |
|
144
|
|
|
'ip6' => 'fd00:4242:4242:4242::', |
|
145
|
|
|
'connected_at' => 12345678, |
|
146
|
|
|
'disconnected_at' => 23456789, |
|
147
|
|
|
'bytes_transferred' => 2222222, |
|
148
|
|
|
] |
|
149
|
|
|
) |
|
150
|
|
|
); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
View Code Duplication |
public function testVerifyOtp() |
|
|
|
|
|
|
154
|
|
|
{ |
|
155
|
|
|
$otp = new Otp(); |
|
156
|
|
|
$totpSecret = 'CN2XAL23SIFTDFXZ'; |
|
157
|
|
|
$totpKey = $otp->totp(Encoding::base32DecodeUpper($totpSecret)); |
|
158
|
|
|
|
|
159
|
|
|
$this->assertTrue( |
|
160
|
|
|
$this->makeRequest( |
|
161
|
|
|
['vpn-server-node', 'aabbcc'], |
|
162
|
|
|
'POST', |
|
163
|
|
|
'verify_two_factor', |
|
164
|
|
|
[], |
|
165
|
|
|
[ |
|
166
|
|
|
'common_name' => '12345678901234567890123456789012', |
|
167
|
|
|
'two_factor_type' => 'totp', |
|
168
|
|
|
'two_factor_value' => $totpKey, |
|
169
|
|
|
] |
|
170
|
|
|
) |
|
171
|
|
|
); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
View Code Duplication |
private function makeRequest(array $basicAuth, $requestMethod, $pathInfo, array $getData = [], array $postData = []) |
|
|
|
|
|
|
175
|
|
|
{ |
|
176
|
|
|
$response = $this->service->run( |
|
177
|
|
|
new Request( |
|
178
|
|
|
[ |
|
179
|
|
|
'SERVER_PORT' => 80, |
|
180
|
|
|
'SERVER_NAME' => 'vpn.example', |
|
181
|
|
|
'REQUEST_METHOD' => $requestMethod, |
|
182
|
|
|
'SCRIPT_NAME' => '/index.php', |
|
183
|
|
|
'REQUEST_URI' => sprintf('/%s', $pathInfo), |
|
184
|
|
|
'PHP_AUTH_USER' => $basicAuth[0], |
|
185
|
|
|
'PHP_AUTH_PW' => $basicAuth[1], |
|
186
|
|
|
], |
|
187
|
|
|
$getData, |
|
188
|
|
|
$postData |
|
189
|
|
|
) |
|
190
|
|
|
); |
|
191
|
|
|
|
|
192
|
|
|
$responseArray = json_decode($response->getBody(), true)[$pathInfo]; |
|
193
|
|
|
if ($responseArray['ok']) { |
|
194
|
|
|
if (array_key_exists('data', $responseArray)) { |
|
195
|
|
|
return $responseArray['data']; |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
return true; |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
// in case of errors... |
|
202
|
|
|
return $responseArray; |
|
203
|
|
|
} |
|
204
|
|
|
} |
|
205
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: