FederationController.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Jobs\InboxPipeline\{
  4. InboxWorker,
  5. InboxValidator
  6. };
  7. use App\Jobs\RemoteFollowPipeline\RemoteFollowPipeline;
  8. use App\{
  9. AccountLog,
  10. Like,
  11. Profile,
  12. Status,
  13. User
  14. };
  15. use App\Util\Lexer\Nickname;
  16. use App\Util\Webfinger\Webfinger;
  17. use Auth;
  18. use Cache;
  19. use Carbon\Carbon;
  20. use Illuminate\Http\Request;
  21. use League\Fractal;
  22. use App\Util\Site\Nodeinfo;
  23. use App\Util\ActivityPub\{
  24. Helpers,
  25. HttpSignature,
  26. Outbox
  27. };
  28. use Zttp\Zttp;
  29. class FederationController extends Controller
  30. {
  31. public function nodeinfoWellKnown()
  32. {
  33. abort_if(!config('federation.nodeinfo.enabled'), 404);
  34. return response()->json(Nodeinfo::wellKnown());
  35. }
  36. public function nodeinfo()
  37. {
  38. abort_if(!config('federation.nodeinfo.enabled'), 404);
  39. return response()->json(Nodeinfo::get())
  40. ->header('Access-Control-Allow-Origin','*');
  41. }
  42. public function webfinger(Request $request)
  43. {
  44. abort_if(!config('federation.webfinger.enabled'), 400);
  45. abort_if(!$request->filled('resource'), 400);
  46. $resource = $request->input('resource');
  47. $parsed = Nickname::normalizeProfileUrl($resource);
  48. if($parsed['domain'] !== config('pixelfed.domain.app')) {
  49. abort(400);
  50. }
  51. $username = $parsed['username'];
  52. $profile = Profile::whereNull('domain')->whereUsername($username)->firstOrFail();
  53. if($profile->status != null) {
  54. return ProfileController::accountCheck($profile);
  55. }
  56. $webfinger = (new Webfinger($profile))->generate();
  57. return response()->json($webfinger, 200, [], JSON_PRETTY_PRINT);
  58. }
  59. public function hostMeta(Request $request)
  60. {
  61. abort_if(!config('federation.webfinger.enabled'), 404);
  62. $path = route('well-known.webfinger');
  63. $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>';
  64. return response($xml)->header('Content-Type', 'application/xrd+xml');
  65. }
  66. public function userOutbox(Request $request, $username)
  67. {
  68. abort_if(!config('federation.activitypub.enabled'), 404);
  69. abort_if(!config('federation.activitypub.outbox'), 404);
  70. $profile = Profile::whereNull('domain')
  71. ->whereNull('status')
  72. ->whereIsPrivate(false)
  73. ->whereUsername($username)
  74. ->firstOrFail();
  75. $key = 'ap:outbox:latest_10:pid:' . $profile->id;
  76. $ttl = now()->addMinutes(15);
  77. $res = Cache::remember($key, $ttl, function() use($profile) {
  78. return Outbox::get($profile);
  79. });
  80. return response(json_encode($res, JSON_UNESCAPED_SLASHES))->header('Content-Type', 'application/activity+json');
  81. }
  82. public function userInbox(Request $request, $username)
  83. {
  84. abort_if(!config('federation.activitypub.enabled'), 404);
  85. abort_if(!config('federation.activitypub.inbox'), 404);
  86. // $headers = $request->headers->all();
  87. // $payload = $request->getContent();
  88. // InboxValidator::dispatch($username, $headers, $payload);
  89. $profile = Profile::whereNull('domain')->whereUsername($username)->firstOrFail();
  90. if($profile->status != null) {
  91. return ProfileController::accountCheck($profile);
  92. }
  93. $body = $request->getContent();
  94. $bodyDecoded = json_decode($body, true, 12);
  95. if($this->verifySignature($request, $profile) == true) {
  96. InboxWorker::dispatch($request->headers->all(), $profile, $bodyDecoded);
  97. } else if($this->blindKeyRotation($request, $profile) == true) {
  98. InboxWorker::dispatch($request->headers->all(), $profile, $bodyDecoded);
  99. } else {
  100. abort(400, 'Bad Signature');
  101. }
  102. return;
  103. }
  104. protected function verifySignature(Request $request, Profile $profile)
  105. {
  106. $body = $request->getContent();
  107. $bodyDecoded = json_decode($body, true, 8);
  108. $signature = $request->header('signature');
  109. $date = $request->header('date');
  110. $digest = $request->header('digest');
  111. if(!$digest) {
  112. abort(400, 'Missing digest header');
  113. }
  114. if(!$signature) {
  115. abort(400, 'Missing signature header');
  116. }
  117. if(!$date) {
  118. abort(400, 'Missing date header');
  119. }
  120. if(!now()->parse($date)->gt(now()->subDays(1)) || !now()->parse($date)->lt(now()->addDays(1))) {
  121. abort(400, 'Invalid date');
  122. }
  123. $signatureData = HttpSignature::parseSignatureHeader($signature);
  124. $keyId = Helpers::validateUrl($signatureData['keyId']);
  125. $id = Helpers::validateUrl($bodyDecoded['id']);
  126. $keyDomain = parse_url($keyId, PHP_URL_HOST);
  127. $idDomain = parse_url($id, PHP_URL_HOST);
  128. if($keyDomain == config('pixelfed.domain.app') || $idDomain == config('pixelfed.domain.app')) {
  129. return false;
  130. }
  131. if(isset($bodyDecoded['object'])
  132. && is_array($bodyDecoded['object'])
  133. && isset($bodyDecoded['object']['attributedTo'])
  134. ) {
  135. if(parse_url($bodyDecoded['object']['attributedTo'], PHP_URL_HOST) !== $keyDomain) {
  136. abort(400, 'Invalid request');
  137. }
  138. }
  139. if(!$keyDomain || !$idDomain || $keyDomain !== $idDomain) {
  140. abort(400, 'Invalid request');
  141. }
  142. $actor = Profile::whereKeyId($keyId)->first();
  143. if(!$actor) {
  144. $actor = Helpers::profileFirstOrNew($bodyDecoded['actor']);
  145. }
  146. if(!$actor) {
  147. return false;
  148. }
  149. $pkey = openssl_pkey_get_public($actor->public_key);
  150. $inboxPath = "/users/{$profile->username}/inbox";
  151. list($verified, $headers) = HttpSignature::verify($pkey, $signatureData, $request->headers->all(), $inboxPath, $body);
  152. if($verified == 1) {
  153. return true;
  154. } else {
  155. return false;
  156. }
  157. }
  158. protected function blindKeyRotation(Request $request, Profile $profile)
  159. {
  160. $signature = $request->header('signature');
  161. $date = $request->header('date');
  162. if(!$signature) {
  163. abort(400, 'Missing signature header');
  164. }
  165. if(!$date) {
  166. abort(400, 'Missing date header');
  167. }
  168. if(!now()->parse($date)->gt(now()->subDays(1)) || !now()->parse($date)->lt(now()->addDays(1))) {
  169. abort(400, 'Invalid date');
  170. }
  171. $signatureData = HttpSignature::parseSignatureHeader($signature);
  172. $keyId = Helpers::validateUrl($signatureData['keyId']);
  173. $actor = Profile::whereKeyId($keyId)->whereNotNull('remote_url')->firstOrFail();
  174. $res = Zttp::timeout(5)->withHeaders([
  175. 'Accept' => 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"',
  176. 'User-Agent' => 'PixelfedBot v0.1 - https://pixelfed.org',
  177. ])->get($actor->remote_url);
  178. $res = json_decode($res->body(), true, 8);
  179. if($res['publicKey']['id'] !== $actor->key_id) {
  180. return false;
  181. }
  182. $actor->public_key = $res['publicKey']['publicKeyPem'];
  183. $actor->save();
  184. return $this->verifySignature($request, $profile);
  185. }
  186. public function userFollowing(Request $request, $username)
  187. {
  188. abort_if(!config('federation.activitypub.enabled'), 404);
  189. $profile = Profile::whereNull('remote_url')
  190. ->whereUsername($username)
  191. ->whereIsPrivate(false)
  192. ->firstOrFail();
  193. if($profile->status != null) {
  194. abort(404);
  195. }
  196. $obj = [
  197. '@context' => 'https://www.w3.org/ns/activitystreams',
  198. 'id' => $request->getUri(),
  199. 'type' => 'OrderedCollectionPage',
  200. 'totalItems' => 0,
  201. 'orderedItems' => []
  202. ];
  203. return response()->json($obj);
  204. }
  205. public function userFollowers(Request $request, $username)
  206. {
  207. abort_if(!config('federation.activitypub.enabled'), 404);
  208. $profile = Profile::whereNull('remote_url')
  209. ->whereUsername($username)
  210. ->whereIsPrivate(false)
  211. ->firstOrFail();
  212. if($profile->status != null) {
  213. abort(404);
  214. }
  215. $obj = [
  216. '@context' => 'https://www.w3.org/ns/activitystreams',
  217. 'id' => $request->getUri(),
  218. 'type' => 'OrderedCollectionPage',
  219. 'totalItems' => 0,
  220. 'orderedItems' => []
  221. ];
  222. return response()->json($obj);
  223. }
  224. }