|
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
|
|
|
require_once sprintf('%s/Test/TestCa.php', dirname(__DIR__)); |
|
22
|
|
|
|
|
23
|
|
|
use PDO; |
|
24
|
|
|
use PHPUnit_Framework_TestCase; |
|
25
|
|
|
use SURFnet\VPN\Common\Http\BasicAuthenticationHook; |
|
26
|
|
|
use SURFnet\VPN\Common\Http\Request; |
|
27
|
|
|
use SURFnet\VPN\Common\Http\Service; |
|
28
|
|
|
use SURFnet\VPN\Server\Storage; |
|
29
|
|
|
use SURFnet\VPN\Server\Test\TestCa; |
|
30
|
|
|
use SURFnet\VPN\Server\TlsAuth; |
|
31
|
|
|
|
|
32
|
|
|
class CertificatesModuleTest extends PHPUnit_Framework_TestCase |
|
33
|
|
|
{ |
|
34
|
|
|
/** @var \SURFnet\VPN\Common\Http\Service */ |
|
35
|
|
|
private $service; |
|
36
|
|
|
|
|
37
|
|
|
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
|
|
|
|
|
48
|
|
|
$this->service = new Service(); |
|
49
|
|
|
$this->service->addModule( |
|
50
|
|
|
new CertificatesModule( |
|
51
|
|
|
new TestCa(), |
|
52
|
|
|
$storage, |
|
53
|
|
|
new TlsAuth(sprintf('%s/data', dirname(__DIR__))), |
|
54
|
|
|
$random |
|
55
|
|
|
) |
|
56
|
|
|
); |
|
57
|
|
|
|
|
58
|
|
|
$bearerAuthentication = new BasicAuthenticationHook( |
|
59
|
|
|
[ |
|
60
|
|
|
'vpn-user-portal' => 'abcdef', |
|
61
|
|
|
'vpn-admin-portal' => 'ffeedd', |
|
62
|
|
|
'vpn-server-node' => 'aabbcc', |
|
63
|
|
|
] |
|
64
|
|
|
); |
|
65
|
|
|
|
|
66
|
|
|
$this->service->addBeforeHook('auth', $bearerAuthentication); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
View Code Duplication |
public function testGenerateCert() |
|
|
|
|
|
|
70
|
|
|
{ |
|
71
|
|
|
$this->assertSame( |
|
72
|
|
|
[ |
|
73
|
|
|
'data' => [ |
|
74
|
|
|
'add_client_certificate' => [ |
|
75
|
|
|
'certificate' => 'ClientCert for random_1', |
|
76
|
|
|
'private_key' => 'ClientKey for random_1', |
|
77
|
|
|
'valid_from' => 1234567890, |
|
78
|
|
|
'valid_to' => 2345678901, |
|
79
|
|
|
'ta' => 'Test_Ta_Key', |
|
80
|
|
|
'ca' => 'Ca', |
|
81
|
|
|
], |
|
82
|
|
|
], |
|
83
|
|
|
], |
|
84
|
|
|
$this->makeRequest( |
|
85
|
|
|
['vpn-user-portal', 'abcdef'], |
|
86
|
|
|
'POST', |
|
87
|
|
|
'/add_client_certificate', |
|
88
|
|
|
[], |
|
89
|
|
|
['user_id' => 'foo', 'display_name' => 'bar'] |
|
90
|
|
|
) |
|
91
|
|
|
); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
View Code Duplication |
public function testGenerateServerCert() |
|
|
|
|
|
|
95
|
|
|
{ |
|
96
|
|
|
$this->assertSame( |
|
97
|
|
|
[ |
|
98
|
|
|
'data' => [ |
|
99
|
|
|
'add_server_certificate' => [ |
|
100
|
|
|
'certificate' => 'ServerCert for vpn.example', |
|
101
|
|
|
'private_key' => 'ServerCert for vpn.example', |
|
102
|
|
|
'valid_from' => 1234567890, |
|
103
|
|
|
'valid_to' => 2345678901, |
|
104
|
|
|
'ta' => 'Test_Ta_Key', |
|
105
|
|
|
'ca' => 'Ca', |
|
106
|
|
|
], |
|
107
|
|
|
], |
|
108
|
|
|
], |
|
109
|
|
|
$this->makeRequest( |
|
110
|
|
|
['vpn-server-node', 'aabbcc'], |
|
111
|
|
|
'POST', |
|
112
|
|
|
'/add_server_certificate', |
|
113
|
|
|
[], |
|
114
|
|
|
['common_name' => 'vpn.example'] |
|
115
|
|
|
) |
|
116
|
|
|
); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
View Code Duplication |
private function makeRequest(array $basicAuth, $requestMethod, $pathInfo, array $getData = [], array $postData = []) |
|
|
|
|
|
|
120
|
|
|
{ |
|
121
|
|
|
$response = $this->service->run( |
|
122
|
|
|
new Request( |
|
123
|
|
|
[ |
|
124
|
|
|
'SERVER_PORT' => 80, |
|
125
|
|
|
'SERVER_NAME' => 'vpn.example', |
|
126
|
|
|
'REQUEST_METHOD' => $requestMethod, |
|
127
|
|
|
'PATH_INFO' => $pathInfo, |
|
128
|
|
|
'REQUEST_URI' => $pathInfo, |
|
129
|
|
|
'PHP_AUTH_USER' => $basicAuth[0], |
|
130
|
|
|
'PHP_AUTH_PW' => $basicAuth[1], |
|
131
|
|
|
], |
|
132
|
|
|
$getData, |
|
133
|
|
|
$postData |
|
134
|
|
|
) |
|
135
|
|
|
); |
|
136
|
|
|
|
|
137
|
|
|
return json_decode($response->getBody(), true); |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.