AuthController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. <?php
  2. namespace App\Http\Controllers\Passport;
  3. use App\Http\Controllers\Controller;
  4. use App\Http\Requests\Passport\AuthRegister;
  5. use App\Http\Requests\Passport\AuthForget;
  6. use App\Http\Requests\Passport\AuthLogin;
  7. use App\Jobs\SendEmailJob;
  8. use Illuminate\Http\Request;
  9. use Illuminate\Support\Facades\Cache;
  10. use App\Models\Plan;
  11. use App\Models\User;
  12. use App\Models\InviteCode;
  13. use App\Utils\Helper;
  14. use App\Utils\Dict;
  15. use App\Utils\CacheKey;
  16. use ReCaptcha\ReCaptcha;
  17. class AuthController extends Controller
  18. {
  19. public function loginWithMailLink(Request $request)
  20. {
  21. if (!(int)config('v2board.login_with_mail_link_enable')) {
  22. abort(404);
  23. }
  24. $params = $request->validate([
  25. 'email' => 'required|email',
  26. 'redirect' => 'nullable'
  27. ]);
  28. if (Cache::get(CacheKey::get('LAST_SEND_LOGIN_WITH_MAIL_LINK_TIMESTAMP', $params['email']))) {
  29. abort(500, __('Sending frequently, please try again later'));
  30. }
  31. $user = User::where('email', $params['email'])->first();
  32. if (!$user) {
  33. return response([
  34. 'data' => true
  35. ]);
  36. }
  37. $code = Helper::guid();
  38. $key = CacheKey::get('TEMP_TOKEN', $code);
  39. Cache::put($key, $user->id, 300);
  40. Cache::put(CacheKey::get('LAST_SEND_LOGIN_WITH_MAIL_LINK_TIMESTAMP', $params['email']), time(), 60);
  41. $redirect = '/#/login?verify=' . $code . '&redirect=' . ($request->input('redirect') ? $request->input('redirect') : 'dashboard');
  42. if (config('v2board.app_url')) {
  43. $link = config('v2board.app_url') . $redirect;
  44. } else {
  45. $link = url($redirect);
  46. }
  47. SendEmailJob::dispatch([
  48. 'email' => $user->email,
  49. 'subject' => __('Login to :name', [
  50. 'name' => config('v2board.app_name', 'V2Board')
  51. ]),
  52. 'template_name' => 'login',
  53. 'template_value' => [
  54. 'name' => config('v2board.app_name', 'V2Board'),
  55. 'link' => $link,
  56. 'url' => config('v2board.app_url')
  57. ]
  58. ]);
  59. return response([
  60. 'data' => $link
  61. ]);
  62. }
  63. public function register(AuthRegister $request)
  64. {
  65. if ((int)config('v2board.register_limit_by_ip_enable', 0)) {
  66. $registerCountByIP = Cache::get(CacheKey::get('REGISTER_IP_RATE_LIMIT', $request->ip())) ?? 0;
  67. if ((int)$registerCountByIP >= (int)config('v2board.register_limit_count', 3)) {
  68. abort(500, __('Register frequently, please try again after 1 hour'));
  69. }
  70. }
  71. if ((int)config('v2board.recaptcha_enable', 0)) {
  72. $recaptcha = new ReCaptcha(config('v2board.recaptcha_key'));
  73. $recaptchaResp = $recaptcha->verify($request->input('recaptcha_data'));
  74. if (!$recaptchaResp->isSuccess()) {
  75. abort(500, __('Invalid code is incorrect'));
  76. }
  77. }
  78. if ((int)config('v2board.email_whitelist_enable', 0)) {
  79. if (!Helper::emailSuffixVerify(
  80. $request->input('email'),
  81. config('v2board.email_whitelist_suffix', Dict::EMAIL_WHITELIST_SUFFIX_DEFAULT))
  82. ) {
  83. abort(500, __('Email suffix is not in the Whitelist'));
  84. }
  85. }
  86. if ((int)config('v2board.email_gmail_limit_enable', 0)) {
  87. $prefix = explode('@', $request->input('email'))[0];
  88. if (strpos($prefix, '.') !== false || strpos($prefix, '+') !== false) {
  89. abort(500, __('Gmail alias is not supported'));
  90. }
  91. }
  92. if ((int)config('v2board.stop_register', 0)) {
  93. abort(500, __('Registration has closed'));
  94. }
  95. if ((int)config('v2board.invite_force', 0)) {
  96. if (empty($request->input('invite_code'))) {
  97. abort(500, __('You must use the invitation code to register'));
  98. }
  99. }
  100. if ((int)config('v2board.email_verify', 0)) {
  101. if (empty($request->input('email_code'))) {
  102. abort(500, __('Email verification code cannot be empty'));
  103. }
  104. if (Cache::get(CacheKey::get('EMAIL_VERIFY_CODE', $request->input('email'))) !== $request->input('email_code')) {
  105. abort(500, __('Incorrect email verification code'));
  106. }
  107. }
  108. $email = $request->input('email');
  109. $password = $request->input('password');
  110. $exist = User::where('email', $email)->first();
  111. if ($exist) {
  112. abort(500, __('Email already exists'));
  113. }
  114. $user = new User();
  115. $user->email = $email;
  116. $user->password = password_hash($password, PASSWORD_DEFAULT);
  117. $user->uuid = Helper::guid(true);
  118. $user->token = Helper::guid();
  119. if ($request->input('invite_code')) {
  120. $inviteCode = InviteCode::where('code', $request->input('invite_code'))
  121. ->where('status', 0)
  122. ->first();
  123. if (!$inviteCode) {
  124. if ((int)config('v2board.invite_force', 0)) {
  125. abort(500, __('Invalid invitation code'));
  126. }
  127. } else {
  128. $user->invite_user_id = $inviteCode->user_id ? $inviteCode->user_id : null;
  129. if (!(int)config('v2board.invite_never_expire', 0)) {
  130. $inviteCode->status = 1;
  131. $inviteCode->save();
  132. }
  133. }
  134. }
  135. // try out
  136. if ((int)config('v2board.try_out_plan_id', 0)) {
  137. $plan = Plan::find(config('v2board.try_out_plan_id'));
  138. if ($plan) {
  139. $user->transfer_enable = $plan->transfer_enable * 1073741824;
  140. $user->plan_id = $plan->id;
  141. $user->group_id = $plan->group_id;
  142. $user->expired_at = time() + (config('v2board.try_out_hour', 1) * 3600);
  143. }
  144. }
  145. if (!$user->save()) {
  146. abort(500, __('Register failed'));
  147. }
  148. if ((int)config('v2board.email_verify', 0)) {
  149. Cache::forget(CacheKey::get('EMAIL_VERIFY_CODE', $request->input('email')));
  150. }
  151. $data = [
  152. 'token' => $user->token,
  153. 'auth_data' => base64_encode("{$user->email}:{$user->password}")
  154. ];
  155. Helper::setSession($request, $user);
  156. $user->last_login_at = time();
  157. $user->save();
  158. if ((int)config('v2board.register_limit_by_ip_enable', 0)) {
  159. Cache::put(
  160. CacheKey::get('REGISTER_IP_RATE_LIMIT', $request->ip()),
  161. (int)$registerCountByIP + 1,
  162. (int)config('v2board.register_limit_expire', 60) * 60
  163. );
  164. }
  165. return response()->json([
  166. 'data' => $data
  167. ]);
  168. }
  169. public function login(AuthLogin $request)
  170. {
  171. $email = $request->input('email');
  172. $password = $request->input('password');
  173. $user = User::where('email', $email)->first();
  174. if (!$user) {
  175. abort(500, __('Incorrect email or password'));
  176. }
  177. if (!Helper::multiPasswordVerify(
  178. $user->password_algo,
  179. $user->password_salt,
  180. $password,
  181. $user->password)
  182. ) {
  183. abort(500, __('Incorrect email or password'));
  184. }
  185. if ($user->banned) {
  186. abort(500, __('Your account has been suspended'));
  187. }
  188. $data = [
  189. 'token' => $user->token,
  190. 'auth_data' => base64_encode("{$user->email}:{$user->password}")
  191. ];
  192. if ($user->is_admin) $data['is_admin'] = true;
  193. return response([
  194. 'data' => $data
  195. ]);
  196. }
  197. public function token2Login(Request $request)
  198. {
  199. if ($request->input('token')) {
  200. $redirect = '/#/login?verify=' . $request->input('token') . '&redirect=' . ($request->input('redirect') ? $request->input('redirect') : 'dashboard');
  201. if (config('v2board.app_url')) {
  202. $location = config('v2board.app_url') . $redirect;
  203. } else {
  204. $location = url($redirect);
  205. }
  206. return redirect()->to($location)->send();
  207. }
  208. if ($request->input('verify')) {
  209. $key = CacheKey::get('TEMP_TOKEN', $request->input('verify'));
  210. $userId = Cache::get($key);
  211. if (!$userId) {
  212. abort(500, __('Token error'));
  213. }
  214. $user = User::find($userId);
  215. if (!$user) {
  216. abort(500, __('The user does not '));
  217. }
  218. if ($user->banned) {
  219. abort(500, __('Your account has been suspended'));
  220. }
  221. Helper::setSession($request, $user);
  222. Cache::forget($key);
  223. return response([
  224. 'data' => true
  225. ]);
  226. }
  227. }
  228. public function getTempToken(Request $request)
  229. {
  230. $user = User::where('token', $request->input('token'))->first();
  231. if (!$user) {
  232. abort(500, __('Token error'));
  233. }
  234. $code = Helper::guid();
  235. $key = CacheKey::get('TEMP_TOKEN', $code);
  236. Cache::put($key, $user->id, 60);
  237. return response([
  238. 'data' => $code
  239. ]);
  240. }
  241. public function getQuickLoginUrl(Request $request)
  242. {
  243. $authData = explode(':', base64_decode($request->input('auth_data')));
  244. if (!isset($authData[0])) abort(403, __('Token error'));
  245. $user = User::where('email', $authData[0])
  246. ->where('password', $authData[1])
  247. ->first();
  248. if (!$user) {
  249. abort(500, __('Token error'));
  250. }
  251. $code = Helper::guid();
  252. $key = CacheKey::get('TEMP_TOKEN', $code);
  253. Cache::put($key, $user->id, 60);
  254. $redirect = '/#/login?verify=' . $code . '&redirect=' . ($request->input('redirect') ? $request->input('redirect') : 'dashboard');
  255. if (config('v2board.app_url')) {
  256. $url = config('v2board.app_url') . $redirect;
  257. } else {
  258. $url = url($redirect);
  259. }
  260. return response([
  261. 'data' => $url
  262. ]);
  263. }
  264. public function forget(AuthForget $request)
  265. {
  266. if (Cache::get(CacheKey::get('EMAIL_VERIFY_CODE', $request->input('email'))) !== $request->input('email_code')) {
  267. abort(500, __('Incorrect email verification code'));
  268. }
  269. $user = User::where('email', $request->input('email'))->first();
  270. if (!$user) {
  271. abort(500, __('This email is not registered in the system'));
  272. }
  273. $user->password = password_hash($request->input('password'), PASSWORD_DEFAULT);
  274. $user->password_algo = NULL;
  275. $user->password_salt = NULL;
  276. if (!$user->save()) {
  277. abort(500, __('Reset failed'));
  278. }
  279. Cache::forget(CacheKey::get('EMAIL_VERIFY_CODE', $request->input('email')));
  280. return response([
  281. 'data' => true
  282. ]);
  283. }
  284. }