Skip to content

Commit 3c93419

Browse files
committed
let us create a base serverconfig to get the settings
1 parent 7c5b82f commit 3c93419

3 files changed

Lines changed: 78 additions & 58 deletions

File tree

solid/lib/BaseServerConfig.php

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
namespace OCA\Solid;
3+
4+
use OCP\IConfig;
5+
6+
class BaseServerConfig {
7+
private IConfig $config;
8+
9+
/**
10+
* @param IConfig $config
11+
*/
12+
public function __construct(IConfig $config) {
13+
$this->config = $config;
14+
}
15+
16+
/**
17+
* @return string
18+
*/
19+
public function getPrivateKey() {
20+
$result = $this->config->getAppValue('solid','privateKey');
21+
if (!$result) {
22+
// generate and save a new set if we don't have a private key;
23+
$keys = $this->generateKeySet();
24+
$this->config->setAppValue('solid','privateKey',$keys['privateKey']);
25+
$this->config->setAppValue('solid','encryptionKey',$keys['encryptionKey']);
26+
}
27+
return $this->config->getAppValue('solid','privateKey');
28+
}
29+
30+
/**
31+
* @param string $privateKey
32+
*/
33+
public function setPrivateKey($privateKey) {
34+
$this->config->setAppValue('solid','privateKey',$privateKey);
35+
}
36+
37+
/**
38+
* @return string
39+
*/
40+
public function getEncryptionKey() {
41+
return $this->config->getAppValue('solid','encryptionKey');
42+
}
43+
44+
/**
45+
* @param string $publicKey
46+
*/
47+
public function setEncryptionKey($publicKey) {
48+
$this->config->setAppValue('solid','encryptionKey',$publicKey);
49+
}
50+
51+
private function generateKeySet() {
52+
$config = array(
53+
"digest_alg" => "sha256",
54+
"private_key_bits" => 2048,
55+
"private_key_type" => OPENSSL_KEYTYPE_RSA,
56+
);
57+
// Create the private and public key
58+
$key = openssl_pkey_new($config);
59+
60+
// Extract the private key from $key to $privateKey
61+
openssl_pkey_export($key, $privateKey);
62+
$encryptionKey = base64_encode(random_bytes(32));
63+
$result = array(
64+
"privateKey" => $privateKey,
65+
"encryptionKey" => $encryptionKey
66+
);
67+
return $result;
68+
}
69+
}
70+

solid/lib/ServerConfig.php

Lines changed: 3 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44
use OCP\IConfig;
55
use OCP\IUserManager;
66
use OCP\IUrlGenerator;
7-
7+
use OCA\Solid\BaseServerConfig;
8+
89
/**
910
* @package OCA\Solid
1011
*/
11-
class ServerConfig {
12+
class ServerConfig extends BaseServerConfig {
1213
private IConfig $config;
1314
private IUrlGenerator $urlGenerator;
1415
private IUserManager $userManager;
@@ -24,41 +25,6 @@ public function __construct(IConfig $config, IUrlGenerator $urlGenerator, IUserM
2425
$this->urlGenerator = $urlGenerator;
2526
}
2627

27-
/**
28-
* @return string
29-
*/
30-
public function getPrivateKey() {
31-
$result = $this->config->getAppValue('solid','privateKey');
32-
if (!$result) {
33-
// generate and save a new set if we don't have a private key;
34-
$keys = $this->generateKeySet();
35-
$this->config->setAppValue('solid','privateKey',$keys['privateKey']);
36-
$this->config->setAppValue('solid','encryptionKey',$keys['encryptionKey']);
37-
}
38-
return $this->config->getAppValue('solid','privateKey');
39-
}
40-
41-
/**
42-
* @param string $privateKey
43-
*/
44-
public function setPrivateKey($privateKey) {
45-
$this->config->setAppValue('solid','privateKey',$privateKey);
46-
}
47-
48-
/**
49-
* @return string
50-
*/
51-
public function getEncryptionKey() {
52-
return $this->config->getAppValue('solid','encryptionKey');
53-
}
54-
55-
/**
56-
* @param string $publicKey
57-
*/
58-
public function setEncryptionKey($publicKey) {
59-
$this->config->setAppValue('solid','encryptionKey',$publicKey);
60-
}
61-
6228
/**
6329
* @param string $clientId
6430
* @return array|null
@@ -197,22 +163,4 @@ public function setProfileData($userId, $profileData) {
197163
$user->setDisplayName($fields['name']);
198164
}
199165
}
200-
private function generateKeySet() {
201-
$config = array(
202-
"digest_alg" => "sha256",
203-
"private_key_bits" => 2048,
204-
"private_key_type" => OPENSSL_KEYTYPE_RSA,
205-
);
206-
// Create the private and public key
207-
$key = openssl_pkey_new($config);
208-
209-
// Extract the private key from $key to $privateKey
210-
openssl_pkey_export($key, $privateKey);
211-
$encryptionKey = base64_encode(random_bytes(32));
212-
$result = array(
213-
"privateKey" => $privateKey,
214-
"encryptionKey" => $encryptionKey
215-
);
216-
return $result;
217-
}
218166
}

solid/lib/Settings/SolidAdmin.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,26 @@
55
use OCP\IConfig;
66
use OCP\IL10N;
77
use OCP\Settings\ISettings;
8-
use OCA\Solid\ServerConfig;
8+
use OCA\Solid\BaseServerConfig;
99

1010
class SolidAdmin implements ISettings {
1111
private IL10N $l;
1212
private IConfig $config;
13+
private BaseServerConfig $serverConfig;
1314

1415
public function __construct(IConfig $config, IL10N $l) {
1516
$this->config = $config;
1617
$this->l = $l;
18+
$this->serverConfig = new BaseServerConfig($config);
1719
}
1820

1921
/**
2022
* @return TemplateResponse
2123
*/
2224
public function getForm() {
2325
$parameters = [
24-
'privateKey' => $this->config->getAppValue('solid','privateKey'),
25-
'encryptionKey' => $this->config->getAppValue('solid','encryptionKey')
26+
'privateKey' => $this->serverConfig->getPrivateKey(),
27+
'encryptionKey' => $this->serverConfig->getEncryptionKey()
2628
];
2729

2830
return new TemplateResponse('solid', 'admin', $parameters, '');

0 commit comments

Comments
 (0)