GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 70617b...52097e )
by François
05:51
created

UsersModuleTest::testSetVootToken()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 27
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 27
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 16
nc 1
nop 0
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\Tests\Api;
20
21
use DateTime;
22
use fkooman\OAuth\Client\AccessToken;
23
use Otp\Otp;
24
use ParagonIE\ConstantTime\Encoding;
25
use PDO;
26
use PHPUnit_Framework_TestCase;
27
use SURFnet\VPN\Common\Config;
28
use SURFnet\VPN\Common\Http\BasicAuthenticationHook;
29
use SURFnet\VPN\Common\Http\Request;
30
use SURFnet\VPN\Common\Http\Service;
31
use SURFnet\VPN\Server\Acl\Provider\StaticProvider;
32
use SURFnet\VPN\Server\Api\UsersModule;
33
use SURFnet\VPN\Server\Storage;
34
35
class UsersModuleTest extends PHPUnit_Framework_TestCase
36
{
37
    /** @var \SURFnet\VPN\Common\Http\Service */
38
    private $service;
39
40
    public function setUp()
0 ignored issues
show
Coding Style introduced by
setUp uses the super-global variable $GLOBALS which is generally not recommended.

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:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
41
    {
42
        $storage = new Storage(
43
            new PDO(
44
                $GLOBALS['DB_DSN'],
45
                $GLOBALS['DB_USER'],
46
                $GLOBALS['DB_PASSWD']
47
            ),
48
            new DateTime()
49
        );
50
        $storage->init();
51
        $storage->addCertificate('foo', 'abcd1234', 'ABCD1234', new DateTime('@12345678'), new DateTime('@23456789'));
52
        $storage->disableUser('bar');
53
        $storage->setTotpSecret('bar', 'CN2XAL23SIFTDFXZ');
54
55
//        $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...
56
        $vootToken = AccessToken::fromStorage(
57
            json_encode([
58
                'access_token' => '12345',
59
                'token_type' => 'bearer',
60
                'scope' => 'groups',
61
                'refresh_token' => null,
62
                'expires_in' => 3600,
63
                'issued_at' => '2016-01-01 00:00:00',
64
            ])
65
        );
66
67
        $storage->setVootToken('bar', $vootToken);
68
69
        // user "baz" has a secret, and already used a key for replay testing
70
        $storage->setTotpSecret('baz', 'SWIXJ4V7VYALWH6E');
71
        $otp = new Otp();
72
        $storage->recordTotpKey('baz', $otp->totp(Encoding::base32DecodeUpper('SWIXJ4V7VYALWH6E')));
73
74
        $config = Config::fromFile(sprintf('%s/data/user_groups_config.php', __DIR__));
75
        $groupProviders = [
76
            new StaticProvider(
77
                $config->getSection('groupProviders')->getSection('StaticProvider')
78
            ),
79
        ];
80
81
        $this->service = new Service();
82
        $this->service->addModule(
83
            new UsersModule(
84
                $config,
85
                $storage,
86
                $groupProviders
87
            )
88
        );
89
90
        $bearerAuthentication = new BasicAuthenticationHook(
91
            [
92
                'vpn-user-portal' => 'aabbcc',
93
                'vpn-admin-portal' => 'bbccdd',
94
            ]
95
        );
96
97
        $this->service->addBeforeHook('auth', $bearerAuthentication);
98
    }
99
100
    public function testListUsers()
101
    {
102
        $this->assertSame(
103
            [
104
                [
105
                    'user_id' => 'foo',
106
                    'is_disabled' => false,
107
                    'has_yubi_key_id' => false,
108
                    'has_totp_secret' => false,
109
                ],
110
                [
111
                    'user_id' => 'bar',
112
                    'is_disabled' => true,
113
                    'has_yubi_key_id' => false,
114
                    'has_totp_secret' => true,
115
                ],
116
                [
117
                    'user_id' => 'baz',
118
                    'is_disabled' => false,
119
                    'has_yubi_key_id' => false,
120
                    'has_totp_secret' => true,
121
                ],
122
            ],
123
            $this->makeRequest(
124
                ['vpn-admin-portal', 'bbccdd'],
125
                'GET',
126
                'user_list',
127
                ['profile_id' => 'internet'],
128
                []
129
            )
130
        );
131
    }
132
133 View Code Duplication
    public function testSetTotpSecret()
0 ignored issues
show
Duplication introduced by
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...
134
    {
135
        $otp = new Otp();
136
        $totpSecret = 'MM7TTLHPA7WZOJFB';
137
        $totpKey = $otp->totp(Encoding::base32DecodeUpper($totpSecret));
138
139
        $this->assertTrue(
140
            $this->makeRequest(
141
                ['vpn-user-portal', 'aabbcc'],
142
                'POST',
143
                'set_totp_secret',
144
                [],
145
                [
146
                    'user_id' => 'foo',
147
                    'totp_secret' => $totpSecret,
148
                    'totp_key' => $totpKey,
149
                ]
150
            )
151
        );
152
    }
153
154 View Code Duplication
    public function testVerifyOtpKey()
0 ignored issues
show
Duplication introduced by
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...
155
    {
156
        $otp = new Otp();
157
        $totpSecret = 'CN2XAL23SIFTDFXZ';
158
        $totpKey = $otp->totp(Encoding::base32DecodeUpper($totpSecret));
159
160
        $this->assertTrue(
161
            $this->makeRequest(
162
                ['vpn-user-portal', 'aabbcc'],
163
                'POST',
164
                'verify_totp_key',
165
                [],
166
                [
167
                    'user_id' => 'bar',
168
                    'totp_key' => $totpKey,
169
                ]
170
            )
171
        );
172
    }
173
174
    public function testVerifyOtpKeyWrong()
175
    {
176
        // in theory this totp_key, 123456 could be correct at one point in
177
        // time... then this test will fail!
178
        $this->assertSame(
179
            [
180
                'ok' => false,
181
                'error' => 'TOTP validation failed: invalid TOTP key',
182
            ],
183
            $this->makeRequest(
184
                ['vpn-user-portal', 'aabbcc'],
185
                'POST',
186
                'verify_totp_key',
187
                [],
188
                [
189
                    'user_id' => 'bar',
190
                    'totp_key' => '123456',
191
                ]
192
            )
193
        );
194
    }
195
196
    public function testVerifyOtpKeyReplay()
197
    {
198
        $otp = new Otp();
199
        $totpKey = $otp->totp(Encoding::base32DecodeUpper('SWIXJ4V7VYALWH6E'));
200
201
        $this->assertSame(
202
            [
203
                'ok' => false,
204
                'error' => 'TOTP validation failed: TOTP key replay',
205
            ],
206
            $this->makeRequest(
207
                ['vpn-user-portal', 'aabbcc'],
208
                'POST',
209
                'verify_totp_key',
210
                [],
211
                [
212
                    'user_id' => 'baz',
213
                    'totp_key' => $totpKey,
214
                ]
215
            )
216
        );
217
    }
218
219
    public function testHasTotpSecret()
220
    {
221
        $this->assertTrue(
222
            $this->makeRequest(
223
                ['vpn-user-portal', 'aabbcc'],
224
                'GET',
225
                'has_totp_secret',
226
                [
227
                    'user_id' => 'bar',
228
                ],
229
                []
230
            )
231
        );
232
    }
233
234 View Code Duplication
    public function testDeleteTotpSecret()
0 ignored issues
show
Duplication introduced by
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...
235
    {
236
        $this->assertTrue(
237
            $this->makeRequest(
238
                ['vpn-admin-portal', 'bbccdd'],
239
                'POST',
240
                'delete_totp_secret',
241
                [],
242
                [
243
                    'user_id' => 'bar',
244
                ]
245
            )
246
        );
247
    }
248
249
    public function testSetVootToken()
250
    {
251
        //        $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...
252
        $vootToken = AccessToken::fromStorage(
253
            json_encode([
254
                'access_token' => 'AT',
255
                'token_type' => 'bearer',
256
                'scope' => 'groups',
257
                'refresh_token' => 'RT',
258
                'expires_in' => 3600,
259
                'issued_at' => '2016-01-02 00:00:00',
260
            ])
261
        );
262
263
        $this->assertTrue(
264
            $this->makeRequest(
265
                ['vpn-user-portal', 'aabbcc'],
266
                'POST',
267
                'set_voot_token',
268
                [],
269
                [
270
                    'user_id' => 'foo',
271
                    'voot_token' => $vootToken->toStorage(),
272
                ]
273
            )
274
        );
275
    }
276
277 View Code Duplication
    public function testDeleteVootToken()
0 ignored issues
show
Duplication introduced by
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...
278
    {
279
        $this->assertTrue(
280
            $this->makeRequest(
281
                ['vpn-admin-portal', 'bbccdd'],
282
                'POST',
283
                'delete_voot_token',
284
                [],
285
                [
286
                    'user_id' => 'bar',
287
                ]
288
            )
289
        );
290
    }
291
292 View Code Duplication
    public function testDisableUser()
0 ignored issues
show
Duplication introduced by
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...
293
    {
294
        $this->assertTrue(
295
            $this->makeRequest(
296
                ['vpn-admin-portal', 'bbccdd'],
297
                'POST',
298
                'disable_user',
299
                [],
300
                [
301
                    'user_id' => 'foo',
302
                ]
303
            )
304
        );
305
    }
306
307 View Code Duplication
    public function testEnableUser()
0 ignored issues
show
Duplication introduced by
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...
308
    {
309
        $this->assertTrue(
310
            $this->makeRequest(
311
                ['vpn-admin-portal', 'bbccdd'],
312
                'POST',
313
                'enable_user',
314
                [],
315
                [
316
                    'user_id' => 'bar',
317
                ]
318
            )
319
        );
320
    }
321
322 View Code Duplication
    public function testDeleteUser()
0 ignored issues
show
Duplication introduced by
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...
323
    {
324
        $this->assertTrue(
325
            $this->makeRequest(
326
                ['vpn-admin-portal', 'bbccdd'],
327
                'POST',
328
                'delete_user',
329
                [],
330
                [
331
                    'user_id' => 'foo',
332
                ]
333
            )
334
        );
335
    }
336
337
    public function testUserGroups()
338
    {
339
        $this->assertSame(
340
            [
341
                [
342
                    'id' => 'all',
343
                    'displayName' => 'All',
344
                ],
345
                [
346
                    'id' => 'employees',
347
                    'displayName' => 'Employees',
348
                ],
349
            ],
350
            $this->makeRequest(
351
                ['vpn-user-portal', 'aabbcc'],
352
                'GET',
353
                'user_groups',
354
                [
355
                    'user_id' => 'bar',
356
                ],
357
                []
358
            )
359
        );
360
    }
361
362 View Code Duplication
    private function makeRequest(array $basicAuth, $requestMethod, $pathInfo, array $getData = [], array $postData = [])
0 ignored issues
show
Duplication introduced by
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...
363
    {
364
        $response = $this->service->run(
365
            new Request(
366
                [
367
                    'SERVER_PORT' => 80,
368
                    'SERVER_NAME' => 'vpn.example',
369
                    'REQUEST_METHOD' => $requestMethod,
370
                    'SCRIPT_NAME' => '/index.php',
371
                    'REQUEST_URI' => sprintf('/%s', $pathInfo),
372
                    'PHP_AUTH_USER' => $basicAuth[0],
373
                    'PHP_AUTH_PW' => $basicAuth[1],
374
                ],
375
                $getData,
376
                $postData
377
            )
378
        );
379
380
        $responseArray = json_decode($response->getBody(), true)[$pathInfo];
381
        if ($responseArray['ok']) {
382
            if (array_key_exists('data', $responseArray)) {
383
                return $responseArray['data'];
384
            }
385
386
            return true;
387
        }
388
389
        // in case of errors...
390
        return $responseArray;
391
    }
392
}
393