1
0

WellKnownUrls.php 3.2 KB

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