| 
									
										
										
										
											2021-08-24 20:29:26 +01:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-24 01:58:41 +00:00
										 |  |  | declare(strict_types = 1); | 
					
						
							| 
									
										
										
										
											2021-10-10 09:26:18 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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/>.
 | 
					
						
							|  |  |  | // }}}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-04 04:07:08 +00:00
										 |  |  | /** | 
					
						
							|  |  |  |  * ActivityPub implementation for GNU social | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @package   GNUsocial | 
					
						
							|  |  |  |  * @category  ActivityPub | 
					
						
							| 
									
										
										
										
											2021-12-24 01:58:41 +00:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2021-12-04 04:07:08 +00:00
										 |  |  |  * @author    Diogo Peralta Cordeiro <@diogo.site> | 
					
						
							|  |  |  |  * @copyright 2018-2019, 2021 Free Software Foundation, Inc http://www.fsf.org | 
					
						
							|  |  |  |  * @license   https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-24 20:29:26 +01:00
										 |  |  | namespace Plugin\ActivityPub\Controller; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | use App\Core\Controller; | 
					
						
							|  |  |  | use App\Core\DB\DB; | 
					
						
							| 
									
										
										
										
											2021-12-24 01:58:41 +00:00
										 |  |  | use function App\Core\I18n\_m; | 
					
						
							| 
									
										
										
										
											2021-11-30 16:47:31 +00:00
										 |  |  | use App\Core\Log; | 
					
						
							| 
									
										
										
										
											2022-03-05 14:23:08 +00:00
										 |  |  | use App\Core\Queue\Queue; | 
					
						
							| 
									
										
										
										
											2021-11-30 16:47:31 +00:00
										 |  |  | use App\Core\Router\Router; | 
					
						
							|  |  |  | use App\Entity\Actor; | 
					
						
							| 
									
										
										
										
											2022-02-25 01:05:28 +00:00
										 |  |  | use App\Util\Common; | 
					
						
							| 
									
										
										
										
											2021-12-04 04:07:08 +00:00
										 |  |  | use App\Util\Exception\ClientException; | 
					
						
							| 
									
										
										
										
											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-12-04 04:07:08 +00:00
										 |  |  | use Plugin\ActivityPub\Util\Model; | 
					
						
							|  |  |  | use Plugin\ActivityPub\Util\TypeResponse; | 
					
						
							| 
									
										
										
										
											2022-02-25 01:05:28 +00:00
										 |  |  | use Symfony\Component\HttpFoundation\Request; | 
					
						
							| 
									
										
										
										
											2021-12-04 04:07:08 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * ActivityPub Inbox Handler | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @copyright 2018-2019, 2021 Free Software Foundation, Inc http://www.fsf.org | 
					
						
							|  |  |  |  * @license   https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2021-08-24 20:29:26 +01:00
										 |  |  | class Inbox extends Controller | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2022-02-20 05:03:05 +00:00
										 |  |  |     public function onGet(Request $request, ?int $gsactor_id = null): TypeResponse | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return new TypeResponse(json_encode(['error' => 'No AP C2S inbox yet.']), 400); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-24 20:29:26 +01:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2021-12-04 04:07:08 +00:00
										 |  |  |      * Create an Inbox Handler to receive something from someone. | 
					
						
							| 
									
										
										
										
											2021-08-24 20:29:26 +01:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2022-02-20 05:03:05 +00:00
										 |  |  |     public function onPost(Request $request, ?int $gsactor_id = null): TypeResponse | 
					
						
							| 
									
										
										
										
											2021-08-24 20:29:26 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-12-19 19:45:38 +00:00
										 |  |  |         $error = function (string $m, ?Exception $e = null): TypeResponse { | 
					
						
							|  |  |  |             Log::error('ActivityPub Error Answer: ' . ($json = json_encode(['error' => $m, 'exception' => var_export($e, true)]))); | 
					
						
							| 
									
										
										
										
											2022-02-25 01:05:28 +00:00
										 |  |  |             if (\is_null($e)) { | 
					
						
							| 
									
										
										
										
											2022-02-17 22:47:37 +00:00
										 |  |  |                 return new TypeResponse($json, 400); | 
					
						
							|  |  |  |             } else { | 
					
						
							|  |  |  |                 throw $e; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2021-12-19 19:45:38 +00:00
										 |  |  |         }; | 
					
						
							| 
									
										
										
										
											2021-11-30 16:47:31 +00:00
										 |  |  |         $path = Router::url('activitypub_inbox', type: Router::ABSOLUTE_PATH); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-24 01:58:41 +00: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) { | 
					
						
							| 
									
										
										
										
											2022-02-17 22:47:37 +00:00
										 |  |  |                 throw new ClientException(_m('No such actor.'), 404, previous: $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.'); | 
					
						
							| 
									
										
										
										
											2021-12-24 01:58:41 +00:00
										 |  |  |         $body = (string) $this->request->getContent(); | 
					
						
							|  |  |  |         Log::debug('ActivityPub Inbox: Request Body content: ' . $body); | 
					
						
							| 
									
										
										
										
											2021-12-04 04:07:08 +00:00
										 |  |  |         $type = Model::jsonToType($body); | 
					
						
							| 
									
										
										
										
											2021-08-24 20:29:26 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-30 16:47:31 +00:00
										 |  |  |         if ($type->has('actor') === false) { | 
					
						
							| 
									
										
										
										
											2021-12-05 03:11:08 +00:00
										 |  |  |             return $error('Actor not found in the request.'); | 
					
						
							| 
									
										
										
										
											2021-11-30 16:47:31 +00:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         try { | 
					
						
							| 
									
										
										
										
											2021-12-08 22:24:52 +00:00
										 |  |  |             $resource_parts = parse_url($type->get('actor')); | 
					
						
							| 
									
										
										
										
											2022-02-11 00:17:20 +00:00
										 |  |  |             if ($resource_parts['host'] !== Common::config('site', 'server')) { | 
					
						
							| 
									
										
										
										
											2022-02-26 14:45:38 +00:00
										 |  |  |                 $actor    = DB::wrapInTransaction(fn () => Explorer::getOneFromUri($type->get('actor'))); | 
					
						
							| 
									
										
										
										
											2022-02-25 01:05:28 +00:00
										 |  |  |                 $ap_actor = DB::findOneBy(ActivitypubActor::class, ['actor_id' => $actor->getId()]); | 
					
						
							| 
									
										
										
										
											2021-12-08 22:24:52 +00:00
										 |  |  |             } else { | 
					
						
							|  |  |  |                 throw new Exception('Only remote actors can use this endpoint.'); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             unset($resource_parts); | 
					
						
							| 
									
										
										
										
											2021-12-04 04:07:08 +00:00
										 |  |  |         } catch (Exception $e) { | 
					
						
							| 
									
										
										
										
											2021-12-19 19:45:38 +00:00
										 |  |  |             return $error('Invalid actor.', $e); | 
					
						
							| 
									
										
										
										
											2021-11-30 16:47:31 +00:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-24 01:58:41 +00:00
										 |  |  |         $activitypub_rsa  = ActivitypubRsa::getByActor($actor); | 
					
						
							| 
									
										
										
										
											2021-12-05 03:11:08 +00:00
										 |  |  |         $actor_public_key = $activitypub_rsa->getPublicKey(); | 
					
						
							| 
									
										
										
										
											2021-11-30 16:47:31 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         $headers = $this->request->headers->all(); | 
					
						
							| 
									
										
										
										
											2022-02-17 22:47:37 +00:00
										 |  |  |         Log::debug('ActivityPub Inbox: Request Headers.', [$headers]); | 
					
						
							| 
									
										
										
										
											2021-11-30 16:47:31 +00:00
										 |  |  |         // Flattify headers
 | 
					
						
							|  |  |  |         foreach ($headers as $key => $val) { | 
					
						
							|  |  |  |             $headers[$key] = $val[0]; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (!isset($headers['signature'])) { | 
					
						
							|  |  |  |             Log::debug('ActivityPub Inbox: HTTP Signature: Missing Signature header.'); | 
					
						
							| 
									
										
										
										
											2021-12-19 19:45:38 +00:00
										 |  |  |             return $error('Missing Signature header.'); | 
					
						
							| 
									
										
										
										
											2021-11-30 16:47:31 +00:00
										 |  |  |             // TODO: support other methods beyond HTTP Signatures
 | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // Extract the signature properties
 | 
					
						
							|  |  |  |         $signatureData = HTTPSignature::parseSignatureHeader($headers['signature']); | 
					
						
							| 
									
										
										
										
											2022-02-17 22:47:37 +00:00
										 |  |  |         Log::debug('ActivityPub Inbox: HTTP Signature Data.', [$signatureData]); | 
					
						
							| 
									
										
										
										
											2021-11-30 16:47:31 +00:00
										 |  |  |         if (isset($signatureData['error'])) { | 
					
						
							| 
									
										
										
										
											2021-12-24 01:58:41 +00:00
										 |  |  |             return $error(json_encode($signatureData, \JSON_PRETTY_PRINT)); | 
					
						
							| 
									
										
										
										
											2021-11-30 16:47:31 +00:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-05 03:11:08 +00:00
										 |  |  |         [$verified, /*$headers*/] = HTTPSignature::verify($actor_public_key, $signatureData, $headers, $path, $body); | 
					
						
							| 
									
										
										
										
											2021-11-30 16:47:31 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         // If the signature fails verification the first time, update profile as it might have changed public key
 | 
					
						
							|  |  |  |         if ($verified !== 1) { | 
					
						
							|  |  |  |             try { | 
					
						
							| 
									
										
										
										
											2022-02-25 01:05:28 +00:00
										 |  |  |                 $res = Explorer::getRemoteActorActivity($ap_actor->getUri()); | 
					
						
							| 
									
										
										
										
											2021-12-24 01:58:41 +00:00
										 |  |  |                 if (\is_null($res)) { | 
					
						
							| 
									
										
										
										
											2021-12-19 19:45:38 +00:00
										 |  |  |                     return $error('Invalid remote actor (null response).'); | 
					
						
							| 
									
										
										
										
											2021-12-05 03:11:08 +00:00
										 |  |  |                 } | 
					
						
							| 
									
										
										
										
											2021-12-19 19:45:38 +00:00
										 |  |  |             } catch (Exception $e) { | 
					
						
							|  |  |  |                 return $error('Invalid remote actor.', $e); | 
					
						
							| 
									
										
										
										
											2021-11-30 16:47:31 +00:00
										 |  |  |             } | 
					
						
							|  |  |  |             try { | 
					
						
							| 
									
										
										
										
											2021-12-05 03:11:08 +00:00
										 |  |  |                 ActivitypubActor::update_profile($ap_actor, $actor, $activitypub_rsa, $res); | 
					
						
							| 
									
										
										
										
											2021-12-19 19:45:38 +00:00
										 |  |  |             } catch (Exception $e) { | 
					
						
							| 
									
										
										
										
											2022-03-05 14:23:08 +00:00
										 |  |  |                 return $error('Failed to update remote actor information.', $e); | 
					
						
							| 
									
										
										
										
											2021-11-30 16:47:31 +00:00
										 |  |  |             } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             [$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.'); | 
					
						
							| 
									
										
										
										
											2021-12-05 03:11:08 +00:00
										 |  |  |             return $error('Invalid signature.'); | 
					
						
							| 
									
										
										
										
											2021-11-30 16:47:31 +00:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // HTTP signature checked out, make sure the "actor" of the activity matches that of the signature
 | 
					
						
							| 
									
										
										
										
											2021-12-24 01:58:41 +00:00
										 |  |  |         Log::debug('ActivityPub Inbox: HTTP Signature: Authorised request. Will now start the inbox handler.'); | 
					
						
							| 
									
										
										
										
											2021-08-24 20:29:26 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-05 14:23:08 +00:00
										 |  |  |         Queue::enqueue( | 
					
						
							|  |  |  |             payload: [$ap_actor, $actor, $type], | 
					
						
							|  |  |  |             queue: 'activitypub_inbox', | 
					
						
							|  |  |  |             priority: false, | 
					
						
							| 
									
										
										
										
											2021-12-04 04:07:08 +00:00
										 |  |  |         ); | 
					
						
							| 
									
										
										
										
											2021-08-24 20:29:26 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return new TypeResponse($type, status: 202); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } |