|
|
@ -2,19 +2,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
namespace App\Util\ActivityPub;
|
|
|
|
namespace App\Util\ActivityPub;
|
|
|
|
|
|
|
|
|
|
|
|
use Cache, Log;
|
|
|
|
|
|
|
|
use App\Models\InstanceActor;
|
|
|
|
use App\Models\InstanceActor;
|
|
|
|
use App\Profile;
|
|
|
|
use App\Profile;
|
|
|
|
use \DateTime;
|
|
|
|
use Cache;
|
|
|
|
|
|
|
|
use DateTime;
|
|
|
|
class HttpSignature {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class HttpSignature
|
|
|
|
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
/*
|
|
|
|
* source: https://github.com/aaronpk/Nautilus/blob/master/app/ActivityPub/HTTPSignature.php
|
|
|
|
* source: https://github.com/aaronpk/Nautilus/blob/master/app/ActivityPub/HTTPSignature.php
|
|
|
|
* thanks aaronpk!
|
|
|
|
* thanks aaronpk!
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
public static function sign(Profile $profile, $url, $body = false, $addlHeaders = []) {
|
|
|
|
public static function sign(Profile $profile, $url, $body = false, $addlHeaders = [])
|
|
|
|
|
|
|
|
{
|
|
|
|
if ($body) {
|
|
|
|
if ($body) {
|
|
|
|
$digest = self::_digest($body);
|
|
|
|
$digest = self::_digest($body);
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -56,7 +57,8 @@ class HttpSignature {
|
|
|
|
return $headers;
|
|
|
|
return $headers;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static function parseSignatureHeader($signature) {
|
|
|
|
public static function parseSignatureHeader($signature)
|
|
|
|
|
|
|
|
{
|
|
|
|
$parts = explode(',', $signature);
|
|
|
|
$parts = explode(',', $signature);
|
|
|
|
$signatureData = [];
|
|
|
|
$signatureData = [];
|
|
|
|
|
|
|
|
|
|
|
@ -68,26 +70,33 @@ class HttpSignature {
|
|
|
|
|
|
|
|
|
|
|
|
if (! isset($signatureData['keyId'])) {
|
|
|
|
if (! isset($signatureData['keyId'])) {
|
|
|
|
return [
|
|
|
|
return [
|
|
|
|
'error' => 'No keyId was found in the signature header. Found: '.implode(', ', array_keys($signatureData))
|
|
|
|
'error' => 'No keyId was found in the signature header. Found: '.implode(', ', array_keys($signatureData)),
|
|
|
|
];
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (! filter_var($signatureData['keyId'], FILTER_VALIDATE_URL)) {
|
|
|
|
if (! filter_var($signatureData['keyId'], FILTER_VALIDATE_URL)) {
|
|
|
|
return [
|
|
|
|
return [
|
|
|
|
'error' => 'keyId is not a URL: '.$signatureData['keyId']
|
|
|
|
'error' => 'keyId is not a URL: '.$signatureData['keyId'],
|
|
|
|
|
|
|
|
];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (! Helpers::validateUrl($signatureData['keyId'])) {
|
|
|
|
|
|
|
|
return [
|
|
|
|
|
|
|
|
'error' => 'keyId is not a URL: '.$signatureData['keyId'],
|
|
|
|
];
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (! isset($signatureData['headers']) || ! isset($signatureData['signature'])) {
|
|
|
|
if (! isset($signatureData['headers']) || ! isset($signatureData['signature'])) {
|
|
|
|
return [
|
|
|
|
return [
|
|
|
|
'error' => 'Signature is missing headers or signature parts'
|
|
|
|
'error' => 'Signature is missing headers or signature parts',
|
|
|
|
];
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return $signatureData;
|
|
|
|
return $signatureData;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static function verify($publicKey, $signatureData, $inputHeaders, $path, $body) {
|
|
|
|
public static function verify($publicKey, $signatureData, $inputHeaders, $path, $body)
|
|
|
|
|
|
|
|
{
|
|
|
|
$digest = 'SHA-256='.base64_encode(hash('sha256', $body, true));
|
|
|
|
$digest = 'SHA-256='.base64_encode(hash('sha256', $body, true));
|
|
|
|
$headersToSign = [];
|
|
|
|
$headersToSign = [];
|
|
|
|
foreach (explode(' ', $signatureData['headers']) as $h) {
|
|
|
|
foreach (explode(' ', $signatureData['headers']) as $h) {
|
|
|
@ -106,26 +115,31 @@ class HttpSignature {
|
|
|
|
return [$verified, $signingString];
|
|
|
|
return [$verified, $signingString];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static function _headersToSigningString($headers) {
|
|
|
|
private static function _headersToSigningString($headers)
|
|
|
|
|
|
|
|
{
|
|
|
|
return implode("\n", array_map(function ($k, $v) {
|
|
|
|
return implode("\n", array_map(function ($k, $v) {
|
|
|
|
return strtolower($k).': '.$v;
|
|
|
|
return strtolower($k).': '.$v;
|
|
|
|
}, array_keys($headers), $headers));
|
|
|
|
}, array_keys($headers), $headers));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static function _headersToCurlArray($headers) {
|
|
|
|
private static function _headersToCurlArray($headers)
|
|
|
|
|
|
|
|
{
|
|
|
|
return array_map(function ($k, $v) {
|
|
|
|
return array_map(function ($k, $v) {
|
|
|
|
return "$k: $v";
|
|
|
|
return "$k: $v";
|
|
|
|
}, array_keys($headers), $headers);
|
|
|
|
}, array_keys($headers), $headers);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static function _digest($body) {
|
|
|
|
private static function _digest($body)
|
|
|
|
|
|
|
|
{
|
|
|
|
if (is_array($body)) {
|
|
|
|
if (is_array($body)) {
|
|
|
|
$body = json_encode($body);
|
|
|
|
$body = json_encode($body);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return base64_encode(hash('sha256', $body, true));
|
|
|
|
return base64_encode(hash('sha256', $body, true));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected static function _headersToSign($url, $digest = false, $method = 'post') {
|
|
|
|
protected static function _headersToSign($url, $digest = false, $method = 'post')
|
|
|
|
|
|
|
|
{
|
|
|
|
$date = new DateTime('UTC');
|
|
|
|
$date = new DateTime('UTC');
|
|
|
|
|
|
|
|
|
|
|
|
if (! in_array($method, ['post', 'get'])) {
|
|
|
|
if (! in_array($method, ['post', 'get'])) {
|
|
|
@ -143,5 +157,4 @@ class HttpSignature {
|
|
|
|
|
|
|
|
|
|
|
|
return $headers;
|
|
|
|
return $headers;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|