mirror of
https://github.com/pixelfed/pixelfed.git
synced 2024-11-10 00:34:50 +00:00
Add ActivityPubFetchService for signed GET requests
This commit is contained in:
parent
6131907290
commit
8763bfc5c4
1 changed files with 59 additions and 0 deletions
59
app/Services/ActivityPubFetchService.php
Normal file
59
app/Services/ActivityPubFetchService.php
Normal file
|
@ -0,0 +1,59 @@
|
|||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use Zttp\Zttp;
|
||||
use App\Profile;
|
||||
use App\Util\ActivityPub\Helpers;
|
||||
use App\Util\ActivityPub\HttpSignature;
|
||||
|
||||
class ActivityPubFetchService
|
||||
{
|
||||
public $signed = true;
|
||||
public $actor;
|
||||
public $url;
|
||||
public $headers = [
|
||||
'Accept' => '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();
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue