Skip to content
Snippets Groups Projects
ActivityRequest.php 3.46 KiB
Newer Older
/**
 * @copyright  2021 Podlibre
 * @license    https://www.gnu.org/licenses/agpl-3.0.en.html AGPL3
 * @link       https://castopod.org/
 */

use CodeIgniter\HTTP\ResponseInterface;
use phpseclib\Crypt\RSA;

class ActivityRequest
{
    public function __construct(string $uri, ?string $activityPayload = null)
        if ($activityPayload !== null) {
            $this->request->setBody($activityPayload);
        }

        $this->options = [
            'headers' => [
                'Content-Type' => 'application/activity+json',
                'Accept' => 'application/activity+json',
                'User-Agent' => 'Castopod/' . CP_VERSION . '; +' . base_url('', 'https'),
                // TODO: outgoing and incoming requests
            ],
        ];

        $this->request->post((string) $this->uri, $this->options);
        return $this->request->get((string) $this->uri, $this->options);
    {
        return $this->uri->getHost() .
            ($this->uri->getPort() ? ':' . $this->uri->getPort() : '');
    }

    public function sign(string $keyId, string $privateKey): void
        $rsa->setHash('sha256');
        $rsa->setSignatureMode(RSA::SIGNATURE_PKCS1);

        $path =
            $this->uri->getPath() .
            ($this->uri->getQuery() !== '' ? "?{$this->uri->getQuery()}" : '');
        $host = $this->uri->getHost();
        $date = Time::now('GMT')->format('D, d M Y H:i:s T');
        $digest = 'SHA-256=' . base64_encode($this->getBodyDigest());
        $contentType = $this->options['headers']['Content-Type'];
        $contentLength = (string) strlen($this->request->getBody());
        $userAgent = 'Castopod/' . CP_VERSION . '; +' . base_url('', 'https');
        $plainText = "(request-target): post {$path}\nhost: {$host}\ndate: {$date}\ndigest: {$digest}\ncontent-type: {$contentType}\ncontent-length: {$contentLength}\nuser-agent: {$userAgent}";

        $signature = $rsa->sign($plainText);

        $signatureHeader =
            'keyId="' .
            $keyId .
            '",algorithm="rsa-sha256",headers="(request-target) host date digest content-type content-length user-agent",signature="' .
            base64_encode($signature) .
            '"';

        $this->options = [
            'headers' => [
                'Content-Type' => $contentType,
                'Content-Length' => $contentLength,
                'Authorization' => "Signature {$signatureHeader}",
                'Signature' => $signatureHeader,
                'Host' => $host,
                'Date' => $date,
                'User-Agent' => $userAgent,
                'Digest' => $digest,
            ],
        ];
    }

    {
        return hash('sha256', $this->request->getBody(), true);
    }
}