FederationController.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Jobs\InboxPipeline\{
  4. DeleteWorker,
  5. InboxWorker,
  6. InboxValidator
  7. };
  8. use App\Jobs\RemoteFollowPipeline\RemoteFollowPipeline;
  9. use App\{
  10. AccountLog,
  11. Like,
  12. Profile,
  13. Status,
  14. User
  15. };
  16. use App\Util\Lexer\Nickname;
  17. use App\Util\Webfinger\Webfinger;
  18. use Auth;
  19. use Cache;
  20. use Carbon\Carbon;
  21. use Illuminate\Http\Request;
  22. use League\Fractal;
  23. use App\Util\Site\Nodeinfo;
  24. use App\Util\ActivityPub\{
  25. Helpers,
  26. HttpSignature,
  27. Outbox
  28. };
  29. use Zttp\Zttp;
  30. class FederationController extends Controller
  31. {
  32. public function nodeinfoWellKnown()
  33. {
  34. abort_if(!config('federation.nodeinfo.enabled'), 404);
  35. return response()->json(Nodeinfo::wellKnown(), 200, [], JSON_UNESCAPED_SLASHES)
  36. ->header('Access-Control-Allow-Origin','*');
  37. }
  38. public function nodeinfo()
  39. {
  40. abort_if(!config('federation.nodeinfo.enabled'), 404);
  41. return response()->json(Nodeinfo::get(), 200, [], JSON_UNESCAPED_SLASHES)
  42. ->header('Access-Control-Allow-Origin','*');
  43. }
  44. public function webfinger(Request $request)
  45. {
  46. abort_if(!config('federation.webfinger.enabled'), 400);
  47. abort_if(!$request->has('resource') || !$request->filled('resource'), 400);
  48. $resource = $request->input('resource');
  49. $hash = hash('sha256', $resource);
  50. $key = 'federation:webfinger:sha256:' . $hash;
  51. if($cached = Cache::get($key)) {
  52. return response()->json($cached, 200, [], JSON_UNESCAPED_SLASHES);
  53. }
  54. $domain = config('pixelfed.domain.app');
  55. abort_if(strpos($resource, $domain) == false, 400);
  56. $parsed = Nickname::normalizeProfileUrl($resource);
  57. if(empty($parsed) || $parsed['domain'] !== $domain) {
  58. abort(400);
  59. }
  60. $username = $parsed['username'];
  61. $profile = Profile::whereNull('domain')->whereUsername($username)->firstOrFail();
  62. abort_if($profile->status != null, 400);
  63. $webfinger = (new Webfinger($profile))->generate();
  64. Cache::put($key, $webfinger, 1209600);
  65. return response()->json($webfinger, 200, [], JSON_UNESCAPED_SLASHES)
  66. ->header('Access-Control-Allow-Origin','*');
  67. }
  68. public function hostMeta(Request $request)
  69. {
  70. abort_if(!config('federation.webfinger.enabled'), 404);
  71. $path = route('well-known.webfinger');
  72. $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>';
  73. return response($xml)->header('Content-Type', 'application/xrd+xml');
  74. }
  75. public function userOutbox(Request $request, $username)
  76. {
  77. abort_if(!config_cache('federation.activitypub.enabled'), 404);
  78. abort_if(!config('federation.activitypub.outbox'), 404);
  79. $profile = Profile::whereNull('domain')
  80. ->whereNull('status')
  81. ->whereIsPrivate(false)
  82. ->whereUsername($username)
  83. ->firstOrFail();
  84. $key = 'ap:outbox:latest_10:pid:' . $profile->id;
  85. $ttl = now()->addMinutes(15);
  86. $res = Cache::remember($key, $ttl, function() use($profile) {
  87. return Outbox::get($profile);
  88. });
  89. return response(json_encode($res, JSON_UNESCAPED_SLASHES))->header('Content-Type', 'application/activity+json');
  90. }
  91. public function userInbox(Request $request, $username)
  92. {
  93. abort_if(!config_cache('federation.activitypub.enabled'), 404);
  94. abort_if(!config('federation.activitypub.inbox'), 404);
  95. $headers = $request->headers->all();
  96. $payload = $request->getContent();
  97. $obj = json_decode($payload, true, 8);
  98. if(isset($obj['type']) && $obj['type'] === 'Delete') {
  99. dispatch(new DeleteWorker($headers, $payload))->onQueue('delete');
  100. } else {
  101. dispatch(new InboxValidator($username, $headers, $payload))->onQueue('high');
  102. }
  103. return;
  104. }
  105. public function sharedInbox(Request $request)
  106. {
  107. abort_if(!config_cache('federation.activitypub.enabled'), 404);
  108. abort_if(!config('federation.activitypub.sharedInbox'), 404);
  109. $headers = $request->headers->all();
  110. $payload = $request->getContent();
  111. $obj = json_decode($payload, true, 8);
  112. if(isset($obj['type']) && $obj['type'] === 'Delete') {
  113. dispatch(new DeleteWorker($headers, $payload))->onQueue('delete');
  114. } else {
  115. dispatch(new InboxWorker($headers, $payload))->onQueue('high');
  116. }
  117. return;
  118. }
  119. public function userFollowing(Request $request, $username)
  120. {
  121. abort_if(!config_cache('federation.activitypub.enabled'), 404);
  122. $profile = Profile::whereNull('remote_url')
  123. ->whereUsername($username)
  124. ->whereIsPrivate(false)
  125. ->firstOrFail();
  126. if($profile->status != null) {
  127. abort(404);
  128. }
  129. $obj = [
  130. '@context' => 'https://www.w3.org/ns/activitystreams',
  131. 'id' => $request->getUri(),
  132. 'type' => 'OrderedCollectionPage',
  133. 'totalItems' => 0,
  134. 'orderedItems' => []
  135. ];
  136. return response()->json($obj);
  137. }
  138. public function userFollowers(Request $request, $username)
  139. {
  140. abort_if(!config_cache('federation.activitypub.enabled'), 404);
  141. $profile = Profile::whereNull('remote_url')
  142. ->whereUsername($username)
  143. ->whereIsPrivate(false)
  144. ->firstOrFail();
  145. if($profile->status != null) {
  146. abort(404);
  147. }
  148. $obj = [
  149. '@context' => 'https://www.w3.org/ns/activitystreams',
  150. 'id' => $request->getUri(),
  151. 'type' => 'OrderedCollectionPage',
  152. 'totalItems' => 0,
  153. 'orderedItems' => []
  154. ];
  155. return response()->json($obj);
  156. }
  157. }