-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathAuthenticatorService.php
More file actions
348 lines (307 loc) · 9.48 KB
/
AuthenticatorService.php
File metadata and controls
348 lines (307 loc) · 9.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
<?php
/**
* sysPass
*
* @author nuxsmin
* @link https://syspass.org
* @copyright 2012-2019, Rubén Domínguez nuxsmin@$syspass.org
*
* This file is part of sysPass.
*
* sysPass is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* sysPass is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with sysPass. If not, see <http://www.gnu.org/licenses/>.
*/
namespace SP\Modules\Web\Plugins\Authenticator\Services;
use BaconQrCode\Renderer\Image\Png;
use BaconQrCode\Renderer\Image\SvgImageBackEnd;
use BaconQrCode\Renderer\ImageRenderer;
use BaconQrCode\Renderer\RendererStyle\RendererStyle;
use BaconQrCode\Writer;
use Base2n;
use Defuse\Crypto\Exception\CryptoException;
use Defuse\Crypto\Exception\EnvironmentIsBrokenException;
use Exception;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use SP\Core\Exceptions\CheckException;
use SP\Core\Exceptions\ConstraintException;
use SP\Core\Exceptions\NoSuchPropertyException;
use SP\Core\Exceptions\QueryException;
use SP\Core\PhpExtensionChecker;
use SP\Modules\Web\Plugins\Authenticator\Models\AuthenticatorData;
use SP\Modules\Web\Plugins\Authenticator\Plugin;
use SP\Modules\Web\Plugins\Authenticator\Util\Google2FA;
use SP\Plugin\PluginManager;
use SP\Repositories\NoSuchItemException;
use SP\Services\Service;
use SP\Services\ServiceException;
use SP\Util\PasswordUtil;
use stdClass;
defined('APP_ROOT') || die();
/**
* Class AuthenticatorService
*
* @package SP\Auth
*/
final class AuthenticatorService extends Service
{
/**
* @var Plugin
*/
private $plugin;
/**
* @var PhpExtensionChecker
*/
private $extensionChecker;
/**
* Generar una clave de inicialización codificada en Base32
*
* @return string
* @throws EnvironmentIsBrokenException
*/
public static function makeInitializationKey(): string
{
$iv = PasswordUtil::generateRandomBytes(32);
$base32 = new Base2n(
5,
'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567',
false,
true,
true
);
return substr($base32->encode($iv), 0, 16);
}
/**
* Verificar el código de 2FA
*
* @param string $key
* @param string $iv
*
* @return bool
* @throws Exception
*/
public static function verifyKey(string $key, string $iv): bool
{
return Google2FA::verify_key($iv, $key);
}
/**
* Comprobar el token del usuario
*
* @param string $userToken EL código del usuario
* @param string $iv
*
* @return bool
* @throws Exception
*/
public static function checkUserToken(string $userToken, string $iv): bool
{
$totp = Google2FA::oath_totp(
Google2FA::base32_decode($iv),
Google2FA::get_timestamp()
);
return $totp === $userToken;
}
/**
* getQrCode
*
* @param string $login
* @param string $iv
*
* @return bool
* @throws CheckException
*/
public function getQrCodeFromUrl(string $login, string $iv)
{
try {
$this->extensionChecker->checkCurlAvailable(true);
$request = $this->dic->get(Client::class)
->request('GET', $this->getUserQRUrl($login, $iv));
logger($request->getHeaderLine('content-type'));
if ($request->getStatusCode() === 200
&& strpos($request->getHeaderLine('content-type'), 'image/png') !== false
) {
return base64_encode($request->getBody());
}
} catch (GuzzleException $e) {
processException($e);
}
return false;
}
/**
* Devolver la cadena con la URL para solicitar el código QR
*
* @param string $login
* @param string $iv
*
* @return string
*/
public function getUserQRUrl(string $login, string $iv): string
{
$qrUrl = 'https://api.qrserver.com/v1/create-qr-code/?size=150x150&data=';
$qrUrl .= urlencode('otpauth://totp/sysPass:syspass/' . $login . '?secret=' . $iv . '&issuer=sysPass');
return $qrUrl;
}
/**
* getQrCode
*
* @param string $login
* @param string $iv
*
* @return string
*/
public function getQrCodeFromServer(string $login, string $iv): string
{
$renderer = new ImageRenderer(
new RendererStyle(200),
new SvgImageBackEnd()
);
$writer = new Writer($renderer);
return base64_encode($writer->writeString('otpauth://totp/sysPass:syspass/' . $login . '?secret=' . $iv . '&issuer=sysPass'));
}
/**
* Devolver un código de recuperación
*
* @param AuthenticatorData $authenticatorData
*
* @return string
* @throws AuthenticatorException
* @throws EnvironmentIsBrokenException
*/
public function pickRecoveryCode(AuthenticatorData $authenticatorData): string
{
$recoveryTime = $authenticatorData->getLastRecoveryTime();
$codes = $authenticatorData->getRecoveryCodes();
$numCodes = count($codes);
if ($numCodes > 0) {
return $codes[0];
}
if ($recoveryTime === 0
|| (time() - $recoveryTime >= Plugin::RECOVERY_GRACE_TIME
&& $numCodes === 0)
) {
$codes = $this->generateRecoveryCodes();
return $codes[0];
}
throw new AuthenticatorException(_t('authenticator', 'There aren\'t any recovery codes available'));
}
/**
* Generar códigos de recuperación
*
* @return array
* @throws EnvironmentIsBrokenException
*/
public function generateRecoveryCodes(): array
{
$codes = [];
$i = 1;
do {
$codes[] = PasswordUtil::generateRandomBytes(10);
$i++;
} while ($i <= 10);
return $codes;
}
/**
* Eliminar los datos del Plugin de un usuario
*
* @param int $id
*
* @return void
* @throws ConstraintException
* @throws QueryException
* @throws NoSuchItemException
* @internal param AuthenticatorData $AuthenticatorData
*/
public function deletePluginUserData(int $id)
{
$this->plugin->deleteDataForId($id);
}
/**
* Comprobar la versión del plugin
*/
public function checkVersion()
{
try {
$this->extensionChecker->checkCurlAvailable(true);
$request = $this->dic->get(Client::class)
->request('GET', Plugin::VERSION_URL);
if ($request->getStatusCode() === 200
&& strpos($request->getHeaderLine('content-type'), 'application/json') !== false
) {
$data = $request->getBody();
if (isset($data->{$pluginName})) {
$out = new stdClass();
$out->plugin = $pluginName;
$out->remoteVersion = $data->{$pluginName}->version;
$out->localVersion = implode('.', $this->plugin->getVersion());
$out->result = version_compare($out->remoteVersion, $out->localVersion) === 1;
return $out;
}
}
} catch (GuzzleException $e) {
processException($e);
}
return false;
}
/**
* Usar un código de recuperación y deshabilitar 2FA
*
* @param AuthenticatorData $authenticatorData
* @param string $code
*
* @return bool
* @throws CryptoException
* @throws ConstraintException
* @throws NoSuchPropertyException
* @throws QueryException
* @throws ServiceException
*/
public function useRecoveryCode(AuthenticatorData $authenticatorData, string $code): bool
{
$codes = $authenticatorData->getRecoveryCodes();
$usedKey = array_search($code, $codes, true);
if ($usedKey !== false) {
$this->saveRecoveryCodes(
array_values(
array_filter($codes,
function ($key) use ($usedKey) {
return $key !== $usedKey;
}, ARRAY_FILTER_USE_KEY)
),
$authenticatorData);
return true;
}
return false;
}
/**
* @param array $codes
* @param AuthenticatorData $authenticatorData
*
* @throws CryptoException
* @throws ConstraintException
* @throws NoSuchPropertyException
* @throws QueryException
* @throws ServiceException
*/
private function saveRecoveryCodes(array $codes, AuthenticatorData $authenticatorData)
{
$authenticatorData->setRecoveryCodes($codes);
$authenticatorData->setLastRecoveryTime(time());
$this->plugin->saveData($authenticatorData->getUserId(), $authenticatorData);
}
protected function initialize()
{
$this->extensionChecker = $this->dic->get(PhpExtensionChecker::class);
$this->plugin = $this->dic->get(PluginManager::class)
->getPlugin(Plugin::PLUGIN_NAME);
}
}