X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FUtil%2FHTTPSignature.php;h=07ff5e805d9b3293c921786d7ac195ac6ea02ff5;hb=e447375cddb847edbc9d4486be3938674ae66b8c;hp=408f4d978941e21bf42d023bef0b5a569e266cf3;hpb=2f28c2ebbfd8f66093d7081d40b1d48f65fcff20;p=friendica.git diff --git a/src/Util/HTTPSignature.php b/src/Util/HTTPSignature.php index 408f4d9789..07ff5e805d 100644 --- a/src/Util/HTTPSignature.php +++ b/src/Util/HTTPSignature.php @@ -90,7 +90,7 @@ class HTTPSignature $key = $key($sig_block['keyId']); } - Logger::log('Got keyID ' . $sig_block['keyId']); + Logger::log('Got keyID ' . $sig_block['keyId'], Logger::DEBUG); if (!$key) { return $result; @@ -204,6 +204,8 @@ class HTTPSignature if (preg_match('/algorithm="(.*?)"/ism', $header, $matches)) { $ret['algorithm'] = $matches[1]; + } else { + $ret['algorithm'] = 'rsa-sha256'; } if (preg_match('/headers="(.*?)"/ism', $header, $matches)) { @@ -308,11 +310,59 @@ class HTTPSignature $postResult = Network::post($target, $content, $headers); $return_code = $postResult->getReturnCode(); - Logger::log('Transmit to ' . $target . ' returned ' . $return_code); + Logger::log('Transmit to ' . $target . ' returned ' . $return_code, Logger::DEBUG); return ($return_code >= 200) && ($return_code <= 299); } + /** + * @brief Fetches JSON data for a user + * + * @param string $request request url + * @param integer $uid User id of the requester + * + * @return array JSON array + */ + public static function fetch($request, $uid) + { + $owner = User::getOwnerDataById($uid); + + if (!$owner) { + return; + } + + // Header data that is about to be signed. + $host = parse_url($request, PHP_URL_HOST); + $path = parse_url($request, PHP_URL_PATH); + + $headers = ['Host: ' . $host]; + + $signed_data = "(request-target): get " . $path . "\nhost: " . $host; + + $signature = base64_encode(Crypto::rsaSign($signed_data, $owner['uprvkey'], 'sha256')); + + $headers[] = 'Signature: keyId="' . $owner['url'] . '#main-key' . '",algorithm="rsa-sha256",headers="(request-target) host",signature="' . $signature . '"'; + + $headers[] = 'Accept: application/activity+json, application/ld+json'; + + $curlResult = Network::curl($request, false, $redirects, ['header' => $headers]); + $return_code = $curlResult->getReturnCode(); + + Logger::log('Fetched for user ' . $uid . ' from ' . $request . ' returned ' . $return_code, Logger::DEBUG); + + if (!$curlResult->isSuccess() || empty($curlResult->getBody())) { + return false; + } + + $content = json_decode($curlResult->getBody(), true); + + if (empty($content) || !is_array($content)) { + return false; + } + + return $content; + } + /** * @brief Gets a signer from a given HTTP request *