|
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 PDO; |
|
14
|
|
|
use PHPUnit_Framework_TestCase; |
|
15
|
|
|
use SURFnet\VPN\Common\Http\BasicAuthenticationHook; |
|
16
|
|
|
use SURFnet\VPN\Common\Http\Request; |
|
17
|
|
|
use SURFnet\VPN\Common\Http\Service; |
|
18
|
|
|
use SURFnet\VPN\Server\Api\CertificatesModule; |
|
19
|
|
|
use SURFnet\VPN\Server\Storage; |
|
20
|
|
|
use SURFnet\VPN\Server\Tests\TestCa; |
|
21
|
|
|
use SURFnet\VPN\Server\TlsAuth; |
|
22
|
|
|
|
|
23
|
|
|
class CertificatesModuleTest extends PHPUnit_Framework_TestCase |
|
24
|
|
|
{ |
|
25
|
|
|
/** @var \SURFnet\VPN\Common\Http\Service */ |
|
26
|
|
|
private $service; |
|
27
|
|
|
|
|
28
|
|
|
public function setUp() |
|
|
|
|
|
|
29
|
|
|
{ |
|
30
|
|
|
$random = $this->getMockBuilder('SURFnet\VPN\Common\RandomInterface')->getMock(); |
|
31
|
|
|
$random->method('get')->will($this->onConsecutiveCalls('random_1', 'random_2')); |
|
32
|
|
|
|
|
33
|
|
|
$storage = new Storage( |
|
34
|
|
|
new PDO( |
|
35
|
|
|
$GLOBALS['DB_DSN'], |
|
36
|
|
|
$GLOBALS['DB_USER'], |
|
37
|
|
|
$GLOBALS['DB_PASSWD'] |
|
38
|
|
|
), |
|
39
|
|
|
new DateTime() |
|
40
|
|
|
); |
|
41
|
|
|
$storage->init(); |
|
42
|
|
|
$this->service = new Service(); |
|
43
|
|
|
$this->service->addModule( |
|
44
|
|
|
new CertificatesModule( |
|
45
|
|
|
new TestCa(), |
|
46
|
|
|
$storage, |
|
47
|
|
|
new TlsAuth(sprintf('%s/data', dirname(__DIR__))), |
|
48
|
|
|
$random |
|
49
|
|
|
) |
|
50
|
|
|
); |
|
51
|
|
|
|
|
52
|
|
|
$bearerAuthentication = new BasicAuthenticationHook( |
|
53
|
|
|
[ |
|
54
|
|
|
'vpn-user-portal' => 'abcdef', |
|
55
|
|
|
'vpn-admin-portal' => 'ffeedd', |
|
56
|
|
|
'vpn-server-node' => 'aabbcc', |
|
57
|
|
|
] |
|
58
|
|
|
); |
|
59
|
|
|
|
|
60
|
|
|
$this->service->addBeforeHook('auth', $bearerAuthentication); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function testGenerateCert() |
|
64
|
|
|
{ |
|
65
|
|
|
$this->assertSame( |
|
66
|
|
|
[ |
|
67
|
|
|
'certificate' => 'ClientCert for random_1', |
|
68
|
|
|
'private_key' => 'ClientKey for random_1', |
|
69
|
|
|
'valid_from' => 1234567890, |
|
70
|
|
|
'valid_to' => 2345678901, |
|
71
|
|
|
], |
|
72
|
|
|
$this->makeRequest( |
|
73
|
|
|
['vpn-user-portal', 'abcdef'], |
|
74
|
|
|
'POST', |
|
75
|
|
|
'add_client_certificate', |
|
76
|
|
|
[], |
|
77
|
|
|
['user_id' => 'foo', 'display_name' => 'bar'] |
|
78
|
|
|
) |
|
79
|
|
|
); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public function testServerInfo() |
|
83
|
|
|
{ |
|
84
|
|
|
$this->assertSame( |
|
85
|
|
|
[ |
|
86
|
|
|
'ta' => 'Test_Ta_Key', |
|
87
|
|
|
'ca' => 'Ca', |
|
88
|
|
|
], |
|
89
|
|
|
$this->makeRequest( |
|
90
|
|
|
['vpn-user-portal', 'abcdef'], |
|
91
|
|
|
'GET', |
|
92
|
|
|
'server_info', |
|
93
|
|
|
[], |
|
94
|
|
|
[] |
|
95
|
|
|
) |
|
96
|
|
|
); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
public function testGenerateServerCert() |
|
100
|
|
|
{ |
|
101
|
|
|
$this->assertSame( |
|
102
|
|
|
[ |
|
103
|
|
|
'certificate' => 'ServerCert for vpn.example', |
|
104
|
|
|
'private_key' => 'ServerCert for vpn.example', |
|
105
|
|
|
'valid_from' => 1234567890, |
|
106
|
|
|
'valid_to' => 2345678901, |
|
107
|
|
|
'ta' => 'Test_Ta_Key', |
|
108
|
|
|
'ca' => 'Ca', |
|
109
|
|
|
], |
|
110
|
|
|
$this->makeRequest( |
|
111
|
|
|
['vpn-server-node', 'aabbcc'], |
|
112
|
|
|
'POST', |
|
113
|
|
|
'add_server_certificate', |
|
114
|
|
|
[], |
|
115
|
|
|
['common_name' => 'vpn.example'] |
|
116
|
|
|
) |
|
117
|
|
|
); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
View Code Duplication |
private function makeRequest(array $basicAuth, $requestMethod, $pathInfo, array $getData = [], array $postData = []) |
|
|
|
|
|
|
121
|
|
|
{ |
|
122
|
|
|
$response = $this->service->run( |
|
123
|
|
|
new Request( |
|
124
|
|
|
[ |
|
125
|
|
|
'SERVER_PORT' => 80, |
|
126
|
|
|
'SERVER_NAME' => 'vpn.example', |
|
127
|
|
|
'REQUEST_METHOD' => $requestMethod, |
|
128
|
|
|
'SCRIPT_NAME' => '/index.php', |
|
129
|
|
|
'REQUEST_URI' => sprintf('/%s', $pathInfo), |
|
130
|
|
|
'PHP_AUTH_USER' => $basicAuth[0], |
|
131
|
|
|
'PHP_AUTH_PW' => $basicAuth[1], |
|
132
|
|
|
], |
|
133
|
|
|
$getData, |
|
134
|
|
|
$postData |
|
135
|
|
|
) |
|
136
|
|
|
); |
|
137
|
|
|
|
|
138
|
|
|
$responseArray = json_decode($response->getBody(), true)[$pathInfo]; |
|
139
|
|
|
if ($responseArray['ok']) { |
|
140
|
|
|
if (array_key_exists('data', $responseArray)) { |
|
141
|
|
|
return $responseArray['data']; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
return true; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
// in case of errors... |
|
148
|
|
|
return $responseArray; |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
|
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: