From 3c93419cc2a11c54c560f2dbd27f67b812e7a5f4 Mon Sep 17 00:00:00 2001 From: Yvo Brevoort Date: Fri, 4 Oct 2024 14:38:30 +0200 Subject: [PATCH] let us create a base serverconfig to get the settings --- solid/lib/BaseServerConfig.php | 70 +++++++++++++++++++++++++++++++ solid/lib/ServerConfig.php | 58 ++----------------------- solid/lib/Settings/SolidAdmin.php | 8 ++-- 3 files changed, 78 insertions(+), 58 deletions(-) create mode 100644 solid/lib/BaseServerConfig.php diff --git a/solid/lib/BaseServerConfig.php b/solid/lib/BaseServerConfig.php new file mode 100644 index 0000000..fc0b283 --- /dev/null +++ b/solid/lib/BaseServerConfig.php @@ -0,0 +1,70 @@ +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; + } + } + diff --git a/solid/lib/ServerConfig.php b/solid/lib/ServerConfig.php index ac6332f..328b906 100644 --- a/solid/lib/ServerConfig.php +++ b/solid/lib/ServerConfig.php @@ -4,11 +4,12 @@ use OCP\IConfig; use OCP\IUserManager; use OCP\IUrlGenerator; - + use OCA\Solid\BaseServerConfig; + /** * @package OCA\Solid */ - class ServerConfig { + class ServerConfig extends BaseServerConfig { private IConfig $config; private IUrlGenerator $urlGenerator; private IUserManager $userManager; @@ -24,41 +25,6 @@ public function __construct(IConfig $config, IUrlGenerator $urlGenerator, IUserM $this->urlGenerator = $urlGenerator; } - /** - * @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); - } - /** * @param string $clientId * @return array|null @@ -197,22 +163,4 @@ public function setProfileData($userId, $profileData) { $user->setDisplayName($fields['name']); } } - 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; - } } diff --git a/solid/lib/Settings/SolidAdmin.php b/solid/lib/Settings/SolidAdmin.php index fddbe4e..78701e9 100644 --- a/solid/lib/Settings/SolidAdmin.php +++ b/solid/lib/Settings/SolidAdmin.php @@ -5,15 +5,17 @@ use OCP\IConfig; use OCP\IL10N; use OCP\Settings\ISettings; -use OCA\Solid\ServerConfig; +use OCA\Solid\BaseServerConfig; class SolidAdmin implements ISettings { private IL10N $l; private IConfig $config; + private BaseServerConfig $serverConfig; public function __construct(IConfig $config, IL10N $l) { $this->config = $config; $this->l = $l; + $this->serverConfig = new BaseServerConfig($config); } /** @@ -21,8 +23,8 @@ public function __construct(IConfig $config, IL10N $l) { */ public function getForm() { $parameters = [ - 'privateKey' => $this->config->getAppValue('solid','privateKey'), - 'encryptionKey' => $this->config->getAppValue('solid','encryptionKey') + 'privateKey' => $this->serverConfig->getPrivateKey(), + 'encryptionKey' => $this->serverConfig->getEncryptionKey() ]; return new TemplateResponse('solid', 'admin', $parameters, '');