Skip to content

Commit

Permalink
Merge pull request #550 from nextcloud/enhancement/noid/fediverse-rig…
Browse files Browse the repository at this point in the history
…ht-access

limit request to fediverse
  • Loading branch information
ArtificialOwl authored May 30, 2019
2 parents 427f79e + b53e520 commit a5a0855
Show file tree
Hide file tree
Showing 13 changed files with 643 additions and 20 deletions.
1 change: 1 addition & 0 deletions appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
<commands>
<command>OCA\Social\Command\CacheRefresh</command>
<command>OCA\Social\Command\CheckInstall</command>
<command>OCA\Social\Command\Fediverse</command>
<command>OCA\Social\Command\NoteCreate</command>
<command>OCA\Social\Command\NoteBoost</command>
<command>OCA\Social\Command\Reset</command>
Expand Down
232 changes: 232 additions & 0 deletions lib/Command/Fediverse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,232 @@
<?php
declare(strict_types=1);


/**
* Nextcloud - Social Support
*
* This file is licensed under the Affero General Public License version 3 or
* later. See the COPYING file.
*
* @author Maxence Lange <[email protected]>
* @copyright 2018, Maxence Lange <[email protected]>
* @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\Social\Command;


use Exception;
use OC\Core\Command\Base;
use OCA\Social\Exceptions\UnauthorizedFediverseException;
use OCA\Social\Service\ConfigService;
use OCA\Social\Service\FediverseService;
use OCA\Social\Service\MiscService;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;


/**
* Class Fediverse
*
* @package OCA\Social\Command
*/
class Fediverse extends Base {


/** @var FediverseService */
private $fediverseService;

/** @var ConfigService */
private $configService;

/** @var MiscService */
private $miscService;


/** @var OutputInterface */
private $output;


/**
* CacheUpdate constructor.
*
* @param FediverseService $fediverseService
* @param ConfigService $configService
* @param MiscService $miscService
*/
public function __construct(
FediverseService $fediverseService, ConfigService $configService,
MiscService $miscService
) {
parent::__construct();

$this->fediverseService = $fediverseService;
$this->configService = $configService;
$this->miscService = $miscService;
}


/**
*
*/
protected function configure() {
parent::configure();
$this->setName('social:fediverse')
->addOption(
'type', 't', InputArgument::OPTIONAL,
'Change the type of access management', ''
)
->addArgument('action', InputArgument::OPTIONAL, 'add/remove/test address', '')
->addArgument('address', InputArgument::OPTIONAL, 'address/host', '')
->setDescription('Allow or deny access to the fediverse');
}


/**
* @param InputInterface $input
* @param OutputInterface $output
*
* @throws Exception
*/
protected function execute(InputInterface $input, OutputInterface $output) {
$this->output = $output;

if ($this->typeAccess($input->getOption('type'))) {
return;
}

$this->output->writeln(
'Current access type: <info>' . $this->fediverseService->getAccessType() . '</info>'
);

switch ($input->getArgument('action')) {
case '':
$this->listAddresses(false);
break;

case 'list':
$this->listAddresses(true);
break;

case 'add':
$this->addAddress($input->getArgument('address'));
break;

case 'remove':
$this->removeAddress($input->getArgument('address'));
break;

case 'test':
$this->testAddress($input->getArgument('address'));
break;

case 'reset':
$this->resetAddresses();
break;

default:
throw new Exception('specify action: add, remove, list, reset');
}
}


/**
* @param string $type
*
* @return bool
* @throws Exception
*/
private function typeAccess(string $type) {
if ($type === '') {
return false;
}

$this->fediverseService->setAccessType($type);

return true;
}


/**
* @param bool $allKnownAddress
*/
private function listAddresses(bool $allKnownAddress = false) {

if ($allKnownAddress) {
$this->output->writeln('- Known address:');
foreach ($this->fediverseService->getKnownAddresses() as $address) {
$this->output->writeln(' <info>' . $address . '</info>');
}
}

$this->output->writeln('- List:');
foreach ($this->fediverseService->getListedAddresses() as $address) {
$this->output->writeln(' <info>' . $address . '</info>');
}

}


/**
* @param string $address
*
* @throws Exception
*/
private function addAddress(string $address) {
$this->fediverseService->addAddress($address);
$this->output->writeln('<info>' . $address . '</info> added to the list');
}


/**
* @param string $address
*
* @throws Exception
*/
private function removeAddress(string $address) {
$this->fediverseService->removeAddress($address);
$this->output->writeln('<info>' . $address . '</info> removed from the list');
}


/**
* @param string $address
*/
private function testAddress(string $address) {
try {
$this->fediverseService->authorized($address);
$this->output->writeln('<info>Authorized</info>');
} catch (UnauthorizedFediverseException $e) {
$this->output->writeln('<comment>Unauthorized</comment>');
}
}


/**
*
*/
private function resetAddresses() {
$this->fediverseService->resetAddresses();
$this->output->writeln('list is now empty');
}


}

14 changes: 11 additions & 3 deletions lib/Controller/ActivityPubController.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
use OCA\Social\Exceptions\ItemUnknownException;
use OCA\Social\Exceptions\UrlCloudException;
use OCA\Social\Service\CacheActorService;
use OCA\Social\Service\FediverseService;
use OCA\Social\Service\FollowService;
use OCA\Social\Service\ImportService;
use OCA\Social\Service\MiscService;
Expand All @@ -61,6 +62,9 @@ class ActivityPubController extends Controller {
/** @var SocialPubController */
private $socialPubController;

/** @var FediverseService */
private $fediverseService;

/** @var CacheActorService */
private $cacheActorService;

Expand All @@ -85,6 +89,7 @@ class ActivityPubController extends Controller {
*
* @param IRequest $request
* @param SocialPubController $socialPubController
* @param FediverseService $fediverseService
* @param CacheActorService $cacheActorService
* @param SignatureService $signatureService
* @param StreamQueueService $streamQueueService
Expand All @@ -94,13 +99,14 @@ class ActivityPubController extends Controller {
*/
public function __construct(
IRequest $request, SocialPubController $socialPubController,
CacheActorService $cacheActorService, SignatureService $signatureService,
StreamQueueService $streamQueueService, ImportService $importService,
FollowService $followService, MiscService $miscService
FediverseService $fediverseService, CacheActorService $cacheActorService,
SignatureService $signatureService, StreamQueueService $streamQueueService,
ImportService $importService, FollowService $followService, MiscService $miscService
) {
parent::__construct(Application::APP_NAME, $request);

$this->socialPubController = $socialPubController;
$this->fediverseService = $fediverseService;
$this->cacheActorService = $cacheActorService;
$this->signatureService = $signatureService;
$this->streamQueueService = $streamQueueService;
Expand Down Expand Up @@ -178,6 +184,7 @@ public function sharedInbox(): Response {

$requestTime = 0;
$origin = $this->signatureService->checkRequest($this->request, $requestTime);
$this->fediverseService->authorized($origin);

$activity = $this->importService->importFromJson($body);
if (!$this->signatureService->checkObject($activity)) {
Expand Down Expand Up @@ -221,6 +228,7 @@ public function inbox(string $username): Response {

$requestTime = 0;
$origin = $this->signatureService->checkRequest($this->request, $requestTime);
$this->fediverseService->authorized($origin);

// TODO - check the recipient <-> username
// $actor = $this->actorService->getActor($username);
Expand Down
40 changes: 40 additions & 0 deletions lib/Exceptions/UnauthorizedFediverseException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
declare(strict_types=1);


/**
* Nextcloud - Social Support
*
* This file is licensed under the Affero General Public License version 3 or
* later. See the COPYING file.
*
* @author Maxence Lange <[email protected]>
* @copyright 2018, Maxence Lange <[email protected]>
* @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\Social\Exceptions;


use Exception;


class UnauthorizedFediverseException extends Exception {

}

3 changes: 3 additions & 0 deletions lib/Service/ActivityService.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
use OCA\Social\Exceptions\RequestResultSizeException;
use OCA\Social\Exceptions\RequestServerException;
use OCA\Social\Exceptions\SocialAppConfigException;
use OCA\Social\Exceptions\UnauthorizedFediverseException;
use OCA\Social\Model\ActivityPub\ACore;
use OCA\Social\Model\ActivityPub\Activity\Create;
use OCA\Social\Model\ActivityPub\Activity\Delete;
Expand Down Expand Up @@ -284,6 +285,8 @@ public function manageRequest(RequestQueue $queue) {
$this->signatureService->signRequest($request, $queue);
$this->curlService->request($request);
$this->requestQueueService->endRequest($queue, true);
} catch (UnauthorizedFediverseException $e) {
$this->requestQueueService->endRequest($queue, true);
} catch (RequestResultNotJsonException $e) {
$this->requestQueueService->endRequest($queue, true);
} catch (ActorDoesNotExistException $e) {
Expand Down
2 changes: 2 additions & 0 deletions lib/Service/CacheActorService.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
use OCA\Social\Exceptions\RequestServerException;
use OCA\Social\Exceptions\SocialAppConfigException;
use OCA\Social\Exceptions\ItemUnknownException;
use OCA\Social\Exceptions\UnauthorizedFediverseException;
use OCA\Social\Model\ActivityPub\Actor\Person;


Expand Down Expand Up @@ -118,6 +119,7 @@ public function setViewer(Person $viewer) {
* @throws SocialAppConfigException
* @throws ItemUnknownException
* @throws RequestResultNotJsonException
* @throws UnauthorizedFediverseException
*/
public function getFromId(string $id, bool $refresh = false): Person {

Expand Down
Loading

0 comments on commit a5a0855

Please sign in to comment.