-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathBaseServerConfig.php
More file actions
260 lines (220 loc) · 7.28 KB
/
BaseServerConfig.php
File metadata and controls
260 lines (220 loc) · 7.28 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
<?php
namespace OCA\Solid;
use InvalidArgumentException;
use OCP\IConfig;
class BaseServerConfig {
////////////////////////////// CLASS PROPERTIES \\\\\\\\\\\\\\\\\\\\\\\\\\\\
public const ERROR_INVALID_ARGUMENT = 'Invalid %s for %s: %s. Must be one of %s.';
protected IConfig $config;
//////////////////////////////// PUBLIC API \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
public function __construct(IConfig $config) {
$this->config = $config;
}
/**
* @return string
*/
public function getPrivateKey() {
$result = $this->config->getAppValue('solid','privateKey');
if (!$result) {
// generate and save a new set if we don't have a private key;
$keys = $this->generateKeySet();
$this->config->setAppValue('solid','privateKey',$keys['privateKey']);
$this->config->setAppValue('solid','encryptionKey',$keys['encryptionKey']);
}
return $this->config->getAppValue('solid','privateKey');
}
/**
* @param string $privateKey
*/
public function setPrivateKey($privateKey) {
$this->config->setAppValue('solid','privateKey',$privateKey);
}
/**
* @return string
*/
public function getEncryptionKey() {
return $this->config->getAppValue('solid','encryptionKey');
}
/**
* @param string $publicKey
*/
public function setEncryptionKey($publicKey) {
$this->config->setAppValue('solid','encryptionKey',$publicKey);
}
private function generateKeySet() {
$config = array(
"digest_alg" => "sha256",
"private_key_bits" => 2048,
"private_key_type" => OPENSSL_KEYTYPE_RSA,
);
// Create the private and public key
$key = openssl_pkey_new($config);
// Extract the private key from $key to $privateKey
openssl_pkey_export($key, $privateKey);
$encryptionKey = base64_encode(random_bytes(32));
$result = array(
"privateKey" => $privateKey,
"encryptionKey" => $encryptionKey
);
return $result;
}
/**
* @param string $clientId
* @return array|null
*/
public function getClientConfigById($clientId) {
$clients = (array)$this->config->getAppValue('solid','clients');
if (array_key_exists($clientId, $clients)) {
return $clients[$clientId];
}
return null;
}
/**
* @return array|null
*/
public function getClients() {
$configKeys = (array)$this->config->getAppKeys('solid');
$clients = [];
foreach ($configKeys as $key) {
if (preg_match("/^client-([a-z0-9]+)$/", $key, $matches)) {
$clientRegistration = json_decode($this->config->getAppValue('solid', $key, '{}'), true);
$clients[] = [
"clientId" => $matches[1],
"clientName" => $clientRegistration['client_name'],
"clientBlocked" => $clientRegistration['blocked'] ?? false,
];
}
}
return $clients;
}
/**
* @param array $clientConfig
* @return string
*/
public function saveClientConfig($clientId, $clientConfig) {
$clients = (array)$this->config->getAppValue('solid', 'clients');
$clients[$clientId] = $clientConfig;
$this->config->setAppValue('solid','clients', $clients);
return $clientId;
}
/**
* @param string $clientId
* @param array $scopes
*/
public function addScopesToClient($clientId, $scopes) {
$clientScopes = $this->getClientScopes($clientId);
$clientScopes = array_unique(array_merge($clientScopes, $scopes));
$this->setClientScopes($clientId, $clientScopes);
}
/**
* @param string $clientId
* @param array $scopes
*/
public function setClientScopes($clientId, $scopes) {
$clientScopes = (array)$this->config->getAppValue('solid', 'clientScopes');
$clientScopes[$clientId] = $scopes;
$this->config->setAppValue('solid', 'clientScopes', $clientScopes);
}
/**
* @param string $clientId
* @return array
*/
public function getClientScopes($clientId) {
$clientScopes = (array)$this->config->getAppValue('solid', 'clientScopes');
if (array_key_exists($clientId, $clientScopes)) {
return $clientScopes[$clientId];
}
return [];
}
/**
* @param string $clientId
*/
public function removeClientConfig($clientId) {
$clients = (array)$this->config->getAppValue('solid', 'clients');
unset($clients[$clientId]);
$this->config->setAppValue('solid','clients', $clients);
$scopes = (array)$this->config->getAppValue('solid', 'clientScopes');
unset($scopes[$clientId]);
$this->config->setAppValue('solid', 'clientScopes', $scopes);
}
public function saveClientRegistration($clientData)
{
$generatedClientId = bin2hex(random_bytes(16)); // 32 chars for the client Id
// Avoid collision, give up after 5 tries.
for ($i = 0; $i < 5; $i++) {
$existingRegistration = $this->getClientRegistration($generatedClientId);
if ($existingRegistration['client_id'] ?? false) {
$generatedClientId = bin2hex(random_bytes(16));
} else {
break;
}
}
if ($existingRegistration['client_id'] ?? false) {
throw new \Exception("Could not generate unique client ID");
}
$generatedClientSecret = bin2hex(random_bytes(32)); // and 64 chars for the client secret
$clientData['client_id'] = $generatedClientId;
$clientData['client_secret'] = $generatedClientSecret;
if (!isset($clientData['client_name'])) {
$clientData['client_name'] = "Client $generatedClientId";
}
$this->config->setAppValue('solid', "client-" . $generatedClientId, json_encode($clientData));
return $clientData;
}
public function removeClientRegistration($clientId) {
$this->config->deleteAppValue('solid', "client-" . $clientId);
}
public function getClientRegistration($clientId) {
$data = $this->config->getAppValue('solid', "client-" . $clientId, "{}");
return json_decode($data, true);
}
public function getTrustedApps()
{
$appValue = $this->config->getAppValue('solid', 'trustedApps', '[]');
return json_decode($appValue, true, 512, JSON_THROW_ON_ERROR);
}
public function getUserSubDomainsEnabled() {
$value = $this->config->getAppValue('solid', 'userSubDomainsEnabled', false);
return $this->castToBool($value);
}
public function setUserSubDomainsEnabled($enabled) {
$value = $this->castToBool($enabled);
$this->config->setAppValue('solid', 'userSubDomainsEnabled', $value);
}
////////////////////////////// UTILITY METHODS \\\\\\\\\\\\\\\\\\\\\\\\\\\\\
private function castToBool(?string $mixedValue): bool
{
$type = gettype($mixedValue);
if ($type === 'boolean' || $type === 'NULL' || $type === 'integer') {
$value = (bool) $mixedValue;
} else {
if ($type === 'string') {
$mixedValue = strtolower($mixedValue);
if ($mixedValue === 'true' || $mixedValue === '1') {
$value = true;
} elseif ($mixedValue === 'false' || $mixedValue === '0' || $mixedValue === '') {
$value = false;
} else {
$error = [
'invalid' => 'value',
'for' => 'userSubDomainsEnabled',
'received' => $mixedValue,
'expected' => implode(',', ['true', 'false', '1', '0'])
];
}
} else {
$error = [
'invalid' => 'type',
'for' => 'userSubDomainsEnabled',
'received' => $type,
'expected' => implode(',', ['boolean', 'NULL', 'integer', 'string'])
];
}
}
if (isset($error)) {
$errorMessage = vsprintf(self::ERROR_INVALID_ARGUMENT, $error);
throw new InvalidArgumentException($errorMessage);
}
return $value;
}
}