diff options
Diffstat (limited to 'lib/private/OCM/Model')
-rw-r--r-- | lib/private/OCM/Model/OCMProvider.php | 141 | ||||
-rw-r--r-- | lib/private/OCM/Model/OCMResource.php | 33 |
2 files changed, 110 insertions, 64 deletions
diff --git a/lib/private/OCM/Model/OCMProvider.php b/lib/private/OCM/Model/OCMProvider.php index 084d4f8479d..be13d65a40f 100644 --- a/lib/private/OCM/Model/OCMProvider.php +++ b/lib/private/OCM/Model/OCMProvider.php @@ -3,51 +3,41 @@ declare(strict_types=1); /** - * @copyright 2023, Maxence Lange <maxence@artificial-owl.com> - * - * @author Maxence Lange <maxence@artificial-owl.com> - * - * @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/>. - * + * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ namespace OC\OCM\Model; +use NCU\Security\Signature\Model\Signatory; use OCP\EventDispatcher\IEventDispatcher; +use OCP\IConfig; use OCP\OCM\Events\ResourceTypeRegisterEvent; use OCP\OCM\Exceptions\OCMArgumentException; use OCP\OCM\Exceptions\OCMProviderException; -use OCP\OCM\IOCMProvider; +use OCP\OCM\ICapabilityAwareOCMProvider; use OCP\OCM\IOCMResource; /** * @since 28.0.0 */ -class OCMProvider implements IOCMProvider { +class OCMProvider implements ICapabilityAwareOCMProvider { + private string $provider; private bool $enabled = false; private string $apiVersion = ''; + private string $inviteAcceptDialog = ''; + private array $capabilities = []; private string $endPoint = ''; /** @var IOCMResource[] */ private array $resourceTypes = []; - + private ?Signatory $signatory = null; private bool $emittedEvent = false; public function __construct( protected IEventDispatcher $dispatcher, + protected IConfig $config, ) { + $this->provider = 'Nextcloud ' . $config->getSystemValue('version'); } /** @@ -87,6 +77,30 @@ class OCMProvider implements IOCMProvider { } /** + * returns the invite accept dialog + * + * @return string + * @since 32.0.0 + */ + public function getInviteAcceptDialog(): string { + return $this->inviteAcceptDialog; + } + + /** + * set the invite accept dialog + * + * @param string $inviteAcceptDialog + * + * @return $this + * @since 32.0.0 + */ + public function setInviteAcceptDialog(string $inviteAcceptDialog): static { + $this->inviteAcceptDialog = $inviteAcceptDialog; + + return $this; + } + + /** * @param string $endPoint * * @return $this @@ -105,6 +119,34 @@ class OCMProvider implements IOCMProvider { } /** + * @return string + */ + public function getProvider(): string { + return $this->provider; + } + + /** + * @param array $capabilities + * + * @return $this + */ + public function setCapabilities(array $capabilities): static { + foreach ($capabilities as $value) { + if (!in_array($value, $this->capabilities)) { + array_push($this->capabilities, $value); + } + } + + return $this; + } + + /** + * @return array + */ + public function getCapabilities(): array { + return $this->capabilities; + } + /** * create a new resource to later add it with {@see IOCMProvider::addResourceType()} * @return IOCMResource */ @@ -169,19 +211,27 @@ class OCMProvider implements IOCMProvider { throw new OCMArgumentException('resource not found'); } + public function setSignatory(Signatory $signatory): void { + $this->signatory = $signatory; + } + + public function getSignatory(): ?Signatory { + return $this->signatory; + } + /** * import data from an array * * @param array $data * - * @return $this + * @return OCMProvider&static * @throws OCMProviderException in case a descent provider cannot be generated from data - * @see self::jsonSerialize() */ public function import(array $data): static { $this->setEnabled(is_bool($data['enabled'] ?? '') ? $data['enabled'] : false) - ->setApiVersion((string)($data['apiVersion'] ?? '')) - ->setEndPoint($data['endPoint'] ?? ''); + // Fall back to old apiVersion for Nextcloud 30 compatibility + ->setApiVersion((string)($data['version'] ?? $data['apiVersion'] ?? '')) + ->setEndPoint($data['endPoint'] ?? ''); $resources = []; foreach (($data['resourceTypes'] ?? []) as $resourceData) { @@ -190,6 +240,16 @@ class OCMProvider implements IOCMProvider { } $this->setResourceTypes($resources); + if (isset($data['publicKey'])) { + // import details about the remote request signing public key, if available + $signatory = new Signatory(); + $signatory->setKeyId($data['publicKey']['keyId'] ?? ''); + $signatory->setPublicKey($data['publicKey']['publicKeyPem'] ?? ''); + if ($signatory->getKeyId() !== '' && $signatory->getPublicKey() !== '') { + $this->setSignatory($signatory); + } + } + if (!$this->looksValid()) { throw new OCMProviderException('remote provider does not look valid'); } @@ -205,18 +265,8 @@ class OCMProvider implements IOCMProvider { return ($this->getApiVersion() !== '' && $this->getEndPoint() !== ''); } - /** - * @return array{ - * enabled: bool, - * apiVersion: string, - * endPoint: string, - * resourceTypes: array{ - * name: string, - * shareTypes: string[], - * protocols: array<string, string> - * }[] - * } + * @since 28.0.0 */ public function jsonSerialize(): array { $resourceTypes = []; @@ -224,11 +274,24 @@ class OCMProvider implements IOCMProvider { $resourceTypes[] = $res->jsonSerialize(); } - return [ + $response = [ 'enabled' => $this->isEnabled(), - 'apiVersion' => $this->getApiVersion(), + 'apiVersion' => '1.0-proposal1', // deprecated, but keep it to stay compatible with old version + 'version' => $this->getApiVersion(), // informative but real version 'endPoint' => $this->getEndPoint(), + 'publicKey' => $this->getSignatory()?->jsonSerialize(), 'resourceTypes' => $resourceTypes ]; + + $capabilities = $this->getCapabilities(); + $inviteAcceptDialog = $this->getInviteAcceptDialog(); + if ($capabilities) { + $response['capabilities'] = $capabilities; + } + if ($inviteAcceptDialog) { + $response['inviteAcceptDialog'] = $inviteAcceptDialog; + } + return $response; + } } diff --git a/lib/private/OCM/Model/OCMResource.php b/lib/private/OCM/Model/OCMResource.php index c4a91f2eabf..3d619db1927 100644 --- a/lib/private/OCM/Model/OCMResource.php +++ b/lib/private/OCM/Model/OCMResource.php @@ -3,25 +3,8 @@ declare(strict_types=1); /** - * @copyright 2023, Maxence Lange <maxence@artificial-owl.com> - * - * @author Maxence Lange <maxence@artificial-owl.com> - * - * @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/>. - * + * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ namespace OC\OCM\Model; @@ -33,7 +16,7 @@ use OCP\OCM\IOCMResource; */ class OCMResource implements IOCMResource { private string $name = ''; - /** @var string[] */ + /** @var list<string> */ private array $shareTypes = []; /** @var array<string, string> */ private array $protocols = []; @@ -57,7 +40,7 @@ class OCMResource implements IOCMResource { } /** - * @param string[] $shareTypes + * @param list<string> $shareTypes * * @return $this */ @@ -68,7 +51,7 @@ class OCMResource implements IOCMResource { } /** - * @return string[] + * @return list<string> */ public function getShareTypes(): array { return $this->shareTypes; @@ -102,14 +85,14 @@ class OCMResource implements IOCMResource { */ public function import(array $data): static { return $this->setName((string)($data['name'] ?? '')) - ->setShareTypes($data['shareTypes'] ?? []) - ->setProtocols($data['protocols'] ?? []); + ->setShareTypes($data['shareTypes'] ?? []) + ->setProtocols($data['protocols'] ?? []); } /** * @return array{ * name: string, - * shareTypes: string[], + * shareTypes: list<string>, * protocols: array<string, string> * } */ |