1
0
Fork 0

Support for fetching non-public content / preparations for forum posts

This commit is contained in:
Michael 2018-11-03 21:37:08 +00:00
commit 8f27e3aeb1
5 changed files with 188 additions and 30 deletions

View file

@ -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;
@ -308,11 +308,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
*

View file

@ -83,6 +83,7 @@ class Network
* 'novalidate' => do not validate SSL certs, default is to validate using our CA list
* 'nobody' => only return the header
* 'cookiejar' => path to cookie jar file
* 'header' => header array
*
* @return CurlResult
*/
@ -136,6 +137,10 @@ class Network
);
}
if (!empty($opts['header'])) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $opts['header']);
}
@curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
@curl_setopt($ch, CURLOPT_USERAGENT, $a->getUserAgent());