2021-08-24 20:29:26 +01:00
|
|
|
<?php
|
|
|
|
|
2021-10-10 09:26:18 +01:00
|
|
|
declare(strict_types = 1);
|
|
|
|
|
2021-08-24 20:29:26 +01:00
|
|
|
// {{{ License
|
|
|
|
|
|
|
|
// This file is part of GNU social - https://www.gnu.org/software/social
|
|
|
|
//
|
|
|
|
// GNU social is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// GNU social is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with GNU social. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
// }}}
|
|
|
|
|
|
|
|
namespace Plugin\ActivityPub\Controller;
|
|
|
|
|
|
|
|
use App\Core\Controller;
|
|
|
|
use App\Core\DB\DB;
|
2021-11-30 16:47:31 +00:00
|
|
|
use App\Core\Log;
|
|
|
|
use App\Core\Router\Router;
|
|
|
|
use App\Entity\Actor;
|
2021-12-02 04:25:58 +00:00
|
|
|
use Component\FreeNetwork\Entity\FreeNetworkActorProtocol;
|
2021-11-30 16:47:31 +00:00
|
|
|
use Exception;
|
|
|
|
use Plugin\ActivityPub\Entity\ActivitypubActor;
|
|
|
|
use Plugin\ActivityPub\Entity\ActivitypubRsa;
|
|
|
|
use Plugin\ActivityPub\Util\Explorer;
|
|
|
|
use Plugin\ActivityPub\Util\HTTPSignature;
|
2021-08-24 20:29:26 +01:00
|
|
|
use function App\Core\I18n\_m;
|
|
|
|
use App\Util\Exception\ClientException;
|
|
|
|
use Plugin\ActivityPub\ActivityPub;
|
2021-10-04 17:00:58 +01:00
|
|
|
use Plugin\ActivityPub\Util\Model\AS2ToEntity\AS2ToEntity;
|
|
|
|
use Plugin\ActivityPub\Util\Response\TypeResponse;
|
|
|
|
use Plugin\ActivityPub\Util\Type;
|
|
|
|
use Plugin\ActivityPub\Util\Type\Util;
|
2021-08-24 20:29:26 +01:00
|
|
|
|
|
|
|
class Inbox extends Controller
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Inbox handler
|
|
|
|
*/
|
2021-10-18 13:22:02 +01:00
|
|
|
public function handle(?int $gsactor_id = null): TypeResponse
|
2021-08-24 20:29:26 +01:00
|
|
|
{
|
2021-11-30 16:47:31 +00:00
|
|
|
$path = Router::url('activitypub_inbox', type: Router::ABSOLUTE_PATH);
|
|
|
|
|
2021-10-10 09:26:18 +01:00
|
|
|
if (!\is_null($gsactor_id)) {
|
2021-11-30 16:47:31 +00:00
|
|
|
try {
|
|
|
|
$user = DB::findOneBy('local_user', ['id' => $gsactor_id]);
|
|
|
|
$path = Router::url('activitypub_actor_inbox', ['gsactor_id' => $user->getId()], type: Router::ABSOLUTE_PATH);
|
|
|
|
} catch (Exception $e) {
|
|
|
|
throw new ClientException(_m('No such actor.'), 404, $e);
|
2021-09-20 17:03:23 +01:00
|
|
|
}
|
2021-08-24 20:29:26 +01:00
|
|
|
}
|
|
|
|
|
2021-11-30 16:47:31 +00:00
|
|
|
Log::debug('ActivityPub Inbox: Received a POST request.');
|
|
|
|
$body = (string) $this->request->getContent();
|
|
|
|
$type = Type::fromJson($body);
|
2021-08-24 20:29:26 +01:00
|
|
|
|
2021-11-30 16:47:31 +00:00
|
|
|
if ($type->has('actor') === false) {
|
|
|
|
ActivityPubReturn::error('Actor not found in the request.');
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
$ap_actor = ActivitypubActor::fromUri($type->get('actor'));
|
|
|
|
$actor = Actor::getById($ap_actor->getActorId());
|
|
|
|
DB::flush();
|
|
|
|
} catch (Exception) {
|
|
|
|
ActivityPubReturn::error('Invalid actor.');
|
|
|
|
}
|
|
|
|
|
|
|
|
$actor_public_key = ActivitypubRsa::getByActor($actor)->getPublicKey();
|
|
|
|
|
|
|
|
$headers = $this->request->headers->all();
|
|
|
|
// Flattify headers
|
|
|
|
foreach ($headers as $key => $val) {
|
|
|
|
$headers[$key] = $val[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isset($headers['signature'])) {
|
|
|
|
Log::debug('ActivityPub Inbox: HTTP Signature: Missing Signature header.');
|
|
|
|
ActivityPubReturn::error('Missing Signature header.', 400);
|
|
|
|
// TODO: support other methods beyond HTTP Signatures
|
|
|
|
}
|
|
|
|
|
|
|
|
// Extract the signature properties
|
|
|
|
$signatureData = HTTPSignature::parseSignatureHeader($headers['signature']);
|
|
|
|
Log::debug('ActivityPub Inbox: HTTP Signature Data: ' . print_r($signatureData, true));
|
|
|
|
if (isset($signatureData['error'])) {
|
|
|
|
Log::debug('ActivityPub Inbox: HTTP Signature: ' . json_encode($signatureData, JSON_PRETTY_PRINT));
|
|
|
|
ActivityPubReturn::error(json_encode($signatureData, JSON_PRETTY_PRINT), 400);
|
|
|
|
}
|
|
|
|
|
|
|
|
list($verified, /*$headers*/) = HTTPSignature::verify($actor_public_key, $signatureData, $headers, $path, $body);
|
|
|
|
|
|
|
|
// If the signature fails verification the first time, update profile as it might have changed public key
|
|
|
|
if ($verified !== 1) {
|
|
|
|
try {
|
|
|
|
$res = Explorer::get_remote_user_activity($ap_actor->getUri());
|
|
|
|
} catch (Exception) {
|
|
|
|
ActivityPubReturn::error('Invalid remote actor.');
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
$actor = ActivitypubActor::update_profile($ap_actor, $res);
|
|
|
|
} catch (Exception) {
|
|
|
|
ActivityPubReturn::error('Failed to updated remote actor information.');
|
|
|
|
}
|
|
|
|
|
|
|
|
[$verified, /*$headers*/] = HTTPSignature::verify($actor_public_key, $signatureData, $headers, $path, $body);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If it still failed despite profile update
|
|
|
|
if ($verified !== 1) {
|
|
|
|
Log::debug('ActivityPub Inbox: HTTP Signature: Invalid signature.');
|
|
|
|
ActivityPubReturn::error('Invalid signature.');
|
|
|
|
}
|
|
|
|
|
|
|
|
// HTTP signature checked out, make sure the "actor" of the activity matches that of the signature
|
|
|
|
Log::debug('ActivityPub Inbox: HTTP Signature: Authorized request. Will now start the inbox handler.');
|
2021-08-24 20:29:26 +01:00
|
|
|
|
2021-10-04 17:00:58 +01:00
|
|
|
// TODO: Check if Actor has authority over payload
|
|
|
|
|
|
|
|
// Store Activity
|
2021-11-27 15:06:46 +00:00
|
|
|
$ap_act = AS2ToEntity::store(activity: $type->toArray(), source: 'ActivityPub');
|
2021-12-02 04:25:58 +00:00
|
|
|
FreeNetworkActorProtocol::protocolSucceeded('activitypub', $actor->getId());
|
2021-10-04 17:00:58 +01:00
|
|
|
DB::flush();
|
2021-11-27 15:06:46 +00:00
|
|
|
dd($ap_act, $act = $ap_act->getActivity(), $act->getActor(), $act->getObject());
|
2021-08-24 20:29:26 +01:00
|
|
|
|
|
|
|
return new TypeResponse($type, status: 202);
|
|
|
|
}
|
|
|
|
}
|