]> git.mxchange.org Git - friendica.git/blobdiff - src/Util/HTTPSignature.php
Rename DI::httpRequest() into DI::httpClient()
[friendica.git] / src / Util / HTTPSignature.php
index c8ff99eba0f5e6b68a1d359229dea8f86101bc57..89cb0452a3c2a1b0a8b93c798587e1d19e71c0e9 100644 (file)
@@ -29,6 +29,8 @@ use Friendica\Model\APContact;
 use Friendica\Model\Contact;
 use Friendica\Model\User;
 use Friendica\Network\CurlResult;
+use Friendica\Network\HTTPClientOptions;
+use Friendica\Network\IHTTPResult;
 
 /**
  * Implements HTTP Signatures per draft-cavage-http-signatures-07.
@@ -139,6 +141,9 @@ class HTTPSignature
        public static function createSig($head, $prvkey, $keyid = 'Key')
        {
                $return_headers = [];
+               if (!empty($head)) {
+                       $return_headers = $head;
+               }
 
                $alg = 'sha512';
                $algorithm = 'rsa-sha512';
@@ -148,15 +153,7 @@ class HTTPSignature
                $headerval = 'keyId="' . $keyid . '",algorithm="' . $algorithm
                        . '",headers="' . $x['headers'] . '",signature="' . $x['signature'] . '"';
 
-               $sighead = 'Authorization: Signature ' . $headerval;
-
-               if ($head) {
-                       foreach ($head as $k => $v) {
-                               $return_headers[] = $k . ': ' . $v;
-                       }
-               }
-
-               $return_headers[] = $sighead;
+               $return_headers['Authorization'] = ['Signature ' . $headerval];
 
                return $return_headers;
        }
@@ -175,6 +172,9 @@ class HTTPSignature
                $fields  = '';
 
                foreach ($head as $k => $v) {
+                       if (is_array($v)) {
+                               $v = implode(', ', $v);
+                       }
                        $headers .= strtolower($k) . ': ' . trim($v) . "\n";
                        if ($fields) {
                                $fields .= ' ';
@@ -290,17 +290,22 @@ class HTTPSignature
                $content_length = strlen($content);
                $date = DateTimeFormat::utcNow(DateTimeFormat::HTTP);
 
-               $headers = ['Date: ' . $date, 'Content-Length: ' . $content_length, 'Digest: ' . $digest, 'Host: ' . $host];
+               $headers = [
+                       'Date' => $date,
+                       'Content-Length' => $content_length,
+                       'Digest' => $digest,
+                       'Host' => $host
+               ];
 
                $signed_data = "(request-target): post " . $path . "\ndate: ". $date . "\ncontent-length: " . $content_length . "\ndigest: " . $digest . "\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) date content-length digest host",signature="' . $signature . '"';
+               $headers['Signature'] = 'keyId="' . $owner['url'] . '#main-key' . '",algorithm="rsa-sha256",headers="(request-target) date content-length digest host",signature="' . $signature . '"';
 
-               $headers[] = 'Content-Type: application/activity+json';
+               $headers['Content-Type'] = 'application/activity+json';
 
-               $postResult = DI::httpRequest()->post($target, $content, $headers);
+               $postResult = DI::httpClient()->post($target, $content, $headers);
                $return_code = $postResult->getReturnCode();
 
                Logger::log('Transmit to ' . $target . ' returned ' . $return_code, Logger::DEBUG);
@@ -409,10 +414,10 @@ class HTTPSignature
         *                         'nobody' => only return the header
         *                         'cookiejar' => path to cookie jar file
         *
-        * @return CurlResult|void CurlResult
+        * @return IHTTPResult CurlResult
         * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         */
-       public static function fetchRaw($request, $uid = 0, $opts = ['accept_content' => 'application/activity+json, application/ld+json'])
+       public static function fetchRaw($request, $uid = 0, $opts = ['accept_content' => ['application/activity+json', 'application/ld+json']])
        {
                $header = [];
 
@@ -434,26 +439,23 @@ class HTTPSignature
                        $path = parse_url($request, PHP_URL_PATH);
                        $date = DateTimeFormat::utcNow(DateTimeFormat::HTTP);
 
-                       $header = ['Date: ' . $date, 'Host: ' . $host];
+                       $header['Date'] = $date;
+                       $header['Host'] = $host;
 
                        $signed_data = "(request-target): get " . $path . "\ndate: ". $date . "\nhost: " . $host;
 
                        $signature = base64_encode(Crypto::rsaSign($signed_data, $owner['uprvkey'], 'sha256'));
 
-                       $header[] = 'Signature: keyId="' . $owner['url'] . '#main-key' . '",algorithm="rsa-sha256",headers="(request-target) date host",signature="' . $signature . '"';
-               }
-
-               if (!empty($opts['accept_content'])) {
-                       $header[] = 'Accept: ' . $opts['accept_content'];
+                       $header['Signature'] = 'keyId="' . $owner['url'] . '#main-key' . '",algorithm="rsa-sha256",headers="(request-target) date host",signature="' . $signature . '"';
                }
 
-               $curl_opts = $opts;
-               $curl_opts['header'] = $header;
+               $curl_opts                             = $opts;
+               $curl_opts[HTTPClientOptions::HEADERS] = $header;
 
                if (!empty($opts['nobody'])) {
-                       $curlResult = DI::httpRequest()->head($request, $curl_opts);
+                       $curlResult = DI::httpClient()->head($request, $curl_opts);
                } else {
-                       $curlResult = DI::httpRequest()->get($request, $curl_opts);
+                       $curlResult = DI::httpClient()->get($request, $curl_opts);
                }
                $return_code = $curlResult->getReturnCode();