AuthController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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 :minute minute', [
  69. 'minute' => config('v2board.register_limit_expire', 60)
  70. ]));
  71. }
  72. }
  73. if ((int)config('v2board.recaptcha_enable', 0)) {
  74. $recaptcha = new ReCaptcha(config('v2board.recaptcha_key'));
  75. $recaptchaResp = $recaptcha->verify($request->input('recaptcha_data'));
  76. if (!$recaptchaResp->isSuccess()) {
  77. abort(500, __('Invalid code is incorrect'));
  78. }
  79. }
  80. if ((int)config('v2board.email_whitelist_enable', 0)) {
  81. if (!Helper::emailSuffixVerify(
  82. $request->input('email'),
  83. config('v2board.email_whitelist_suffix', Dict::EMAIL_WHITELIST_SUFFIX_DEFAULT))
  84. ) {
  85. abort(500, __('Email suffix is not in the Whitelist'));
  86. }
  87. }
  88. if ((int)config('v2board.email_gmail_limit_enable', 0)) {
  89. $prefix = explode('@', $request->input('email'))[0];
  90. if (strpos($prefix, '.') !== false || strpos($prefix, '+') !== false) {
  91. abort(500, __('Gmail alias is not supported'));
  92. }
  93. }
  94. if ((int)config('v2board.stop_register', 0)) {
  95. abort(500, __('Registration has closed'));
  96. }
  97. if ((int)config('v2board.invite_force', 0)) {
  98. if (empty($request->input('invite_code'))) {
  99. abort(500, __('You must use the invitation code to register'));
  100. }
  101. }
  102. if ((int)config('v2board.email_verify', 0)) {
  103. if (empty($request->input('email_code'))) {
  104. abort(500, __('Email verification code cannot be empty'));
  105. }
  106. if (Cache::get(CacheKey::get('EMAIL_VERIFY_CODE', $request->input('email'))) !== $request->input('email_code')) {
  107. abort(500, __('Incorrect email verification code'));
  108. }
  109. }
  110. $email = $request->input('email');
  111. $password = $request->input('password');
  112. $exist = User::where('email', $email)->first();
  113. if ($exist) {
  114. abort(500, __('Email already exists'));
  115. }
  116. $user = new User();
  117. $user->email = $email;
  118. $user->password = password_hash($password, PASSWORD_DEFAULT);
  119. $user->uuid = Helper::guid(true);
  120. $user->token = Helper::guid();
  121. if ($request->input('invite_code')) {
  122. $inviteCode = InviteCode::where('code', $request->input('invite_code'))
  123. ->where('status', 0)
  124. ->first();
  125. if (!$inviteCode) {
  126. if ((int)config('v2board.invite_force', 0)) {
  127. abort(500, __('Invalid invitation code'));
  128. }
  129. } else {
  130. $user->invite_user_id = $inviteCode->user_id ? $inviteCode->user_id : null;
  131. if (!(int)config('v2board.invite_never_expire', 0)) {
  132. $inviteCode->status = 1;
  133. $inviteCode->save();
  134. }
  135. }
  136. }
  137. // try out
  138. if ((int)config('v2board.try_out_plan_id', 0)) {
  139. $plan = Plan::find(config('v2board.try_out_plan_id'));
  140. if ($plan) {
  141. $user->transfer_enable = $plan->transfer_enable * 1073741824;
  142. $user->plan_id = $plan->id;
  143. $user->group_id = $plan->group_id;
  144. $user->expired_at = time() + (config('v2board.try_out_hour', 1) * 3600);
  145. }
  146. }
  147. if (!$user->save()) {
  148. abort(500, __('Register failed'));
  149. }
  150. if ((int)config('v2board.email_verify', 0)) {
  151. Cache::forget(CacheKey::get('EMAIL_VERIFY_CODE', $request->input('email')));
  152. }
  153. $data = [
  154. 'token' => $user->token,
  155. 'auth_data' => base64_encode("{$user->email}:{$user->password}")
  156. ];
  157. $user->last_login_at = time();
  158. $user->save();
  159. if ((int)config('v2board.register_limit_by_ip_enable', 0)) {
  160. Cache::put(
  161. CacheKey::get('REGISTER_IP_RATE_LIMIT', $request->ip()),
  162. (int)$registerCountByIP + 1,
  163. (int)config('v2board.register_limit_expire', 60) * 60
  164. );
  165. }
  166. return response()->json([
  167. 'data' => $data
  168. ]);
  169. }
  170. public function login(AuthLogin $request)
  171. {
  172. $email = $request->input('email');
  173. $password = $request->input('password');
  174. $user = User::where('email', $email)->first();
  175. if (!$user) {
  176. abort(500, __('Incorrect email or password'));
  177. }
  178. if (!Helper::multiPasswordVerify(
  179. $user->password_algo,
  180. $user->password_salt,
  181. $password,
  182. $user->password)
  183. ) {
  184. abort(500, __('Incorrect email or password'));
  185. }
  186. if ($user->banned) {
  187. abort(500, __('Your account has been suspended'));
  188. }
  189. $data = [
  190. 'token' => $user->token,
  191. 'auth_data' => base64_encode("{$user->email}:{$user->password}")
  192. ];
  193. if ($user->is_admin) $data['is_admin'] = true;
  194. return response([
  195. 'data' => $data
  196. ]);
  197. }
  198. public function token2Login(Request $request)
  199. {
  200. if ($request->input('token')) {
  201. $redirect = '/#/login?verify=' . $request->input('token') . '&redirect=' . ($request->input('redirect') ? $request->input('redirect') : 'dashboard');
  202. if (config('v2board.app_url')) {
  203. $location = config('v2board.app_url') . $redirect;
  204. } else {
  205. $location = url($redirect);
  206. }
  207. return redirect()->to($location)->send();
  208. }
  209. if ($request->input('verify')) {
  210. $key = CacheKey::get('TEMP_TOKEN', $request->input('verify'));
  211. $userId = Cache::get($key);
  212. if (!$userId) {
  213. abort(500, __('Token error'));
  214. }
  215. $user = User::find($userId);
  216. if (!$user) {
  217. abort(500, __('The user does not '));
  218. }
  219. if ($user->banned) {
  220. abort(500, __('Your account has been suspended'));
  221. }
  222. $data = [
  223. 'token' => $user->token,
  224. 'auth_data' => base64_encode("{$user->email}:{$user->password}")
  225. ];
  226. Cache::forget($key);
  227. return response([
  228. 'data' => $data
  229. ]);
  230. }
  231. }
  232. public function getTempToken(Request $request)
  233. {
  234. $user = User::where('token', $request->input('token'))->first();
  235. if (!$user) {
  236. abort(500, __('Token error'));
  237. }
  238. $code = Helper::guid();
  239. $key = CacheKey::get('TEMP_TOKEN', $code);
  240. Cache::put($key, $user->id, 60);
  241. return response([
  242. 'data' => $code
  243. ]);
  244. }
  245. public function getQuickLoginUrl(Request $request)
  246. {
  247. $authorization = $request->input('auth_data') ?? $request->header('authorization');
  248. if (!$authorization) abort(403, '未登录或登陆已过期');
  249. $authData = explode(':', base64_decode($authorization));
  250. if (!isset($authData[0]) || !isset($authData[1])) abort(403, __('Token error'));
  251. $user = User::where('email', $authData[0])
  252. ->where('password', $authData[1])
  253. ->first();
  254. if (!$user) {
  255. abort(500, __('Token error'));
  256. }
  257. $code = Helper::guid();
  258. $key = CacheKey::get('TEMP_TOKEN', $code);
  259. Cache::put($key, $user->id, 60);
  260. $redirect = '/#/login?verify=' . $code . '&redirect=' . ($request->input('redirect') ? $request->input('redirect') : 'dashboard');
  261. if (config('v2board.app_url')) {
  262. $url = config('v2board.app_url') . $redirect;
  263. } else {
  264. $url = url($redirect);
  265. }
  266. return response([
  267. 'data' => $url
  268. ]);
  269. }
  270. public function forget(AuthForget $request)
  271. {
  272. if (Cache::get(CacheKey::get('EMAIL_VERIFY_CODE', $request->input('email'))) !== $request->input('email_code')) {
  273. abort(500, __('Incorrect email verification code'));
  274. }
  275. $user = User::where('email', $request->input('email'))->first();
  276. if (!$user) {
  277. abort(500, __('This email is not registered in the system'));
  278. }
  279. $user->password = password_hash($request->input('password'), PASSWORD_DEFAULT);
  280. $user->password_algo = NULL;
  281. $user->password_salt = NULL;
  282. if (!$user->save()) {
  283. abort(500, __('Reset failed'));
  284. }
  285. Cache::forget(CacheKey::get('EMAIL_VERIFY_CODE', $request->input('email')));
  286. return response([
  287. 'data' => true
  288. ]);
  289. }
  290. }