OCMDiscoveryService.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC\OCM;
  8. use JsonException;
  9. use OCP\AppFramework\Http;
  10. use OCP\Http\Client\IClientService;
  11. use OCP\ICache;
  12. use OCP\ICacheFactory;
  13. use OCP\IConfig;
  14. use OCP\OCM\Exceptions\OCMProviderException;
  15. use OCP\OCM\IOCMDiscoveryService;
  16. use OCP\OCM\IOCMProvider;
  17. use Psr\Log\LoggerInterface;
  18. /**
  19. * @since 28.0.0
  20. */
  21. class OCMDiscoveryService implements IOCMDiscoveryService {
  22. private ICache $cache;
  23. private array $supportedAPIVersion =
  24. [
  25. '1.0-proposal1',
  26. '1.0',
  27. '1.1'
  28. ];
  29. public function __construct(
  30. ICacheFactory $cacheFactory,
  31. private IClientService $clientService,
  32. private IConfig $config,
  33. private IOCMProvider $provider,
  34. private LoggerInterface $logger,
  35. ) {
  36. $this->cache = $cacheFactory->createDistributed('ocm-discovery');
  37. }
  38. /**
  39. * @param string $remote
  40. * @param bool $skipCache
  41. *
  42. * @return IOCMProvider
  43. * @throws OCMProviderException
  44. */
  45. public function discover(string $remote, bool $skipCache = false): IOCMProvider {
  46. $remote = rtrim($remote, '/');
  47. if (!$skipCache) {
  48. try {
  49. $this->provider->import(json_decode($this->cache->get($remote) ?? '', true, 8, JSON_THROW_ON_ERROR) ?? []);
  50. if ($this->supportedAPIVersion($this->provider->getApiVersion())) {
  51. return $this->provider; // if cache looks valid, we use it
  52. }
  53. } catch (JsonException|OCMProviderException $e) {
  54. // we ignore cache on issues
  55. }
  56. }
  57. $client = $this->clientService->newClient();
  58. try {
  59. $options = [
  60. 'timeout' => 10,
  61. 'connect_timeout' => 10,
  62. ];
  63. if ($this->config->getSystemValueBool('sharing.federation.allowSelfSignedCertificates') === true) {
  64. $options['verify'] = false;
  65. }
  66. $response = $client->get(
  67. $remote . '/ocm-provider/',
  68. $options,
  69. );
  70. if ($response->getStatusCode() === Http::STATUS_OK) {
  71. $body = $response->getBody();
  72. // update provider with data returned by the request
  73. $this->provider->import(json_decode($body, true, 8, JSON_THROW_ON_ERROR) ?? []);
  74. $this->cache->set($remote, $body, 60 * 60 * 24);
  75. }
  76. } catch (JsonException|OCMProviderException $e) {
  77. throw new OCMProviderException('data returned by remote seems invalid - ' . ($body ?? ''));
  78. } catch (\Exception $e) {
  79. $this->logger->warning('error while discovering ocm provider', [
  80. 'exception' => $e,
  81. 'remote' => $remote
  82. ]);
  83. throw new OCMProviderException('error while requesting remote ocm provider');
  84. }
  85. if (!$this->supportedAPIVersion($this->provider->getApiVersion())) {
  86. throw new OCMProviderException('API version not supported');
  87. }
  88. return $this->provider;
  89. }
  90. /**
  91. * Check the version from remote is supported.
  92. * The minor version of the API will be ignored:
  93. * 1.0.1 is identified as 1.0
  94. *
  95. * @param string $version
  96. *
  97. * @return bool
  98. */
  99. private function supportedAPIVersion(string $version): bool {
  100. $dot1 = strpos($version, '.');
  101. $dot2 = strpos($version, '.', $dot1 + 1);
  102. if ($dot2 > 0) {
  103. $version = substr($version, 0, $dot2);
  104. }
  105. return (in_array($version, $this->supportedAPIVersion));
  106. }
  107. }