AuthController.php 11 KB

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