|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace fkooman\VPN\Server\Log; |
|
4
|
|
|
|
|
5
|
|
|
use PDO; |
|
6
|
|
|
use PDOException; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Keep track of the used OTP keys so they cannot be replayed. |
|
10
|
|
|
*/ |
|
11
|
|
|
class OtpLog |
|
12
|
|
|
{ |
|
13
|
|
|
/** @var PDO */ |
|
14
|
|
|
private $db; |
|
15
|
|
|
|
|
16
|
|
|
/** @var string */ |
|
17
|
|
|
private $prefix; |
|
18
|
|
|
|
|
19
|
|
|
public function __construct(PDO $db, $prefix = '') |
|
20
|
|
|
{ |
|
21
|
|
|
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
|
22
|
|
|
$this->db = $db; |
|
23
|
|
|
$this->prefix = $prefix; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
public function record($userId, $otpKey, $timeUnix) |
|
27
|
|
|
{ |
|
28
|
|
|
$stmt = $this->db->prepare( |
|
29
|
|
|
sprintf( |
|
30
|
|
|
'INSERT INTO %s ( |
|
31
|
|
|
user_id, |
|
32
|
|
|
otp_key, |
|
33
|
|
|
time_unix |
|
34
|
|
|
) |
|
35
|
|
|
VALUES( |
|
36
|
|
|
:user_id, |
|
37
|
|
|
:otp_key, |
|
38
|
|
|
:time_unix |
|
39
|
|
|
)', |
|
40
|
|
|
$this->prefix.'otp_log' |
|
41
|
|
|
) |
|
42
|
|
|
); |
|
43
|
|
|
|
|
44
|
|
|
$stmt->bindValue(':user_id', $userId, PDO::PARAM_STR); |
|
45
|
|
|
$stmt->bindValue(':otp_key', $otpKey, PDO::PARAM_STR); |
|
46
|
|
|
$stmt->bindValue(':time_unix', $timeUnix, PDO::PARAM_INT); |
|
47
|
|
|
try { |
|
48
|
|
|
$stmt->execute(); |
|
49
|
|
|
} catch (PDOException $e) { |
|
50
|
|
|
return false; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
return true; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
View Code Duplication |
public function housekeeping($timeUnix) |
|
|
|
|
|
|
57
|
|
|
{ |
|
58
|
|
|
$stmt = $this->db->prepare( |
|
59
|
|
|
sprintf( |
|
60
|
|
|
'DELETE FROM %s |
|
61
|
|
|
WHERE time_unix < :time_unix', |
|
62
|
|
|
$this->prefix.'otp_log' |
|
63
|
|
|
) |
|
64
|
|
|
); |
|
65
|
|
|
|
|
66
|
|
|
$stmt->bindValue(':time_unix', $timeUnix, PDO::PARAM_INT); |
|
67
|
|
|
$stmt->execute(); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
public static function createTableQueries($prefix) |
|
71
|
|
|
{ |
|
72
|
|
|
$query = array( |
|
73
|
|
|
sprintf( |
|
74
|
|
|
'CREATE TABLE IF NOT EXISTS %s ( |
|
75
|
|
|
user_id VARCHAR(255) NOT NULL, |
|
76
|
|
|
otp_key VARCHAR(255) NOT NULL, |
|
77
|
|
|
time_unix INTEGER NOT NULL, |
|
78
|
|
|
UNIQUE(user_id, otp_key) |
|
79
|
|
|
)', |
|
80
|
|
|
$prefix.'otp_log' |
|
81
|
|
|
), |
|
82
|
|
|
); |
|
83
|
|
|
|
|
84
|
|
|
return $query; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
View Code Duplication |
public function initDatabase() |
|
|
|
|
|
|
88
|
|
|
{ |
|
89
|
|
|
$queries = self::createTableQueries($this->prefix); |
|
90
|
|
|
foreach ($queries as $q) { |
|
91
|
|
|
$this->db->query($q); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
$tables = array('otp_log'); |
|
95
|
|
|
foreach ($tables as $t) { |
|
|
|
|
|
|
96
|
|
|
// make sure the tables are empty |
|
97
|
|
|
# $this->db->query( |
|
98
|
|
|
# sprintf( |
|
99
|
|
|
# 'DELETE FROM %s', |
|
100
|
|
|
# $this->prefix.$t |
|
101
|
|
|
# ) |
|
102
|
|
|
# ); |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
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.