|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the tiqr project. |
|
4
|
|
|
* |
|
5
|
|
|
* The tiqr project aims to provide an open implementation for |
|
6
|
|
|
* authentication using mobile devices. It was initiated by |
|
7
|
|
|
* SURFnet and developed by Egeniq. |
|
8
|
|
|
* |
|
9
|
|
|
* More information: http://www.tiqr.org |
|
10
|
|
|
* |
|
11
|
|
|
* @author Ivo Jansch <[email protected]> |
|
12
|
|
|
* |
|
13
|
|
|
* @package tiqr |
|
14
|
|
|
* |
|
15
|
|
|
* @license New BSD License - See LICENSE file for details. |
|
16
|
|
|
* |
|
17
|
|
|
* @copyright (C) 2010-2012 SURFnet BV |
|
18
|
|
|
*/ |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* This user storage implementation implements a simple user storage using json files. |
|
22
|
|
|
* This is mostly for demonstration and development purposes. In a production environment |
|
23
|
|
|
* please supply your own implementation that hosts the data in your user database OR |
|
24
|
|
|
* in a secure (e.g. hardware encrypted) storage. |
|
25
|
|
|
* @author ivo |
|
26
|
|
|
* |
|
27
|
|
|
* Note: This implementation does not employ locking or transactions, and is not safe when the same user is updated |
|
28
|
|
|
* concurrently |
|
29
|
|
|
*/ |
|
30
|
|
|
abstract class Tiqr_UserStorage_GenericStore extends Tiqr_UserStorage_Abstract |
|
31
|
|
|
{ |
|
32
|
|
|
|
|
33
|
|
|
/** Return data of user $userId |
|
34
|
|
|
* @param string $userId |
|
35
|
|
|
* @return array |
|
36
|
|
|
* @throws Exception |
|
37
|
|
|
*/ |
|
38
|
|
|
abstract protected function _loadUser(string $userId): array; |
|
39
|
|
|
|
|
40
|
|
|
/** Write data of $userId |
|
41
|
|
|
* @param string $userId |
|
42
|
|
|
* @param array $data |
|
43
|
|
|
* @throws Exception |
|
44
|
|
|
*/ |
|
45
|
|
|
abstract protected function _saveUser(string $userId, array $data): void; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param string $userId |
|
49
|
|
|
* @return bool trues when user exists, false otherwise |
|
50
|
|
|
* @throws Exception |
|
51
|
|
|
*/ |
|
52
|
|
|
abstract protected function _userExists(string $userId): bool; |
|
53
|
|
|
|
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @see Tiqr_UserStorage_Interface::createUser() |
|
57
|
|
|
*/ |
|
58
|
1 |
|
public function createUser(string $userId, string $displayName) : void |
|
59
|
|
|
{ |
|
60
|
1 |
|
$user = array("userId"=>$userId, |
|
61
|
1 |
|
"displayName"=>$displayName); |
|
62
|
1 |
|
$this->_saveUser($userId, $user); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @see Tiqr_UserStorage_Interface::userExists() |
|
67
|
|
|
*/ |
|
68
|
1 |
|
public function userExists(string $userId): bool |
|
69
|
|
|
{ |
|
70
|
1 |
|
return $this->_userExists($userId); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @see Tiqr_UserStorage_Interface::getDisplayName() |
|
75
|
|
|
*/ |
|
76
|
1 |
|
public function getDisplayName(string $userId): string |
|
77
|
|
|
{ |
|
78
|
1 |
|
if ($data = $this->_loadUser($userId)) { |
|
79
|
1 |
|
return $data["displayName"]; |
|
80
|
|
|
} |
|
81
|
|
|
return ''; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @see Tiqr_UserStorage_Interface::getNotificationType() |
|
86
|
|
|
*/ |
|
87
|
1 |
|
public function getNotificationType(string $userId): string |
|
88
|
|
|
{ |
|
89
|
1 |
|
if ($data = $this->_loadUser($userId)) { |
|
90
|
1 |
|
if (isset($data["notificationType"])) { |
|
91
|
1 |
|
return $data["notificationType"]; |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
1 |
|
return ''; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @see Tiqr_UserStorage_Interface::setNotificationType() |
|
99
|
|
|
*/ |
|
100
|
1 |
|
public function setNotificationType(string $userId, string $type): void |
|
101
|
|
|
{ |
|
102
|
1 |
|
$data = $this->_loadUser($userId); |
|
103
|
1 |
|
$data["notificationType"] = $type; |
|
104
|
1 |
|
$this->_saveUser($userId, $data); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @see Tiqr_UserStorage_Interface::getNotificationAddress() |
|
109
|
|
|
*/ |
|
110
|
1 |
|
public function getNotificationAddress(string $userId): string |
|
111
|
|
|
{ |
|
112
|
1 |
|
if ($data = $this->_loadUser($userId)) { |
|
113
|
1 |
|
if (isset($data["notificationAddress"])) { |
|
114
|
1 |
|
return $data["notificationAddress"]; |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
1 |
|
$this->logger->info('Unable to find notification address for user'); |
|
118
|
1 |
|
return ''; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @see Tiqr_UserStorage_Interface::setNotificationAddress() |
|
123
|
|
|
*/ |
|
124
|
1 |
|
public function setNotificationAddress(string $userId, string $address): void |
|
125
|
|
|
{ |
|
126
|
1 |
|
$data = $this->_loadUser($userId); |
|
127
|
1 |
|
$data["notificationAddress"] = $address; |
|
128
|
1 |
|
$this->_saveUser($userId, $data); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* @see Tiqr_UserStorage_Interface::getLoginAttempts() |
|
133
|
|
|
*/ |
|
134
|
1 |
|
public function getLoginAttempts(string $userId): int |
|
135
|
|
|
{ |
|
136
|
1 |
|
if ($data = $this->_loadUser($userId)) { |
|
137
|
1 |
|
if (isset($data["loginattempts"])) { |
|
138
|
1 |
|
return $data["loginattempts"]; |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
1 |
|
return 0; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* @see Tiqr_UserStorage_Interface::setLoginAttempts() |
|
146
|
|
|
*/ |
|
147
|
1 |
|
public function setLoginAttempts(string $userId, int $amount): void |
|
148
|
|
|
{ |
|
149
|
1 |
|
$data = $this->_loadUser($userId); |
|
150
|
1 |
|
$data["loginattempts"] = $amount; |
|
151
|
1 |
|
$this->_saveUser($userId, $data); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* @see Tiqr_UserStorage_Interface::isBlocked() |
|
156
|
|
|
*/ |
|
157
|
1 |
|
public function isBlocked(string $userId, int $tempBlockDuration = 0): bool |
|
158
|
|
|
{ |
|
159
|
1 |
|
if ($data = $this->_loadUser($userId)) { |
|
160
|
1 |
|
if (isset($data["blocked"]) && $data["blocked"]) { |
|
161
|
1 |
|
return true; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
// Check temporary block |
|
165
|
1 |
|
$timestamp = $this->getTemporaryBlockTimestamp($userId); |
|
166
|
1 |
|
if (0 == $timestamp || 0 == $tempBlockDuration) { |
|
167
|
1 |
|
return false; // No temp block timestamp set or no tempBlockDuration provided |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
1 |
|
if ($timestamp + $tempBlockDuration * 60 < time()) { |
|
171
|
1 |
|
return false; // Temp block expired |
|
172
|
|
|
} |
|
173
|
|
|
} |
|
174
|
1 |
|
return true; // Blocked by temp block |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* @see Tiqr_UserStorage_Interface::setTemporaryBlockAttempts() |
|
179
|
|
|
*/ |
|
180
|
1 |
|
public function setTemporaryBlockAttempts(string $userId, int $amount): void { |
|
181
|
1 |
|
$data = $this->_loadUser($userId); |
|
182
|
1 |
|
$data["temporaryBlockAttempts"] = $amount; |
|
183
|
1 |
|
$this->_saveUser($userId, $data); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* @see Tiqr_UserStorage_Interface::getTemporaryBlockAttempts() |
|
188
|
|
|
*/ |
|
189
|
1 |
|
public function getTemporaryBlockAttempts(string $userId): int { |
|
190
|
1 |
|
if ($data = $this->_loadUser($userId)) { |
|
191
|
1 |
|
if (isset($data["temporaryBlockAttempts"])) { |
|
192
|
1 |
|
return $data["temporaryBlockAttempts"]; |
|
193
|
|
|
} |
|
194
|
|
|
} |
|
195
|
1 |
|
return 0; |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* @see Tiqr_UserStorage_Interface::setTemporaryBlockTimestamp() |
|
200
|
|
|
*/ |
|
201
|
1 |
|
public function setTemporaryBlockTimestamp(string $userId, int $timestamp): void { |
|
202
|
1 |
|
$data = $this->_loadUser($userId); |
|
203
|
1 |
|
$data["temporaryBlockTimestamp"] = $timestamp; |
|
204
|
1 |
|
$this->_saveUser($userId, $data); |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
/** |
|
208
|
|
|
* @see Tiqr_UserStorage_Interface::getTemporaryBlockTimestamp() |
|
209
|
|
|
*/ |
|
210
|
1 |
|
public function getTemporaryBlockTimestamp(string $userId): int { |
|
211
|
1 |
|
if ($data = $this->_loadUser($userId)) { |
|
212
|
1 |
|
if (isset($data["temporaryBlockTimestamp"])) { |
|
213
|
1 |
|
return $data["temporaryBlockTimestamp"]; |
|
214
|
|
|
} |
|
215
|
|
|
} |
|
216
|
1 |
|
return 0; |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
/** |
|
220
|
|
|
* @see Tiqr_UserStorage_Interface::block() |
|
221
|
|
|
*/ |
|
222
|
1 |
|
public function setBlocked(string $userId, bool $blocked): void |
|
223
|
|
|
{ |
|
224
|
1 |
|
$data = $this->_loadUser($userId); |
|
225
|
1 |
|
$data["blocked"] = $blocked; |
|
226
|
1 |
|
$this->_saveUser($userId, $data); |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
} |
|
230
|
|
|
|