OCMDiscoveryService.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 OC\OCM\Model\OCMProvider;
  27. use OCP\AppFramework\Http;
  28. use OCP\Http\Client\IClientService;
  29. use OCP\ICache;
  30. use OCP\ICacheFactory;
  31. use OCP\IConfig;
  32. use OCP\OCM\Exceptions\OCMProviderException;
  33. use OCP\OCM\IOCMDiscoveryService;
  34. use OCP\OCM\IOCMProvider;
  35. use Psr\Log\LoggerInterface;
  36. /**
  37. * @since 28.0.0
  38. */
  39. class OCMDiscoveryService implements IOCMDiscoveryService {
  40. private ICache $cache;
  41. private array $supportedAPIVersion =
  42. [
  43. '1.0-proposal1',
  44. '1.0',
  45. '1.1'
  46. ];
  47. public function __construct(
  48. ICacheFactory $cacheFactory,
  49. private IClientService $clientService,
  50. private IConfig $config,
  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. $provider = new OCMProvider();
  65. if (!$skipCache) {
  66. try {
  67. $provider->import(json_decode($this->cache->get($remote) ?? '', true, 8, JSON_THROW_ON_ERROR) ?? []);
  68. if ($this->supportedAPIVersion($provider->getApiVersion())) {
  69. return $provider; // if cache looks valid, we use it
  70. }
  71. } catch (JsonException|OCMProviderException $e) {
  72. // we ignore cache on issues
  73. }
  74. }
  75. $client = $this->clientService->newClient();
  76. try {
  77. $response = $client->get(
  78. $remote . '/ocm-provider/',
  79. [
  80. 'timeout' => 10,
  81. 'verify' => !$this->config->getSystemValueBool('sharing.federation.allowSelfSignedCertificates'),
  82. 'connect_timeout' => 10,
  83. ]
  84. );
  85. if ($response->getStatusCode() === Http::STATUS_OK) {
  86. $body = $response->getBody();
  87. // update provider with data returned by the request
  88. $provider->import(json_decode($body, true, 8, JSON_THROW_ON_ERROR) ?? []);
  89. $this->cache->set($remote, $body, 60 * 60 * 24);
  90. }
  91. } catch (JsonException|OCMProviderException $e) {
  92. throw new OCMProviderException('data returned by remote seems invalid - ' . ($body ?? ''));
  93. } catch (\Exception $e) {
  94. $this->logger->warning('error while discovering ocm provider', [
  95. 'exception' => $e,
  96. 'remote' => $remote
  97. ]);
  98. throw new OCMProviderException('error while requesting remote ocm provider');
  99. }
  100. if (!$this->supportedAPIVersion($provider->getApiVersion())) {
  101. throw new OCMProviderException('API version not supported');
  102. }
  103. return $provider;
  104. }
  105. /**
  106. * Check the version from remote is supported.
  107. * The minor version of the API will be ignored:
  108. * 1.0.1 is identified as 1.0
  109. *
  110. * @param string $version
  111. *
  112. * @return bool
  113. */
  114. private function supportedAPIVersion(string $version): bool {
  115. $dot1 = strpos($version, '.');
  116. $dot2 = strpos($version, '.', $dot1 + 1);
  117. if ($dot2 > 0) {
  118. $version = substr($version, 0, $dot2);
  119. }
  120. return (in_array($version, $this->supportedAPIVersion));
  121. }
  122. }