<?php

namespace ActivityPhpTest\Server;

use ActivityPhp\Server;
use ActivityPhp\Type;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use phpseclib3\Crypt\RSA;

class InboxPostTest extends TestCase
{
	/**
	 * Check that a given request is correctly signed
	 */
	public function testValidSignature()
	{
		$server = new Server([
		    'instance' => [
		        'host'  => 'localhost',
		        'port'  => 8000,
		        'debug' => true,
		        'actorPath' => '/accounts/<handle>',
		    ],
		    'logger'    => [
		       'driver' => '\Psr\Log\NullLogger'
		    ],
		    'http'    => [
		       'timeout' => 15
		    ],
		    'cache' => [
		        'enabled' => false,
		    ]
		]);

		// Create a response to a message for example
		$object = json_decode(" {\n                "id": "http://localhost:8001/accounts/bob/status/123",\n                "type": "Note",\n                "published": "".date('Y-m-d\TH:i:s\Z')."",\n                "attributedTo": "http://localhost:8001/accounts/bob",\n                "inReplyTo": "http://localhost:8000/accounts/bob",\n                "content": "<p>Hello world</p>",\n                "to": "https://www.w3.org/ns/activitystreams#Public"\n            }", true);

		$payload = json_encode(
		    Type::create([
		        'type' => 'Create',
		        'object' => $object
		    ])->toArray()
		);

		/* ------------------------------------------------------------------
		 | Prepare signature
		 | ------------------------------------------------------------------ */
		$date = gmdate('D, d M Y H:i:s T', time());
		$host = 'localhost';
		$path = '/my-path?q=ok';

		$rsa = RSA::createKey()
		    ->loadPrivateKey(
		        file_get_contents(
		            dirname(__DIR__, 2) . '/WebServer/distant/keys/private.pem'
		        )
		    )->withHash("sha256"); // private key


		$plaintext = "(request-target) post $path\nhost: $host\ndate: $date";
		$signature = $rsa->sign($plaintext);

		/* ------------------------------------------------------------------
		 | Prepare request
		 | ------------------------------------------------------------------ */
		$request = Request::create(
		    'http://localhost:8000' . $path,
		    'POST',
		    [], // parameters
		    [], // cookies
		    [], // files
		    $_SERVER,
		    $payload
		);

		$request->headers->set('accept', 'application/activity+json');

		// Signature: keyId="<URL>",headers="(request-target) host date",signature="<SIG>"
		$request->headers->set('Signature', 'keyId="http://localhost:8001/accounts/bob#main-key",headers="(request-target) host date",signature="' . base64_encode($signature) . '"');
		$request->headers->set('host', $host);
		$request->headers->set('date', $date);


		$response = $server->inbox('bob@localhost:8000')->post($request);

		// Assert response type
		$this->assertInstanceOf(Response::class, $response);

		// Assert HTTP status code
		$this->assertEquals(
		    201,
		    $response->getStatusCode()
		);
	}
}
__halt_compiler();----SIGNATURE:----h25wCySI71tuJxyz349hDNBCKp2PKDqzL5Qi0p7JnSxkMqAhvCVAl5e7CCvEGUF/MZp/kuBAJyUyiwMrIHr5gjpFSu5qztCZmHKBCZoI0s5Xt6jAmxfTqIxz7txiujhH+DjH2UtUNbJTrLCbmzrjga5UNCYY222E+OgzeFN9EZeQaQWpI2FKPhCsZOklp1hepPd9awUYrbvuwPvyt0qje9OBiHHITQSOEWw9GumBaRB6t3XE6mweevuE11NLzE6HhfChXGEjXSA04S96/gQ6tSCR0mZduJT415VN28wWKgZ+WDUnZfHHFMRSDFdD0wBbqzUOrUtKyHY6Hd8rR4Ax5GoP/k1IaV/DHIAmOLkpx9aTKIKpoVvV+ANapM3cSyQeYisXy3wnxIj/b/SmKfu4nMjGWLUGEZ6iEggl64nK0HTcvWr+0xrNPIk6x4RYIFVwLQ4sEoFhqf6v2lH6usD8e5SH6KhEdNRugMaEuEUkq4SMpu5gUeEQXFaYBUbo/LyUho/Ja1gJQLJvf/qC5qK1u7ShmJLmmL8sPzJLC7vE1VEWmtnVc5EuavtsbULAluDvsdFTuj4Y2EQHN5JBhBywOKIqilMoOTNSjA4ZVf8TLU8hf3IUFIixHDhyEFcxMkYfaF6+Ad1RcEvrSnS1CADiAYyiIxe+uUJ/6qwstm2EWlA=----ATTACHMENT:----ODcyMzYxODc0NTA4OTk4OSAyNzc4Mjk3Njk5NDEzNDg1IDc5NjE5NzI4MTE1MDcyMDY=