|
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
|
|
|
namespace SURFnet\VPN\Server; |
|
19
|
|
|
|
|
20
|
|
|
require_once sprintf('%s/Test/TestHttpClient.php', __DIR__); |
|
21
|
|
|
|
|
22
|
|
|
use PHPUnit_Framework_TestCase; |
|
23
|
|
|
use SURFnet\VPN\Common\HttpClient\ServerClient; |
|
24
|
|
|
use Psr\Log\NullLogger; |
|
25
|
|
|
use SURFnet\VPN\Server\Test\TestHttpClient; |
|
26
|
|
|
|
|
27
|
|
|
class OtpTest extends PHPUnit_Framework_TestCase |
|
28
|
|
|
{ |
|
29
|
|
|
public function setUp() |
|
30
|
|
|
{ |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
View Code Duplication |
public function testValidOtp() |
|
|
|
|
|
|
34
|
|
|
{ |
|
35
|
|
|
$serverClient = new ServerClient(new TestHttpClient(), 'serverClient'); |
|
36
|
|
|
|
|
37
|
|
|
$otp = new Otp(new NullLogger(), $serverClient); |
|
38
|
|
|
$this->assertTrue( |
|
39
|
|
|
$otp->verify( |
|
40
|
|
|
[ |
|
41
|
|
|
'username' => 'totp', |
|
42
|
|
|
'common_name' => 'foo_bar', |
|
43
|
|
|
'password' => '123456', |
|
44
|
|
|
] |
|
45
|
|
|
) |
|
46
|
|
|
); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
View Code Duplication |
public function testNoOtpSecret() |
|
|
|
|
|
|
50
|
|
|
{ |
|
51
|
|
|
$serverClient = new ServerClient(new TestHttpClient(), 'serverClient'); |
|
52
|
|
|
|
|
53
|
|
|
$otp = new Otp(new NullLogger(), $serverClient); |
|
54
|
|
|
$this->assertFalse( |
|
55
|
|
|
$otp->verify( |
|
56
|
|
|
[ |
|
57
|
|
|
'username' => 'totp', |
|
58
|
|
|
'common_name' => 'bar_foo', |
|
59
|
|
|
'password' => '123456', |
|
60
|
|
|
] |
|
61
|
|
|
) |
|
62
|
|
|
); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
View Code Duplication |
public function testNoInvalidOtpKey() |
|
|
|
|
|
|
66
|
|
|
{ |
|
67
|
|
|
$serverClient = new ServerClient(new TestHttpClient(), 'serverClient'); |
|
68
|
|
|
|
|
69
|
|
|
$otp = new Otp(new NullLogger(), $serverClient); |
|
70
|
|
|
$this->assertFalse( |
|
71
|
|
|
$otp->verify( |
|
72
|
|
|
[ |
|
73
|
|
|
'username' => 'totp', |
|
74
|
|
|
'common_name' => 'foo_bar', |
|
75
|
|
|
'password' => '654321', |
|
76
|
|
|
] |
|
77
|
|
|
) |
|
78
|
|
|
); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
View Code Duplication |
public function testInvalidOtpPattern() |
|
|
|
|
|
|
82
|
|
|
{ |
|
83
|
|
|
$serverClient = new ServerClient(new TestHttpClient(), 'serverClient'); |
|
84
|
|
|
|
|
85
|
|
|
$otp = new Otp(new NullLogger(), $serverClient); |
|
86
|
|
|
$this->assertFalse( |
|
87
|
|
|
$otp->verify( |
|
88
|
|
|
[ |
|
89
|
|
|
'username' => 'totp', |
|
90
|
|
|
'common_name' => 'foo_bar', |
|
91
|
|
|
'password' => '123', |
|
92
|
|
|
] |
|
93
|
|
|
) |
|
94
|
|
|
); |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
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.