Skip to content

Commit

Permalink
Split up KeyStorage into KeyStorage and MetaDataStorage
Browse files Browse the repository at this point in the history
Signed-off-by: Georg Ehrke <developer@georgehrke.com>
  • Loading branch information
georgehrke committed Jul 8, 2020
1 parent fa84404 commit 58a69e4
Show file tree
Hide file tree
Showing 10 changed files with 1,718 additions and 303 deletions.
11 changes: 11 additions & 0 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
use OCA\EndToEndEncryption\Capabilities;
use OCA\EndToEndEncryption\Connector\Sabre\LockPlugin;
use OCA\EndToEndEncryption\EncryptionManager;
use OCA\EndToEndEncryption\IKeyStorage;
use OCA\EndToEndEncryption\IMetaDataStorage;
use OCA\EndToEndEncryption\KeyStorage;
use OCA\EndToEndEncryption\MetaDataStorage;
use OCA\EndToEndEncryption\UserManager;
use OCA\Files_Trashbin\Events\MoveToTrashEvent;
use OCA\Files_Versions\Events\CreateVersionEvent;
Expand All @@ -46,6 +50,13 @@ public function __construct(array $urlParams = []) {
parent::__construct(self::APP_ID, $urlParams);

$container = $this->getContainer();
$container->registerService(IKeyStorage::class, static function ($c) {
return $c->query(KeyStorage::class);
});
$container->registerService(IMetaDataStorage::class, static function ($c) {
return $c->query(MetaDataStorage::class);
});

$container->registerCapability(Capabilities::class);
}

Expand Down
23 changes: 15 additions & 8 deletions lib/Controller/RequestHandlerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
use OCA\EndToEndEncryption\Exceptions\KeyExistsException;
use OCA\EndToEndEncryption\Exceptions\MetaDataExistsException;
use OCA\EndToEndEncryption\Exceptions\MissingMetaDataException;
use OCA\EndToEndEncryption\KeyStorage;
use OCA\EndToEndEncryption\IKeyStorage;
use OCA\EndToEndEncryption\IMetaDataStorage;
use OCA\EndToEndEncryption\LockManager;
use OCA\EndToEndEncryption\SignatureHandler;
use OCP\AppFramework\Http;
Expand Down Expand Up @@ -65,9 +66,12 @@ class RequestHandlerController extends OCSController {
/** @var string */
private $userId;

/** @var KeyStorage */
/** @var IKeyStorage */
private $keyStorage;

/** @var IMetaDataStorage */
private $metaDataStorage;

/** @var SignatureHandler */
private $signatureHandler;

Expand All @@ -89,7 +93,8 @@ class RequestHandlerController extends OCSController {
* @param string $AppName
* @param IRequest $request
* @param string $UserId
* @param KeyStorage $keyStorage
* @param IKeyStorage $keyStorage
* @param IMetaDataStorage $metaDataStorage
* @param SignatureHandler $signatureHandler
* @param EncryptionManager $manager
* @param LockManager $lockManager
Expand All @@ -99,7 +104,8 @@ class RequestHandlerController extends OCSController {
public function __construct($AppName,
IRequest $request,
$UserId,
KeyStorage $keyStorage,
IKeyStorage $keyStorage,
IMetaDataStorage $metaDataStorage,
SignatureHandler $signatureHandler,
EncryptionManager $manager,
LockManager $lockManager,
Expand All @@ -109,6 +115,7 @@ public function __construct($AppName,
parent::__construct($AppName, $request);
$this->userId = $UserId;
$this->keyStorage = $keyStorage;
$this->metaDataStorage = $metaDataStorage;
$this->signatureHandler = $signatureHandler;
$this->manager = $manager;
$this->logger = $logger;
Expand Down Expand Up @@ -306,7 +313,7 @@ public function deletePublicKey(): ?DataResponse {
*/
public function getMetaData(int $id): DataResponse {
try {
$metaData = $this->keyStorage->getMetaData($id);
$metaData = $this->metaDataStorage->getMetaData($id);
} catch (NotFoundException $e) {
throw new OCSNotFoundException($this->l->t('Could not find metadata for "%s"', [$id]));
} catch (Exception $e) {
Expand All @@ -331,7 +338,7 @@ public function getMetaData(int $id): DataResponse {
*/
public function setMetaData(int $id, string $metaData): DataResponse {
try {
$this->keyStorage->setMetaData($id, $metaData);
$this->metaDataStorage->setMetaData($id, $metaData);
} catch (MetaDataExistsException $e) {
return new DataResponse([], Http::STATUS_CONFLICT);
} catch (NotFoundException $e) {
Expand Down Expand Up @@ -366,7 +373,7 @@ public function updateMetaData(int $id, string $metaData): DataResponse {
}

try {
$this->keyStorage->updateMetaData($id, $metaData);
$this->metaDataStorage->updateMetaData($id, $metaData);
} catch (MissingMetaDataException $e) {
throw new OCSNotFoundException($this->l->t("Metadata-file doesn\'t exist"));
} catch (NotFoundException $e) {
Expand Down Expand Up @@ -394,7 +401,7 @@ public function updateMetaData(int $id, string $metaData): DataResponse {
*/
public function deleteMetaData(int $id): DataResponse {
try {
$this->keyStorage->deleteMetaData($id);
$this->metaDataStorage->deleteMetaData($id);
} catch (NotFoundException $e) {
throw new OCSNotFoundException($this->l->t('Could not find metadata for "%s"', [$id]));
} catch (NotPermittedException $e) {
Expand Down
138 changes: 138 additions & 0 deletions lib/IKeyStorage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
<?php

declare(strict_types=1);
/**
* @copyright Copyright (c) 2020 Georg Ehrke <georg-nextcloud@ehrke.email>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\EndToEndEncryption;

use OCA\EndToEndEncryption\Exceptions\KeyExistsException;
use OCP\Files\ForbiddenException;
use OCP\Files\NotFoundException;
use OCP\Files\NotPermittedException;
use OCP\IUser;

/**
* Interface IKeyStorage
*
* @package OCA\EndToEndEncryption
*/
interface IKeyStorage {

/**
* Get users public key
*
* @param string $uid
* @return string
*
* @throws NotFoundException
* @throws NotPermittedException
*/
public function getPublicKey(string $uid): string;

/**
* Check if a public key exists
*
* @param string $uid
* @return bool
*
* @throws NotFoundException
* @throws NotPermittedException
*/
public function publicKeyExists(string $uid): bool;

/**
* store public key
*
* @param string $publicKey
* @param string $uid
*
* @throws KeyExistsException
* @throws NotFoundException
* @throws NotPermittedException
*/
public function setPublicKey(string $publicKey, string $uid): void;

/**
* delete the users public key
*
* @param string $uid
*
* @throws NotPermittedException
* @throws NotFoundException
*/
public function deletePublicKey(string $uid): void;

/**
* get users private key
*
* @param string $uid
* @return string
*
* @throws NotFoundException
* @throws NotPermittedException
* @throws ForbiddenException
*/
public function getPrivateKey(string $uid): string;

/**
* check if a private key exists
*
* @param string $uid
* @return bool
*
* @throws NotFoundException
* @throws NotPermittedException
* @throws ForbiddenException
*/
public function privateKeyExists(string $uid): bool;

/**
* store private key
*
* @param string $privateKey
* @param string $uid
*
* @throws KeyExistsException
* @throws NotFoundException
* @throws NotPermittedException
* @throws ForbiddenException
*/
public function setPrivateKey(string $privateKey, string $uid): void;

/**
* get users private key
*
* @param string $uid
*
* @throws NotPermittedException
* @throws NotFoundException
*/
public function deletePrivateKey(string $uid): void;

/**
* delete all user private and public key permanently
*
* @param IUser $user
*
* @throws NotFoundException
* @throws NotPermittedException
*/
public function deleteUserKeys(IUser $user): void;
}
81 changes: 81 additions & 0 deletions lib/IMetaDataStorage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

declare(strict_types=1);
/**
* @copyright Copyright (c) 2020 Georg Ehrke <georg-nextcloud@ehrke.email>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\EndToEndEncryption;

use OCA\EndToEndEncryption\Exceptions\MetaDataExistsException;
use OCA\EndToEndEncryption\Exceptions\MissingMetaDataException;
use OCP\Files\NotFoundException;
use OCP\Files\NotPermittedException;

/**
* Interface IMetaDataStorage
*
* @package OCA\EndToEndEncryption
*/
interface IMetaDataStorage {

/**
* get meta data file
*
* @param int $id
* @return string
*
* @throws NotPermittedException
* @throws NotFoundException
*/
public function getMetaData(int $id): string;

/**
* set meta data file
*
* @param int $id file id
* @param string $metaData
*
* @throws NotPermittedException
* @throws NotFoundException
* @throws MetaDataExistsException
*/
public function setMetaData(int $id, string $metaData): void;

/**
* update meta data file
*
* @param int $id file id
* @param string $fileKey
*
* @throws NotPermittedException
* @throws NotFoundException
* @throws MissingMetaDataException
*/
public function updateMetaData(int $id, string $fileKey): void;

/**
* delete meta data file
*
* @param int $id
*
* @throws NotPermittedException
* @throws NotFoundException
*/
public function deleteMetaData(int $id): void;
}
Loading

0 comments on commit 58a69e4

Please sign in to comment.