|
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; |
|
20
|
|
|
|
|
21
|
|
|
use DateTime; |
|
22
|
|
|
use PDO; |
|
23
|
|
|
use PHPUnit_Framework_TestCase; |
|
24
|
|
|
use SURFnet\VPN\Server\Exception\TotpException; |
|
25
|
|
|
use SURFnet\VPN\Server\Storage; |
|
26
|
|
|
use SURFnet\VPN\Server\Totp; |
|
27
|
|
|
|
|
28
|
|
|
class TotpTest extends PHPUnit_Framework_TestCase |
|
29
|
|
|
{ |
|
30
|
|
|
/** @var Totp */ |
|
31
|
|
|
private $totp; |
|
32
|
|
|
|
|
33
|
|
|
public function setUp() |
|
|
|
|
|
|
34
|
|
|
{ |
|
35
|
|
|
$storage = new Storage( |
|
36
|
|
|
new PDO( |
|
37
|
|
|
$GLOBALS['DB_DSN'], |
|
38
|
|
|
$GLOBALS['DB_USER'], |
|
39
|
|
|
$GLOBALS['DB_PASSWD'] |
|
40
|
|
|
), |
|
41
|
|
|
new DateTime() |
|
42
|
|
|
); |
|
43
|
|
|
$storage->init(); |
|
44
|
|
|
$storage->setTotpSecret('foo', 'CN2XAL23SIFTDFXZ'); |
|
45
|
|
|
|
|
46
|
|
|
$this->totp = new Totp($storage); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @expectedException \SURFnet\VPN\Server\Exception\TotpException |
|
51
|
|
|
* @expectedExceptionMessage too many attempts at TOTP |
|
52
|
|
|
*/ |
|
53
|
|
|
public function testTooManyReplays() |
|
54
|
|
|
{ |
|
55
|
|
|
for ($i = 0; $i < 10; ++$i) { |
|
56
|
|
|
try { |
|
57
|
|
|
$this->totp->verify('foo', (string) 123456 + $i); |
|
58
|
|
|
} catch (TotpException $e) { |
|
59
|
|
|
$this->assertSame('invalid TOTP key', $e->getMessage()); |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
$this->totp->verify('foo', '555555'); |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|
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: