1
0

WellKnownController.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC\Core\Controller;
  8. use OC\Http\WellKnown\RequestManager;
  9. use OCP\AppFramework\Controller;
  10. use OCP\AppFramework\Http;
  11. use OCP\AppFramework\Http\Attribute\FrontpageRoute;
  12. use OCP\AppFramework\Http\Attribute\NoCSRFRequired;
  13. use OCP\AppFramework\Http\Attribute\OpenAPI;
  14. use OCP\AppFramework\Http\Attribute\PublicPage;
  15. use OCP\AppFramework\Http\JSONResponse;
  16. use OCP\AppFramework\Http\Response;
  17. use OCP\IRequest;
  18. #[OpenAPI(scope: OpenAPI::SCOPE_IGNORE)]
  19. class WellKnownController extends Controller {
  20. public function __construct(
  21. IRequest $request,
  22. private RequestManager $requestManager,
  23. ) {
  24. parent::__construct('core', $request);
  25. }
  26. /**
  27. * @return Response
  28. */
  29. #[PublicPage]
  30. #[NoCSRFRequired]
  31. #[FrontpageRoute(verb: 'GET', url: '.well-known/{service}')]
  32. public function handle(string $service): Response {
  33. $response = $this->requestManager->process(
  34. $service,
  35. $this->request
  36. );
  37. if ($response === null) {
  38. $httpResponse = new JSONResponse(['message' => "$service not supported"], Http::STATUS_NOT_FOUND);
  39. } else {
  40. $httpResponse = $response->toHttpResponse();
  41. }
  42. // We add a custom header so that setup checks can detect if their requests are answered by this controller
  43. return $httpResponse->addHeader('X-NEXTCLOUD-WELL-KNOWN', '1');
  44. }
  45. }