FederationController.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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'), 404);
  45. $this->validate($request, ['resource'=>'required|string|min:3|max:255']);
  46. $resource = $request->input('resource');
  47. $parsed = Nickname::normalizeProfileUrl($resource);
  48. if($parsed['domain'] !== config('pixelfed.domain.app')) {
  49. abort(404);
  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. $res = Outbox::get($username);
  71. return response(json_encode($res))->header('Content-Type', 'application/activity+json');
  72. }
  73. public function userInbox(Request $request, $username)
  74. {
  75. abort_if(!config('federation.activitypub.enabled'), 404);
  76. abort_if(!config('federation.activitypub.inbox'), 404);
  77. // $headers = $request->headers->all();
  78. // $payload = $request->getContent();
  79. // InboxValidator::dispatch($username, $headers, $payload);
  80. $profile = Profile::whereNull('domain')->whereUsername($username)->firstOrFail();
  81. if($profile->status != null) {
  82. return ProfileController::accountCheck($profile);
  83. }
  84. $body = $request->getContent();
  85. $bodyDecoded = json_decode($body, true, 8);
  86. if($this->verifySignature($request, $profile) == true) {
  87. InboxWorker::dispatch($request->headers->all(), $profile, $bodyDecoded);
  88. } else if($this->blindKeyRotation($request, $profile) == true) {
  89. InboxWorker::dispatch($request->headers->all(), $profile, $bodyDecoded);
  90. } else {
  91. abort(400, 'Bad Signature');
  92. }
  93. return;
  94. }
  95. protected function verifySignature(Request $request, Profile $profile)
  96. {
  97. $body = $request->getContent();
  98. $bodyDecoded = json_decode($body, true, 8);
  99. $signature = $request->header('signature');
  100. $date = $request->header('date');
  101. $digest = $request->header('digest');
  102. if(!$digest) {
  103. abort(400, 'Missing digest header');
  104. }
  105. if(!$signature) {
  106. abort(400, 'Missing signature header');
  107. }
  108. if(!$date) {
  109. abort(400, 'Missing date header');
  110. }
  111. if(!now()->parse($date)->gt(now()->subDays(1)) || !now()->parse($date)->lt(now()->addDays(1))) {
  112. abort(400, 'Invalid date');
  113. }
  114. $signatureData = HttpSignature::parseSignatureHeader($signature);
  115. $keyId = Helpers::validateUrl($signatureData['keyId']);
  116. $id = Helpers::validateUrl($bodyDecoded['id']);
  117. $keyDomain = parse_url($keyId, PHP_URL_HOST);
  118. $idDomain = parse_url($id, PHP_URL_HOST);
  119. if($keyDomain == config('pixelfed.domain.app') || $idDomain == config('pixelfed.domain.app')) {
  120. return false;
  121. }
  122. if(isset($bodyDecoded['object'])
  123. && is_array($bodyDecoded['object'])
  124. && isset($bodyDecoded['object']['attributedTo'])
  125. ) {
  126. if(parse_url($bodyDecoded['object']['attributedTo'], PHP_URL_HOST) !== $keyDomain) {
  127. abort(400, 'Invalid request');
  128. }
  129. }
  130. if(!$keyDomain || !$idDomain || $keyDomain !== $idDomain) {
  131. abort(400, 'Invalid request');
  132. }
  133. $actor = Profile::whereKeyId($keyId)->first();
  134. if(!$actor) {
  135. $actor = Helpers::profileFirstOrNew($bodyDecoded['actor']);
  136. }
  137. if(!$actor) {
  138. return false;
  139. }
  140. $pkey = openssl_pkey_get_public($actor->public_key);
  141. $inboxPath = "/users/{$profile->username}/inbox";
  142. list($verified, $headers) = HttpSignature::verify($pkey, $signatureData, $request->headers->all(), $inboxPath, $body);
  143. if($verified == 1) {
  144. return true;
  145. } else {
  146. return false;
  147. }
  148. }
  149. protected function blindKeyRotation(Request $request, Profile $profile)
  150. {
  151. $signature = $request->header('signature');
  152. $date = $request->header('date');
  153. if(!$signature) {
  154. abort(400, 'Missing signature header');
  155. }
  156. if(!$date) {
  157. abort(400, 'Missing date header');
  158. }
  159. if(!now()->parse($date)->gt(now()->subDays(1)) || !now()->parse($date)->lt(now()->addDays(1))) {
  160. abort(400, 'Invalid date');
  161. }
  162. $signatureData = HttpSignature::parseSignatureHeader($signature);
  163. $keyId = Helpers::validateUrl($signatureData['keyId']);
  164. $actor = Profile::whereKeyId($keyId)->whereNotNull('remote_url')->firstOrFail();
  165. $res = Zttp::timeout(5)->withHeaders([
  166. 'Accept' => 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"',
  167. 'User-Agent' => 'PixelfedBot v0.1 - https://pixelfed.org',
  168. ])->get($actor->remote_url);
  169. $res = json_decode($res->body(), true, 8);
  170. if($res['publicKey']['id'] !== $actor->key_id) {
  171. return false;
  172. }
  173. $actor->public_key = $res['publicKey']['publicKeyPem'];
  174. $actor->save();
  175. return $this->verifySignature($request, $profile);
  176. }
  177. public function userFollowing(Request $request, $username)
  178. {
  179. abort_if(!config('federation.activitypub.enabled'), 404);
  180. $profile = Profile::whereNull('remote_url')
  181. ->whereUsername($username)
  182. ->whereIsPrivate(false)
  183. ->firstOrFail();
  184. if($profile->status != null) {
  185. abort(404);
  186. }
  187. $obj = [
  188. '@context' => 'https://www.w3.org/ns/activitystreams',
  189. 'id' => $request->getUri(),
  190. 'type' => 'OrderedCollectionPage',
  191. 'totalItems' => 0,
  192. 'orderedItems' => []
  193. ];
  194. return response()->json($obj);
  195. }
  196. public function userFollowers(Request $request, $username)
  197. {
  198. abort_if(!config('federation.activitypub.enabled'), 404);
  199. $profile = Profile::whereNull('remote_url')
  200. ->whereUsername($username)
  201. ->whereIsPrivate(false)
  202. ->firstOrFail();
  203. if($profile->status != null) {
  204. abort(404);
  205. }
  206. $obj = [
  207. '@context' => 'https://www.w3.org/ns/activitystreams',
  208. 'id' => $request->getUri(),
  209. 'type' => 'OrderedCollectionPage',
  210. 'totalItems' => 0,
  211. 'orderedItems' => []
  212. ];
  213. return response()->json($obj);
  214. }
  215. }