From 804ec2cb05c8f7d69bf6ac3ecf505d827102569b Mon Sep 17 00:00:00 2001 From: Mike Macgirvin Date: Sun, 30 Jun 2024 07:11:41 +1000 Subject: [PATCH] fixes for nomadic signing --- src/Lib/Activity.php | 79 +++++++++++++++++++------------------ src/Lib/ActivityStreams.php | 47 ++++++++++++---------- 2 files changed, 67 insertions(+), 59 deletions(-) diff --git a/src/Lib/Activity.php b/src/Lib/Activity.php index 86fd5ab4b..e84e3f14a 100644 --- a/src/Lib/Activity.php +++ b/src/Lib/Activity.php @@ -1461,7 +1461,7 @@ class Activity ); if ($d) { - if (in_array($d[0]['hubloc_network'], ['activitypub', 'apnomadic'])) { + if ($d[0]['hubloc_network'] === 'activitypub') { $addr = $d[0]['hubloc_hash']; } else { $addr = $d[0]['hubloc_id_url']; @@ -2666,47 +2666,48 @@ class Activity $baseurl = $collection['scheme'] . '://' . $collection['host'] . ((isset($collection['port']) && intval($collection['port'])) ? ':' . $collection['port'] : ''); } - if (!$h) { - hubloc_store_lowlevel([ - 'hubloc_guid' => $url, - 'hubloc_hash' => $url, - 'hubloc_id_url' => $profile, - 'hubloc_addr' => $webfingerAddress, - 'hubloc_network' => $isDid ? 'apnomadic' : 'activitypub', - 'hubloc_url' => $baseurl, - 'hubloc_host' => $hostname, - 'hubloc_callback' => $inbox, - 'hubloc_updated' => Time::convert(), - 'hubloc_primary' => 1 - ]); - } - else { - if ($webfingerAddress !== $h[0]['hubloc_addr']) { + if (! $isDid) { + if (!$h) { + hubloc_store_lowlevel([ + 'hubloc_guid' => $url, + 'hubloc_hash' => $url, + 'hubloc_id_url' => $profile, + 'hubloc_addr' => $webfingerAddress, + 'hubloc_network' => $isDid ? 'apnomadic' : 'activitypub', + 'hubloc_url' => $baseurl, + 'hubloc_host' => $hostname, + 'hubloc_callback' => $inbox, + 'hubloc_updated' => Time::convert(), + 'hubloc_primary' => 1 + ]); + } else { + if ($webfingerAddress !== $h[0]['hubloc_addr']) { + q( + "update hubloc set hubloc_addr = '%s' where hubloc_hash = '%s'", + dbesc($webfingerAddress), + dbesc($url) + ); + } + if ($inbox !== $h[0]['hubloc_callback']) { + q( + "update hubloc set hubloc_callback = '%s' where hubloc_hash = '%s'", + dbesc($inbox), + dbesc($url) + ); + } + if ($profile !== $h[0]['hubloc_id_url']) { + q( + "update hubloc set hubloc_id_url = '%s' where hubloc_hash = '%s'", + dbesc($profile), + dbesc($url) + ); + } q( - "update hubloc set hubloc_addr = '%s' where hubloc_hash = '%s'", - dbesc($webfingerAddress), + "update hubloc set hubloc_updated = '%s' where hubloc_hash = '%s'", + dbesc(Time::convert()), dbesc($url) ); } - if ($inbox !== $h[0]['hubloc_callback']) { - q( - "update hubloc set hubloc_callback = '%s' where hubloc_hash = '%s'", - dbesc($inbox), - dbesc($url) - ); - } - if ($profile !== $h[0]['hubloc_id_url']) { - q( - "update hubloc set hubloc_id_url = '%s' where hubloc_hash = '%s'", - dbesc($profile), - dbesc($url) - ); - } - q( - "update hubloc set hubloc_updated = '%s' where hubloc_hash = '%s'", - dbesc(Time::convert()), - dbesc($url) - ); } if (!$icon) { @@ -5012,7 +5013,7 @@ class Activity "select * from hubloc left join xchan on hubloc_hash = xchan_hash where hubloc_hash = '%s' $sql_options order by hubloc_id DESC ", dbesc($url) ), - 'zot6', 'nomad' => q( + 'zot6', 'nomad', 'apnomadic' => q( "select * from hubloc left join xchan on hubloc_hash = xchan_hash where hubloc_id_url = '%s' $sql_options order by hubloc_id DESC ", dbesc($url) ), diff --git a/src/Lib/ActivityStreams.php b/src/Lib/ActivityStreams.php index daf27e46c..dce5433c5 100644 --- a/src/Lib/ActivityStreams.php +++ b/src/Lib/ActivityStreams.php @@ -508,29 +508,36 @@ class ActivityStreams public function checkEddsaSignature() { $signer = $this->get_property_obj('verificationMethod', $this->edsig); - - $parseUrl = parse_url($signer); - if (!empty($parseUrl['fragment']) && str_starts_with($parseUrl['fragment'],'z6Mk')) { - $publicKey = $parseUrl['fragment']; - unset($parseUrl['fragment']); - unset($parseUrl['query']); + + if ($signer && str_starts_with($signer, 'did:key:')) { + $publicKey = str_replace('did:key:', '', $signer); + $this->signer = ['id' => $signer]; } - $url = unparse_url($parseUrl); - $this->signer = [ 'id' => $url ]; - $hublocs = Activity::get_actor_hublocs($url); - $hasStoredKey = false; - if ($hublocs) { - foreach ($hublocs as $hubloc) { - if ($publicKey && $hubloc['xchan_epubkey'] === $publicKey) { - $hasStoredKey = true; - break; + else { + $parseUrl = parse_url($signer); + if (!empty($parseUrl['fragment']) && str_starts_with($parseUrl['fragment'], 'z6Mk')) { + $publicKey = $parseUrl['fragment']; + unset($parseUrl['fragment']); + unset($parseUrl['query']); + } + $url = unparse_url($parseUrl); + $this->signer = ['id' => $url]; + + $hublocs = Activity::get_actor_hublocs($url); + $hasStoredKey = false; + if ($hublocs) { + foreach ($hublocs as $hubloc) { + if ($publicKey && $hubloc['xchan_epubkey'] === $publicKey) { + $hasStoredKey = true; + break; + } } } - } - if (! $hasStoredKey) { - $this->signer = Activity::fetch($url); - if ($this->signer && !empty($this->signer['assertionMethod'])) { - $publicKey = Activity::getEddsaPublicKey($this->signer); + if (!$hasStoredKey) { + $this->signer = Activity::fetch($url); + if ($this->signer && !empty($this->signer['assertionMethod'])) { + $publicKey = Activity::getEddsaPublicKey($this->signer); + } } } if ($publicKey) {