ActivitypubActor.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. <?php
  2. declare(strict_types=1);
  3. // {{{ License
  4. // This file is part of GNU social - https://www.gnu.org/software/social
  5. //
  6. // GNU social is free software: you can redistribute it and/or modify
  7. // it under the terms of the GNU Affero General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // GNU social is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU Affero General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Affero General Public License
  17. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  18. // }}}
  19. /**
  20. * ActivityPub implementation for GNU social
  21. *
  22. * @package GNUsocial
  23. * @category ActivityPub
  24. * @author Diogo Peralta Cordeiro <@diogo.site>
  25. * @copyright 2018-2019, 2021 Free Software Foundation, Inc http://www.fsf.org
  26. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  27. */
  28. namespace Plugin\ActivityPub\Entity;
  29. use App\Core\Cache;
  30. use App\Core\Entity;
  31. use App\Core\Log;
  32. use App\Entity\Actor;
  33. use Component\FreeNetwork\Util\Discovery;
  34. use DateTimeInterface;
  35. use Exception;
  36. use Plugin\ActivityPub\Util\DiscoveryHints;
  37. use Plugin\ActivityPub\Util\Explorer;
  38. use XML_XRD;
  39. use function App\Core\I18n\_m;
  40. use function array_key_exists;
  41. use function is_null;
  42. /**
  43. * Table Definition for activitypub_actor
  44. *
  45. * @copyright 2018-2019, 2021 Free Software Foundation, Inc http://www.fsf.org
  46. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  47. */
  48. class ActivitypubActor extends Entity
  49. {
  50. // {{{ Autocode
  51. // @codeCoverageIgnoreStart
  52. private string $uri;
  53. private int $actor_id;
  54. private string $inbox_uri;
  55. private ?string $inbox_shared_uri = null;
  56. private string $url;
  57. private DateTimeInterface $created;
  58. private DateTimeInterface $modified;
  59. public function getUri(): string
  60. {
  61. return $this->uri;
  62. }
  63. public function setUri(string $uri): self
  64. {
  65. $this->uri = $uri;
  66. return $this;
  67. }
  68. public function getUrl(): string
  69. {
  70. return $this->url;
  71. }
  72. public function setUrl(string $url): self
  73. {
  74. $this->url = $url;
  75. return $this;
  76. }
  77. public function getActorId(): int
  78. {
  79. return $this->actor_id;
  80. }
  81. public function setActorId(int $actor_id): self
  82. {
  83. $this->actor_id = $actor_id;
  84. return $this;
  85. }
  86. public function getInboxUri(): string
  87. {
  88. return $this->inbox_uri;
  89. }
  90. public function setInboxUri(string $inbox_uri): self
  91. {
  92. $this->inbox_uri = $inbox_uri;
  93. return $this;
  94. }
  95. public function getInboxSharedUri(): ?string
  96. {
  97. return $this->inbox_shared_uri;
  98. }
  99. public function setInboxSharedUri(?string $inbox_shared_uri = null): self
  100. {
  101. $this->inbox_shared_uri = $inbox_shared_uri;
  102. return $this;
  103. }
  104. public function getCreated(): DateTimeInterface
  105. {
  106. return $this->created;
  107. }
  108. public function setCreated(DateTimeInterface $created): self
  109. {
  110. $this->created = $created;
  111. return $this;
  112. }
  113. public function getModified(): DateTimeInterface
  114. {
  115. return $this->modified;
  116. }
  117. public function setModified(DateTimeInterface $modified): self
  118. {
  119. $this->modified = $modified;
  120. return $this;
  121. }
  122. // @codeCoverageIgnoreEnd
  123. // }}} Autocode
  124. /**
  125. * Look up, and if necessary create, an Activitypub_profile for the remote
  126. * entity with the given WebFinger address.
  127. * This should never return null -- you will either get an object or
  128. * an exception will be thrown.
  129. *
  130. * @param string $addr WebFinger address
  131. *
  132. * @throws Exception on error conditions
  133. */
  134. public static function getByAddr(string $addr): self
  135. {
  136. // Normalize $addr, i.e. add 'acct:' if missing
  137. $addr = Discovery::normalize($addr);
  138. // Try the cache
  139. $uri = Cache::get(sprintf('ActivitypubActor-webfinger-%s', urlencode($addr)), fn() => false);
  140. if ($uri !== false) {
  141. if (is_null($uri)) {
  142. // TRANS: Exception.
  143. throw new Exception(_m('Not a valid WebFinger address (via cache).'));
  144. }
  145. try {
  146. return self::fromUri($uri);
  147. } catch (Exception $e) {
  148. Log::error(sprintf(__METHOD__ . ': WebFinger address cache inconsistent with database, did not find Activitypub_profile uri==%s', $uri));
  149. Cache::set(sprintf('ActivitypubActor-webfinger-%s', urlencode($addr)), false);
  150. }
  151. }
  152. // Now, try some discovery
  153. $disco = new Discovery();
  154. try {
  155. $xrd = $disco->lookup($addr);
  156. } catch (Exception $e) {
  157. // Save negative cache entry so we don't waste time looking it up again.
  158. // @todo FIXME: Distinguish temporary failures?
  159. Cache::set(sprintf('ActivitypubActor-webfinger-%s', urlencode($addr)), null);
  160. // TRANS: Exception.
  161. throw new Exception(_m('Not a valid WebFinger address: ' . $e->getMessage()));
  162. }
  163. return self::fromXrd($addr, $xrd);
  164. }
  165. public static function fromXrd(string $addr, XML_XRD $xrd): self
  166. {
  167. $hints = array_merge(
  168. ['webfinger' => $addr],
  169. DiscoveryHints::fromXRD($xrd),
  170. );
  171. if (array_key_exists('activitypub', $hints)) {
  172. $uri = $hints['activitypub'];
  173. try {
  174. LOG::info("Discovery on acct:{$addr} with URI:{$uri}");
  175. $aprofile = self::fromUri($hints['activitypub']);
  176. Cache::set(sprintf('ActivitypubActor-webfinger-%s', urlencode($addr)), $aprofile->getUri());
  177. return $aprofile;
  178. } catch (Exception $e) {
  179. Log::warning("Failed creating profile from URI:'{$uri}', error:" . $e->getMessage());
  180. throw $e;
  181. // keep looking
  182. //
  183. // @todo FIXME: This means an error discovering from profile page
  184. // may give us a corrupt entry using the webfinger URI, which
  185. // will obscure the correct page-keyed profile later on.
  186. }
  187. }
  188. // XXX: try hcard
  189. // XXX: try FOAF
  190. // TRANS: Exception. %s is a WebFinger address.
  191. throw new Exception(sprintf(_m('Could not find a valid profile for "%s".'), $addr));
  192. }
  193. /**
  194. * Ensures a valid Activitypub_profile when provided with a valid URI.
  195. *
  196. * @param bool $grab_online whether to try online grabbing, defaults to true
  197. *
  198. * @throws Exception if it isn't possible to return an Activitypub_profile
  199. */
  200. public static function fromUri(string $url, bool $grab_online = true): self
  201. {
  202. try {
  203. return Explorer::get_profile_from_url($url, $grab_online);
  204. } catch (Exception $e) {
  205. throw new Exception('No valid ActivityPub profile found for given URI.', previous: $e);
  206. }
  207. }
  208. /**
  209. * @param ActivitypubActor $ap_actor
  210. * @param Actor $actor
  211. * @param ActivitypubRsa $activitypub_rsa
  212. * @param string $res
  213. * @throws Exception
  214. */
  215. public static function update_profile(self &$ap_actor, Actor &$actor, ActivitypubRsa &$activitypub_rsa, string $res): void
  216. {
  217. \Plugin\ActivityPub\Util\Model\Actor::fromJson($res, ['objects' => ['ActivitypubActor' => &$ap_actor, 'Actor' => &$actor, 'ActivitypubRsa' => &$activitypub_rsa]]);
  218. }
  219. public static function schemaDef(): array
  220. {
  221. return [
  222. 'name' => 'activitypub_actor',
  223. 'fields' => [
  224. 'uri' => ['type' => 'text', 'not null' => true],
  225. 'actor_id' => ['type' => 'int', 'not null' => true],
  226. 'inbox_uri' => ['type' => 'text', 'not null' => true],
  227. 'inbox_shared_uri' => ['type' => 'text'],
  228. 'url' => ['type' => 'text'],
  229. 'created' => ['type' => 'datetime', 'not null' => true, 'default' => 'CURRENT_TIMESTAMP', 'description' => 'date this record was created'],
  230. 'modified' => ['type' => 'timestamp', 'not null' => true, 'default' => 'CURRENT_TIMESTAMP', 'description' => 'date this record was modified'],
  231. ],
  232. 'primary key' => ['actor_id'],
  233. 'foreign keys' => [
  234. 'activitypub_actor_actor_id_fkey' => ['actor', ['actor_id' => 'id']],
  235. ],
  236. ];
  237. }
  238. }