WellKnownUrls.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\Settings\SetupChecks;
  8. use OCP\Http\Client\IClientService;
  9. use OCP\IConfig;
  10. use OCP\IL10N;
  11. use OCP\IURLGenerator;
  12. use OCP\SetupCheck\ISetupCheck;
  13. use OCP\SetupCheck\SetupResult;
  14. use Psr\Log\LoggerInterface;
  15. class WellKnownUrls implements ISetupCheck {
  16. use CheckServerResponseTrait;
  17. public function __construct(
  18. protected IL10N $l10n,
  19. protected IConfig $config,
  20. protected IURLGenerator $urlGenerator,
  21. protected IClientService $clientService,
  22. protected LoggerInterface $logger,
  23. ) {
  24. }
  25. public function getCategory(): string {
  26. return 'network';
  27. }
  28. public function getName(): string {
  29. return $this->l10n->t('.well-known URLs');
  30. }
  31. public function run(): SetupResult {
  32. if (!$this->config->getSystemValueBool('check_for_working_wellknown_setup', true)) {
  33. return SetupResult::info($this->l10n->t('`check_for_working_wellknown_setup` is set to false in your configuration, so this check was skipped.'));
  34. }
  35. $urls = [
  36. ['get', '/.well-known/webfinger', [200, 404], true],
  37. ['get', '/.well-known/nodeinfo', [200, 404], true],
  38. ['propfind', '/.well-known/caldav', [207], false],
  39. ['propfind', '/.well-known/carddav', [207], false],
  40. ];
  41. foreach ($urls as [$verb,$url,$validStatuses,$checkCustomHeader]) {
  42. $works = null;
  43. foreach ($this->runRequest($verb, $url, ['httpErrors' => false, 'options' => ['allow_redirects' => ['track_redirects' => true]]]) as $response) {
  44. // Check that the response status matches
  45. $works = in_array($response->getStatusCode(), $validStatuses);
  46. // and (if needed) the custom Nextcloud header is set
  47. if ($checkCustomHeader) {
  48. $works = $works && !empty($response->getHeader('X-NEXTCLOUD-WELL-KNOWN'));
  49. } else {
  50. // For default DAV endpoints we lack authorization, but we still can check that the redirect works as expected
  51. if (!$works && $response->getStatusCode() === 401) {
  52. $redirectHops = explode(',', $response->getHeader('X-Guzzle-Redirect-History'));
  53. $effectiveUri = end($redirectHops);
  54. $works = str_ends_with(rtrim($effectiveUri, '/'), '/remote.php/dav');
  55. }
  56. }
  57. // Skip the other requests if one works
  58. if ($works === true) {
  59. break;
  60. }
  61. }
  62. // If 'works' is null then we could not connect to the server
  63. if ($works === null) {
  64. return SetupResult::info(
  65. $this->l10n->t('Could not check that your web server serves `.well-known` correctly. Please check manually.') . "\n" . $this->serverConfigHelp(),
  66. $this->urlGenerator->linkToDocs('admin-setup-well-known-URL'),
  67. );
  68. }
  69. // Otherwise if we fail we can abort here
  70. if ($works === false) {
  71. return SetupResult::warning(
  72. $this->l10n->t("Your web server is not properly set up to resolve `.well-known` URLs, failed on:\n`%s`", [$url]),
  73. $this->urlGenerator->linkToDocs('admin-setup-well-known-URL'),
  74. );
  75. }
  76. }
  77. return SetupResult::success(
  78. $this->l10n->t('Your server is correctly configured to serve `.well-known` URLs.')
  79. );
  80. }
  81. }