From 8763bfc5c470162b6477b063a457157003e99727 Mon Sep 17 00:00:00 2001 From: Daniel Supernault Date: Sun, 26 Apr 2020 23:43:54 -0600 Subject: [PATCH] Add ActivityPubFetchService for signed GET requests --- app/Services/ActivityPubFetchService.php | 59 ++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 app/Services/ActivityPubFetchService.php diff --git a/app/Services/ActivityPubFetchService.php b/app/Services/ActivityPubFetchService.php new file mode 100644 index 00000000..765c6276 --- /dev/null +++ b/app/Services/ActivityPubFetchService.php @@ -0,0 +1,59 @@ + 'application/activity+json, application/json', + 'User-Agent' => 'PixelfedBot - https://pixelfed.org' + ]; + + public static function queue() + { + return new self; + } + + public function signed($signed = true) + { + $this->signed = $signed; + return $this; + } + + public function actor($profile) + { + $this->actor = $profile; + return $this; + } + + public function url($url) + { + if(!Helpers::validateUrl($url)) { + throw new \Exception('Invalid URL'); + } + $this->url = $url; + return $this; + } + + public function get() + { + if($this->signed == true && $this->actor == null) { + throw new \Exception('Cannot sign request without actor'); + } + return $this->signedRequest(); + } + + protected function signedRequest() + { + $this->headers = HttpSignature::sign($this->actor, $this->url, false, $this->headers); + return Zttp::withHeaders($this->headers)->get($this->url)->body(); + } +} \ No newline at end of file