ActivitypubActor.php 8.5 KB

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