Skip to content

Commit

Permalink
signing gs request
Browse files Browse the repository at this point in the history
Signed-off-by: Maxence Lange <maxence@artificial-owl.com>
  • Loading branch information
ArtificialOwl committed Jun 11, 2020
1 parent 899f8df commit 1a64c94
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 5 deletions.
3 changes: 1 addition & 2 deletions appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@ Users won't be able to find this Circle using Nextcloud search engine.
<repair-steps>
<post-migration>
<step>OCA\Circles\Migration\ImportOwncloudCustomGroups</step>
<step>OCA\Circles\Migration\GenerateUniqueIdOnCreatedCircle</step>
<step>OCA\Circles\Migration\UsingShortenUniqueIdInsteadOfCircleId</step>
<step>OCA\Circles\Migration\RemoveDeadShares</step>
</post-migration>
</repair-steps>
Expand All @@ -66,6 +64,7 @@ Users won't be able to find this Circle using Nextcloud search engine.
<command>OCA\Circles\Command\CirclesDetails</command>
<command>OCA\Circles\Command\CirclesList</command>
<command>OCA\Circles\Command\CirclesSync</command>
<command>OCA\Circles\Command\CirclesTest</command>
<command>OCA\Circles\Command\Clean</command>
<command>OCA\Circles\Command\MembersCreate</command>
<command>OCA\Circles\Command\MembersDetails</command>
Expand Down
101 changes: 101 additions & 0 deletions lib/Command/CirclesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php declare(strict_types=1);


/**
* Circles - Bring cloud-users closer together.
*
* This file is licensed under the Affero General Public License version 3 or
* later. See the COPYING file.
*
* @author Maxence Lange <maxence@artificial-owl.com>
* @copyright 2017
* @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\Circles\Command;

use daita\MySmallPhpTools\Traits\TArrayTools;
use OC\Core\Command\Base;
use OCA\Circles\Exceptions\ConfigNoCircleAvailableException;
use OCA\Circles\Service\ConfigService;
use OCA\Circles\Service\GlobalScaleService;
use OCP\IL10N;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;


/**
* Class CirclesList
*
* @package OCA\Circles\Command
*/
class CirclesTest extends Base {


use TArrayTools;


/** @var IL10N */
private $l10n;

/** @var GlobalScaleService */
private $globalScaleService;

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

/**
* CirclesList constructor.
*
* @param IL10N $l10n
* @param GlobalScaleService $globalScaleService
* @param ConfigService $configService
*/
public function __construct(
IL10N $l10n, GlobalScaleService $globalScaleService, ConfigService $configService
) {
parent::__construct();

$this->l10n = $l10n;
$this->globalScaleService = $globalScaleService;
$this->configService = $configService;
}


protected function configure() {
parent::configure();
$this->setName('circles:test')
->setDescription('testing some features');
}


/**
* @param InputInterface $input
* @param OutputInterface $output
*
* @return int
*/
protected function execute(InputInterface $input, OutputInterface $output): int {
$instances = $this->globalScaleService->getInstances(true);
$output->writeln('<info>Instances: </info>' . json_encode($instances));

return 0;
}

}

2 changes: 2 additions & 0 deletions lib/Service/ConfigService.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ class ConfigService {
const GS_KEY = 'key';
const GS_LOOKUP = 'lookup';

const GS_LOOKUP_INSTANCES = '/instances';


private $defaults = [
self::CIRCLES_ALLOW_CIRCLES => Circle::CIRCLES_ALL,
Expand Down
52 changes: 49 additions & 3 deletions lib/Service/GlobalScaleService.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
use daita\MySmallPhpTools\Traits\TRequest;
use daita\MySmallPhpTools\Traits\TStringTools;
use OC;
use OC\Security\IdentityProof\Signer;
use OC\User\NoUserException;
use OCA\Circles\Db\GSEventsRequest;
use OCA\Circles\Exceptions\GlobalScaleEventException;
use OCA\Circles\Exceptions\GSKeyException;
Expand All @@ -48,6 +50,9 @@
use OCA\Circles\Model\GlobalScale\GSWrapper;
use OCP\AppFramework\QueryException;
use OCP\IURLGenerator;
use OCP\IUser;
use OCP\IUserManager;
use OCP\IUserSession;


/**
Expand All @@ -65,6 +70,15 @@ class GlobalScaleService {
/** @var IURLGenerator */
private $urlGenerator;

/** @var IUserManager */
private $userManager;

/** @var IUserSession */
private $userSession;

/** @var Signer */
private $signer;

/** @var GSEventsRequest */
private $gsEventsRequest;

Expand All @@ -79,17 +93,26 @@ class GlobalScaleService {
* GlobalScaleService constructor.
*
* @param IURLGenerator $urlGenerator
* @param IUserManager $userManager
* @param IUserSession $userSession
* @param Signer $signer
* @param GSEventsRequest $gsEventsRequest
* @param ConfigService $configService
* @param MiscService $miscService
*/
public function __construct(
IURLGenerator $urlGenerator,
IUserManager $userManager,
IUserSession $userSession,
Signer $signer,
GSEventsRequest $gsEventsRequest,
ConfigService $configService,
MiscService $miscService
) {
$this->urlGenerator = $urlGenerator;
$this->userManager = $userManager;
$this->userSession = $userSession;
$this->signer = $signer;
$this->gsEventsRequest = $gsEventsRequest;
$this->configService = $configService;
$this->miscService = $miscService;
Expand Down Expand Up @@ -194,7 +217,11 @@ public function getInstances(bool $all = false): array {
/** @var string $lookup */
try {
$lookup = $this->configService->getGSStatus(ConfigService::GS_LOOKUP);
$request = new Request('/instances', Request::TYPE_GET);
$request = new Request(ConfigService::GS_LOOKUP_INSTANCES, Request::TYPE_POST);

$user = $this->getRandomUser();
$data = $this->signer->sign('lookupserver', ['federationId' => $user->getCloudId()], $user);
$request->setData($data);
$request->setAddressFromUrl($lookup);

try {
Expand All @@ -204,15 +231,15 @@ public function getInstances(bool $all = false): array {

return [];
}
} catch (GSStatusException $e) {
} catch (NoUserException | GSStatusException $e) {
$instances = [$this->configService->getLocalCloudId()];
}

if ($all) {
return $instances;
}

return array_diff($instances, $this->configService->getTrustedDomains());
return array_values(array_diff($instances, $this->configService->getTrustedDomains()));
}


Expand All @@ -233,5 +260,24 @@ private function getClassNameFromEvent(GSEvent $event): string {
return $className;
}


/**
* @return IUser
* @throws NoUserException
*/
private function getRandomUser(): IUser {
$user = $this->userSession->getUser();
if ($user !== null) {
return $user;
}

$random = $this->userManager->search('', 1);
if (sizeof($random) > 0) {
return array_shift($random);
}

throw new NoUserException();
}

}

0 comments on commit 1a64c94

Please sign in to comment.