1
0

FederationController.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Jobs\InboxPipeline\InboxWorker;
  4. use App\Jobs\RemoteFollowPipeline\RemoteFollowPipeline;
  5. use App\Profile;
  6. use App\Transformer\ActivityPub\ProfileOutbox;
  7. use App\Util\Lexer\Nickname;
  8. use App\Util\Webfinger\Webfinger;
  9. use Auth;
  10. use Cache;
  11. use Carbon\Carbon;
  12. use Illuminate\Http\Request;
  13. use League\Fractal;
  14. use App\Util\ActivityPub\Helpers;
  15. use App\Util\ActivityPub\HttpSignature;
  16. class FederationController extends Controller
  17. {
  18. public function authCheck()
  19. {
  20. if (!Auth::check()) {
  21. return abort(403);
  22. }
  23. }
  24. public function authorizeFollow(Request $request)
  25. {
  26. $this->authCheck();
  27. $this->validate($request, [
  28. 'acct' => 'required|string|min:3|max:255',
  29. ]);
  30. $acct = $request->input('acct');
  31. $nickname = Nickname::normalizeProfileUrl($acct);
  32. return view('federation.authorizefollow', compact('acct', 'nickname'));
  33. }
  34. public function remoteFollow()
  35. {
  36. $this->authCheck();
  37. return view('federation.remotefollow');
  38. }
  39. public function remoteFollowStore(Request $request)
  40. {
  41. $this->authCheck();
  42. $this->validate($request, [
  43. 'url' => 'required|string',
  44. ]);
  45. if (config('pixelfed.remote_follow_enabled') !== true) {
  46. abort(403);
  47. }
  48. $follower = Auth::user()->profile;
  49. $url = $request->input('url');
  50. RemoteFollowPipeline::dispatch($follower, $url);
  51. return redirect()->back();
  52. }
  53. public function nodeinfoWellKnown()
  54. {
  55. $res = [
  56. 'links' => [
  57. [
  58. 'href' => config('pixelfed.nodeinfo.url'),
  59. 'rel' => 'http://nodeinfo.diaspora.software/ns/schema/2.0',
  60. ],
  61. ],
  62. ];
  63. return response()->json($res);
  64. }
  65. public function nodeinfo()
  66. {
  67. $res = Cache::remember('api:nodeinfo', 60, function () {
  68. return [
  69. 'metadata' => [
  70. 'nodeName' => config('app.name'),
  71. 'software' => [
  72. 'homepage' => 'https://pixelfed.org',
  73. 'github' => 'https://github.com/pixelfed',
  74. 'follow' => 'https://mastodon.social/@pixelfed',
  75. ],
  76. ],
  77. 'openRegistrations' => config('pixelfed.open_registration'),
  78. 'protocols' => [
  79. 'activitypub',
  80. ],
  81. 'services' => [
  82. 'inbound' => [],
  83. 'outbound' => [],
  84. ],
  85. 'software' => [
  86. 'name' => 'pixelfed',
  87. 'version' => config('pixelfed.version'),
  88. ],
  89. 'usage' => [
  90. 'localPosts' => \App\Status::whereLocal(true)->whereHas('media')->count(),
  91. 'localComments' => \App\Status::whereLocal(true)->whereNotNull('in_reply_to_id')->count(),
  92. 'users' => [
  93. 'total' => \App\User::count(),
  94. 'activeHalfyear' => \App\AccountLog::select('user_id')->whereAction('auth.login')->where('updated_at', '>',Carbon::now()->subMonths(6)->toDateTimeString())->groupBy('user_id')->get()->count(),
  95. 'activeMonth' => \App\AccountLog::select('user_id')->whereAction('auth.login')->where('updated_at', '>',Carbon::now()->subMonths(1)->toDateTimeString())->groupBy('user_id')->get()->count(),
  96. ],
  97. ],
  98. 'version' => '2.0',
  99. ];
  100. });
  101. return response()->json($res, 200, [
  102. 'Access-Control-Allow-Origin' => '*'
  103. ]);
  104. }
  105. public function webfinger(Request $request)
  106. {
  107. $this->validate($request, ['resource'=>'required|string|min:3|max:255']);
  108. $resource = $request->input('resource');
  109. $hash = hash('sha256', $resource);
  110. $parsed = Nickname::normalizeProfileUrl($resource);
  111. $username = $parsed['username'];
  112. $profile = Profile::whereUsername($username)->firstOrFail();
  113. if($profile->status != null) {
  114. return ProfileController::accountCheck($profile);
  115. }
  116. $webfinger = (new Webfinger($profile))->generate();
  117. return response()->json($webfinger, 200, [], JSON_PRETTY_PRINT);
  118. }
  119. public function hostMeta(Request $request)
  120. {
  121. $path = route('well-known.webfinger');
  122. $xml = <<<XML
  123. <?xml version="1.0" encoding="UTF-8"?>
  124. <XRD xmlns="http://docs.oasis-open.org/ns/xri/xrd-1.0">
  125. <Link rel="lrdd" type="application/xrd+xml" template="{$path}?resource={uri}"/>
  126. </XRD>
  127. XML;
  128. return response($xml)->header('Content-Type', 'application/xrd+xml');
  129. }
  130. public function userOutbox(Request $request, $username)
  131. {
  132. if (config('pixelfed.activitypub_enabled') == false) {
  133. abort(403);
  134. }
  135. $profile = Profile::whereNull('remote_url')->whereUsername($username)->firstOrFail();
  136. if($profile->status != null) {
  137. return ProfileController::accountCheck($profile);
  138. }
  139. if($profile->is_private) {
  140. return response()->json(['error'=>'403', 'msg' => 'private profile'], 403);
  141. }
  142. $timeline = $profile->statuses()->whereVisibility('public')->orderBy('created_at', 'desc')->paginate(10);
  143. $fractal = new Fractal\Manager();
  144. $resource = new Fractal\Resource\Item($profile, new ProfileOutbox());
  145. $res = $fractal->createData($resource)->toArray();
  146. return response(json_encode($res['data']))->header('Content-Type', 'application/activity+json');
  147. }
  148. public function userInbox(Request $request, $username)
  149. {
  150. if (config('pixelfed.activitypub_enabled') == false) {
  151. abort(403);
  152. }
  153. $profile = Profile::whereNull('domain')->whereUsername($username)->firstOrFail();
  154. if($profile->status != null) {
  155. return ProfileController::accountCheck($profile);
  156. }
  157. $body = $request->getContent();
  158. $bodyDecoded = json_decode($body, true);
  159. $signature = $request->header('signature');
  160. if(!$signature) {
  161. abort(400, 'Missing signature header');
  162. }
  163. $signatureData = HttpSignature::parseSignatureHeader($signature);
  164. $actor = Profile::whereKeyId($signatureData['keyId'])->first();
  165. if(!$actor) {
  166. $actor = Helpers::profileFirstOrNew($bodyDecoded['actor']);
  167. }
  168. $pkey = openssl_pkey_get_public($actor->public_key);
  169. $inboxPath = "/users/{$profile->username}/inbox";
  170. list($verified, $headers) = HTTPSignature::verify($pkey, $signatureData, $request->headers->all(), $inboxPath, $body);
  171. if($verified !== 1) {
  172. abort(400, 'Invalid signature.');
  173. }
  174. InboxWorker::dispatch($request->headers->all(), $profile, $bodyDecoded);
  175. return;
  176. }
  177. public function userFollowing(Request $request, $username)
  178. {
  179. if (config('pixelfed.activitypub_enabled') == false) {
  180. abort(403);
  181. }
  182. $profile = Profile::whereNull('remote_url')
  183. ->whereUsername($username)
  184. ->whereIsPrivate(false)
  185. ->firstOrFail();
  186. if($profile->status != null) {
  187. return ProfileController::accountCheck($profile);
  188. }
  189. $obj = [
  190. '@context' => 'https://www.w3.org/ns/activitystreams',
  191. 'id' => $request->getUri(),
  192. 'type' => 'OrderedCollectionPage',
  193. 'totalItems' => $profile->following()->count(),
  194. 'orderedItems' => $profile->following->map(function($f) {
  195. return $f->permalink();
  196. })
  197. ];
  198. return response()->json($obj);
  199. }
  200. public function userFollowers(Request $request, $username)
  201. {
  202. if (config('pixelfed.activitypub_enabled') == false) {
  203. abort(403);
  204. }
  205. $profile = Profile::whereNull('remote_url')
  206. ->whereUsername($username)
  207. ->whereIsPrivate(false)
  208. ->firstOrFail();
  209. if($profile->status != null) {
  210. return ProfileController::accountCheck($profile);
  211. }
  212. $obj = [
  213. '@context' => 'https://www.w3.org/ns/activitystreams',
  214. 'id' => $request->getUri(),
  215. 'type' => 'OrderedCollectionPage',
  216. 'totalItems' => $profile->followers()->count(),
  217. 'orderedItems' => $profile->followers->map(function($f) {
  218. return $f->permalink();
  219. })
  220. ];
  221. return response()->json($obj);
  222. }
  223. }