|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Copyright 2016 François Kooman <[email protected]>. |
|
4
|
|
|
* |
|
5
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
6
|
|
|
* you may not use this file except in compliance with the License. |
|
7
|
|
|
* You may obtain a copy of the License at |
|
8
|
|
|
* |
|
9
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
10
|
|
|
* |
|
11
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
12
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
13
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
14
|
|
|
* See the License for the specific language governing permissions and |
|
15
|
|
|
* limitations under the License. |
|
16
|
|
|
*/ |
|
17
|
|
|
|
|
18
|
|
|
namespace fkooman\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
|
|
|
/** @var string */ |
|
32
|
|
|
private $prefix; |
|
33
|
|
|
|
|
34
|
|
|
public function __construct(PDO $db, $prefix = '') |
|
35
|
|
|
{ |
|
36
|
|
|
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
|
37
|
|
|
$this->db = $db; |
|
38
|
|
|
$this->prefix = $prefix; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function record($userId, $otpKey, $timeUnix) |
|
42
|
|
|
{ |
|
43
|
|
|
$stmt = $this->db->prepare( |
|
44
|
|
|
sprintf( |
|
45
|
|
|
'INSERT INTO %s ( |
|
46
|
|
|
user_id, |
|
47
|
|
|
otp_key, |
|
48
|
|
|
time_unix |
|
49
|
|
|
) |
|
50
|
|
|
VALUES( |
|
51
|
|
|
:user_id, |
|
52
|
|
|
:otp_key, |
|
53
|
|
|
:time_unix |
|
54
|
|
|
)', |
|
55
|
|
|
$this->prefix.'otp_log' |
|
56
|
|
|
) |
|
57
|
|
|
); |
|
58
|
|
|
|
|
59
|
|
|
$stmt->bindValue(':user_id', $userId, PDO::PARAM_STR); |
|
60
|
|
|
$stmt->bindValue(':otp_key', $otpKey, PDO::PARAM_STR); |
|
61
|
|
|
$stmt->bindValue(':time_unix', $timeUnix, PDO::PARAM_INT); |
|
62
|
|
|
try { |
|
63
|
|
|
$stmt->execute(); |
|
64
|
|
|
} catch (PDOException $e) { |
|
65
|
|
|
return false; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
return true; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
View Code Duplication |
public function housekeeping($timeUnix) |
|
|
|
|
|
|
72
|
|
|
{ |
|
73
|
|
|
$stmt = $this->db->prepare( |
|
74
|
|
|
sprintf( |
|
75
|
|
|
'DELETE FROM %s |
|
76
|
|
|
WHERE time_unix < :time_unix', |
|
77
|
|
|
$this->prefix.'otp_log' |
|
78
|
|
|
) |
|
79
|
|
|
); |
|
80
|
|
|
|
|
81
|
|
|
$stmt->bindValue(':time_unix', $timeUnix, PDO::PARAM_INT); |
|
82
|
|
|
$stmt->execute(); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
public static function createTableQueries($prefix) |
|
86
|
|
|
{ |
|
87
|
|
|
$query = array( |
|
88
|
|
|
sprintf( |
|
89
|
|
|
'CREATE TABLE IF NOT EXISTS %s ( |
|
90
|
|
|
user_id VARCHAR(255) NOT NULL, |
|
91
|
|
|
otp_key VARCHAR(255) NOT NULL, |
|
92
|
|
|
time_unix INTEGER NOT NULL, |
|
93
|
|
|
UNIQUE(user_id, otp_key) |
|
94
|
|
|
)', |
|
95
|
|
|
$prefix.'otp_log' |
|
96
|
|
|
), |
|
97
|
|
|
); |
|
98
|
|
|
|
|
99
|
|
|
return $query; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
public function initDatabase() |
|
103
|
|
|
{ |
|
104
|
|
|
$queries = self::createTableQueries($this->prefix); |
|
105
|
|
|
foreach ($queries as $q) { |
|
106
|
|
|
$this->db->query($q); |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
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.