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 ( 2536bf...961881 )
by François
01:49
created

UsersModuleTest::testVerifyOtpKeyWrong()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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