Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions apps/federation/appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
</background-jobs>

<commands>
<command>OCA\Federation\Command\ManageTrustedServers</command>
<command>OCA\Federation\Command\SyncFederationAddressBooks</command>
<command>OCA\Federation\Command\SyncFederationCalendars</command>
</commands>
Expand Down
1 change: 1 addition & 0 deletions apps/federation/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
'OCA\\Federation\\AppInfo\\Application' => $baseDir . '/../lib/AppInfo/Application.php',
'OCA\\Federation\\BackgroundJob\\GetSharedSecret' => $baseDir . '/../lib/BackgroundJob/GetSharedSecret.php',
'OCA\\Federation\\BackgroundJob\\RequestSharedSecret' => $baseDir . '/../lib/BackgroundJob/RequestSharedSecret.php',
'OCA\\Federation\\Command\\ManageTrustedServers' => $baseDir . '/../lib/Command/ManageTrustedServers.php',
'OCA\\Federation\\Command\\SyncFederationAddressBooks' => $baseDir . '/../lib/Command/SyncFederationAddressBooks.php',
'OCA\\Federation\\Command\\SyncFederationCalendars' => $baseDir . '/../lib/Command/SyncFederationCalendars.php',
'OCA\\Federation\\Controller\\OCSAuthAPIController' => $baseDir . '/../lib/Controller/OCSAuthAPIController.php',
Expand Down
1 change: 1 addition & 0 deletions apps/federation/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class ComposerStaticInitFederation
'OCA\\Federation\\AppInfo\\Application' => __DIR__ . '/..' . '/../lib/AppInfo/Application.php',
'OCA\\Federation\\BackgroundJob\\GetSharedSecret' => __DIR__ . '/..' . '/../lib/BackgroundJob/GetSharedSecret.php',
'OCA\\Federation\\BackgroundJob\\RequestSharedSecret' => __DIR__ . '/..' . '/../lib/BackgroundJob/RequestSharedSecret.php',
'OCA\\Federation\\Command\\ManageTrustedServers' => __DIR__ . '/..' . '/../lib/Command/ManageTrustedServers.php',
'OCA\\Federation\\Command\\SyncFederationAddressBooks' => __DIR__ . '/..' . '/../lib/Command/SyncFederationAddressBooks.php',
'OCA\\Federation\\Command\\SyncFederationCalendars' => __DIR__ . '/..' . '/../lib/Command/SyncFederationCalendars.php',
'OCA\\Federation\\Controller\\OCSAuthAPIController' => __DIR__ . '/..' . '/../lib/Controller/OCSAuthAPIController.php',
Expand Down
148 changes: 148 additions & 0 deletions apps/federation/lib/Command/ManageTrustedServers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
* SPDX-License-Identifier: AGPL-3.0-only
*/
namespace OCA\Federation\Command;

use OCA\Federation\TrustedServers;
use OCP\Console\Attribute\Argument;
use OCP\Console\Attribute\AsCommand;
use OCP\Console\ExitCode;
use OCP\Console\IOutput;

class ManageTrustedServers {
private const STATUS_LABELS = [
TrustedServers::STATUS_OK => 'ok',
TrustedServers::STATUS_PENDING => 'pending',
TrustedServers::STATUS_FAILURE => 'failure',
TrustedServers::STATUS_ACCESS_REVOKED => 'access revoked',
];

public function __construct(
private TrustedServers $trustedServersManager,
) {
}

#[AsCommand(
name: 'federation:trusted-servers:add',
description: 'Add trusted servers',
)]
public function add(
IOutput $output,
#[Argument(description: 'space-separated list of server URLs to add to the trusted servers list')]
array $servers = [],
): ExitCode {
if (empty($servers)) {
$output->writeln('<error>You must provide at least one server URL.</error>');
return ExitCode::Invalid;
}

$output->writeln('Adding trusted servers:');
$hasFailure = false;
foreach ($servers as $server) {
if ($this->trustedServersManager->isTrustedServer($server)) {
$output->writeln(" - $server already trusted, skipping");
continue;
}

try {
$this->trustedServersManager->addServer($server);
$output->writeln(" - $server added");
} catch (\Exception $e) {
$output->writeln(" - $server failed: " . $e->getMessage());
$hasFailure = true;
}
}

return $hasFailure ? ExitCode::Failure : ExitCode::Success;
}

#[AsCommand(
name: 'federation:trusted-servers:remove',
description: 'Remove trusted servers',
)]
public function remove(
IOutput $output,
#[Argument(description: 'space-separated list of server URLs to remove from the trusted servers list')]
array $servers = [],
): ExitCode {
if (empty($servers)) {
$output->writeln('<error>You must provide at least one server URL.</error>');
return ExitCode::Invalid;
}

$output->writeln('Removing trusted servers:');
$hasFailure = false;
foreach ($servers as $server) {
$id = $this->findServerId($server);
if ($id === null) {
$output->writeln(" - $server not found, skipping");
continue;
}

try {
$this->trustedServersManager->removeServer($id);
$output->writeln(" - $server removed");
} catch (\Exception $e) {
$output->writeln(" - $server failed: " . $e->getMessage());
$hasFailure = true;
}
}

return $hasFailure ? ExitCode::Failure : ExitCode::Success;
}

#[AsCommand(
name: 'federation:trusted-servers:list',
description: 'List trusted servers',
supportsOutputFormat: true,
)]
public function list(
IOutput $output,
): ExitCode {
$servers = $this->trustedServersManager->getServers();

if (empty($servers)) {
$output->writeln('No trusted servers configured.');
return ExitCode::Success;
}

$output->writeTableInOutputFormat(

Check failure on line 115 in apps/federation/lib/Command/ManageTrustedServers.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

TooManyArguments

apps/federation/lib/Command/ManageTrustedServers.php:115:12: TooManyArguments: Too many arguments for method OCP\Console\IOutput::writetableinoutputformat - saw 2 (see https://psalm.dev/026)
['id', 'url', 'status'],

Check failure on line 116 in apps/federation/lib/Command/ManageTrustedServers.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

InvalidArgument

apps/federation/lib/Command/ManageTrustedServers.php:116:4: InvalidArgument: Argument 1 of OCP\Console\IOutput::writeTableInOutputFormat expects list<array<string, mixed>>, but list{'id', 'url', 'status'} provided (see https://psalm.dev/004)
array_map(fn (array $server) => [
$server['id'],
$server['url'],
self::STATUS_LABELS[$server['status']] ?? (string)$server['status'],
], $servers),
);

return ExitCode::Success;
}

/**
* Trusted servers are stored with a normalized (https:// prefixed,
* trailing-slash trimmed) URL, so removal has to look up the id by
* matching that same normalization rather than the raw user input.
*/
private function findServerId(string $url): ?int {
$normalized = rtrim(
(str_starts_with($url, 'http://') || str_starts_with($url, 'https://'))
? $url
: 'https://' . $url,
'/'
);

foreach ($this->trustedServersManager->getServers() ?? [] as $server) {
if (rtrim($server['url'], '/') === $normalized) {
return $server['id'];
}
}

return null;
}
}
Loading