|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Teampass - a collaborative passwords manager. |
|
7
|
|
|
* --- |
|
8
|
|
|
* This file is part of the TeamPass project. |
|
9
|
|
|
* |
|
10
|
|
|
* TeamPass is free software: you can redistribute it and/or modify it |
|
11
|
|
|
* under the terms of the GNU General Public License as published by |
|
12
|
|
|
* the Free Software Foundation, version 3 of the License. |
|
13
|
|
|
* |
|
14
|
|
|
* TeamPass is distributed in the hope that it will be useful, |
|
15
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
16
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
17
|
|
|
* GNU General Public License for more details. |
|
18
|
|
|
* |
|
19
|
|
|
* You should have received a copy of the GNU General Public License |
|
20
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>. |
|
21
|
|
|
* |
|
22
|
|
|
* Certain components of this file may be under different licenses. For |
|
23
|
|
|
* details, see the `licenses` directory or individual file headers. |
|
24
|
|
|
* --- |
|
25
|
|
|
* @file OneTimeCodeService.php |
|
26
|
|
|
* @author Nils Laumaillé ([email protected]) |
|
27
|
|
|
* @copyright 2009-2025 Teampass.net |
|
28
|
|
|
* @license GPL-3.0 |
|
29
|
|
|
* @see https://www.teampass.net |
|
30
|
|
|
*/ |
|
31
|
|
|
|
|
32
|
|
|
use TeampassClasses\SessionManager\SessionManager; |
|
33
|
|
|
use TeampassClasses\Language\Language; |
|
34
|
|
|
|
|
35
|
|
|
class OneTimeCodeService |
|
36
|
|
|
{ |
|
37
|
|
|
private Language $lang; |
|
38
|
|
|
|
|
39
|
|
|
public function __construct() |
|
40
|
|
|
{ |
|
41
|
|
|
$session = SessionManager::getSession(); |
|
42
|
|
|
$this->lang = new Language($session->get('user-language') ?? 'english'); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function generateForUser(int $userId): string |
|
46
|
|
|
{ |
|
47
|
|
|
if (!isUserIdValid($userId)) { |
|
48
|
|
|
return $this->respondError($this->lang->get('error_no_user')); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
$user = $this->getUser($userId); |
|
52
|
|
|
|
|
53
|
|
|
if (!$user || empty($user['email'])) { |
|
54
|
|
|
return $this->respondError($this->lang->get('no_email_set')); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
$password = generateQuickPassword(); |
|
58
|
|
|
$keys = generateUserKeys($password); |
|
59
|
|
|
|
|
60
|
|
|
$this->storeUserKeys($userId, $keys); |
|
61
|
|
|
|
|
62
|
|
|
return $this->respondSuccess($password); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
private function getUser(int $userId): ?array |
|
66
|
|
|
{ |
|
67
|
|
|
$user = DB::queryFirstRow( |
|
68
|
|
|
'SELECT email, auth_type, login FROM ' . prefixTable('users') . ' WHERE id = %i', |
|
69
|
|
|
$userId |
|
70
|
|
|
); |
|
71
|
|
|
|
|
72
|
|
|
return DB::count() > 0 ? $user : null; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
private function storeUserKeys(int $userId, array $keys): void |
|
76
|
|
|
{ |
|
77
|
|
|
DB::update( |
|
78
|
|
|
prefixTable('users'), |
|
79
|
|
|
[ |
|
80
|
|
|
'public_key' => $keys['public_key'], |
|
81
|
|
|
'private_key' => $keys['private_key'], |
|
82
|
|
|
'special' => 'generate-keys', |
|
83
|
|
|
], |
|
84
|
|
|
'id=%i', |
|
85
|
|
|
$userId |
|
86
|
|
|
); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
private function respondError(string $message): string |
|
90
|
|
|
{ |
|
91
|
|
|
return prepareExchangedData([ |
|
92
|
|
|
'error' => true, |
|
93
|
|
|
'message' => $message, |
|
94
|
|
|
], 'encode'); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
private function respondSuccess(string $code): string |
|
98
|
|
|
{ |
|
99
|
|
|
return prepareExchangedData([ |
|
100
|
|
|
'error' => false, |
|
101
|
|
|
'message' => '', |
|
102
|
|
|
'code' => $code, |
|
103
|
|
|
'visible_otp' => ADMIN_VISIBLE_OTP_ON_LDAP_IMPORT, |
|
104
|
|
|
], 'encode'); |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|