From 72b07245f44c73ed41bfcca2465a9ee34426a922 Mon Sep 17 00:00:00 2001 From: winter Date: Thu, 19 Dec 2024 20:59:18 +0000 Subject: implement follow relationships (untested) does NOT implement the actual behaviour behind follows (receiving posts and such) --- misc/send_request_authenticated.php | 66 +++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 misc/send_request_authenticated.php (limited to 'misc/send_request_authenticated.php') diff --git a/misc/send_request_authenticated.php b/misc/send_request_authenticated.php new file mode 100644 index 0000000..9019da8 --- /dev/null +++ b/misc/send_request_authenticated.php @@ -0,0 +1,66 @@ +getCanonicalHost()) { + throw new RuntimeException('refusing to fetch content from myself'); + } + + $instance = null; + if (!$dontLookUpInstance) { + $instance = Instance::findByHostname($remoteHost); + } + + if (!isset($instance->auth->outboundToken) || $instance->auth->outboundToken == null) { + // hopefully we can make the request anyway? + $context = stream_context_create(['http' => ['method' => $method]]); + $resp = file_get_contents($uri); + // $http_response_header just poofs into existence . + // i don't like this api. i should probably use curl instead. hmm + // TODO: use curl instead of file_get_contents + if (str_contains($http_response_header[0], '401') || str_contains($http_response_header[0], '403')) { + // we can't authenticate right now because it's asynchronous + // 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(); + } + $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' => $header + ]]); + $result = new stdClass(); + $result->body = file_get_contents($uri, context: $context); + $result->headers = $http_response_header; + return $result; +} \ No newline at end of file -- cgit v1.3