Skip to content

Commit

Permalink
let us create a base serverconfig to get the settings
Browse files Browse the repository at this point in the history
  • Loading branch information
ylebre committed Oct 4, 2024
1 parent 7c5b82f commit 3c93419
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 58 deletions.
70 changes: 70 additions & 0 deletions solid/lib/BaseServerConfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php
namespace OCA\Solid;

use OCP\IConfig;

class BaseServerConfig {
private IConfig $config;

/**
* @param IConfig $config
*/
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;
}
}

58 changes: 3 additions & 55 deletions solid/lib/ServerConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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;
}
}
8 changes: 5 additions & 3 deletions solid/lib/Settings/SolidAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,26 @@
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);
}

/**
* @return TemplateResponse
*/
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, '');
Expand Down

0 comments on commit 3c93419

Please sign in to comment.