diff options
Diffstat (limited to 'misc')
| -rw-r--r-- | misc/send_request_authenticated.php (renamed from misc/get_remote_object_authenticated.php) | 36 |
1 files changed, 26 insertions, 10 deletions
diff --git a/misc/get_remote_object_authenticated.php b/misc/send_request_authenticated.php index 9f2db43..9019da8 100644 --- a/misc/get_remote_object_authenticated.php +++ b/misc/send_request_authenticated.php @@ -1,6 +1,7 @@ <?php use Digitigrade\GlobalConfig; +use Digitigrade\Model\Actor; use Digitigrade\Model\Instance; /** @@ -8,16 +9,22 @@ use Digitigrade\Model\Instance; * @param string $uri * @param bool $dontLookUpInstance if true, will skip looking up the Instance, and consequently the auth token * @param string $method HTTP verb - * @return false|string the response data, or false if it failed + * @param ?Actor $actingAs if specified, the local actor to send the request on behalf of + * @return stdClass object with fields `body` and `headers` */ -function get_remote_object_authenticated(string $uri, bool $dontLookUpInstance = false, string $method = 'GET'): false|string { - if (!str_starts_with($uri, 'https://')) - return false; +function send_request_authenticated( + string $uri, + bool $dontLookUpInstance = false, + string $method = 'GET', + ?Actor $actingAs = null +) { + if (!str_starts_with($uri, 'https://')) { + throw new RuntimeException('refusing to fetch a non-https uri'); + } $remoteHost = hostname_from_uri($uri); if ($remoteHost == GlobalConfig::getInstance()->getCanonicalHost()) { - // refuse to fetch objects from ourself. that's silly - return false; + throw new RuntimeException('refusing to fetch content from myself'); } $instance = null; @@ -37,14 +44,23 @@ function get_remote_object_authenticated(string $uri, bool $dontLookUpInstance = // so i'm choosing to begin the auth process now and return false in the hopes that // this request will be retried later $instance?->beginOutboundAuth(); - return false; } - return $resp; + $result = new stdClass(); + $result->body = $resp; + $result->headers = $http_response_header; + return $result; } + $header = 'Authorization: Bearer ' . $instance->auth->outboundToken; + if ($actingAs != null) { + $header .= "\nX-PawPub-Actor: $actingAs->uri"; + } $context = stream_context_create(['http' => [ 'method' => $method, - 'header' => 'Authorization: Bearer ' . $instance->auth->outboundToken + 'header' => $header ]]); - return file_get_contents($uri, context: $context); + $result = new stdClass(); + $result->body = file_get_contents($uri, context: $context); + $result->headers = $http_response_header; + return $result; }
\ No newline at end of file |
