eduVPN /
vpn-server-api
This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 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 fkooman\OAuth\Client\AccessToken; |
||
| 14 | use Otp\Otp; |
||
| 15 | use ParagonIE\ConstantTime\Encoding; |
||
| 16 | use PDO; |
||
| 17 | use PHPUnit_Framework_TestCase; |
||
| 18 | use SURFnet\VPN\Common\Config; |
||
| 19 | use SURFnet\VPN\Common\Http\BasicAuthenticationHook; |
||
| 20 | use SURFnet\VPN\Common\Http\Request; |
||
| 21 | use SURFnet\VPN\Common\Http\Service; |
||
| 22 | use SURFnet\VPN\Server\Acl\Provider\StaticProvider; |
||
| 23 | use SURFnet\VPN\Server\Api\UsersModule; |
||
| 24 | use SURFnet\VPN\Server\Storage; |
||
| 25 | |||
| 26 | class UsersModuleTest extends PHPUnit_Framework_TestCase |
||
| 27 | { |
||
| 28 | /** @var \SURFnet\VPN\Common\Http\Service */ |
||
| 29 | private $service; |
||
| 30 | |||
| 31 | public function setUp() |
||
|
0 ignored issues
–
show
|
|||
| 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 | $storage->addCertificate('foo', 'abcd1234', 'ABCD1234', new DateTime('@12345678'), new DateTime('@23456789')); |
||
| 43 | $storage->disableUser('bar'); |
||
| 44 | $storage->setTotpSecret('bar', 'CN2XAL23SIFTDFXZ'); |
||
| 45 | |||
| 46 | // $vootToken = new AccessToken('12345', 'bearer', 'groups', null, new DateTime('2016-01-01')); |
||
|
0 ignored issues
–
show
Unused Code
Comprehensibility
introduced
by
59% of this comment could be valid code. Did you maybe forget this after debugging?
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it. The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production. This check looks for comments that seem to be mostly valid code and reports them. Loading history...
|
|||
| 47 | $vootToken = AccessToken::fromJson( |
||
| 48 | json_encode([ |
||
| 49 | 'provider_id' => 'foo|bar', |
||
| 50 | 'user_id' => 'foo', |
||
| 51 | 'access_token' => '12345', |
||
| 52 | 'token_type' => 'bearer', |
||
| 53 | 'scope' => 'groups', |
||
| 54 | 'refresh_token' => null, |
||
| 55 | 'expires_in' => 3600, |
||
| 56 | 'issued_at' => '2016-01-01 00:00:00', |
||
| 57 | ]) |
||
| 58 | ); |
||
| 59 | |||
| 60 | $storage->setVootToken('bar', $vootToken); |
||
| 61 | |||
| 62 | // user "baz" has a secret, and already used a key for replay testing |
||
| 63 | $storage->setTotpSecret('baz', 'SWIXJ4V7VYALWH6E'); |
||
| 64 | $otp = new Otp(); |
||
| 65 | $storage->recordTotpKey('baz', $otp->totp(Encoding::base32DecodeUpper('SWIXJ4V7VYALWH6E'))); |
||
| 66 | |||
| 67 | $config = Config::fromFile(sprintf('%s/data/user_groups_config.php', __DIR__)); |
||
| 68 | $groupProviders = [ |
||
| 69 | new StaticProvider( |
||
| 70 | $config->getSection('groupProviders')->getSection('StaticProvider') |
||
| 71 | ), |
||
| 72 | ]; |
||
| 73 | |||
| 74 | $this->service = new Service(); |
||
| 75 | $this->service->addModule( |
||
| 76 | new UsersModule( |
||
| 77 | $config, |
||
| 78 | $storage, |
||
| 79 | $groupProviders |
||
| 80 | ) |
||
| 81 | ); |
||
| 82 | |||
| 83 | $bearerAuthentication = new BasicAuthenticationHook( |
||
| 84 | [ |
||
| 85 | 'vpn-user-portal' => 'aabbcc', |
||
| 86 | 'vpn-admin-portal' => 'bbccdd', |
||
| 87 | ] |
||
| 88 | ); |
||
| 89 | |||
| 90 | $this->service->addBeforeHook('auth', $bearerAuthentication); |
||
| 91 | } |
||
| 92 | |||
| 93 | public function testListUsers() |
||
| 94 | { |
||
| 95 | $this->assertSame( |
||
| 96 | [ |
||
| 97 | [ |
||
| 98 | 'user_id' => 'foo', |
||
| 99 | 'is_disabled' => false, |
||
| 100 | 'has_yubi_key_id' => false, |
||
| 101 | 'has_totp_secret' => false, |
||
| 102 | ], |
||
| 103 | [ |
||
| 104 | 'user_id' => 'bar', |
||
| 105 | 'is_disabled' => true, |
||
| 106 | 'has_yubi_key_id' => false, |
||
| 107 | 'has_totp_secret' => true, |
||
| 108 | ], |
||
| 109 | [ |
||
| 110 | 'user_id' => 'baz', |
||
| 111 | 'is_disabled' => false, |
||
| 112 | 'has_yubi_key_id' => false, |
||
| 113 | 'has_totp_secret' => true, |
||
| 114 | ], |
||
| 115 | ], |
||
| 116 | $this->makeRequest( |
||
| 117 | ['vpn-admin-portal', 'bbccdd'], |
||
| 118 | 'GET', |
||
| 119 | 'user_list', |
||
| 120 | ['profile_id' => 'internet'], |
||
| 121 | [] |
||
| 122 | ) |
||
| 123 | ); |
||
| 124 | } |
||
| 125 | |||
| 126 | View Code Duplication | public function testSetTotpSecret() |
|
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
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. Loading history...
|
|||
| 127 | { |
||
| 128 | $otp = new Otp(); |
||
| 129 | $totpSecret = 'MM7TTLHPA7WZOJFB'; |
||
| 130 | $totpKey = $otp->totp(Encoding::base32DecodeUpper($totpSecret)); |
||
| 131 | |||
| 132 | $this->assertTrue( |
||
| 133 | $this->makeRequest( |
||
| 134 | ['vpn-user-portal', 'aabbcc'], |
||
| 135 | 'POST', |
||
| 136 | 'set_totp_secret', |
||
| 137 | [], |
||
| 138 | [ |
||
| 139 | 'user_id' => 'foo', |
||
| 140 | 'totp_secret' => $totpSecret, |
||
| 141 | 'totp_key' => $totpKey, |
||
| 142 | ] |
||
| 143 | ) |
||
| 144 | ); |
||
| 145 | } |
||
| 146 | |||
| 147 | View Code Duplication | public function testVerifyOtpKey() |
|
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
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. Loading history...
|
|||
| 148 | { |
||
| 149 | $otp = new Otp(); |
||
| 150 | $totpSecret = 'CN2XAL23SIFTDFXZ'; |
||
| 151 | $totpKey = $otp->totp(Encoding::base32DecodeUpper($totpSecret)); |
||
| 152 | |||
| 153 | $this->assertTrue( |
||
| 154 | $this->makeRequest( |
||
| 155 | ['vpn-user-portal', 'aabbcc'], |
||
| 156 | 'POST', |
||
| 157 | 'verify_totp_key', |
||
| 158 | [], |
||
| 159 | [ |
||
| 160 | 'user_id' => 'bar', |
||
| 161 | 'totp_key' => $totpKey, |
||
| 162 | ] |
||
| 163 | ) |
||
| 164 | ); |
||
| 165 | } |
||
| 166 | |||
| 167 | public function testVerifyOtpKeyWrong() |
||
| 168 | { |
||
| 169 | // in theory this totp_key, 123456 could be correct at one point in |
||
| 170 | // time... then this test will fail! |
||
| 171 | $this->assertSame( |
||
| 172 | [ |
||
| 173 | 'ok' => false, |
||
| 174 | 'error' => 'TOTP validation failed: invalid TOTP key', |
||
| 175 | ], |
||
| 176 | $this->makeRequest( |
||
| 177 | ['vpn-user-portal', 'aabbcc'], |
||
| 178 | 'POST', |
||
| 179 | 'verify_totp_key', |
||
| 180 | [], |
||
| 181 | [ |
||
| 182 | 'user_id' => 'bar', |
||
| 183 | 'totp_key' => '123456', |
||
| 184 | ] |
||
| 185 | ) |
||
| 186 | ); |
||
| 187 | } |
||
| 188 | |||
| 189 | public function testVerifyOtpKeyReplay() |
||
| 190 | { |
||
| 191 | $otp = new Otp(); |
||
| 192 | $totpKey = $otp->totp(Encoding::base32DecodeUpper('SWIXJ4V7VYALWH6E')); |
||
| 193 | |||
| 194 | $this->assertSame( |
||
| 195 | [ |
||
| 196 | 'ok' => false, |
||
| 197 | 'error' => 'TOTP validation failed: TOTP key replay', |
||
| 198 | ], |
||
| 199 | $this->makeRequest( |
||
| 200 | ['vpn-user-portal', 'aabbcc'], |
||
| 201 | 'POST', |
||
| 202 | 'verify_totp_key', |
||
| 203 | [], |
||
| 204 | [ |
||
| 205 | 'user_id' => 'baz', |
||
| 206 | 'totp_key' => $totpKey, |
||
| 207 | ] |
||
| 208 | ) |
||
| 209 | ); |
||
| 210 | } |
||
| 211 | |||
| 212 | public function testHasTotpSecret() |
||
| 213 | { |
||
| 214 | $this->assertTrue( |
||
| 215 | $this->makeRequest( |
||
| 216 | ['vpn-user-portal', 'aabbcc'], |
||
| 217 | 'GET', |
||
| 218 | 'has_totp_secret', |
||
| 219 | [ |
||
| 220 | 'user_id' => 'bar', |
||
| 221 | ], |
||
| 222 | [] |
||
| 223 | ) |
||
| 224 | ); |
||
| 225 | } |
||
| 226 | |||
| 227 | View Code Duplication | public function testDeleteTotpSecret() |
|
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
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. Loading history...
|
|||
| 228 | { |
||
| 229 | $this->assertTrue( |
||
| 230 | $this->makeRequest( |
||
| 231 | ['vpn-admin-portal', 'bbccdd'], |
||
| 232 | 'POST', |
||
| 233 | 'delete_totp_secret', |
||
| 234 | [], |
||
| 235 | [ |
||
| 236 | 'user_id' => 'bar', |
||
| 237 | ] |
||
| 238 | ) |
||
| 239 | ); |
||
| 240 | } |
||
| 241 | |||
| 242 | public function testSetVootToken() |
||
| 243 | { |
||
| 244 | // $vootToken = new AccessToken('AT', 'bearer', 'groups', 'RT', new DateTime('2016-01-02')); |
||
|
0 ignored issues
–
show
Unused Code
Comprehensibility
introduced
by
59% of this comment could be valid code. Did you maybe forget this after debugging?
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it. The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production. This check looks for comments that seem to be mostly valid code and reports them. Loading history...
|
|||
| 245 | $vootToken = AccessToken::fromJson( |
||
| 246 | json_encode([ |
||
| 247 | 'provider_id' => 'foo|bar', |
||
| 248 | 'user_id' => 'foo', |
||
| 249 | 'access_token' => 'AT', |
||
| 250 | 'token_type' => 'bearer', |
||
| 251 | 'scope' => 'groups', |
||
| 252 | 'refresh_token' => 'RT', |
||
| 253 | 'expires_in' => 3600, |
||
| 254 | 'issued_at' => '2016-01-02 00:00:00', |
||
| 255 | ]) |
||
| 256 | ); |
||
| 257 | |||
| 258 | $this->assertTrue( |
||
| 259 | $this->makeRequest( |
||
| 260 | ['vpn-user-portal', 'aabbcc'], |
||
| 261 | 'POST', |
||
| 262 | 'set_voot_token', |
||
| 263 | [], |
||
| 264 | [ |
||
| 265 | 'user_id' => 'foo', |
||
| 266 | 'voot_token' => $vootToken->toJson(), |
||
| 267 | ] |
||
| 268 | ) |
||
| 269 | ); |
||
| 270 | } |
||
| 271 | |||
| 272 | View Code Duplication | public function testDeleteVootToken() |
|
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
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. Loading history...
|
|||
| 273 | { |
||
| 274 | $this->assertTrue( |
||
| 275 | $this->makeRequest( |
||
| 276 | ['vpn-admin-portal', 'bbccdd'], |
||
| 277 | 'POST', |
||
| 278 | 'delete_voot_token', |
||
| 279 | [], |
||
| 280 | [ |
||
| 281 | 'user_id' => 'bar', |
||
| 282 | ] |
||
| 283 | ) |
||
| 284 | ); |
||
| 285 | } |
||
| 286 | |||
| 287 | View Code Duplication | public function testDisableUser() |
|
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
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. Loading history...
|
|||
| 288 | { |
||
| 289 | $this->assertTrue( |
||
| 290 | $this->makeRequest( |
||
| 291 | ['vpn-admin-portal', 'bbccdd'], |
||
| 292 | 'POST', |
||
| 293 | 'disable_user', |
||
| 294 | [], |
||
| 295 | [ |
||
| 296 | 'user_id' => 'foo', |
||
| 297 | ] |
||
| 298 | ) |
||
| 299 | ); |
||
| 300 | } |
||
| 301 | |||
| 302 | View Code Duplication | public function testEnableUser() |
|
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
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. Loading history...
|
|||
| 303 | { |
||
| 304 | $this->assertTrue( |
||
| 305 | $this->makeRequest( |
||
| 306 | ['vpn-admin-portal', 'bbccdd'], |
||
| 307 | 'POST', |
||
| 308 | 'enable_user', |
||
| 309 | [], |
||
| 310 | [ |
||
| 311 | 'user_id' => 'bar', |
||
| 312 | ] |
||
| 313 | ) |
||
| 314 | ); |
||
| 315 | } |
||
| 316 | |||
| 317 | View Code Duplication | public function testDeleteUser() |
|
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
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. Loading history...
|
|||
| 318 | { |
||
| 319 | $this->assertTrue( |
||
| 320 | $this->makeRequest( |
||
| 321 | ['vpn-admin-portal', 'bbccdd'], |
||
| 322 | 'POST', |
||
| 323 | 'delete_user', |
||
| 324 | [], |
||
| 325 | [ |
||
| 326 | 'user_id' => 'foo', |
||
| 327 | ] |
||
| 328 | ) |
||
| 329 | ); |
||
| 330 | } |
||
| 331 | |||
| 332 | public function testUserGroups() |
||
| 333 | { |
||
| 334 | $this->assertSame( |
||
| 335 | [ |
||
| 336 | [ |
||
| 337 | 'id' => 'all', |
||
| 338 | 'displayName' => 'All', |
||
| 339 | ], |
||
| 340 | [ |
||
| 341 | 'id' => 'employees', |
||
| 342 | 'displayName' => 'Employees', |
||
| 343 | ], |
||
| 344 | ], |
||
| 345 | $this->makeRequest( |
||
| 346 | ['vpn-user-portal', 'aabbcc'], |
||
| 347 | 'GET', |
||
| 348 | 'user_groups', |
||
| 349 | [ |
||
| 350 | 'user_id' => 'bar', |
||
| 351 | ], |
||
| 352 | [] |
||
| 353 | ) |
||
| 354 | ); |
||
| 355 | } |
||
| 356 | |||
| 357 | View Code Duplication | private function makeRequest(array $basicAuth, $requestMethod, $pathInfo, array $getData = [], array $postData = []) |
|
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
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. Loading history...
|
|||
| 358 | { |
||
| 359 | $response = $this->service->run( |
||
| 360 | new Request( |
||
| 361 | [ |
||
| 362 | 'SERVER_PORT' => 80, |
||
| 363 | 'SERVER_NAME' => 'vpn.example', |
||
| 364 | 'REQUEST_METHOD' => $requestMethod, |
||
| 365 | 'SCRIPT_NAME' => '/index.php', |
||
| 366 | 'REQUEST_URI' => sprintf('/%s', $pathInfo), |
||
| 367 | 'PHP_AUTH_USER' => $basicAuth[0], |
||
| 368 | 'PHP_AUTH_PW' => $basicAuth[1], |
||
| 369 | ], |
||
| 370 | $getData, |
||
| 371 | $postData |
||
| 372 | ) |
||
| 373 | ); |
||
| 374 | |||
| 375 | $responseArray = json_decode($response->getBody(), true)[$pathInfo]; |
||
| 376 | if ($responseArray['ok']) { |
||
| 377 | if (array_key_exists('data', $responseArray)) { |
||
| 378 | return $responseArray['data']; |
||
| 379 | } |
||
| 380 | |||
| 381 | return true; |
||
| 382 | } |
||
| 383 | |||
| 384 | // in case of errors... |
||
| 385 | return $responseArray; |
||
| 386 | } |
||
| 387 | } |
||
| 388 |
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: