RemoteAuthService.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. namespace App\Services\Account;
  3. use Illuminate\Support\Facades\Cache;
  4. use Illuminate\Support\Facades\Http;
  5. use App\Models\RemoteAuthInstance;
  6. use Illuminate\Http\Client\ConnectionException;
  7. use Illuminate\Http\Client\RequestException;
  8. class RemoteAuthService
  9. {
  10. const CACHE_KEY = 'pf:services:remoteauth:';
  11. public static function getConfig()
  12. {
  13. return json_encode([
  14. 'default_only' => config('remote-auth.mastodon.domains.only_default'),
  15. 'custom_only' => config('remote-auth.mastodon.domains.only_custom'),
  16. ]);
  17. }
  18. public static function getMastodonClient($domain)
  19. {
  20. if(RemoteAuthInstance::whereDomain($domain)->exists()) {
  21. return RemoteAuthInstance::whereDomain($domain)->first();
  22. }
  23. try {
  24. $url = 'https://' . $domain . '/api/v1/apps';
  25. $res = Http::asForm()->throw()->timeout(10)->post($url, [
  26. 'client_name' => config('pixelfed.domain.app', 'pixelfed'),
  27. 'redirect_uris' => url('/auth/mastodon/callback'),
  28. 'scopes' => 'read',
  29. 'website' => 'https://pixelfed.org'
  30. ]);
  31. if(!$res->ok()) {
  32. return false;
  33. }
  34. } catch (RequestException $e) {
  35. return false;
  36. } catch (ConnectionException $e) {
  37. return false;
  38. } catch (Exception $e) {
  39. return false;
  40. }
  41. $body = $res->json();
  42. if(!$body || !isset($body['client_id'])) {
  43. return false;
  44. }
  45. $raw = RemoteAuthInstance::updateOrCreate([
  46. 'domain' => $domain
  47. ], [
  48. 'client_id' => $body['client_id'],
  49. 'client_secret' => $body['client_secret'],
  50. 'redirect_uri' => $body['redirect_uri'],
  51. ]);
  52. return $raw;
  53. }
  54. public static function getToken($domain, $code)
  55. {
  56. $raw = RemoteAuthInstance::whereDomain($domain)->first();
  57. if(!$raw || !$raw->active || $raw->banned) {
  58. return false;
  59. }
  60. $url = 'https://' . $domain . '/oauth/token';
  61. $res = Http::asForm()->post($url, [
  62. 'code' => $code,
  63. 'grant_type' => 'authorization_code',
  64. 'client_id' => $raw->client_id,
  65. 'client_secret' => $raw->client_secret,
  66. 'redirect_uri' => $raw->redirect_uri,
  67. 'scope' => 'read'
  68. ]);
  69. return $res;
  70. }
  71. public static function getVerifyCredentials($domain, $code)
  72. {
  73. $raw = RemoteAuthInstance::whereDomain($domain)->first();
  74. if(!$raw || !$raw->active || $raw->banned) {
  75. return false;
  76. }
  77. $url = 'https://' . $domain . '/api/v1/accounts/verify_credentials';
  78. $res = Http::withToken($code)->get($url);
  79. return $res->json();
  80. }
  81. public static function getFollowing($domain, $code, $id)
  82. {
  83. $raw = RemoteAuthInstance::whereDomain($domain)->first();
  84. if(!$raw || !$raw->active || $raw->banned) {
  85. return false;
  86. }
  87. $url = 'https://' . $domain . '/api/v1/accounts/' . $id . '/following?limit=80';
  88. $key = self::CACHE_KEY . 'get-following:code:' . substr($code, 0, 16) . substr($code, -5) . ':domain:' . $domain. ':id:' .$id;
  89. return Cache::remember($key, 3600, function() use($url, $code) {
  90. $res = Http::withToken($code)->get($url);
  91. return $res->json();
  92. });
  93. }
  94. public static function isDomainCompatible($domain = false)
  95. {
  96. if(!$domain) {
  97. return false;
  98. }
  99. return Cache::remember(self::CACHE_KEY . 'domain-compatible:' . $domain, 14400, function() use($domain) {
  100. try {
  101. $res = Http::timeout(20)->retry(3, 750)->get('https://beagle.pixelfed.net/api/v1/raa/domain?domain=' . $domain);
  102. if(!$res->ok()) {
  103. return false;
  104. }
  105. } catch (RequestException $e) {
  106. return false;
  107. } catch (ConnectionException $e) {
  108. return false;
  109. } catch (Exception $e) {
  110. return false;
  111. }
  112. $json = $res->json();
  113. if(!in_array('compatible', $json)) {
  114. return false;
  115. }
  116. return $res['compatible'];
  117. });
  118. }
  119. public static function lookupWebfingerUses($wf)
  120. {
  121. try {
  122. $res = Http::timeout(20)->retry(3, 750)->get('https://beagle.pixelfed.net/api/v1/raa/lookup?webfinger=' . $wf);
  123. if(!$res->ok()) {
  124. return false;
  125. }
  126. } catch (RequestException $e) {
  127. return false;
  128. } catch (ConnectionException $e) {
  129. return false;
  130. } catch (Exception $e) {
  131. return false;
  132. }
  133. $json = $res->json();
  134. if(!$json || !isset($json['count'])) {
  135. return false;
  136. }
  137. return $json['count'];
  138. }
  139. public static function submitToBeagle($ow, $ou, $dw, $du)
  140. {
  141. try {
  142. $url = 'https://beagle.pixelfed.net/api/v1/raa/submit';
  143. $res = Http::throw()->timeout(10)->get($url, [
  144. 'ow' => $ow,
  145. 'ou' => $ou,
  146. 'dw' => $dw,
  147. 'du' => $du,
  148. ]);
  149. if(!$res->ok()) {
  150. return;
  151. }
  152. } catch (RequestException $e) {
  153. return;
  154. } catch (ConnectionException $e) {
  155. return;
  156. } catch (Exception $e) {
  157. return;
  158. }
  159. return;
  160. }
  161. }