|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* SPDX-FileCopyrightText: 2024 Christoph Wurst <[email protected]> |
|
7
|
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace OCA\TwoFactorGateway\Service; |
|
11
|
|
|
|
|
12
|
|
|
use Exception; |
|
13
|
|
|
use OCA\TwoFactorGateway\AppInfo\Application; |
|
14
|
|
|
use OCA\TwoFactorGateway\Provider\State; |
|
15
|
|
|
use OCP\IConfig; |
|
16
|
|
|
use OCP\IUser; |
|
17
|
|
|
|
|
18
|
|
|
class StateStorage { |
|
19
|
|
|
public const STATE_DISABLED = 0; |
|
20
|
|
|
public const STATE_START_VERIFICATION = 1; |
|
21
|
|
|
public const STATE_VERIFYING = 2; |
|
22
|
|
|
public const STATE_ENABLED = 3; |
|
23
|
|
|
|
|
24
|
6 |
|
public function __construct( |
|
25
|
|
|
private IConfig $config, |
|
26
|
|
|
) { |
|
27
|
6 |
|
} |
|
28
|
|
|
|
|
29
|
6 |
|
private function buildConfigKey(string $gatewayName, string $key): string { |
|
30
|
6 |
|
return $gatewayName . "_$key"; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
4 |
|
private function getUserValue(IUser $user, string $gatewayName, string $key, string $default = ''): string { |
|
34
|
4 |
|
$gatewayKey = $this->buildConfigKey($gatewayName, $key); |
|
35
|
4 |
|
return $this->config->getUserValue($user->getUID(), Application::APP_ID, $gatewayKey, $default); |
|
|
|
|
|
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
2 |
|
private function setUserValue(IUser $user, string $gatewayName, string $key, ?string $value): void { |
|
39
|
2 |
|
$gatewayKey = $this->buildConfigKey($gatewayName, $key); |
|
40
|
2 |
|
$this->config->setUserValue($user->getUID(), Application::APP_ID, $gatewayKey, $value); |
|
|
|
|
|
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
private function deleteUserValue(IUser $user, string $gatewayName, string $key): void { |
|
44
|
|
|
$gatewayKey = $this->buildConfigKey($gatewayName, $key); |
|
45
|
|
|
$this->config->deleteUserValue($user->getUID(), Application::APP_ID, $gatewayKey); |
|
|
|
|
|
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
4 |
|
public function get(IUser $user, string $gatewayName): State { |
|
49
|
4 |
|
$isVerified = $this->getUserValue($user, $gatewayName, 'verified', 'false') === 'true'; |
|
50
|
4 |
|
$identifier = $this->getUserValue($user, $gatewayName, 'identifier'); |
|
51
|
4 |
|
$verificationCode = $this->getUserValue($user, $gatewayName, 'verification_code'); |
|
52
|
|
|
|
|
53
|
4 |
|
if ($isVerified) { |
|
54
|
1 |
|
$state = StateStorage::STATE_ENABLED; |
|
55
|
3 |
|
} elseif ($identifier !== '' && $verificationCode !== '') { |
|
56
|
1 |
|
$state = StateStorage::STATE_VERIFYING; |
|
57
|
|
|
} else { |
|
58
|
2 |
|
$state = StateStorage::STATE_DISABLED; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
4 |
|
return new State( |
|
62
|
4 |
|
$user, |
|
63
|
4 |
|
$state, |
|
64
|
4 |
|
$gatewayName, |
|
65
|
4 |
|
$identifier, |
|
66
|
4 |
|
$verificationCode |
|
67
|
4 |
|
); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
2 |
|
public function persist(State $state): State { |
|
71
|
2 |
|
switch ($state->getState()) { |
|
72
|
|
|
case StateStorage::STATE_DISABLED: |
|
73
|
|
|
$this->deleteUserValue( |
|
74
|
|
|
$state->getUser(), |
|
75
|
|
|
$state->getGatewayName(), |
|
76
|
|
|
'verified' |
|
77
|
|
|
); |
|
78
|
|
|
$this->deleteUserValue( |
|
79
|
|
|
$state->getUser(), |
|
80
|
|
|
$state->getGatewayName(), |
|
81
|
|
|
'verification_code' |
|
82
|
|
|
); |
|
83
|
|
|
|
|
84
|
|
|
break; |
|
85
|
|
|
case StateStorage::STATE_VERIFYING: |
|
86
|
1 |
|
$this->setUserValue( |
|
87
|
1 |
|
$state->getUser(), |
|
88
|
1 |
|
$state->getGatewayName(), |
|
89
|
1 |
|
'identifier', |
|
90
|
1 |
|
$state->getIdentifier() |
|
91
|
1 |
|
); |
|
92
|
1 |
|
$this->setUserValue( |
|
93
|
1 |
|
$state->getUser(), |
|
94
|
1 |
|
$state->getGatewayName(), |
|
95
|
1 |
|
'verification_code', |
|
96
|
1 |
|
$state->getVerificationCode() |
|
97
|
1 |
|
); |
|
98
|
1 |
|
$this->setUserValue( |
|
99
|
1 |
|
$state->getUser(), |
|
100
|
1 |
|
$state->getGatewayName(), |
|
101
|
1 |
|
'verified', |
|
102
|
1 |
|
'false' |
|
103
|
1 |
|
); |
|
104
|
|
|
|
|
105
|
1 |
|
break; |
|
106
|
|
|
case StateStorage::STATE_ENABLED: |
|
107
|
1 |
|
$this->setUserValue( |
|
108
|
1 |
|
$state->getUser(), |
|
109
|
1 |
|
$state->getGatewayName(), |
|
110
|
1 |
|
'identifier', |
|
111
|
1 |
|
$state->getIdentifier() |
|
112
|
1 |
|
); |
|
113
|
1 |
|
$this->setUserValue( |
|
114
|
1 |
|
$state->getUser(), |
|
115
|
1 |
|
$state->getGatewayName(), |
|
116
|
1 |
|
'verification_code', |
|
117
|
1 |
|
$state->getVerificationCode() |
|
118
|
1 |
|
); |
|
119
|
1 |
|
$this->setUserValue( |
|
120
|
1 |
|
$state->getUser(), |
|
121
|
1 |
|
$state->getGatewayName(), |
|
122
|
1 |
|
'verified', |
|
123
|
1 |
|
'true' |
|
124
|
1 |
|
); |
|
125
|
|
|
|
|
126
|
1 |
|
break; |
|
127
|
|
|
default: |
|
128
|
|
|
throw new Exception('invalid provider state'); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
2 |
|
return $state; |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.