From 6deb049245339fb406c83a7de943f0709701d643 Mon Sep 17 00:00:00 2001 From: Sandro Mesterheide Date: Mon, 20 Feb 2023 03:36:17 +0100 Subject: [PATCH] Add cloud id resolver interface Signed-off-by: Sandro Mesterheide --- .../tests/ExternalStorageTest.php | 2 +- lib/private/Federation/CloudIdManager.php | 32 +++++++++++ .../Federation/CloudId.php | 4 +- lib/public/Federation/ICloudIdManager.php | 14 +++++ lib/public/Federation/ICloudIdResolver.php | 55 +++++++++++++++++++ .../Collaborators/LookupPluginTest.php | 2 +- tests/lib/Federation/CloudIdTest.php | 2 +- 7 files changed, 105 insertions(+), 6 deletions(-) rename lib/{private => public}/Federation/CloudId.php (97%) create mode 100644 lib/public/Federation/ICloudIdResolver.php diff --git a/apps/files_sharing/tests/ExternalStorageTest.php b/apps/files_sharing/tests/ExternalStorageTest.php index 7709abbf6eb53..06947ff497937 100644 --- a/apps/files_sharing/tests/ExternalStorageTest.php +++ b/apps/files_sharing/tests/ExternalStorageTest.php @@ -27,7 +27,7 @@ */ namespace OCA\Files_Sharing\Tests; -use OC\Federation\CloudId; +use OCP\Federation\CloudId; use OCP\Http\Client\IClient; use OCP\Http\Client\IClientService; use OCP\Http\Client\IResponse; diff --git a/lib/private/Federation/CloudIdManager.php b/lib/private/Federation/CloudIdManager.php index 85aae8e5ec529..ce16fd7849681 100644 --- a/lib/private/Federation/CloudIdManager.php +++ b/lib/private/Federation/CloudIdManager.php @@ -34,8 +34,10 @@ use OCP\Contacts\IManager; use OCP\EventDispatcher\Event; use OCP\EventDispatcher\IEventDispatcher; +use OCP\Federation\CloudId; use OCP\Federation\ICloudId; use OCP\Federation\ICloudIdManager; +use OCP\Federation\ICloudIdResolver; use OCP\ICache; use OCP\ICacheFactory; use OCP\IURLGenerator; @@ -52,6 +54,8 @@ class CloudIdManager implements ICloudIdManager { private ICache $memCache; /** @var array[] */ private array $cache = []; + /** @var ICloudIdResolver[] */ + private array $cloudIdResolvers = []; public function __construct( IManager $contactsManager, @@ -100,6 +104,12 @@ public function handleCardEvent(Event $event): void { */ public function resolveCloudId(string $cloudId): ICloudId { // TODO magic here to get the url and user instead of just splitting on @ + + foreach ($this->cloudIdResolvers as $resolver) { + if ($resolver->isValidCloudId($cloudId)) { + return $resolver->resolveCloudId($cloudId); + } + } if (!$this->isValidCloudId($cloudId)) { throw new \InvalidArgumentException('Invalid cloud id'); @@ -246,6 +256,28 @@ protected function fixRemoteURL(string $remote): string { * @return bool */ public function isValidCloudId(string $cloudId): bool { + foreach ($this->cloudIdResolvers as $resolver) { + if ($resolver->isValidCloudId($cloudId)) { + return true; + } + } + return strpos($cloudId, '@') !== false; } + + /** + * @param ICloudIdResolver $resolver + */ + public function registerCloudIdResolver(ICloudIdResolver $resolver) { + array_unshift($this->cloudIdResolvers, $resolver); + } + + /** + * @param ICloudIdResolver $resolver + */ + public function unregisterCloudIdResolver(ICloudIdResolver $resolver) { + if (($key = array_search($resolver, $this->cloudIdResolvers)) !== false) { + array_splice($this->cloudIdResolvers, $key, 1); + } + } } diff --git a/lib/private/Federation/CloudId.php b/lib/public/Federation/CloudId.php similarity index 97% rename from lib/private/Federation/CloudId.php rename to lib/public/Federation/CloudId.php index 50e974831a6fe..048d5ae299abe 100644 --- a/lib/private/Federation/CloudId.php +++ b/lib/public/Federation/CloudId.php @@ -25,9 +25,7 @@ * along with this program. If not, see . * */ -namespace OC\Federation; - -use OCP\Federation\ICloudId; +namespace OCP\Federation; class CloudId implements ICloudId { /** @var string */ diff --git a/lib/public/Federation/ICloudIdManager.php b/lib/public/Federation/ICloudIdManager.php index 1612c03ba4af0..0479bb14dcf47 100644 --- a/lib/public/Federation/ICloudIdManager.php +++ b/lib/public/Federation/ICloudIdManager.php @@ -62,4 +62,18 @@ public function getCloudId(string $user, ?string $remote): ICloudId; * @since 12.0.0 */ public function isValidCloudId(string $cloudId): bool; + + /** + * @param ICloudIdResolver $resolver + * + * @since 26.0.0 + */ + public function registerCloudIdResolver(ICloudIdResolver $resolver); + + /** + * @param ICloudIdResolver $resolver + * + * @since 26.0.0 + */ + public function unregisterCloudIdResolver(ICloudIdResolver $resolver); } diff --git a/lib/public/Federation/ICloudIdResolver.php b/lib/public/Federation/ICloudIdResolver.php new file mode 100644 index 0000000000000..5a8a10c21ca08 --- /dev/null +++ b/lib/public/Federation/ICloudIdResolver.php @@ -0,0 +1,55 @@ + + * + * @author Joas Schilling + * @author Robin Appelman + * @author Roeland Jago Douma + * @author Sandro Mesterheide + * + * @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 . + * + */ +namespace OCP\Federation; + +/** + * Interface for resolving federated cloud ids + * + * @since 26.0.0 + */ +interface ICloudIdResolver { + /** + * @param string $cloudId + * @return ICloudId + * @throws \InvalidArgumentException + * + * @since 26.0.0 + */ + public function resolveCloudId(string $cloudId): ICloudId; + + /** + * Check if the input is a correctly formatted cloud id + * + * @param string $cloudId + * @return bool + * + * @since 26.0.0 + */ + public function isValidCloudId(string $cloudId): bool; +} diff --git a/tests/lib/Collaboration/Collaborators/LookupPluginTest.php b/tests/lib/Collaboration/Collaborators/LookupPluginTest.php index 462497c637bf0..dd4d00ebd5d5d 100644 --- a/tests/lib/Collaboration/Collaborators/LookupPluginTest.php +++ b/tests/lib/Collaboration/Collaborators/LookupPluginTest.php @@ -24,7 +24,7 @@ namespace Test\Collaboration\Collaborators; use OC\Collaboration\Collaborators\LookupPlugin; -use OC\Federation\CloudId; +use OCP\Federation\CloudId; use OCP\Collaboration\Collaborators\ISearchResult; use OCP\Collaboration\Collaborators\SearchResultType; use OCP\Federation\ICloudId; diff --git a/tests/lib/Federation/CloudIdTest.php b/tests/lib/Federation/CloudIdTest.php index feb4108efe547..7d6aca22c7a15 100644 --- a/tests/lib/Federation/CloudIdTest.php +++ b/tests/lib/Federation/CloudIdTest.php @@ -21,7 +21,7 @@ namespace Test\Federation; -use OC\Federation\CloudId; +use OCP\Federation\CloudId; use Test\TestCase; class CloudIdTest extends TestCase {