1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* @author Christoph Wurst <[email protected]> |
7
|
|
|
* |
8
|
|
|
* @license GNU AGPL version 3 or any later version |
9
|
|
|
* |
10
|
|
|
* This program is free software: you can redistribute it and/or modify |
11
|
|
|
* it under the terms of the GNU Affero General Public License as |
12
|
|
|
* published by the Free Software Foundation, either version 3 of the |
13
|
|
|
* License, or (at your option) any later version. |
14
|
|
|
* |
15
|
|
|
* This program is distributed in the hope that it will be useful, |
16
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
17
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
18
|
|
|
* GNU Affero General Public License for more details. |
19
|
|
|
* |
20
|
|
|
* You should have received a copy of the GNU Affero General Public License |
21
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
22
|
|
|
* |
23
|
|
|
*/ |
24
|
|
|
|
25
|
|
|
namespace OCA\TwoFactorGateway\Service; |
26
|
|
|
|
27
|
|
|
use Exception; |
28
|
|
|
use OCA\TwoFactorGateway\AppInfo\Application; |
29
|
|
|
use OCA\TwoFactorGateway\Provider\SmsProvider; |
30
|
|
|
use OCA\TwoFactorGateway\Provider\State; |
31
|
|
|
use OCP\IConfig; |
32
|
|
|
use OCP\IUser; |
33
|
|
|
|
34
|
|
|
class StateStorage { |
35
|
|
|
|
36
|
|
|
/** @var IConfig */ |
37
|
|
|
private $config; |
38
|
|
|
|
39
|
|
|
public function __construct(IConfig $config) { |
40
|
|
|
$this->config = $config; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
private function buildConfigKey(string $gatewayName, string $key) { |
44
|
|
|
return "$gatewayName" . "_$key"; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
private function getUserValue(IUser $user, string $gatewayName, string $key, $default = '') { |
48
|
|
|
$gatewayKey = $this->buildConfigKey($gatewayName, $key); |
49
|
|
|
return $this->config->getUserValue($user->getUID(), Application::APP_ID, $gatewayKey, $default); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
private function setUserValue(IUser $user, string $gatewayName, string $key, $value) { |
53
|
|
|
$gatewayKey = $this->buildConfigKey($gatewayName, $key); |
54
|
|
|
$this->config->setUserValue($user->getUID(), Application::APP_ID, $gatewayKey, $value); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
private function deleteUserValue(IUser $user, string $gatewayName, string $key) { |
58
|
|
|
$gatewayKey = $this->buildConfigKey($gatewayName, $key); |
59
|
|
|
$this->config->deleteUserValue($user->getUID(), Application::APP_ID, $gatewayKey); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function get(IUser $user, string $gatewayName): State { |
63
|
|
|
$isVerified = $this->getUserValue($user, $gatewayName, 'verified', 'false') === 'true'; |
64
|
|
|
$identifier = $this->getUserValue($user, $gatewayName, 'identifier'); |
65
|
|
|
$verificationCode = $this->getUserValue($user, $gatewayName, 'verification_code'); |
66
|
|
|
|
67
|
|
|
if ($isVerified) { |
68
|
|
|
$state = SmsProvider::STATE_ENABLED; |
69
|
|
|
} elseif ($identifier !== '' && $verificationCode !== '') { |
70
|
|
|
$state = SmsProvider::STATE_VERIFYING; |
71
|
|
|
} else { |
72
|
|
|
$state = SmsProvider::STATE_DISABLED; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return new State( |
76
|
|
|
$user, |
77
|
|
|
$state, |
78
|
|
|
$gatewayName, |
79
|
|
|
$identifier, |
80
|
|
|
$verificationCode |
81
|
|
|
); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function persist(State $state): State { |
85
|
|
|
switch ($state->getState()) { |
86
|
|
|
case SmsProvider::STATE_DISABLED: |
87
|
|
|
$this->deleteUserValue( |
88
|
|
|
$state->getUser(), |
89
|
|
|
$state->getGatewayName(), |
90
|
|
|
'verified' |
91
|
|
|
); |
92
|
|
|
$this->deleteUserValue( |
93
|
|
|
$state->getUser(), |
94
|
|
|
$state->getGatewayName(), |
95
|
|
|
'verification_code' |
96
|
|
|
); |
97
|
|
|
|
98
|
|
|
break; |
99
|
|
|
case SmsProvider::STATE_VERIFYING: |
100
|
|
|
$this->setUserValue( |
101
|
|
|
$state->getUser(), |
102
|
|
|
$state->getGatewayName(), |
103
|
|
|
'identifier', |
104
|
|
|
$state->getIdentifier() |
105
|
|
|
); |
106
|
|
|
$this->setUserValue( |
107
|
|
|
$state->getUser(), |
108
|
|
|
$state->getGatewayName(), |
109
|
|
|
'verification_code', |
110
|
|
|
$state->getVerificationCode() |
111
|
|
|
); |
112
|
|
|
$this->setUserValue( |
113
|
|
|
$state->getUser(), |
114
|
|
|
$state->getGatewayName(), |
115
|
|
|
'verified', |
116
|
|
|
'false' |
117
|
|
|
); |
118
|
|
|
|
119
|
|
|
break; |
120
|
|
|
case SmsProvider::STATE_ENABLED: |
121
|
|
|
$this->setUserValue( |
122
|
|
|
$state->getUser(), |
123
|
|
|
$state->getGatewayName(), |
124
|
|
|
'identifier', |
125
|
|
|
$state->getIdentifier() |
126
|
|
|
); |
127
|
|
|
$this->setUserValue( |
128
|
|
|
$state->getUser(), |
129
|
|
|
$state->getGatewayName(), |
130
|
|
|
'verification_code', |
131
|
|
|
$state->getVerificationCode() |
132
|
|
|
); |
133
|
|
|
$this->setUserValue( |
134
|
|
|
$state->getUser(), |
135
|
|
|
$state->getGatewayName(), |
136
|
|
|
'verified', |
137
|
|
|
'true' |
138
|
|
|
); |
139
|
|
|
|
140
|
|
|
break; |
141
|
|
|
default: |
142
|
|
|
throw new Exception('invalid provider state'); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
return $state; |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|