1
0

FederationController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Jobs\InboxPipeline\DeleteWorker;
  4. use App\Jobs\InboxPipeline\InboxValidator;
  5. use App\Jobs\InboxPipeline\InboxWorker;
  6. use App\Profile;
  7. use App\Services\AccountService;
  8. use App\Services\InstanceService;
  9. use App\Status;
  10. use App\Util\Lexer\Nickname;
  11. use App\Util\Site\Nodeinfo;
  12. use App\Util\Webfinger\Webfinger;
  13. use Cache;
  14. use Illuminate\Http\Request;
  15. class FederationController extends Controller
  16. {
  17. public function nodeinfoWellKnown()
  18. {
  19. abort_if(! config('federation.nodeinfo.enabled'), 404);
  20. return response()->json(Nodeinfo::wellKnown(), 200, [], JSON_UNESCAPED_SLASHES)
  21. ->header('Access-Control-Allow-Origin', '*');
  22. }
  23. public function nodeinfo()
  24. {
  25. abort_if(! config('federation.nodeinfo.enabled'), 404);
  26. return response()->json(Nodeinfo::get(), 200, [], JSON_UNESCAPED_SLASHES)
  27. ->header('Access-Control-Allow-Origin', '*');
  28. }
  29. public function webfinger(Request $request)
  30. {
  31. if (! config('federation.webfinger.enabled') ||
  32. ! $request->has('resource') ||
  33. ! $request->filled('resource')
  34. ) {
  35. return response('', 400);
  36. }
  37. $resource = $request->input('resource');
  38. $domain = config('pixelfed.domain.app');
  39. // Instance Actor
  40. if (
  41. config('federation.activitypub.sharedInbox') &&
  42. $resource == 'acct:'.$domain.'@'.$domain
  43. ) {
  44. $res = [
  45. 'subject' => 'acct:'.$domain.'@'.$domain,
  46. 'aliases' => [
  47. 'https://'.$domain.'/i/actor',
  48. ],
  49. 'links' => [
  50. [
  51. 'rel' => 'http://webfinger.net/rel/profile-page',
  52. 'type' => 'text/html',
  53. 'href' => 'https://'.$domain.'/site/kb/instance-actor',
  54. ],
  55. [
  56. 'rel' => 'self',
  57. 'type' => 'application/activity+json',
  58. 'href' => 'https://'.$domain.'/i/actor',
  59. ],
  60. [
  61. 'rel' => 'http://ostatus.org/schema/1.0/subscribe',
  62. 'template' => 'https://'.$domain.'/authorize_interaction?uri={uri}',
  63. ],
  64. ],
  65. ];
  66. return response()->json($res, 200, [], JSON_UNESCAPED_SLASHES);
  67. }
  68. if (str_starts_with($resource, 'https://')) {
  69. if (str_starts_with($resource, 'https://'.$domain.'/users/')) {
  70. $username = str_replace('https://'.$domain.'/users/', '', $resource);
  71. if (strlen($username) > 15) {
  72. return response('', 400);
  73. }
  74. $stripped = str_replace(['_', '.', '-'], '', $username);
  75. if (! ctype_alnum($stripped)) {
  76. return response('', 400);
  77. }
  78. $key = 'federation:webfinger:sha256:url-username:'.$username;
  79. if ($cached = Cache::get($key)) {
  80. return response()->json($cached, 200, [], JSON_UNESCAPED_SLASHES);
  81. }
  82. $profile = Profile::whereUsername($username)->first();
  83. if (! $profile || $profile->status !== null || $profile->domain) {
  84. return response('', 400);
  85. }
  86. $webfinger = (new Webfinger($profile))->generate();
  87. Cache::put($key, $webfinger, 1209600);
  88. return response()->json($webfinger, 200, [], JSON_UNESCAPED_SLASHES)
  89. ->header('Access-Control-Allow-Origin', '*');
  90. } else {
  91. return response('', 400);
  92. }
  93. }
  94. $hash = hash('sha256', $resource);
  95. $key = 'federation:webfinger:sha256:'.$hash;
  96. if ($cached = Cache::get($key)) {
  97. return response()->json($cached, 200, [], JSON_UNESCAPED_SLASHES);
  98. }
  99. if (strpos($resource, $domain) == false) {
  100. return response('', 400);
  101. }
  102. $parsed = Nickname::normalizeProfileUrl($resource);
  103. if (empty($parsed) || $parsed['domain'] !== $domain) {
  104. return response('', 400);
  105. }
  106. $username = $parsed['username'];
  107. $profile = Profile::whereUsername($username)->first();
  108. if (! $profile || $profile->status !== null || $profile->domain) {
  109. return response('', 400);
  110. }
  111. $webfinger = (new Webfinger($profile))->generate();
  112. Cache::put($key, $webfinger, 1209600);
  113. return response()->json($webfinger, 200, [], JSON_UNESCAPED_SLASHES)
  114. ->header('Access-Control-Allow-Origin', '*');
  115. }
  116. public function hostMeta(Request $request)
  117. {
  118. abort_if(! config('federation.webfinger.enabled'), 404);
  119. $path = route('well-known.webfinger');
  120. $xml = '<?xml version="1.0" encoding="UTF-8"?><XRD xmlns="http://docs.oasis-open.org/ns/xri/xrd-1.0"><Link rel="lrdd" type="application/xrd+xml" template="'.$path.'?resource={uri}"/></XRD>';
  121. return response($xml)->header('Content-Type', 'application/xrd+xml');
  122. }
  123. public function userOutbox(Request $request, $username)
  124. {
  125. abort_if(! (bool) config_cache('federation.activitypub.enabled'), 404);
  126. if (! $request->wantsJson()) {
  127. return redirect('/'.$username);
  128. }
  129. $id = AccountService::usernameToId($username);
  130. abort_if(! $id, 404);
  131. $account = AccountService::get($id);
  132. abort_if(! $account || ! isset($account['statuses_count']), 404);
  133. $res = [
  134. '@context' => 'https://www.w3.org/ns/activitystreams',
  135. 'id' => 'https://'.config('pixelfed.domain.app').'/users/'.$username.'/outbox',
  136. 'type' => 'OrderedCollection',
  137. 'totalItems' => $account['statuses_count'] ?? 0,
  138. ];
  139. return response(json_encode($res, JSON_UNESCAPED_SLASHES))->header('Content-Type', 'application/activity+json');
  140. }
  141. public function userInbox(Request $request, $username)
  142. {
  143. abort_if(! (bool) config_cache('federation.activitypub.enabled'), 404);
  144. abort_if(! config('federation.activitypub.inbox'), 404);
  145. $headers = $request->headers->all();
  146. $payload = $request->getContent();
  147. if (! $payload || empty($payload)) {
  148. return;
  149. }
  150. $obj = json_decode($payload, true, 8);
  151. if (! isset($obj['id'])) {
  152. return;
  153. }
  154. $domain = parse_url($obj['id'], PHP_URL_HOST);
  155. if (in_array($domain, InstanceService::getBannedDomains())) {
  156. return;
  157. }
  158. if (isset($obj['type']) && $obj['type'] === 'Delete') {
  159. if (isset($obj['object']) && isset($obj['object']['type']) && isset($obj['object']['id'])) {
  160. if ($obj['object']['type'] === 'Person') {
  161. if (Profile::whereRemoteUrl($obj['object']['id'])->exists()) {
  162. dispatch(new DeleteWorker($headers, $payload))->onQueue('inbox');
  163. return;
  164. }
  165. }
  166. if ($obj['object']['type'] === 'Tombstone') {
  167. if (Status::whereObjectUrl($obj['object']['id'])->exists()) {
  168. dispatch(new DeleteWorker($headers, $payload))->onQueue('delete');
  169. return;
  170. }
  171. }
  172. if ($obj['object']['type'] === 'Story') {
  173. dispatch(new DeleteWorker($headers, $payload))->onQueue('story');
  174. return;
  175. }
  176. }
  177. return;
  178. } elseif (isset($obj['type']) && in_array($obj['type'], ['Follow', 'Accept'])) {
  179. dispatch(new InboxValidator($username, $headers, $payload))->onQueue('follow');
  180. } else {
  181. dispatch(new InboxValidator($username, $headers, $payload))->onQueue('high');
  182. }
  183. }
  184. public function sharedInbox(Request $request)
  185. {
  186. abort_if(! (bool) config_cache('federation.activitypub.enabled'), 404);
  187. abort_if(! config('federation.activitypub.sharedInbox'), 404);
  188. $headers = $request->headers->all();
  189. $payload = $request->getContent();
  190. if (! $payload || empty($payload)) {
  191. return;
  192. }
  193. $obj = json_decode($payload, true, 8);
  194. if (! isset($obj['id'])) {
  195. return;
  196. }
  197. $domain = parse_url($obj['id'], PHP_URL_HOST);
  198. if (in_array($domain, InstanceService::getBannedDomains())) {
  199. return;
  200. }
  201. if (isset($obj['type']) && $obj['type'] === 'Delete') {
  202. if (isset($obj['object']) && isset($obj['object']['type']) && isset($obj['object']['id'])) {
  203. if ($obj['object']['type'] === 'Person') {
  204. if (Profile::whereRemoteUrl($obj['object']['id'])->exists()) {
  205. dispatch(new DeleteWorker($headers, $payload))->onQueue('inbox');
  206. return;
  207. }
  208. }
  209. if ($obj['object']['type'] === 'Tombstone') {
  210. if (Status::whereObjectUrl($obj['object']['id'])->exists()) {
  211. dispatch(new DeleteWorker($headers, $payload))->onQueue('delete');
  212. return;
  213. }
  214. }
  215. if ($obj['object']['type'] === 'Story') {
  216. dispatch(new DeleteWorker($headers, $payload))->onQueue('story');
  217. return;
  218. }
  219. }
  220. return;
  221. } elseif (isset($obj['type']) && in_array($obj['type'], ['Follow', 'Accept'])) {
  222. dispatch(new InboxWorker($headers, $payload))->onQueue('follow');
  223. } else {
  224. dispatch(new InboxWorker($headers, $payload))->onQueue('shared');
  225. }
  226. }
  227. public function userFollowing(Request $request, $username)
  228. {
  229. abort_if(! (bool) config_cache('federation.activitypub.enabled'), 404);
  230. $id = AccountService::usernameToId($username);
  231. abort_if(! $id, 404);
  232. $account = AccountService::get($id);
  233. abort_if(! $account || ! isset($account['following_count']), 404);
  234. $obj = [
  235. '@context' => 'https://www.w3.org/ns/activitystreams',
  236. 'id' => $request->getUri(),
  237. 'type' => 'OrderedCollection',
  238. 'totalItems' => $account['following_count'] ?? 0,
  239. ];
  240. return response()->json($obj)->header('Content-Type', 'application/activity+json');
  241. }
  242. public function userFollowers(Request $request, $username)
  243. {
  244. abort_if(! (bool) config_cache('federation.activitypub.enabled'), 404);
  245. $id = AccountService::usernameToId($username);
  246. abort_if(! $id, 404);
  247. $account = AccountService::get($id);
  248. abort_if(! $account || ! isset($account['followers_count']), 404);
  249. $obj = [
  250. '@context' => 'https://www.w3.org/ns/activitystreams',
  251. 'id' => $request->getUri(),
  252. 'type' => 'OrderedCollection',
  253. 'totalItems' => $account['followers_count'] ?? 0,
  254. ];
  255. return response()->json($obj)->header('Content-Type', 'application/activity+json');
  256. }
  257. }