|
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 PDO; |
|
21
|
|
|
use PDOException; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Keep track of the used OTP keys so they cannot be replayed. |
|
25
|
|
|
*/ |
|
26
|
|
|
class OtpLog |
|
27
|
|
|
{ |
|
28
|
|
|
/** @var PDO */ |
|
29
|
|
|
private $db; |
|
30
|
|
|
|
|
31
|
|
|
public function __construct(PDO $db) |
|
32
|
|
|
{ |
|
33
|
|
|
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
|
34
|
|
|
$this->db = $db; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function record($userId, $otpKey, $timeUnix) |
|
38
|
|
|
{ |
|
39
|
|
|
$stmt = $this->db->prepare( |
|
40
|
|
|
'INSERT INTO otp_log ( |
|
41
|
|
|
user_id, |
|
42
|
|
|
otp_key, |
|
43
|
|
|
time_unix |
|
44
|
|
|
) |
|
45
|
|
|
VALUES( |
|
46
|
|
|
:user_id, |
|
47
|
|
|
:otp_key, |
|
48
|
|
|
:time_unix |
|
49
|
|
|
)' |
|
50
|
|
|
); |
|
51
|
|
|
|
|
52
|
|
|
$stmt->bindValue(':user_id', $userId, PDO::PARAM_STR); |
|
53
|
|
|
$stmt->bindValue(':otp_key', $otpKey, PDO::PARAM_STR); |
|
54
|
|
|
$stmt->bindValue(':time_unix', $timeUnix, PDO::PARAM_INT); |
|
55
|
|
|
try { |
|
56
|
|
|
$stmt->execute(); |
|
57
|
|
|
} catch (PDOException $e) { |
|
58
|
|
|
return false; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
return true; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public function housekeeping($timeUnix) |
|
65
|
|
|
{ |
|
66
|
|
|
$stmt = $this->db->prepare( |
|
67
|
|
|
sprintf( |
|
68
|
|
|
'DELETE FROM otp_log |
|
69
|
|
|
WHERE time_unix < :time_unix' |
|
70
|
|
|
) |
|
71
|
|
|
); |
|
72
|
|
|
|
|
73
|
|
|
$stmt->bindValue(':time_unix', $timeUnix, PDO::PARAM_INT); |
|
74
|
|
|
$stmt->execute(); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function init() |
|
78
|
|
|
{ |
|
79
|
|
|
$queryList = [ |
|
80
|
|
|
'CREATE TABLE IF NOT EXISTS otp_log ( |
|
81
|
|
|
user_id VARCHAR(255) NOT NULL, |
|
82
|
|
|
otp_key VARCHAR(255) NOT NULL, |
|
83
|
|
|
time_unix INTEGER NOT NULL, |
|
84
|
|
|
UNIQUE(user_id, otp_key) |
|
85
|
|
|
)', |
|
86
|
|
|
]; |
|
87
|
|
|
|
|
88
|
|
|
foreach ($queryList as $query) { |
|
89
|
|
|
$this->db->query($query); |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|