Skip to content
Merged
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
46 changes: 35 additions & 11 deletions lib/private/OCM/OCMDiscoveryService.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,22 +103,46 @@ public function discover(string $remote, bool $skipCache = false): ICapabilityAw
if ($this->config->getSystemValueBool('sharing.federation.allowSelfSignedCertificates') === true) {
$options['verify'] = false;
}
$response = $client->get($remote . '/ocm-provider/', $options);

$body = null;
if ($response->getStatusCode() === Http::STATUS_OK) {
$body = $response->getBody();
// update provider with data returned by the request
$provider->import(json_decode($body, true, 8, JSON_THROW_ON_ERROR) ?? []);
$this->cache->set($remote, $body, 60 * 60 * 24);
$this->remoteProviders[$remote] = $provider;
return $provider;
$urls = [
$remote . '/.well-known/ocm',
$remote . '/ocm-provider',
];


foreach ($urls as $url) {
$exception = null;
$body = null;
$status = null;
try {
$response = $client->get($url, $options);
if ($response->getStatusCode() === Http::STATUS_OK) {
$body = $response->getBody();
$status = $response->getStatusCode();
// update provider with data returned by the request
$provider->import(json_decode($body, true, 8, JSON_THROW_ON_ERROR) ?? []);
$this->cache->set($remote, $body, 60 * 60 * 24);
$this->remoteProviders[$remote] = $provider;
return $provider;
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if an else-case would be good in order to show that some providers were not able to be discovered or did not answer

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think this is catched by the OCMProviderException from L142

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not talking about an exception here, the request might work without an exception, but just return status code 500 or 422 or 404 or does $client->get throw always an exception if the status code is not 200?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the import() will returns an exception if the data returned from the remote instance are not valid. We could/should add some management regarding the status code, but this is not the purpose of this PR.

} catch (\Exception $e) {
$this->logger->debug("Tried unsuccesfully to do discovery at: {$url}", [
'exception' => $e,
'remote' => $remote
]);
// We want to throw only the last exception
$exception = $e;
continue;
}
}
if ($exception) {
throw $exception;
}


throw new OCMProviderException('invalid remote ocm endpoint');
} catch (JsonException|OCMProviderException) {
$this->cache->set($remote, false, 5 * 60);
throw new OCMProviderException('data returned by remote seems invalid - status:' . $response->getStatusCode() . ' - ' . ($body ?? ''));
throw new OCMProviderException('data returned by remote seems invalid - status: ' . ($status ?? '') . ' - body: ' . ($body ?? ''));
} catch (\Exception $e) {
$this->cache->set($remote, false, 5 * 60);
$this->logger->warning('error while discovering ocm provider', [
Expand Down
Loading