|
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
|
|
|
use PHPUnit_Framework_TestCase; |
|
21
|
|
|
use Otp\Otp; |
|
22
|
|
|
use Base32\Base32; |
|
23
|
|
|
use PDO; |
|
24
|
|
|
|
|
25
|
|
|
class TwoFactorTest extends PHPUnit_Framework_TestCase |
|
26
|
|
|
{ |
|
27
|
|
|
/** @var OtpLog */ |
|
28
|
|
|
private $otpLog; |
|
29
|
|
|
|
|
30
|
|
|
public function setUp() |
|
31
|
|
|
{ |
|
32
|
|
|
$db = new PDO('sqlite::memory:'); |
|
33
|
|
|
$this->otpLog = new OtpLog($db); |
|
34
|
|
|
$this->otpLog->init(); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function testTwoFactorValid() |
|
38
|
|
|
{ |
|
39
|
|
|
$o = new Otp(); |
|
40
|
|
|
$otpKey = $o->totp(Base32::decode('QPXDFE7G7VNRR4BH')); |
|
41
|
|
|
|
|
42
|
|
|
$c = new TwoFactor(__DIR__, $this->otpLog); |
|
43
|
|
|
$c->twoFactor( |
|
44
|
|
|
[ |
|
45
|
|
|
'INSTANCE_ID' => 'vpn.example', |
|
46
|
|
|
'POOL_ID' => 'internet', |
|
47
|
|
|
'common_name' => 'foo_xyz', |
|
48
|
|
|
'username' => 'totp', |
|
49
|
|
|
'password' => $otpKey, |
|
50
|
|
|
] |
|
51
|
|
|
); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @expectedException \SURFnet\VPN\Server\Exception\TwoFactorException |
|
56
|
|
|
* @expectedExceptionMessage invalid OTP key |
|
57
|
|
|
*/ |
|
58
|
|
View Code Duplication |
public function testTwoFactorWrongKey() |
|
|
|
|
|
|
59
|
|
|
{ |
|
60
|
|
|
$c = new TwoFactor(__DIR__, $this->otpLog); |
|
61
|
|
|
$c->twoFactor( |
|
62
|
|
|
[ |
|
63
|
|
|
'INSTANCE_ID' => 'vpn.example', |
|
64
|
|
|
'POOL_ID' => 'internet', |
|
65
|
|
|
'common_name' => 'foo_xyz', |
|
66
|
|
|
'username' => 'totp', |
|
67
|
|
|
'password' => '999999', |
|
68
|
|
|
] |
|
69
|
|
|
); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @expectedException \SURFnet\VPN\Server\Exception\TwoFactorException |
|
74
|
|
|
* @expectedExceptionMessage OTP replayed |
|
75
|
|
|
*/ |
|
76
|
|
|
public function testTwoFactorReplay() |
|
77
|
|
|
{ |
|
78
|
|
|
$o = new Otp(); |
|
79
|
|
|
$otpKey = $o->totp(Base32::decode('QPXDFE7G7VNRR4BH')); |
|
80
|
|
|
|
|
81
|
|
|
$c = new TwoFactor(__DIR__, $this->otpLog); |
|
82
|
|
|
$c->twoFactor( |
|
83
|
|
|
[ |
|
84
|
|
|
'INSTANCE_ID' => 'vpn.example', |
|
85
|
|
|
'POOL_ID' => 'internet', |
|
86
|
|
|
'common_name' => 'foo_xyz', |
|
87
|
|
|
'username' => 'totp', |
|
88
|
|
|
'password' => $otpKey, |
|
89
|
|
|
] |
|
90
|
|
|
); |
|
91
|
|
|
// replay |
|
92
|
|
|
$c->twoFactor( |
|
93
|
|
|
[ |
|
94
|
|
|
'INSTANCE_ID' => 'vpn.example', |
|
95
|
|
|
'POOL_ID' => 'internet', |
|
96
|
|
|
'common_name' => 'foo_xyz', |
|
97
|
|
|
'username' => 'totp', |
|
98
|
|
|
'password' => $otpKey, |
|
99
|
|
|
] |
|
100
|
|
|
); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @expectedException \SURFnet\VPN\Server\Exception\TwoFactorException |
|
105
|
|
|
* @expectedExceptionMessage no OTP secret registered |
|
106
|
|
|
*/ |
|
107
|
|
View Code Duplication |
public function testTwoFactorNotEnrolled() |
|
|
|
|
|
|
108
|
|
|
{ |
|
109
|
|
|
$c = new TwoFactor(__DIR__, $this->otpLog); |
|
110
|
|
|
$c->twoFactor( |
|
111
|
|
|
[ |
|
112
|
|
|
'INSTANCE_ID' => 'vpn.example', |
|
113
|
|
|
'POOL_ID' => 'internet', |
|
114
|
|
|
'common_name' => 'bar_xyz', |
|
115
|
|
|
'username' => 'totp', |
|
116
|
|
|
'password' => '999999', |
|
117
|
|
|
] |
|
118
|
|
|
); |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|
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.