Skip to content

Commit

Permalink
feat: Check remove server connectivity
Browse files Browse the repository at this point in the history
Signed-off-by: Julius Knorr <[email protected]>
  • Loading branch information
juliusknorr committed Dec 27, 2024
1 parent fd8d1eb commit d1501ab
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/Command/ActivateConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,15 @@ protected function execute(InputInterface $input, OutputInterface $output): int
return 1;
}

try {
$this->connectivityService->testCallback($output);
} catch (\Throwable $e) {
// FIXME: Optional when allowing generic WOPI servers
$output->writeln('<error>Failed to fetch wopi');
$output->writeln($e->getMessage());
return 1;
}

// Summarize URLs for easier debugging

$output->writeln('');
Expand Down
2 changes: 2 additions & 0 deletions lib/Controller/SettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public function checkSettings(): DataResponse {
$output = new NullOutput();
$this->connectivityService->testDiscovery($output);
$this->connectivityService->testCapabilities($output);
$this->connectivityService->testCallback($output);
} catch (\Exception $e) {
$this->logger->error($e->getMessage(), ['exception' => $e]);
return new DataResponse([
Expand Down Expand Up @@ -181,6 +182,7 @@ public function setSettings(
$this->connectivityService->testDiscovery($output);
$this->connectivityService->testCapabilities($output);
$this->connectivityService->autoConfigurePublicUrl();
$this->connectivityService->testCallback($output);
} catch (\Throwable $e) {
return new JSONResponse([
'status' => 'error',
Expand Down
36 changes: 36 additions & 0 deletions lib/Service/ConnectivityService.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
use Exception;
use OCA\Richdocuments\AppConfig;
use OCA\Richdocuments\WOPI\Parser;
use OCP\Http\Client\IClientService;
use OCP\IURLGenerator;
use Symfony\Component\Console\Output\OutputInterface;

class ConnectivityService {
Expand All @@ -17,6 +19,8 @@ public function __construct(
private DiscoveryService $discoveryService,
private CapabilitiesService $capabilitiesService,
private Parser $parser,
private IClientService $clientService,
private IURLGenerator $urlGenerator,
) {
}

Expand Down Expand Up @@ -47,6 +51,38 @@ public function testCapabilities(OutputInterface $output): void {
$output->writeln('<info>✓ Detected WOPI server: ' . $this->capabilitiesService->getServerProductName() . ' ' . $this->capabilitiesService->getProductVersion() . '</info>');
}

public function testCallback(OutputInterface $output): void {

Check failure on line 54 in lib/Service/ConnectivityService.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis

UndefinedClass

lib/Service/ConnectivityService.php:54:31: UndefinedClass: Class, interface or enum named Symfony\Component\Console\Output\OutputInterface does not exist (see https://psalm.dev/019)
$url = $this->parser->getUrlSrcValue('Capabilities');
if ($url === '') {
// Fixme can we skip early if the collabora version does not have the wopiAccessCheck endpoint, maybe it can be exposed in discovery
return;
}

$url = str_replace('/hosting/capabilities', '/hosting/wopiAccessCheck', $url);

$callbackUrl = $this->urlGenerator->getAbsoluteURL('/status.php');

$client = $this->clientService->newClient();
try {
$response = $client->post($url, [
...RemoteOptionsService::getDefaultOptions(),
'body' => json_encode([
'callbackUrl' => $callbackUrl,
]),
'headers' => [
'Content-Type' => 'application/json',
],
]);
$result = json_decode($response->getBody(), true, 512, JSON_THROW_ON_ERROR);
if ($result['status'] === 'CheckStatus::WopiHostNotAllowed') {
throw new \Exception('WOPI host not allowed by Collabora');
}
} catch (Exception $e) {
throw $e;
}
$output->writeln('<info>✓ URL Callback ok</info>');
}

/**
* Detect public URL of the WOPI server for setting CSP on Nextcloud
*
Expand Down

0 comments on commit d1501ab

Please sign in to comment.