1
0

RemoteAuthController.php 25 KB

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