OCMDiscoveryService.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright 2023, Maxence Lange <maxence@artificial-owl.com>
  5. *
  6. * @author Maxence Lange <maxence@artificial-owl.com>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OC\OCM;
  25. use JsonException;
  26. use OCP\AppFramework\Http;
  27. use OCP\Http\Client\IClientService;
  28. use OCP\ICache;
  29. use OCP\ICacheFactory;
  30. use OCP\IConfig;
  31. use OCP\OCM\Exceptions\OCMProviderException;
  32. use OCP\OCM\IOCMDiscoveryService;
  33. use OCP\OCM\IOCMProvider;
  34. use Psr\Log\LoggerInterface;
  35. /**
  36. * @since 28.0.0
  37. */
  38. class OCMDiscoveryService implements IOCMDiscoveryService {
  39. private ICache $cache;
  40. private array $supportedAPIVersion =
  41. [
  42. '1.0-proposal1',
  43. '1.0',
  44. '1.1'
  45. ];
  46. public function __construct(
  47. ICacheFactory $cacheFactory,
  48. private IClientService $clientService,
  49. private IConfig $config,
  50. private IOCMProvider $provider,
  51. private LoggerInterface $logger,
  52. ) {
  53. $this->cache = $cacheFactory->createDistributed('ocm-discovery');
  54. }
  55. /**
  56. * @param string $remote
  57. * @param bool $skipCache
  58. *
  59. * @return IOCMProvider
  60. * @throws OCMProviderException
  61. */
  62. public function discover(string $remote, bool $skipCache = false): IOCMProvider {
  63. $remote = rtrim($remote, '/');
  64. if (!$skipCache) {
  65. try {
  66. $cached = $this->cache->get($remote);
  67. if ($cached === false) {
  68. throw new OCMProviderException('Previous discovery failed.');
  69. }
  70. $this->provider->import(json_decode($cached ?? '', true, 8, JSON_THROW_ON_ERROR) ?? []);
  71. if ($this->supportedAPIVersion($this->provider->getApiVersion())) {
  72. return $this->provider; // if cache looks valid, we use it
  73. }
  74. } catch (JsonException|OCMProviderException $e) {
  75. // we ignore cache on issues
  76. }
  77. }
  78. $client = $this->clientService->newClient();
  79. try {
  80. $response = $client->get(
  81. $remote . '/ocm-provider/',
  82. [
  83. 'timeout' => 10,
  84. 'verify' => !$this->config->getSystemValueBool('sharing.federation.allowSelfSignedCertificates'),
  85. 'connect_timeout' => 10,
  86. ]
  87. );
  88. if ($response->getStatusCode() === Http::STATUS_OK) {
  89. $body = $response->getBody();
  90. // update provider with data returned by the request
  91. $this->provider->import(json_decode($body, true, 8, JSON_THROW_ON_ERROR) ?? []);
  92. $this->cache->set($remote, $body, 60 * 60 * 24);
  93. }
  94. } catch (JsonException|OCMProviderException $e) {
  95. $this->cache->set($remote, false, 5 * 60);
  96. throw new OCMProviderException('data returned by remote seems invalid - ' . ($body ?? ''));
  97. } catch (\Exception $e) {
  98. $this->cache->set($remote, false, 5 * 60);
  99. $this->logger->warning('error while discovering ocm provider', [
  100. 'exception' => $e,
  101. 'remote' => $remote
  102. ]);
  103. throw new OCMProviderException('error while requesting remote ocm provider');
  104. }
  105. if (!$this->supportedAPIVersion($this->provider->getApiVersion())) {
  106. $this->cache->set($remote, false, 5 * 60);
  107. throw new OCMProviderException('API version not supported');
  108. }
  109. return $this->provider;
  110. }
  111. /**
  112. * Check the version from remote is supported.
  113. * The minor version of the API will be ignored:
  114. * 1.0.1 is identified as 1.0
  115. *
  116. * @param string $version
  117. *
  118. * @return bool
  119. */
  120. private function supportedAPIVersion(string $version): bool {
  121. $dot1 = strpos($version, '.');
  122. $dot2 = strpos($version, '.', $dot1 + 1);
  123. if ($dot2 > 0) {
  124. $version = substr($version, 0, $dot2);
  125. }
  126. return (in_array($version, $this->supportedAPIVersion));
  127. }
  128. }