OpenWebAuth path is now fetched during probing

This commit is contained in:
Michael 2024-05-20 19:36:40 +00:00
parent ea5e1f1edc
commit da37516abf
13 changed files with 143 additions and 52 deletions

View file

@ -25,8 +25,10 @@ use Exception;
use Friendica\App;
use Friendica\BaseModule;
use Friendica\Core\L10n;
use Friendica\Core\Protocol;
use Friendica\Core\Session\Capability\IHandleUserSessions;
use Friendica\Core\System;
use Friendica\Core\Worker;
use Friendica\Database\Database;
use Friendica\Model\Contact;
use Friendica\Model\GServer;
@ -36,7 +38,7 @@ use Friendica\Network\HTTPClient\Client\HttpClientOptions;
use Friendica\Util\HTTPSignature;
use Friendica\Util\Profiler;
use Friendica\Util\Strings;
use GuzzleHttp\Psr7\Uri;
use Friendica\Worker\UpdateContact;
use Psr\Log\LoggerInterface;
/**
@ -115,22 +117,39 @@ class Magic extends BaseModule
$owner = User::getOwnerDataById($this->userSession->getLocalUserId());
if (!empty($contact['gsid'])) {
$gserver = $this->dba->selectFirst('gserver', ['url'], ['id' => $contact['gsid']]);
if (empty($gserver)) {
$this->logger->notice('Server not found, redirecting to destination.', ['gsid' => $contact['gsid'], 'dest' => $dest]);
System::externalRedirect($dest);
}
$basepath = $gserver['url'];
$gsid = $contact['gsid'];
} elseif (GServer::check($target)) {
$basepath = (string)GServer::cleanUri(new Uri($target));
} else {
$gsid = GServer::getID($target);
}
if (empty($gsid)) {
$this->logger->notice('The target is not a server path, redirecting to destination.', ['target' => $target]);
System::externalRedirect($dest);
}
$gserver = $this->dba->selectFirst('gserver', ['url', 'network', 'openwebauth'], ['id' => $gsid]);
if (empty($gserver)) {
$this->logger->notice('Server not found, redirecting to destination.', ['gsid' => $gsid, 'dest' => $dest]);
System::externalRedirect($dest);
}
$openwebauth = $gserver['openwebauth'];
// This part can be removed, when all server entries had been updated. So removing it in 2025 should be safe.
if (empty($openwebauth) && ($gserver['network'] == Protocol::DFRN)) {
$this->logger->notice('Open Web Auth path not provided. Assume default path', ['gsid' => $gsid, 'dest' => $dest]);
$openwebauth = $gserver['url'] . '/owa';
// Update contact to assign the path to the server
UpdateContact::add(Worker::PRIORITY_MEDIUM, $contact['id']);
}
if (empty($openwebauth)) {
$this->logger->debug('Server does not support open web auth, redirecting to destination.', ['gsid' => $gsid, 'dest' => $dest]);
System::externalRedirect($dest);
}
$header = [
'Accept' => 'application/x-dfrn+json, application/x-zot+json',
'Accept' => 'application/x-zot+json',
'X-Open-Web-Auth' => Strings::getRandomHex()
];
@ -141,13 +160,13 @@ class Magic extends BaseModule
'acct:' . $owner['addr']
);
$this->logger->info('Fetch from remote system', ['basepath' => $basepath, 'headers' => $header]);
$this->logger->info('Fetch from remote system', ['openwebauth' => $openwebauth, 'headers' => $header]);
// Try to get an authentication token from the other instance.
try {
$curlResult = $this->httpClient->request('get', $basepath . '/owa', [HttpClientOptions::HEADERS => $header]);
$curlResult = $this->httpClient->request('get', $openwebauth, [HttpClientOptions::HEADERS => $header]);
} catch (Exception $exception) {
$this->logger->notice('URL is invalid, redirecting to destination.', ['url' => $basepath, 'error' => $exception, 'dest' => $dest]);
$this->logger->notice('URL is invalid, redirecting to destination.', ['url' => $openwebauth, 'error' => $exception, 'dest' => $dest]);
System::externalRedirect($dest);
}
if (!$curlResult->isSuccess()) {

View file

@ -121,7 +121,7 @@ class Xrd extends BaseModule
'aliases' => [$owner['url']],
'links' => [
[
'rel' => 'http://webfinger.net/rel/profile-page',
'rel' => ActivityNamespace::WEBFINGERPROFILE,
'type' => 'text/html',
'href' => $owner['url'],
],
@ -131,7 +131,7 @@ class Xrd extends BaseModule
'href' => $owner['url'],
],
[
'rel' => 'http://ostatus.org/schema/1.0/subscribe',
'rel' => ActivityNamespace::OSTATUSSUB,
'template' => $baseURL . '/contact/follow?url={uri}',
],
[
@ -144,12 +144,12 @@ class Xrd extends BaseModule
'href' => $baseURL . '/salmon/' . $owner['nickname'],
],
[
'rel' => 'http://microformats.org/profile/hcard',
'rel' => ActivityNamespace::HCARD,
'type' => 'text/html',
'href' => $baseURL . '/hcard/' . $owner['nickname'],
],
[
'rel' => 'http://joindiaspora.com/seed_location',
'rel' => ActivityNamespace::DIASPORA_SEED,
'type' => 'text/html',
'href' => $baseURL,
],
@ -171,7 +171,7 @@ class Xrd extends BaseModule
],
'links' => [
[
'rel' => ActivityNamespace::DFRN ,
'rel' => ActivityNamespace::DFRN,
'href' => $owner['url'],
],
[
@ -180,7 +180,7 @@ class Xrd extends BaseModule
'href' => $owner['poll'],
],
[
'rel' => 'http://webfinger.net/rel/profile-page',
'rel' => ActivityNamespace::WEBFINGERPROFILE,
'type' => 'text/html',
'href' => $owner['url'],
],
@ -190,17 +190,17 @@ class Xrd extends BaseModule
'href' => $owner['url'],
],
[
'rel' => 'http://microformats.org/profile/hcard',
'rel' => ActivityNamespace::HCARD,
'type' => 'text/html',
'href' => $baseURL . '/hcard/' . $owner['nickname'],
],
[
'rel' => 'http://webfinger.net/rel/avatar',
'rel' => ActivityNamespace::WEBFINGERAVATAR,
'type' => $avatar['type'],
'href' => User::getAvatarUrl($owner),
],
[
'rel' => 'http://joindiaspora.com/seed_location',
'rel' => ActivityNamespace::DIASPORA_SEED,
'type' => 'text/html',
'href' => $baseURL,
],
@ -217,7 +217,7 @@ class Xrd extends BaseModule
'href' => $baseURL . '/salmon/' . $owner['nickname'] . '/mention',
],
[
'rel' => 'http://ostatus.org/schema/1.0/subscribe',
'rel' => ActivityNamespace::OSTATUSSUB,
'template' => $baseURL . '/contact/follow?url={uri}',
],
[
@ -225,7 +225,7 @@ class Xrd extends BaseModule
'href' => 'data:application/magic-public-key,' . Salmon::salmonKey($owner['spubkey']),
],
[
'rel' => 'http://purl.org/openwebauth/v1',
'rel' => ActivityNamespace::OPENWEBAUTH,
'type' => 'application/x-zot+json',
'href' => $baseURL . '/owa',
],
@ -263,28 +263,28 @@ class Xrd extends BaseModule
],
'3:link' => [
'@attributes' => [
'rel' => 'http://webfinger.net/rel/profile-page',
'rel' => ActivityNamespace::WEBFINGERPROFILE,
'type' => 'text/html',
'href' => $owner['url']
]
],
'4:link' => [
'@attributes' => [
'rel' => 'http://microformats.org/profile/hcard',
'rel' => ActivityNamespace::HCARD,
'type' => 'text/html',
'href' => $baseURL . '/hcard/' . $owner['nickname']
]
],
'5:link' => [
'@attributes' => [
'rel' => 'http://webfinger.net/rel/avatar',
'rel' => ActivityNamespace::WEBFINGERAVATAR,
'type' => $avatar['type'],
'href' => User::getAvatarUrl($owner)
]
],
'6:link' => [
'@attributes' => [
'rel' => 'http://joindiaspora.com/seed_location',
'rel' => ActivityNamespace::DIASPORA_SEED,
'type' => 'text/html',
'href' => $baseURL
]
@ -309,7 +309,7 @@ class Xrd extends BaseModule
],
'10:link' => [
'@attributes' => [
'rel' => 'http://ostatus.org/schema/1.0/subscribe',
'rel' => ActivityNamespace::OSTATUSSUB,
'template' => $baseURL . '/contact/follow?url={uri}'
]
],
@ -321,7 +321,7 @@ class Xrd extends BaseModule
],
'12:link' => [
'@attributes' => [
'rel' => 'http://purl.org/openwebauth/v1',
'rel' => ActivityNamespace::OPENWEBAUTH,
'type' => 'application/x-zot+json',
'href' => $baseURL . '/owa'
]