Skip to content

Commit 1195fa3

Browse files
Merge pull request #56341 from nextcloud/backport/55648/stable32
[stable32] fix(ocm): align discovery process with OCM spec
2 parents 337290c + 0db7d64 commit 1195fa3

File tree

1 file changed

+35
-11
lines changed

1 file changed

+35
-11
lines changed

lib/private/OCM/OCMDiscoveryService.php

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -103,22 +103,46 @@ public function discover(string $remote, bool $skipCache = false): ICapabilityAw
103103
if ($this->config->getSystemValueBool('sharing.federation.allowSelfSignedCertificates') === true) {
104104
$options['verify'] = false;
105105
}
106-
$response = $client->get($remote . '/ocm-provider/', $options);
107-
108-
$body = null;
109-
if ($response->getStatusCode() === Http::STATUS_OK) {
110-
$body = $response->getBody();
111-
// update provider with data returned by the request
112-
$provider->import(json_decode($body, true, 8, JSON_THROW_ON_ERROR) ?? []);
113-
$this->cache->set($remote, $body, 60 * 60 * 24);
114-
$this->remoteProviders[$remote] = $provider;
115-
return $provider;
106+
$urls = [
107+
$remote . '/.well-known/ocm',
108+
$remote . '/ocm-provider',
109+
];
110+
111+
112+
foreach ($urls as $url) {
113+
$exception = null;
114+
$body = null;
115+
$status = null;
116+
try {
117+
$response = $client->get($url, $options);
118+
if ($response->getStatusCode() === Http::STATUS_OK) {
119+
$body = $response->getBody();
120+
$status = $response->getStatusCode();
121+
// update provider with data returned by the request
122+
$provider->import(json_decode($body, true, 8, JSON_THROW_ON_ERROR) ?? []);
123+
$this->cache->set($remote, $body, 60 * 60 * 24);
124+
$this->remoteProviders[$remote] = $provider;
125+
return $provider;
126+
}
127+
} catch (\Exception $e) {
128+
$this->logger->debug("Tried unsuccesfully to do discovery at: {$url}", [
129+
'exception' => $e,
130+
'remote' => $remote
131+
]);
132+
// We want to throw only the last exception
133+
$exception = $e;
134+
continue;
135+
}
136+
}
137+
if ($exception) {
138+
throw $exception;
116139
}
117140

141+
118142
throw new OCMProviderException('invalid remote ocm endpoint');
119143
} catch (JsonException|OCMProviderException) {
120144
$this->cache->set($remote, false, 5 * 60);
121-
throw new OCMProviderException('data returned by remote seems invalid - status:' . $response->getStatusCode() . ' - ' . ($body ?? ''));
145+
throw new OCMProviderException('data returned by remote seems invalid - status: ' . ($status ?? '') . ' - body: ' . ($body ?? ''));
122146
} catch (\Exception $e) {
123147
$this->cache->set($remote, false, 5 * 60);
124148
$this->logger->warning('error while discovering ocm provider', [

0 commit comments

Comments
 (0)