ProcessMovePipeline.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. namespace App\Jobs\MovePipeline;
  3. use App\Services\ActivityPubFetchService;
  4. use DateTime;
  5. use Exception;
  6. use Illuminate\Contracts\Queue\ShouldQueue;
  7. use Illuminate\Foundation\Queue\Queueable;
  8. use Illuminate\Queue\Middleware\ThrottlesExceptions;
  9. use Illuminate\Queue\Middleware\WithoutOverlapping;
  10. use Illuminate\Support\Arr;
  11. use Log;
  12. class ProcessMovePipeline implements ShouldQueue
  13. {
  14. use Queueable;
  15. public $target;
  16. public $activity;
  17. /**
  18. * The number of times the job may be attempted.
  19. *
  20. * @var int
  21. */
  22. public $tries = 6;
  23. /**
  24. * The maximum number of unhandled exceptions to allow before failing.
  25. *
  26. * @var int
  27. */
  28. public $maxExceptions = 3;
  29. /**
  30. * Create a new job instance.
  31. */
  32. public function __construct($target, $activity)
  33. {
  34. $this->target = $target;
  35. $this->activity = $activity;
  36. }
  37. /**
  38. * Get the middleware the job should pass through.
  39. *
  40. * @return array<int, object>
  41. */
  42. public function middleware(): array
  43. {
  44. return [
  45. new WithoutOverlapping('process-move:'.$this->target),
  46. (new ThrottlesExceptions(2, 5 * 60))->backoff(5),
  47. ];
  48. }
  49. /**
  50. * Determine the time at which the job should timeout.
  51. */
  52. public function retryUntil(): DateTime
  53. {
  54. return now()->addMinutes(5);
  55. }
  56. /**
  57. * Execute the job.
  58. */
  59. public function handle(): void
  60. {
  61. if (config('app.env') !== 'production' || (bool) config_cache('federation.activitypub.enabled') == false) {
  62. Log::info('pmp: AP not enabled');
  63. throw new Exception('Activitypub not enabled');
  64. }
  65. $validTarget = $this->checkTarget();
  66. if (! $validTarget) {
  67. Log::info('pmp: invalid target');
  68. throw new Exception('Invalid target');
  69. }
  70. $validActor = $this->checkActor();
  71. if (! $validActor) {
  72. Log::info('pmp: invalid actor');
  73. throw new Exception('Invalid actor');
  74. }
  75. }
  76. protected function checkTarget()
  77. {
  78. $res = ActivityPubFetchService::fetchRequest($this->target, true);
  79. if (! $res || ! isset($res['alsoKnownAs'])) {
  80. Log::info('[AP][INBOX][MOVE] target_aka failure');
  81. return false;
  82. }
  83. $res = Helpers::profileFetch($this->target);
  84. if (! $res) {
  85. Log::info('[AP][INBOX][MOVE] target fetch failure');
  86. return false;
  87. }
  88. if (is_string($res['alsoKnownAs'])) {
  89. return $this->lowerTrim($res['alsoKnownAs']) === $this->lowerTrim($this->activity);
  90. }
  91. if (is_array($res['alsoKnownAs'])) {
  92. $map = Arr::map($res['alsoKnownAs'], function ($value, $key) {
  93. return trim(strtolower($value));
  94. });
  95. $res = in_array($this->activity, $map);
  96. $debugMessage = $res ? '[AP][INBOX][MOVE] aka target is valid' : '[AP][INBOX][MOVE] aka target is invalid';
  97. Log::info($debugMessage);
  98. return $res;
  99. }
  100. return false;
  101. }
  102. protected function checkActor()
  103. {
  104. $res = ActivityPubFetchService::fetchRequest($this->activity, true);
  105. if (! $res || ! isset($res['movedTo']) || empty($res['movedTo'])) {
  106. Log::info('[AP][INBOX][MOVE] actor_movedTo failure');
  107. return false;
  108. }
  109. $res = Helpers::profileFetch($this->activity);
  110. if (! $res) {
  111. Log::info('[AP][INBOX][MOVE] actor fetch failure');
  112. return false;
  113. }
  114. if (is_string($res['movedTo'])) {
  115. $match = $this->lowerTrim($res['movedTo']) === $this->lowerTrim($this->target);
  116. if (! $match) {
  117. $msg = json_encode([
  118. 'movedTo' => $res['movedTo'],
  119. 'target' => $this->target,
  120. ]);
  121. Log::info('[AP][INBOX][MOVE] invalid actor match.'.$msg);
  122. return false;
  123. }
  124. return $match;
  125. }
  126. return false;
  127. }
  128. protected function lowerTrim($str)
  129. {
  130. return trim(strtolower($str));
  131. }
  132. }