|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors |
| 7 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 8 | + */ |
| 9 | + |
| 10 | +namespace OCA\CloudFederationAPI\Controller; |
| 11 | + |
| 12 | +use JsonException; |
| 13 | +use OCP\AppFramework\Http; |
| 14 | +use OCP\AppFramework\Controller; |
| 15 | +use OCP\AppFramework\Http\Attribute\BruteForceProtection; |
| 16 | +use OCP\AppFramework\Http\Attribute\NoCSRFRequired; |
| 17 | +use OCP\AppFramework\Http\Attribute\OpenAPI; |
| 18 | +use OCP\AppFramework\Http\Attribute\PublicPage; |
| 19 | +use OCP\AppFramework\Http\DataResponse; |
| 20 | +use OCP\AppFramework\Http\Response; |
| 21 | +use OCP\EventDispatcher\IEventDispatcher; |
| 22 | +use OCP\IRequest; |
| 23 | +use OCP\OCM\Events\OCMEndpointRequestEvent; |
| 24 | +use OCP\OCM\Exceptions\OCMArgumentException; |
| 25 | +use Psr\Log\LoggerInterface; |
| 26 | + |
| 27 | +#[OpenAPI(scope: OpenAPI::SCOPE_FEDERATION)] |
| 28 | +class OCMRequestController extends Controller { |
| 29 | + public function __construct( |
| 30 | + string $appName, |
| 31 | + IRequest $request, |
| 32 | + private readonly IEventDispatcher $eventDispatcher, |
| 33 | + private readonly LoggerInterface $logger, |
| 34 | + ) { |
| 35 | + parent::__construct($appName, $request); |
| 36 | + } |
| 37 | + |
| 38 | + #[NoCSRFRequired] |
| 39 | + #[PublicPage] |
| 40 | + #[BruteForceProtection(action: 'receiveOcmRequest')] |
| 41 | + public function manageOCMRequests(string $ocmPath): Response { |
| 42 | + try { |
| 43 | + json_encode($ocmPath, JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES); |
| 44 | + } catch (JsonException) { |
| 45 | + throw new OCMArgumentException('path is not UTF-8'); |
| 46 | + } |
| 47 | + |
| 48 | + $event = new OCMEndpointRequestEvent($this->request->getMethod(), str_replace('//', '/', $ocmPath)); |
| 49 | + $this->eventDispatcher->dispatchTyped($event); |
| 50 | + |
| 51 | + if ($event->getResponse() === null) { |
| 52 | + return new DataResponse('', Http::STATUS_NOT_FOUND); |
| 53 | + } |
| 54 | + |
| 55 | + return $event->getResponse(); |
| 56 | + } |
| 57 | +} |
0 commit comments