RemoteAuthController.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Support\Str;
  4. use Illuminate\Http\Request;
  5. use App\Services\Account\RemoteAuthService;
  6. use App\Models\RemoteAuth;
  7. use App\Profile;
  8. use App\Instance;
  9. use App\User;
  10. use Purify;
  11. use Illuminate\Support\Facades\Auth;
  12. use Illuminate\Support\Facades\Hash;
  13. use Illuminate\Auth\Events\Registered;
  14. use App\Util\Lexer\RestrictedNames;
  15. use App\Services\EmailService;
  16. use App\Services\MediaStorageService;
  17. use App\Util\ActivityPub\Helpers;
  18. use InvalidArgumentException;
  19. class RemoteAuthController extends Controller
  20. {
  21. public function start(Request $request)
  22. {
  23. abort_unless((
  24. config_cache('pixelfed.open_registration') &&
  25. config('remote-auth.mastodon.enabled')
  26. ) || (
  27. config('remote-auth.mastodon.ignore_closed_state') &&
  28. config('remote-auth.mastodon.enabled')
  29. ), 404);
  30. if($request->user()) {
  31. return redirect('/');
  32. }
  33. return view('auth.remote.start');
  34. }
  35. public function startRedirect(Request $request)
  36. {
  37. return redirect('/login');
  38. }
  39. public function getAuthDomains(Request $request)
  40. {
  41. abort_unless((
  42. config_cache('pixelfed.open_registration') &&
  43. config('remote-auth.mastodon.enabled')
  44. ) || (
  45. config('remote-auth.mastodon.ignore_closed_state') &&
  46. config('remote-auth.mastodon.enabled')
  47. ), 404);
  48. if(config('remote-auth.mastodon.domains.only_custom')) {
  49. $res = config('remote-auth.mastodon.domains.custom');
  50. if(!$res || !strlen($res)) {
  51. return [];
  52. }
  53. $res = explode(',', $res);
  54. return response()->json($res);
  55. }
  56. if( config('remote-auth.mastodon.domains.custom') &&
  57. !config('remote-auth.mastodon.domains.only_default') &&
  58. strlen(config('remote-auth.mastodon.domains.custom')) > 3 &&
  59. strpos(config('remote-auth.mastodon.domains.custom'), '.') > -1
  60. ) {
  61. $res = config('remote-auth.mastodon.domains.custom');
  62. if(!$res || !strlen($res)) {
  63. return [];
  64. }
  65. $res = explode(',', $res);
  66. return response()->json($res);
  67. }
  68. $res = config('remote-auth.mastodon.domains.default');
  69. $res = explode(',', $res);
  70. return response()->json($res);
  71. }
  72. public function redirect(Request $request)
  73. {
  74. abort_unless((
  75. config_cache('pixelfed.open_registration') &&
  76. config('remote-auth.mastodon.enabled')
  77. ) || (
  78. config('remote-auth.mastodon.ignore_closed_state') &&
  79. config('remote-auth.mastodon.enabled')
  80. ), 404);
  81. $this->validate($request, ['domain' => 'required']);
  82. $domain = $request->input('domain');
  83. if(str_starts_with(strtolower($domain), 'http')) {
  84. $res = [
  85. 'domain' => $domain,
  86. 'ready' => false,
  87. 'action' => 'incompatible_domain'
  88. ];
  89. return response()->json($res);
  90. }
  91. $validateInstance = Helpers::validateUrl('https://' . $domain . '/?block-check=' . time());
  92. if(!$validateInstance) {
  93. $res = [
  94. 'domain' => $domain,
  95. 'ready' => false,
  96. 'action' => 'blocked_domain'
  97. ];
  98. return response()->json($res);
  99. }
  100. $compatible = RemoteAuthService::isDomainCompatible($domain);
  101. if(!$compatible) {
  102. $res = [
  103. 'domain' => $domain,
  104. 'ready' => false,
  105. 'action' => 'incompatible_domain'
  106. ];
  107. return response()->json($res);
  108. }
  109. if(config('remote-auth.mastodon.domains.only_default')) {
  110. $defaultDomains = explode(',', config('remote-auth.mastodon.domains.default'));
  111. if(!in_array($domain, $defaultDomains)) {
  112. $res = [
  113. 'domain' => $domain,
  114. 'ready' => false,
  115. 'action' => 'incompatible_domain'
  116. ];
  117. return response()->json($res);
  118. }
  119. }
  120. if(config('remote-auth.mastodon.domains.only_custom') && config('remote-auth.mastodon.domains.custom')) {
  121. $customDomains = explode(',', config('remote-auth.mastodon.domains.custom'));
  122. if(!in_array($domain, $customDomains)) {
  123. $res = [
  124. 'domain' => $domain,
  125. 'ready' => false,
  126. 'action' => 'incompatible_domain'
  127. ];
  128. return response()->json($res);
  129. }
  130. }
  131. $client = RemoteAuthService::getMastodonClient($domain);
  132. abort_unless($client, 422, 'Invalid mastodon client');
  133. $request->session()->put('state', $state = Str::random(40));
  134. $request->session()->put('oauth_domain', $domain);
  135. $query = http_build_query([
  136. 'client_id' => $client->client_id,
  137. 'redirect_uri' => $client->redirect_uri,
  138. 'response_type' => 'code',
  139. 'scope' => 'read',
  140. 'state' => $state,
  141. ]);
  142. $request->session()->put('oauth_redirect_to', 'https://' . $domain . '/oauth/authorize?' . $query);
  143. $dsh = Str::random(17);
  144. $res = [
  145. 'domain' => $domain,
  146. 'ready' => true,
  147. 'dsh' => $dsh
  148. ];
  149. return response()->json($res);
  150. }
  151. public function preflight(Request $request)
  152. {
  153. abort_unless((
  154. config_cache('pixelfed.open_registration') &&
  155. config('remote-auth.mastodon.enabled')
  156. ) || (
  157. config('remote-auth.mastodon.ignore_closed_state') &&
  158. config('remote-auth.mastodon.enabled')
  159. ), 404);
  160. if(!$request->filled('d') || !$request->filled('dsh') || !$request->session()->exists('oauth_redirect_to')) {
  161. return redirect('/login');
  162. }
  163. return redirect()->away($request->session()->pull('oauth_redirect_to'));
  164. }
  165. public function handleCallback(Request $request)
  166. {
  167. abort_unless((
  168. config_cache('pixelfed.open_registration') &&
  169. config('remote-auth.mastodon.enabled')
  170. ) || (
  171. config('remote-auth.mastodon.ignore_closed_state') &&
  172. config('remote-auth.mastodon.enabled')
  173. ), 404);
  174. $domain = $request->session()->get('oauth_domain');
  175. if($request->filled('code')) {
  176. $code = $request->input('code');
  177. $state = $request->session()->pull('state');
  178. throw_unless(
  179. strlen($state) > 0 && $state === $request->state,
  180. InvalidArgumentException::class,
  181. 'Invalid state value.'
  182. );
  183. $res = RemoteAuthService::getToken($domain, $code);
  184. if(!$res || !isset($res['access_token'])) {
  185. $request->session()->regenerate();
  186. return redirect('/login');
  187. }
  188. $request->session()->put('oauth_remote_session_token', $res['access_token']);
  189. return redirect('/auth/mastodon/getting-started');
  190. }
  191. return redirect('/login');
  192. }
  193. public function onboarding(Request $request)
  194. {
  195. abort_unless((
  196. config_cache('pixelfed.open_registration') &&
  197. config('remote-auth.mastodon.enabled')
  198. ) || (
  199. config('remote-auth.mastodon.ignore_closed_state') &&
  200. config('remote-auth.mastodon.enabled')
  201. ), 404);
  202. if($request->user()) {
  203. return redirect('/');
  204. }
  205. return view('auth.remote.onboarding');
  206. }
  207. public function sessionCheck(Request $request)
  208. {
  209. abort_unless((
  210. config_cache('pixelfed.open_registration') &&
  211. config('remote-auth.mastodon.enabled')
  212. ) || (
  213. config('remote-auth.mastodon.ignore_closed_state') &&
  214. config('remote-auth.mastodon.enabled')
  215. ), 404);
  216. abort_if($request->user(), 403);
  217. abort_unless($request->session()->exists('oauth_domain'), 403);
  218. abort_unless($request->session()->exists('oauth_remote_session_token'), 403);
  219. $domain = $request->session()->get('oauth_domain');
  220. $token = $request->session()->get('oauth_remote_session_token');
  221. $res = RemoteAuthService::getVerifyCredentials($domain, $token);
  222. abort_if(!$res || !isset($res['acct']), 403, 'Invalid credentials');
  223. $webfinger = strtolower('@' . $res['acct'] . '@' . $domain);
  224. $request->session()->put('oauth_masto_webfinger', $webfinger);
  225. if(config('remote-auth.mastodon.max_uses.enabled')) {
  226. $limit = config('remote-auth.mastodon.max_uses.limit');
  227. $uses = RemoteAuthService::lookupWebfingerUses($webfinger);
  228. if($uses >= $limit) {
  229. return response()->json([
  230. 'code' => 200,
  231. 'msg' => 'Success!',
  232. 'action' => 'max_uses_reached'
  233. ]);
  234. }
  235. }
  236. $exists = RemoteAuth::whereDomain($domain)->where('webfinger', $webfinger)->whereNotNull('user_id')->first();
  237. if($exists && $exists->user_id) {
  238. return response()->json([
  239. 'code' => 200,
  240. 'msg' => 'Success!',
  241. 'action' => 'redirect_existing_user'
  242. ]);
  243. }
  244. return response()->json([
  245. 'code' => 200,
  246. 'msg' => 'Success!',
  247. 'action' => 'onboard'
  248. ]);
  249. }
  250. public function sessionGetMastodonData(Request $request)
  251. {
  252. abort_unless((
  253. config_cache('pixelfed.open_registration') &&
  254. config('remote-auth.mastodon.enabled')
  255. ) || (
  256. config('remote-auth.mastodon.ignore_closed_state') &&
  257. config('remote-auth.mastodon.enabled')
  258. ), 404);
  259. abort_if($request->user(), 403);
  260. abort_unless($request->session()->exists('oauth_domain'), 403);
  261. abort_unless($request->session()->exists('oauth_remote_session_token'), 403);
  262. $domain = $request->session()->get('oauth_domain');
  263. $token = $request->session()->get('oauth_remote_session_token');
  264. $res = RemoteAuthService::getVerifyCredentials($domain, $token);
  265. $res['_webfinger'] = strtolower('@' . $res['acct'] . '@' . $domain);
  266. $res['_domain'] = strtolower($domain);
  267. $request->session()->put('oauth_remasto_id', $res['id']);
  268. $ra = RemoteAuth::updateOrCreate([
  269. 'domain' => $domain,
  270. 'webfinger' => $res['_webfinger'],
  271. ], [
  272. 'software' => 'mastodon',
  273. 'ip_address' => $request->ip(),
  274. 'bearer_token' => $token,
  275. 'verify_credentials' => $res,
  276. 'last_verify_credentials_at' => now(),
  277. 'last_successful_login_at' => now()
  278. ]);
  279. $request->session()->put('oauth_masto_raid', $ra->id);
  280. return response()->json($res);
  281. }
  282. public function sessionValidateUsername(Request $request)
  283. {
  284. abort_unless((
  285. config_cache('pixelfed.open_registration') &&
  286. config('remote-auth.mastodon.enabled')
  287. ) || (
  288. config('remote-auth.mastodon.ignore_closed_state') &&
  289. config('remote-auth.mastodon.enabled')
  290. ), 404);
  291. abort_if($request->user(), 403);
  292. abort_unless($request->session()->exists('oauth_domain'), 403);
  293. abort_unless($request->session()->exists('oauth_remote_session_token'), 403);
  294. $this->validate($request, [
  295. 'username' => [
  296. 'required',
  297. 'min:2',
  298. 'max:15',
  299. function ($attribute, $value, $fail) {
  300. $dash = substr_count($value, '-');
  301. $underscore = substr_count($value, '_');
  302. $period = substr_count($value, '.');
  303. if(ends_with($value, ['.php', '.js', '.css'])) {
  304. return $fail('Username is invalid.');
  305. }
  306. if(($dash + $underscore + $period) > 1) {
  307. return $fail('Username is invalid. Can only contain one dash (-), period (.) or underscore (_).');
  308. }
  309. if (!ctype_alnum($value[0])) {
  310. return $fail('Username is invalid. Must start with a letter or number.');
  311. }
  312. if (!ctype_alnum($value[strlen($value) - 1])) {
  313. return $fail('Username is invalid. Must end with a letter or number.');
  314. }
  315. $val = str_replace(['_', '.', '-'], '', $value);
  316. if(!ctype_alnum($val)) {
  317. return $fail('Username is invalid. Username must be alpha-numeric and may contain dashes (-), periods (.) and underscores (_).');
  318. }
  319. $restricted = RestrictedNames::get();
  320. if (in_array(strtolower($value), array_map('strtolower', $restricted))) {
  321. return $fail('Username cannot be used.');
  322. }
  323. }
  324. ]
  325. ]);
  326. $username = strtolower($request->input('username'));
  327. $exists = User::where('username', $username)->exists();
  328. return response()->json([
  329. 'code' => 200,
  330. 'username' => $username,
  331. 'exists' => $exists
  332. ]);
  333. }
  334. public function sessionValidateEmail(Request $request)
  335. {
  336. abort_unless((
  337. config_cache('pixelfed.open_registration') &&
  338. config('remote-auth.mastodon.enabled')
  339. ) || (
  340. config('remote-auth.mastodon.ignore_closed_state') &&
  341. config('remote-auth.mastodon.enabled')
  342. ), 404);
  343. abort_if($request->user(), 403);
  344. abort_unless($request->session()->exists('oauth_domain'), 403);
  345. abort_unless($request->session()->exists('oauth_remote_session_token'), 403);
  346. $this->validate($request, [
  347. 'email' => [
  348. 'required',
  349. 'email:strict,filter_unicode,dns,spoof',
  350. ]
  351. ]);
  352. $email = $request->input('email');
  353. $banned = EmailService::isBanned($email);
  354. $exists = User::where('email', $email)->exists();
  355. return response()->json([
  356. 'code' => 200,
  357. 'email' => $email,
  358. 'exists' => $exists,
  359. 'banned' => $banned
  360. ]);
  361. }
  362. public function sessionGetMastodonFollowers(Request $request)
  363. {
  364. abort_unless((
  365. config_cache('pixelfed.open_registration') &&
  366. config('remote-auth.mastodon.enabled')
  367. ) || (
  368. config('remote-auth.mastodon.ignore_closed_state') &&
  369. config('remote-auth.mastodon.enabled')
  370. ), 404);
  371. abort_unless($request->session()->exists('oauth_domain'), 403);
  372. abort_unless($request->session()->exists('oauth_remote_session_token'), 403);
  373. abort_unless($request->session()->exists('oauth_remasto_id'), 403);
  374. $domain = $request->session()->get('oauth_domain');
  375. $token = $request->session()->get('oauth_remote_session_token');
  376. $id = $request->session()->get('oauth_remasto_id');
  377. $res = RemoteAuthService::getFollowing($domain, $token, $id);
  378. if(!$res) {
  379. return response()->json([
  380. 'code' => 200,
  381. 'following' => []
  382. ]);
  383. }
  384. $res = collect($res)->filter(fn($acct) => Helpers::validateUrl($acct['url']))->values()->toArray();
  385. return response()->json([
  386. 'code' => 200,
  387. 'following' => $res
  388. ]);
  389. }
  390. public function handleSubmit(Request $request)
  391. {
  392. abort_unless((
  393. config_cache('pixelfed.open_registration') &&
  394. config('remote-auth.mastodon.enabled')
  395. ) || (
  396. config('remote-auth.mastodon.ignore_closed_state') &&
  397. config('remote-auth.mastodon.enabled')
  398. ), 404);
  399. abort_unless($request->session()->exists('oauth_domain'), 403);
  400. abort_unless($request->session()->exists('oauth_remote_session_token'), 403);
  401. abort_unless($request->session()->exists('oauth_remasto_id'), 403);
  402. abort_unless($request->session()->exists('oauth_masto_webfinger'), 403);
  403. abort_unless($request->session()->exists('oauth_masto_raid'), 403);
  404. $this->validate($request, [
  405. 'email' => 'required|email:strict,filter_unicode,dns,spoof',
  406. 'username' => [
  407. 'required',
  408. 'min:2',
  409. 'max:15',
  410. 'unique:users,username',
  411. function ($attribute, $value, $fail) {
  412. $dash = substr_count($value, '-');
  413. $underscore = substr_count($value, '_');
  414. $period = substr_count($value, '.');
  415. if(ends_with($value, ['.php', '.js', '.css'])) {
  416. return $fail('Username is invalid.');
  417. }
  418. if(($dash + $underscore + $period) > 1) {
  419. return $fail('Username is invalid. Can only contain one dash (-), period (.) or underscore (_).');
  420. }
  421. if (!ctype_alnum($value[0])) {
  422. return $fail('Username is invalid. Must start with a letter or number.');
  423. }
  424. if (!ctype_alnum($value[strlen($value) - 1])) {
  425. return $fail('Username is invalid. Must end with a letter or number.');
  426. }
  427. $val = str_replace(['_', '.', '-'], '', $value);
  428. if(!ctype_alnum($val)) {
  429. return $fail('Username is invalid. Username must be alpha-numeric and may contain dashes (-), periods (.) and underscores (_).');
  430. }
  431. $restricted = RestrictedNames::get();
  432. if (in_array(strtolower($value), array_map('strtolower', $restricted))) {
  433. return $fail('Username cannot be used.');
  434. }
  435. }
  436. ],
  437. 'password' => 'required|string|min:8|confirmed',
  438. 'name' => 'nullable|max:30'
  439. ]);
  440. $email = $request->input('email');
  441. $username = $request->input('username');
  442. $password = $request->input('password');
  443. $name = $request->input('name');
  444. $user = $this->createUser([
  445. 'name' => $name,
  446. 'username' => $username,
  447. 'password' => $password,
  448. 'email' => $email
  449. ]);
  450. $raid = $request->session()->pull('oauth_masto_raid');
  451. $webfinger = $request->session()->pull('oauth_masto_webfinger');
  452. $token = $user->createToken('Onboarding')->accessToken;
  453. $ra = RemoteAuth::where('id', $raid)->where('webfinger', $webfinger)->firstOrFail();
  454. $ra->user_id = $user->id;
  455. $ra->save();
  456. return [
  457. 'code' => 200,
  458. 'msg' => 'Success',
  459. 'token' => $token
  460. ];
  461. }
  462. public function storeBio(Request $request)
  463. {
  464. abort_unless((
  465. config_cache('pixelfed.open_registration') &&
  466. config('remote-auth.mastodon.enabled')
  467. ) || (
  468. config('remote-auth.mastodon.ignore_closed_state') &&
  469. config('remote-auth.mastodon.enabled')
  470. ), 404);
  471. abort_unless($request->user(), 404);
  472. abort_unless($request->session()->exists('oauth_domain'), 403);
  473. abort_unless($request->session()->exists('oauth_remote_session_token'), 403);
  474. abort_unless($request->session()->exists('oauth_remasto_id'), 403);
  475. $this->validate($request, [
  476. 'bio' => 'required|nullable|max:500',
  477. ]);
  478. $profile = $request->user()->profile;
  479. $profile->bio = Purify::clean($request->input('bio'));
  480. $profile->save();
  481. return [200];
  482. }
  483. public function accountToId(Request $request)
  484. {
  485. abort_unless((
  486. config_cache('pixelfed.open_registration') &&
  487. config('remote-auth.mastodon.enabled')
  488. ) || (
  489. config('remote-auth.mastodon.ignore_closed_state') &&
  490. config('remote-auth.mastodon.enabled')
  491. ), 404);
  492. abort_if($request->user(), 404);
  493. abort_unless($request->session()->exists('oauth_domain'), 403);
  494. abort_unless($request->session()->exists('oauth_remote_session_token'), 403);
  495. abort_unless($request->session()->exists('oauth_remasto_id'), 403);
  496. $this->validate($request, [
  497. 'account' => 'required|url'
  498. ]);
  499. $account = $request->input('account');
  500. abort_unless(substr(strtolower($account), 0, 8) === 'https://', 404);
  501. $host = strtolower(config('pixelfed.domain.app'));
  502. $domain = strtolower(parse_url($account, PHP_URL_HOST));
  503. if($domain == $host) {
  504. $username = Str::of($account)->explode('/')->last();
  505. $user = User::where('username', $username)->first();
  506. if($user) {
  507. return ['id' => (string) $user->profile_id];
  508. } else {
  509. return [];
  510. }
  511. } else {
  512. try {
  513. $profile = Helpers::profileFetch($account);
  514. if($profile) {
  515. return ['id' => (string) $profile->id];
  516. } else {
  517. return [];
  518. }
  519. } catch (\GuzzleHttp\Exception\RequestException $e) {
  520. return;
  521. } catch (Exception $e) {
  522. return [];
  523. }
  524. }
  525. }
  526. public function storeAvatar(Request $request)
  527. {
  528. abort_unless((
  529. config_cache('pixelfed.open_registration') &&
  530. config('remote-auth.mastodon.enabled')
  531. ) || (
  532. config('remote-auth.mastodon.ignore_closed_state') &&
  533. config('remote-auth.mastodon.enabled')
  534. ), 404);
  535. abort_unless($request->user(), 404);
  536. $this->validate($request, [
  537. 'avatar_url' => 'required|active_url',
  538. ]);
  539. $user = $request->user();
  540. $profile = $user->profile;
  541. abort_if(!$profile->avatar, 404, 'Missing avatar');
  542. $avatar = $profile->avatar;
  543. $avatar->remote_url = $request->input('avatar_url');
  544. $avatar->save();
  545. MediaStorageService::avatar($avatar, config_cache('pixelfed.cloud_storage') == false);
  546. return [200];
  547. }
  548. public function finishUp(Request $request)
  549. {
  550. abort_unless((
  551. config_cache('pixelfed.open_registration') &&
  552. config('remote-auth.mastodon.enabled')
  553. ) || (
  554. config('remote-auth.mastodon.ignore_closed_state') &&
  555. config('remote-auth.mastodon.enabled')
  556. ), 404);
  557. abort_unless($request->user(), 404);
  558. $currentWebfinger = '@' . $request->user()->username . '@' . config('pixelfed.domain.app');
  559. $ra = RemoteAuth::where('user_id', $request->user()->id)->firstOrFail();
  560. RemoteAuthService::submitToBeagle(
  561. $ra->webfinger,
  562. $ra->verify_credentials['url'],
  563. $currentWebfinger,
  564. $request->user()->url()
  565. );
  566. return [200];
  567. }
  568. public function handleLogin(Request $request)
  569. {
  570. abort_unless((
  571. config_cache('pixelfed.open_registration') &&
  572. config('remote-auth.mastodon.enabled')
  573. ) || (
  574. config('remote-auth.mastodon.ignore_closed_state') &&
  575. config('remote-auth.mastodon.enabled')
  576. ), 404);
  577. abort_if($request->user(), 404);
  578. abort_unless($request->session()->exists('oauth_domain'), 403);
  579. abort_unless($request->session()->exists('oauth_remote_session_token'), 403);
  580. abort_unless($request->session()->exists('oauth_masto_webfinger'), 403);
  581. $domain = $request->session()->get('oauth_domain');
  582. $wf = $request->session()->get('oauth_masto_webfinger');
  583. $ra = RemoteAuth::where('webfinger', $wf)->where('domain', $domain)->whereNotNull('user_id')->firstOrFail();
  584. $user = User::findOrFail($ra->user_id);
  585. abort_if($user->is_admin || $user->status != null, 422, 'Invalid auth action');
  586. Auth::loginUsingId($ra->user_id);
  587. return [200];
  588. }
  589. protected function createUser($data)
  590. {
  591. event(new Registered($user = User::create([
  592. 'name' => Purify::clean($data['name']),
  593. 'username' => $data['username'],
  594. 'email' => $data['email'],
  595. 'password' => Hash::make($data['password']),
  596. 'email_verified_at' => config('remote-auth.mastodon.contraints.skip_email_verification') ? now() : null,
  597. 'app_register_ip' => request()->ip(),
  598. 'register_source' => 'mastodon'
  599. ])));
  600. $this->guarder()->login($user);
  601. return $user;
  602. }
  603. protected function guarder()
  604. {
  605. return Auth::guard();
  606. }
  607. }