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 ( da2aa4...ac2593 )
by François
02:06
created

UsersModuleTest::testHasTotpSecret()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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