Update AP helpers

This commit is contained in:
Daniel Supernault 2018-11-23 19:32:05 -07:00
parent 19de0874c5
commit 943ae02f0e
No known key found for this signature in database
GPG key ID: 0DEF1C662C9033F7

View file

@ -20,7 +20,7 @@ use App\Jobs\AvatarPipeline\CreateAvatar;
use App\Jobs\RemoteFollowPipeline\RemoteFollowImportRecent; use App\Jobs\RemoteFollowPipeline\RemoteFollowImportRecent;
use App\Jobs\ImageOptimizePipeline\{ImageOptimize,ImageThumbnail}; use App\Jobs\ImageOptimizePipeline\{ImageOptimize,ImageThumbnail};
use App\Jobs\StatusPipeline\NewStatusPipeline; use App\Jobs\StatusPipeline\NewStatusPipeline;
use HttpSignatures\{GuzzleHttpSignatures, KeyStore, Context, Verifier}; use App\Util\HttpSignatures\{GuzzleHttpSignatures, KeyStore, Context, Verifier};
use Symfony\Bridge\PsrHttpMessage\Factory\DiactorosFactory; use Symfony\Bridge\PsrHttpMessage\Factory\DiactorosFactory;
class Helpers { class Helpers {
@ -309,31 +309,27 @@ class Helpers {
{ {
$profile = $senderProfile; $profile = $senderProfile;
$keyId = $profile->keyId(); $keyId = $profile->keyId();
$privateKey = openssl_pkey_get_private($profile->private_key);
$date = new \DateTime('UTC');
$date = date('D, d M Y h:i:s').' GMT'; $date = $date->format('D, d M Y H:i:s \G\M\T');
$host = parse_url($url, PHP_URL_HOST); $host = parse_url($url, PHP_URL_HOST);
$path = parse_url($url, PHP_URL_PATH);
$headers = [ $headers = [
'(request-target)' => 'post '.parse_url($url, PHP_URL_PATH), 'date' => $date,
'Date' => $date, 'host' => $host,
'Host' => $host, 'content-type' => 'application/activity+json',
'Content-Type' => 'application/activity+json',
]; ];
if($body) {
$payload = is_string($body) ? $body : json_encode($body); $context = new Context([
$digest = base64_encode(hash('sha256', $payload, true)); 'keys' => [$profile->keyId() => $profile->private_key],
$headers['Digest'] = 'SHA-256=' . $digest; 'algorithm' => 'rsa-sha256',
} 'headers' => ['(request-target)', 'date', 'host', 'content-type'],
$stringToSign = self::_headersToSigningString($headers); ]);
$signedHeaders = implode(' ', array_map('strtolower', array_keys($headers)));
openssl_sign($stringToSign, $signature, $privateKey, OPENSSL_ALGO_SHA256); $handlerStack = GuzzleHttpSignatures::defaultHandlerFromContext($context);
openssl_free_key($privateKey); $client = new Client(['handler' => $handlerStack]);
$signature = base64_encode($signature);
$signatureHeader = 'keyId="'.$keyId.'",headers="'.$signedHeaders.'",algorithm="rsa-sha256",signature="'.$signature.'"'; $response = $client->request('POST', $url, ['headers' => $headers, 'json' => $body]);
unset($headers['(request-target)']);
$headers['Signature'] = $signatureHeader;
Zttp::withHeaders($headers)->post($url, $body);
return; return;
} }