Skip to content

Commit

Permalink
feat(middleware): Filter federation supported things on the middleware
Browse files Browse the repository at this point in the history
Signed-off-by: Joas Schilling <coding@schilljs.com>
  • Loading branch information
nickvergessen committed Feb 21, 2024
1 parent 2265ea3 commit 081eb55
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/Controller/ChatController.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
use OCA\Talk\Federation\Authenticator;
use OCA\Talk\GuestManager;
use OCA\Talk\MatterbridgeManager;
use OCA\Talk\Middleware\Attribute\FederationSupported;
use OCA\Talk\Middleware\Attribute\RequireLoggedInParticipant;
use OCA\Talk\Middleware\Attribute\RequireModeratorOrNoLobby;
use OCA\Talk\Middleware\Attribute\RequireModeratorParticipant;
Expand Down Expand Up @@ -199,6 +200,7 @@ protected function parseCommentToResponse(IComment $comment, ?Message $parentMes
* 413: Message too long
* 429: Mention rate limit exceeded (guests only)
*/
#[FederationSupported]
#[PublicPage]
#[RequireModeratorOrNoLobby]
#[RequireParticipant]
Expand Down Expand Up @@ -1159,6 +1161,7 @@ protected function getMessagesForRoom(array $messageIds): array {
*
* 200: List of mention suggestions returned
*/
#[FederationSupported]
#[PublicPage]
#[RequireModeratorOrNoLobby]
#[RequireParticipant]
Expand Down
2 changes: 2 additions & 0 deletions lib/Controller/RoomController.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
use OCA\Talk\GuestManager;
use OCA\Talk\Manager;
use OCA\Talk\MatterbridgeManager;
use OCA\Talk\Middleware\Attribute\FederationSupported;
use OCA\Talk\Middleware\Attribute\RequireLoggedInModeratorParticipant;
use OCA\Talk\Middleware\Attribute\RequireLoggedInParticipant;
use OCA\Talk\Middleware\Attribute\RequireModeratorOrNoLobby;
Expand Down Expand Up @@ -833,6 +834,7 @@ public function deleteRoom(): DataResponse {
* 200: Participants returned
* 403: Missing permissions for getting participants
*/
#[FederationSupported]
#[PublicPage]
#[RequireModeratorOrNoLobby]
#[RequireParticipant]
Expand Down
36 changes: 36 additions & 0 deletions lib/Middleware/Attribute/FederationSupported.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

/**
* @copyright Copyright (c) 2024 Joas Schilling <coding@schilljs.com>
*
* @author Joas Schilling <coding@schilljs.com>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

namespace OCA\Talk\Middleware\Attribute;

use Attribute;
use OCA\Talk\Middleware\InjectionMiddleware;

/**
* @see InjectionMiddleware::getRoom()
*/
#[Attribute(Attribute::TARGET_METHOD)]
class FederationSupported {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);
/**
* @copyright Copyright (c) 2023, Joas Schilling <coding@schilljs.com>
*
* @author Joas Schilling <coding@schilljs.com>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace OCA\Talk\Middleware\Exceptions;

use OCP\AppFramework\Http;

class FederationUnsupportedFeatureException extends \Exception {
public function __construct(
) {
parent::__construct('Feature is unsupported for federation', Http::STATUS_UPGRADE_REQUIRED);
}
}
27 changes: 27 additions & 0 deletions lib/Middleware/InjectionMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use OCA\Talk\Exceptions\RoomNotFoundException;
use OCA\Talk\Federation\Authenticator;
use OCA\Talk\Manager;
use OCA\Talk\Middleware\Attribute\FederationSupported;
use OCA\Talk\Middleware\Attribute\RequireLoggedInModeratorParticipant;
use OCA\Talk\Middleware\Attribute\RequireLoggedInParticipant;
use OCA\Talk\Middleware\Attribute\RequireModeratorOrNoLobby;
Expand All @@ -38,6 +39,7 @@
use OCA\Talk\Middleware\Attribute\RequirePermission;
use OCA\Talk\Middleware\Attribute\RequireReadWriteConversation;
use OCA\Talk\Middleware\Attribute\RequireRoom;
use OCA\Talk\Middleware\Exceptions\FederationUnsupportedFeatureException;
use OCA\Talk\Middleware\Exceptions\LobbyException;
use OCA\Talk\Middleware\Exceptions\NotAModeratorException;
use OCA\Talk\Middleware\Exceptions\ReadOnlyException;
Expand Down Expand Up @@ -76,6 +78,7 @@ public function __construct(
}

/**
* @throws FederationUnsupportedFeatureException
* @throws LobbyException
* @throws NotAModeratorException
* @throws ParticipantNotFoundException
Expand Down Expand Up @@ -117,6 +120,11 @@ public function beforeController(Controller $controller, string $methodName): vo
$this->getRoom($controller);
}

if (empty($reflectionMethod->getAttributes(FederationSupported::class))) {
// When federation is not supported, the room needs to be local
$this->checkFederationSupport($controller);
}

if (!empty($reflectionMethod->getAttributes(RequireReadWriteConversation::class))) {
$this->checkReadOnlyState($controller);
}
Expand Down Expand Up @@ -214,6 +222,17 @@ protected function getLoggedInOrGuest(AEnvironmentAwareController $controller, b
}
}

/**
* @param AEnvironmentAwareController $controller
* @throws FederationUnsupportedFeatureException
*/
protected function checkFederationSupport(AEnvironmentAwareController $controller): void {
$room = $controller->getRoom();
if (!$room instanceof Room || $room->getRemoteServer() !== '') {
throw new FederationUnsupportedFeatureException();
}
}

/**
* @param AEnvironmentAwareController $controller
* @throws ReadOnlyException
Expand Down Expand Up @@ -302,6 +321,14 @@ public function afterException(Controller $controller, string $methodName, \Exce
return new RedirectResponse($this->url->linkToDefaultPageUrl());
}

if ($exception instanceof FederationUnsupportedFeatureException) {
if ($controller instanceof OCSController) {
throw new OCSException('', Http::STATUS_NOT_ACCEPTABLE);
}

return new RedirectResponse($this->url->linkToDefaultPageUrl());
}

if ($exception instanceof LobbyException) {
if ($controller instanceof OCSController) {
throw new OCSException('', Http::STATUS_PRECONDITION_FAILED);
Expand Down

0 comments on commit 081eb55

Please sign in to comment.