apinbox.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. // This file is part of GNU social - https://www.gnu.org/software/social
  3. //
  4. // GNU social is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // GNU social is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * ActivityPub implementation for GNU social
  18. *
  19. * @package GNUsocial
  20. * @author Diogo Cordeiro <diogo@fc.up.pt>
  21. * @copyright 2018-2019 Free Software Foundation, Inc http://www.fsf.org
  22. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  23. * @link http://www.gnu.org/software/social/
  24. */
  25. defined('GNUSOCIAL') || die();
  26. /**
  27. * Inbox Request Handler
  28. *
  29. * @category Plugin
  30. * @package GNUsocial
  31. * @author Diogo Cordeiro <diogo@fc.up.pt>
  32. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  33. */
  34. class apInboxAction extends ManagedAction
  35. {
  36. protected $needLogin = false;
  37. protected $canPost = true;
  38. /**
  39. * Handle the Inbox request
  40. *
  41. * @return void
  42. * @throws ServerException
  43. * @author Diogo Cordeiro <diogo@fc.up.pt>
  44. */
  45. protected function handle()
  46. {
  47. $path = !empty($this->trimmed('id')) ? common_local_url('apInbox', ['id' => $this->trimmed('id')]) : common_local_url('apInbox');
  48. $path = parse_url($path, PHP_URL_PATH);
  49. if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
  50. ActivityPubReturn::error('Only POST requests allowed.');
  51. }
  52. common_debug('ActivityPub Inbox: Received a POST request.');
  53. $body = $data = file_get_contents('php://input');
  54. common_debug('ActivityPub Inbox: Request contents: '.$data);
  55. $data = json_decode(file_get_contents('php://input'), true);
  56. if (!isset($data['actor'])) {
  57. ActivityPubReturn::error('Actor not found in the request.');
  58. }
  59. $actor = Activitypub_explorer::get_profile_from_url($data['actor']);
  60. $aprofile = Activitypub_profile::from_profile($actor);
  61. $actor_public_key = new Activitypub_rsa();
  62. $actor_public_key = $actor_public_key->ensure_public_key($actor);
  63. common_debug('ActivityPub Inbox: HTTP Signature: Validation will now start!');
  64. $headers = $this->get_all_headers();
  65. common_debug('ActivityPub Inbox: Request Headers: '.print_r($headers, true));
  66. if (!isset($headers['signature'])) {
  67. common_debug('ActivityPub Inbox: HTTP Signature: Missing Signature header.');
  68. ActivityPubReturn::error('Missing Signature header.', 400);
  69. }
  70. // Extract the signature properties
  71. $signatureData = HTTPSignature::parseSignatureHeader($headers['signature']);
  72. common_debug('ActivityPub Inbox: HTTP Signature Data: '.print_r($signatureData, true));
  73. if (isset($signatureData['error'])) {
  74. common_debug('ActivityPub Inbox: HTTP Signature: '.json_encode($signatureData, true));
  75. ActivityPubReturn::error(json_encode($signatureData, true), 400);
  76. }
  77. list($verified, $headers) = HTTPSignature::verify($actor_public_key, $signatureData, $headers, $path, $body);
  78. // If the signature fails verification the first time, update profile as it might have change public key
  79. if($verified !== 1) {
  80. $res = Activitypub_explorer::get_remote_user_activity($aprofile->getUri());
  81. $actor = Activitypub_profile::update_profile($aprofile, $res);
  82. $actor_public_key = new Activitypub_rsa();
  83. $actor_public_key = $actor_public_key->ensure_public_key($actor);
  84. list($verified, $headers) = HTTPSignature::verify($actor_public_key, $signatureData, $headers, $path, $body);
  85. }
  86. // If it still failed despite profile update
  87. if($verified !== 1) {
  88. common_debug('ActivityPub Inbox: HTTP Signature: Invalid signature.');
  89. ActivityPubReturn::error('Invalid signature.');
  90. }
  91. // HTTP signature checked out, make sure the "actor" of the activity matches that of the signature
  92. common_debug('ActivityPub Inbox: HTTP Signature: Authorized request. Will now start the inbox handler.');
  93. try {
  94. new Activitypub_inbox_handler($data, $actor);
  95. ActivityPubReturn::answer();
  96. } catch (Exception $e) {
  97. ActivityPubReturn::error($e->getMessage());
  98. }
  99. }
  100. /**
  101. * Get all HTTP header key/values as an associative array for the current request.
  102. *
  103. * @return array [string] The HTTP header key/value pairs.
  104. * @author PHP Manual Contributed Notes <joyview@gmail.com>
  105. */
  106. private function get_all_headers()
  107. {
  108. $headers = [];
  109. foreach ($_SERVER as $name => $value) {
  110. if (substr($name, 0, 5) == 'HTTP_') {
  111. $headers[strtolower(str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5))))))] = $value;
  112. }
  113. }
  114. return $headers;
  115. }
  116. }