From 27cfa298c52cb6160c673a8c1d693f2d85ca040b Mon Sep 17 00:00:00 2001 From: Matthieu Gallien Date: Wed, 26 Jun 2024 11:34:05 +0200 Subject: [PATCH] add a new API to allow user to upload an existing certificate is needed to be able to setup sharing when an user has an existing certificate that may have been created outside of Nextcloud end-to-end encryption app that would for example apply when an external certificate authority is in use to deliver user certificates for end-to-end encryption Signed-off-by: Matthieu Gallien --- appinfo/routes.php | 1 + lib/Controller/KeyController.php | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/appinfo/routes.php b/appinfo/routes.php index 1e7d0e50..0fb07f5f 100644 --- a/appinfo/routes.php +++ b/appinfo/routes.php @@ -30,6 +30,7 @@ ['name' => 'Key#setPrivateKey', 'url' => '/api/v{apiVersion}/private-key', 'verb' => 'POST', 'requirements' => ['apiVersion' => '[1-2]']], ['name' => 'Key#getPrivateKey', 'url' => '/api/v{apiVersion}/private-key', 'verb' => 'GET', 'requirements' => ['apiVersion' => '[1-2]']], ['name' => 'Key#deletePrivateKey', 'url' => '/api/v{apiVersion}/private-key', 'verb' => 'DELETE', 'requirements' => ['apiVersion' => '[1-2]']], + ['name' => 'Key#setPublicKey', 'url' => '/api/v{apiVersion}/public-key', 'verb' => 'PUT', 'requirements' => ['apiVersion' => '[1-2]']], ['name' => 'Key#createPublicKey', 'url' => '/api/v{apiVersion}/public-key', 'verb' => 'POST', 'requirements' => ['apiVersion' => '[1-2]']], ['name' => 'Key#getPublicKeys', 'url' => '/api/v{apiVersion}/public-key', 'verb' => 'GET', 'requirements' => ['apiVersion' => '[1-2]']], ['name' => 'Key#deletePublicKey', 'url' => '/api/v{apiVersion}/public-key', 'verb' => 'DELETE', 'requirements' => ['apiVersion' => '[1-2]']], diff --git a/lib/Controller/KeyController.php b/lib/Controller/KeyController.php index 6694963f..541bc45c 100644 --- a/lib/Controller/KeyController.php +++ b/lib/Controller/KeyController.php @@ -205,6 +205,26 @@ public function createPublicKey(string $csr): DataResponse { return new DataResponse(['public-key' => $publicKey]); } + /** + * Set public key + * + * @NoAdminRequired + * @E2ERestrictUserAgent + * @throws OCSBadRequestException + */ + public function setPublicKey(string $publicKey): DataResponse { + try { + $this->keyStorage->setPublicKey($publicKey, $this->userId); + } catch (KeyExistsException $e) { + return new DataResponse([], Http::STATUS_CONFLICT); + } catch (Exception $e) { + $this->logger->error("Fail to set user public key", ['exception' => $e, 'app' => $this->appName]); + throw new OCSBadRequestException($this->l10n->t('Internal error')); + } + + return new DataResponse(['public-key' => $publicKey]); + } + /** * Delete the users public key *