|
|
@ -11,11 +11,19 @@ namespace OC\OCM; |
|
|
|
|
|
|
|
use GuzzleHttp\Exception\ConnectException; |
|
|
|
use JsonException; |
|
|
|
use NCU\Security\Signature\Exceptions\IdentityNotFoundException; |
|
|
|
use NCU\Security\Signature\Exceptions\SignatoryException; |
|
|
|
use OC\Core\AppInfo\ConfigLexicon; |
|
|
|
use OC\OCM\Model\OCMProvider; |
|
|
|
use OCP\AppFramework\Http; |
|
|
|
use OCP\EventDispatcher\IEventDispatcher; |
|
|
|
use OCP\Http\Client\IClientService; |
|
|
|
use OCP\IAppConfig; |
|
|
|
use OCP\ICache; |
|
|
|
use OCP\ICacheFactory; |
|
|
|
use OCP\IConfig; |
|
|
|
use OCP\IURLGenerator; |
|
|
|
use OCP\OCM\Events\ResourceTypeRegisterEvent; |
|
|
|
use OCP\OCM\Exceptions\OCMProviderException; |
|
|
|
use OCP\OCM\ICapabilityAwareOCMProvider; |
|
|
|
use OCP\OCM\IOCMDiscoveryService; |
|
|
@ -26,18 +34,25 @@ use Psr\Log\LoggerInterface; |
|
|
|
*/ |
|
|
|
class OCMDiscoveryService implements IOCMDiscoveryService { |
|
|
|
private ICache $cache; |
|
|
|
public const API_VERSION = '1.1.0'; |
|
|
|
|
|
|
|
private ?ICapabilityAwareOCMProvider $localProvider = null; |
|
|
|
/** @var array<string, ICapabilityAwareOCMProvider> */ |
|
|
|
private array $remoteProviders = []; |
|
|
|
|
|
|
|
public function __construct( |
|
|
|
ICacheFactory $cacheFactory, |
|
|
|
private IClientService $clientService, |
|
|
|
private IConfig $config, |
|
|
|
private ICapabilityAwareOCMProvider $provider, |
|
|
|
private IEventDispatcher $eventDispatcher, |
|
|
|
protected IConfig $config, |
|
|
|
private IAppConfig $appConfig, |
|
|
|
private IURLGenerator $urlGenerator, |
|
|
|
private OCMSignatoryManager $ocmSignatoryManager, |
|
|
|
private LoggerInterface $logger, |
|
|
|
) { |
|
|
|
$this->cache = $cacheFactory->createDistributed('ocm-discovery'); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* @param string $remote |
|
|
|
* @param bool $skipCache |
|
|
@ -56,6 +71,12 @@ class OCMDiscoveryService implements IOCMDiscoveryService { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if (array_key_exists($remote, $this->remoteProviders)) { |
|
|
|
return $this->remoteProviders[$remote]; |
|
|
|
} |
|
|
|
|
|
|
|
$provider = new OCMProvider(); |
|
|
|
|
|
|
|
if (!$skipCache) { |
|
|
|
try { |
|
|
|
$cached = $this->cache->get($remote); |
|
|
@ -63,10 +84,11 @@ class OCMDiscoveryService implements IOCMDiscoveryService { |
|
|
|
throw new OCMProviderException('Previous discovery failed.'); |
|
|
|
} |
|
|
|
|
|
|
|
$this->provider->import(json_decode($cached ?? '', true, 8, JSON_THROW_ON_ERROR) ?? []); |
|
|
|
return $this->provider; |
|
|
|
$provider->import(json_decode($cached ?? '', true, 8, JSON_THROW_ON_ERROR) ?? []); |
|
|
|
$this->remoteProviders[$remote] = $provider; |
|
|
|
return $provider; |
|
|
|
} catch (JsonException|OCMProviderException $e) { |
|
|
|
// we ignore cache on issues
|
|
|
|
$this->logger->warning('cache issue on ocm discovery', ['exception' => $e]); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
@ -81,15 +103,20 @@ class OCMDiscoveryService implements IOCMDiscoveryService { |
|
|
|
} |
|
|
|
$response = $client->get($remote . '/ocm-provider/', $options); |
|
|
|
|
|
|
|
$body = null; |
|
|
|
if ($response->getStatusCode() === Http::STATUS_OK) { |
|
|
|
$body = $response->getBody(); |
|
|
|
// update provider with data returned by the request
|
|
|
|
$this->provider->import(json_decode($body, true, 8, JSON_THROW_ON_ERROR) ?? []); |
|
|
|
$provider->import(json_decode($body, true, 8, JSON_THROW_ON_ERROR) ?? []); |
|
|
|
$this->cache->set($remote, $body, 60 * 60 * 24); |
|
|
|
$this->remoteProviders[$remote] = $provider; |
|
|
|
return $provider; |
|
|
|
} |
|
|
|
} catch (JsonException|OCMProviderException $e) { |
|
|
|
|
|
|
|
throw new OCMProviderException('invalid remote ocm endpoint'); |
|
|
|
} catch (JsonException|OCMProviderException) { |
|
|
|
$this->cache->set($remote, false, 5 * 60); |
|
|
|
throw new OCMProviderException('data returned by remote seems invalid - ' . ($body ?? '')); |
|
|
|
throw new OCMProviderException('data returned by remote seems invalid - status:' . $response->getStatusCode() . ' - ' . ($body ?? '')); |
|
|
|
} catch (\Exception $e) { |
|
|
|
$this->cache->set($remote, false, 5 * 60); |
|
|
|
$this->logger->warning('error while discovering ocm provider', [ |
|
|
@ -98,7 +125,61 @@ class OCMDiscoveryService implements IOCMDiscoveryService { |
|
|
|
]); |
|
|
|
throw new OCMProviderException('error while requesting remote ocm provider'); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return $this->provider; |
|
|
|
/** |
|
|
|
* @return ICapabilityAwareOCMProvider |
|
|
|
*/ |
|
|
|
public function getLocalOCMProvider(bool $fullDetails = true): ICapabilityAwareOCMProvider { |
|
|
|
if ($this->localProvider !== null) { |
|
|
|
return $this->localProvider; |
|
|
|
} |
|
|
|
|
|
|
|
$provider = new OCMProvider('Nextcloud ' . $this->config->getSystemValue('version')); |
|
|
|
if (!$this->appConfig->getValueBool('core', ConfigLexicon::OCM_DISCOVERY_ENABLED)) { |
|
|
|
return $provider; |
|
|
|
} |
|
|
|
|
|
|
|
$url = $this->urlGenerator->linkToRouteAbsolute('cloud_federation_api.requesthandlercontroller.addShare'); |
|
|
|
$pos = strrpos($url, '/'); |
|
|
|
if ($pos === false) { |
|
|
|
$this->logger->debug('generated route should contain a slash character'); |
|
|
|
return $provider; |
|
|
|
} |
|
|
|
|
|
|
|
$provider->setEnabled(true); |
|
|
|
$provider->setApiVersion(self::API_VERSION); |
|
|
|
$provider->setEndPoint(substr($url, 0, $pos)); |
|
|
|
$provider->setCapabilities(['/invite-accepted', '/notifications', '/shares']); |
|
|
|
|
|
|
|
$resource = $provider->createNewResourceType(); |
|
|
|
$resource->setName('file') |
|
|
|
->setShareTypes(['user', 'group']) |
|
|
|
->setProtocols(['webdav' => '/public.php/webdav/']); |
|
|
|
$provider->addResourceType($resource); |
|
|
|
|
|
|
|
if ($fullDetails) { |
|
|
|
// Adding a public key to the ocm discovery
|
|
|
|
try { |
|
|
|
if (!$this->appConfig->getValueBool('core', OCMSignatoryManager::APPCONFIG_SIGN_DISABLED, lazy: true)) { |
|
|
|
/** |
|
|
|
* @experimental 31.0.0 |
|
|
|
* @psalm-suppress UndefinedInterfaceMethod |
|
|
|
*/ |
|
|
|
$provider->setSignatory($this->ocmSignatoryManager->getLocalSignatory()); |
|
|
|
} else { |
|
|
|
$this->logger->debug('ocm public key feature disabled'); |
|
|
|
} |
|
|
|
} catch (SignatoryException|IdentityNotFoundException $e) { |
|
|
|
$this->logger->warning('cannot generate local signatory', ['exception' => $e]); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
$event = new ResourceTypeRegisterEvent($provider); |
|
|
|
$this->eventDispatcher->dispatchTyped($event); |
|
|
|
|
|
|
|
$this->localProvider = $provider; |
|
|
|
return $provider; |
|
|
|
} |
|
|
|
|
|
|
|
} |