diff options
author | Jonas <jonas@freesources.org> | 2024-07-08 11:29:26 +0200 |
---|---|---|
committer | Jonas <jonas@freesources.org> | 2024-07-17 12:56:41 +0200 |
commit | 1671bf3ef219dd641b51b4d154ea701c6a7cf6b9 (patch) | |
tree | 70b971f9fea1968d7ec3c48b71dd5d1522500922 /lib | |
parent | b06ce832d8f280b9c008b91c41757e8eab37dc77 (diff) | |
download | nextcloud-server-1671bf3ef219dd641b51b4d154ea701c6a7cf6b9.tar.gz nextcloud-server-1671bf3ef219dd641b51b4d154ea701c6a7cf6b9.zip |
feat(Reference): Add public API endpoints to get references
Calling the public API endpoints will check for matching registered
reference providers that implement `IPublicReferenceProvider` and call
their respective functions. If no matching provider is found, the
default `LinkReferenceProvider` will be used to provide open graph data.
The frontend reference widget components will call these endpoints from
unauthorized sessions, e.g. in public shares.
If present, the sharing token of the origin URL is passed to
`resolveReferencePublic()` as additional information for the reference
provider to determine the access scope. This allows the respective
reference providers to determine whether the origin share has access to
the linked resource.
`getCacheKeyPublic` also gets the sharing token so it can scope the cached
entry to it.
Contributes to #45978
Signed-off-by: Jonas <jonas@freesources.org>
Diffstat (limited to 'lib')
6 files changed, 81 insertions, 14 deletions
diff --git a/lib/composer/composer/autoload_classmap.php b/lib/composer/composer/autoload_classmap.php index b0b60b3e0bb..4c2c6d8dd0e 100644 --- a/lib/composer/composer/autoload_classmap.php +++ b/lib/composer/composer/autoload_classmap.php @@ -189,6 +189,7 @@ return array( 'OCP\\Collaboration\\Collaborators\\SearchResultType' => $baseDir . '/lib/public/Collaboration/Collaborators/SearchResultType.php', 'OCP\\Collaboration\\Reference\\ADiscoverableReferenceProvider' => $baseDir . '/lib/public/Collaboration/Reference/ADiscoverableReferenceProvider.php', 'OCP\\Collaboration\\Reference\\IDiscoverableReferenceProvider' => $baseDir . '/lib/public/Collaboration/Reference/IDiscoverableReferenceProvider.php', + 'OCP\\Collaboration\\Reference\\IPublicReferenceProvider' => $baseDir . '/lib/public/Collaboration/Reference/IPublicReferenceProvider.php', 'OCP\\Collaboration\\Reference\\IReference' => $baseDir . '/lib/public/Collaboration/Reference/IReference.php', 'OCP\\Collaboration\\Reference\\IReferenceManager' => $baseDir . '/lib/public/Collaboration/Reference/IReferenceManager.php', 'OCP\\Collaboration\\Reference\\IReferenceProvider' => $baseDir . '/lib/public/Collaboration/Reference/IReferenceProvider.php', diff --git a/lib/composer/composer/autoload_static.php b/lib/composer/composer/autoload_static.php index 648b736bb77..edc94a0cb78 100644 --- a/lib/composer/composer/autoload_static.php +++ b/lib/composer/composer/autoload_static.php @@ -222,6 +222,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2 'OCP\\Collaboration\\Collaborators\\SearchResultType' => __DIR__ . '/../../..' . '/lib/public/Collaboration/Collaborators/SearchResultType.php', 'OCP\\Collaboration\\Reference\\ADiscoverableReferenceProvider' => __DIR__ . '/../../..' . '/lib/public/Collaboration/Reference/ADiscoverableReferenceProvider.php', 'OCP\\Collaboration\\Reference\\IDiscoverableReferenceProvider' => __DIR__ . '/../../..' . '/lib/public/Collaboration/Reference/IDiscoverableReferenceProvider.php', + 'OCP\\Collaboration\\Reference\\IPublicReferenceProvider' => __DIR__ . '/../../..' . '/lib/public/Collaboration/Reference/IPublicReferenceProvider.php', 'OCP\\Collaboration\\Reference\\IReference' => __DIR__ . '/../../..' . '/lib/public/Collaboration/Reference/IReference.php', 'OCP\\Collaboration\\Reference\\IReferenceManager' => __DIR__ . '/../../..' . '/lib/public/Collaboration/Reference/IReferenceManager.php', 'OCP\\Collaboration\\Reference\\IReferenceProvider' => __DIR__ . '/../../..' . '/lib/public/Collaboration/Reference/IReferenceProvider.php', diff --git a/lib/private/Collaboration/Reference/ReferenceManager.php b/lib/private/Collaboration/Reference/ReferenceManager.php index 208c50a074b..5a1b39d9dff 100644 --- a/lib/private/Collaboration/Reference/ReferenceManager.php +++ b/lib/private/Collaboration/Reference/ReferenceManager.php @@ -11,6 +11,7 @@ namespace OC\Collaboration\Reference; use OC\AppFramework\Bootstrap\Coordinator; use OC\Collaboration\Reference\File\FileReferenceProvider; use OCP\Collaboration\Reference\IDiscoverableReferenceProvider; +use OCP\Collaboration\Reference\IPublicReferenceProvider; use OCP\Collaboration\Reference\IReference; use OCP\Collaboration\Reference\IReferenceManager; use OCP\Collaboration\Reference\IReferenceProvider; @@ -59,14 +60,14 @@ class ReferenceManager implements IReferenceManager { /** * Try to get a cached reference object from a reference string */ - public function getReferenceFromCache(string $referenceId): ?IReference { - $matchedProvider = $this->getMatchedProvider($referenceId); + public function getReferenceFromCache(string $referenceId, bool $public = false, string $sharingToken = ''): ?IReference { + $matchedProvider = $this->getMatchedProvider($referenceId, $public); if ($matchedProvider === null) { return null; } - $cacheKey = $this->getFullCacheKey($matchedProvider, $referenceId); + $cacheKey = $this->getFullCacheKey($matchedProvider, $referenceId, $public, $sharingToken); return $this->getReferenceByCacheKey($cacheKey); } @@ -86,20 +87,25 @@ class ReferenceManager implements IReferenceManager { * Get a reference object from a reference string with a matching provider * Use a cached reference if possible */ - public function resolveReference(string $referenceId): ?IReference { - $matchedProvider = $this->getMatchedProvider($referenceId); + public function resolveReference(string $referenceId, bool $public = false, $sharingToken = ''): ?IReference { + $matchedProvider = $this->getMatchedProvider($referenceId, $public); if ($matchedProvider === null) { return null; } - $cacheKey = $this->getFullCacheKey($matchedProvider, $referenceId); + $cacheKey = $this->getFullCacheKey($matchedProvider, $referenceId, $public, $sharingToken); $cached = $this->cache->get($cacheKey); if ($cached) { return Reference::fromCache($cached); } - $reference = $matchedProvider->resolveReference($referenceId); + $reference = null; + if ($public && $matchedProvider instanceof IPublicReferenceProvider) { + $reference = $matchedProvider->resolveReferencePublic($referenceId, $sharingToken); + } elseif ($matchedProvider instanceof IReferenceProvider) { + $reference = $matchedProvider->resolveReference($referenceId); + } if ($reference) { $cachePrefix = $matchedProvider->getCachePrefix($referenceId); if ($cachePrefix !== '') { @@ -117,11 +123,14 @@ class ReferenceManager implements IReferenceManager { * Try to match a reference string with all the registered providers * Fallback to the link reference provider (using OpenGraph) * - * @return IReferenceProvider|null the first matching provider + * @return IReferenceProvider|IPublicReferenceProvider|null the first matching provider */ - private function getMatchedProvider(string $referenceId): ?IReferenceProvider { + private function getMatchedProvider(string $referenceId, bool $public): null|IReferenceProvider|IPublicReferenceProvider { $matchedProvider = null; foreach ($this->getProviders() as $provider) { + if ($public && !($provider instanceof IPublicReferenceProvider)) { + continue; + } $matchedProvider = $provider->matchReference($referenceId) ? $provider : null; if ($matchedProvider !== null) { break; @@ -138,8 +147,13 @@ class ReferenceManager implements IReferenceManager { /** * Get a hashed full cache key from a key and prefix given by a provider */ - private function getFullCacheKey(IReferenceProvider $provider, string $referenceId): string { - $cacheKey = $provider->getCacheKey($referenceId); + private function getFullCacheKey(IReferenceProvider $provider, string $referenceId, bool $public, string $sharingToken): string { + if ($public && !($provider instanceof IPublicReferenceProvider)) { + throw new \RuntimeException('Provider doesn\'t support public lookups'); + } + $cacheKey = $public + ? $provider->getCacheKeyPublic($referenceId, $sharingToken) + : $provider->getCacheKey($referenceId); return md5($provider->getCachePrefix($referenceId)) . ( $cacheKey !== null ? ('-' . md5($cacheKey)) : '' ); diff --git a/lib/public/Collaboration/Reference/IPublicReferenceProvider.php b/lib/public/Collaboration/Reference/IPublicReferenceProvider.php new file mode 100644 index 00000000000..db6c3d3828b --- /dev/null +++ b/lib/public/Collaboration/Reference/IPublicReferenceProvider.php @@ -0,0 +1,33 @@ +<?php + +declare(strict_types=1); +/** + * SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +namespace OCP\Collaboration\Reference; + +/** + * @since 30.0.0 + */ +interface IPublicReferenceProvider extends IReferenceProvider { + /** + * Return a reference with its metadata for a given reference identifier and sharingToken + * + * @since 30.0.0 + */ + public function resolveReferencePublic(string $referenceText, string $sharingToken): ?IReference; + + /** + * Return a custom cache key to be used for caching the metadata + * This could be for example the current sharingToken if the reference + * access permissions are different for each share + * + * Should return null, if the cache is only related to the + * reference id and has no further dependency + * + * @since 30.0.0 + */ + public function getCacheKeyPublic(string $referenceId, string $sharingToken): ?string; +} diff --git a/lib/public/Collaboration/Reference/IReferenceManager.php b/lib/public/Collaboration/Reference/IReferenceManager.php index 4a7de266435..c3cf7ca8e7b 100644 --- a/lib/public/Collaboration/Reference/IReferenceManager.php +++ b/lib/public/Collaboration/Reference/IReferenceManager.php @@ -27,8 +27,9 @@ interface IReferenceManager { * but may still return null in case this is disabled or the fetching fails * * @since 25.0.0 + * @since 30.0.0 optional arguments `$public` and `$sharingToken` */ - public function resolveReference(string $referenceId): ?IReference; + public function resolveReference(string $referenceId, bool $public = false, string $sharingToken = ''): ?IReference; /** * Get a reference by its cache key @@ -42,8 +43,9 @@ interface IReferenceManager { * the cache can then be filled with a separate request from the frontend * * @since 25.0.0 + * @since 30.0.0 optional arguments `$public` and `$sharingToken` */ - public function getReferenceFromCache(string $referenceId): ?IReference; + public function getReferenceFromCache(string $referenceId, bool $public = false, string $sharingToken = ''): ?IReference; /** * Invalidate all cache entries with a prefix or just one if the cache key is provided diff --git a/lib/public/Collaboration/Reference/LinkReferenceProvider.php b/lib/public/Collaboration/Reference/LinkReferenceProvider.php index 79b5164950c..a7f54d02c04 100644 --- a/lib/public/Collaboration/Reference/LinkReferenceProvider.php +++ b/lib/public/Collaboration/Reference/LinkReferenceProvider.php @@ -26,7 +26,7 @@ use Psr\Log\LoggerInterface; /** * @since 29.0.0 */ -class LinkReferenceProvider implements IReferenceProvider { +class LinkReferenceProvider implements IReferenceProvider, IPublicReferenceProvider { /** * for image size and webpage header @@ -88,6 +88,14 @@ class LinkReferenceProvider implements IReferenceProvider { } /** + * @inheritDoc + * @since 30.0.0 + */ + public function resolveReferencePublic(string $referenceText, string $sharingToken): ?IReference { + return $this->resolveReference($referenceText); + } + + /** * Populates the reference with OpenGraph data * * @param Reference $reference @@ -201,4 +209,12 @@ class LinkReferenceProvider implements IReferenceProvider { public function getCacheKey(string $referenceId): ?string { return null; } + + /** + * @inheritDoc + * @since 30.0.0 + */ + public function getCacheKeyPublic(string $referenceId, string $sharingToken): ?string { + return null; + } } |