X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FUtil%2FHTTPSignature.php;h=234d896078a6b1710df1885348ad25fdece2e610;hb=dc49ad090eca1bf4e511091418166da6fe68009b;hp=2d8254eeb801e15c1aa731abe23b905d0a47d4e0;hpb=355346298bc99c97fa98157701c3fe7ef4905e5c;p=friendica.git diff --git a/src/Util/HTTPSignature.php b/src/Util/HTTPSignature.php index 2d8254eeb8..234d896078 100644 --- a/src/Util/HTTPSignature.php +++ b/src/Util/HTTPSignature.php @@ -9,6 +9,7 @@ use Friendica\BaseObject; use Friendica\Core\Config; use Friendica\Database\DBA; use Friendica\Model\User; +use Friendica\Model\APContact; use Friendica\Protocol\ActivityPub; /** @@ -16,12 +17,22 @@ use Friendica\Protocol\ActivityPub; * * Ported from Hubzilla: https://framagit.org/hubzilla/core/blob/master/Zotlabs/Web/HTTPSig.php * + * Other parts of the code for HTTP signing are taken from the Osada project. + * https://framagit.org/macgirvin/osada + * * @see https://tools.ietf.org/html/draft-cavage-http-signatures-07 */ class HTTPSignature { // See draft-cavage-http-signatures-08 + /** + * @brief Verifies a magic request + * + * @param $key + * + * @return array with verification data + */ public static function verifyMagic($key) { $headers = null; @@ -254,10 +265,17 @@ class HTTPSignature return ''; } - /** + /* * Functions for ActivityPub */ + /** + * @brief Transmit given data to a target for a user + * + * @param $data + * @param $target + * @param $uid + */ public static function transmit($data, $target, $uid) { $owner = User::getOwnerDataById($uid); @@ -266,7 +284,7 @@ class HTTPSignature return; } - $content = json_encode($data); + $content = json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); // Header data that is about to be signed. $host = parse_url($target, PHP_URL_HOST); @@ -290,7 +308,15 @@ class HTTPSignature logger('Transmit to ' . $target . ' returned ' . $return_code); } - public static function verifyAP($content, $http_headers) + /** + * @brief Gets a signer from a given HTTP request + * + * @param $content + * @param $http_headers + * + * @return signer string + */ + public static function getSigner($content, $http_headers) { $object = json_decode($content, true); @@ -355,7 +381,7 @@ class HTTPSignature return false; } - if (!Crypto::rsaVerify($signed_data, $sig_block['signature'], $key, $algorithm)) { + if (!Crypto::rsaVerify($signed_data, $sig_block['signature'], $key['pubkey'], $algorithm)) { return false; } @@ -383,23 +409,30 @@ class HTTPSignature } } - return true; - + return $key['url']; } + /** + * @brief fetches a key for a given id and actor + * + * @param $id + * @param $actor + * + * @return array with actor url and public key + */ private static function fetchKey($id, $actor) { $url = (strpos($id, '#') ? substr($id, 0, strpos($id, '#')) : $id); - $profile = ActivityPub::fetchprofile($url); + $profile = APContact::getByURL($url); if (!empty($profile)) { logger('Taking key from id ' . $id, LOGGER_DEBUG); - return $profile['pubkey']; + return ['url' => $url, 'pubkey' => $profile['pubkey']]; } elseif ($url != $actor) { - $profile = ActivityPub::fetchprofile($actor); + $profile = APContact::getByURL($actor); if (!empty($profile)) { logger('Taking key from actor ' . $actor, LOGGER_DEBUG); - return $profile['pubkey']; + return ['url' => $actor, 'pubkey' => $profile['pubkey']]; } }