AuthController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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:strict',
  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 ((string)Cache::get(CacheKey::get('EMAIL_VERIFY_CODE', $request->input('email'))) !== (string)$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. $user->speed_limit = $plan->speed_limit;
  147. }
  148. }
  149. if (!$user->save()) {
  150. abort(500, __('Register failed'));
  151. }
  152. if ((int)config('v2board.email_verify', 0)) {
  153. Cache::forget(CacheKey::get('EMAIL_VERIFY_CODE', $request->input('email')));
  154. }
  155. $user->last_login_at = time();
  156. $user->save();
  157. if ((int)config('v2board.register_limit_by_ip_enable', 0)) {
  158. Cache::put(
  159. CacheKey::get('REGISTER_IP_RATE_LIMIT', $request->ip()),
  160. (int)$registerCountByIP + 1,
  161. (int)config('v2board.register_limit_expire', 60) * 60
  162. );
  163. }
  164. $authService = new AuthService($user);
  165. return response()->json([
  166. 'data' => $authService->generateAuthData($request)
  167. ]);
  168. }
  169. public function login(AuthLogin $request)
  170. {
  171. $email = $request->input('email');
  172. $password = $request->input('password');
  173. if ((int)config('v2board.password_limit_enable', 1)) {
  174. $passwordErrorCount = (int)Cache::get(CacheKey::get('PASSWORD_ERROR_LIMIT', $email), 0);
  175. if ($passwordErrorCount >= (int)config('v2board.password_limit_count', 5)) {
  176. abort(500, __('There are too many password errors, please try again after :minute minutes.', [
  177. 'minute' => config('v2board.password_limit_expire', 60)
  178. ]));
  179. }
  180. }
  181. $user = User::where('email', $email)->first();
  182. if (!$user) {
  183. abort(500, __('Incorrect email or password'));
  184. }
  185. if (!Helper::multiPasswordVerify(
  186. $user->password_algo,
  187. $user->password_salt,
  188. $password,
  189. $user->password)
  190. ) {
  191. if ((int)config('v2board.password_limit_enable')) {
  192. Cache::put(
  193. CacheKey::get('PASSWORD_ERROR_LIMIT', $email),
  194. (int)$passwordErrorCount + 1,
  195. 60 * (int)config('v2board.password_limit_expire', 60)
  196. );
  197. }
  198. abort(500, __('Incorrect email or password'));
  199. }
  200. if ($user->banned) {
  201. abort(500, __('Your account has been suspended'));
  202. }
  203. $authService = new AuthService($user);
  204. return response([
  205. 'data' => $authService->generateAuthData($request)
  206. ]);
  207. }
  208. public function token2Login(Request $request)
  209. {
  210. if ($request->input('token')) {
  211. $redirect = '/#/login?verify=' . $request->input('token') . '&redirect=' . ($request->input('redirect') ? $request->input('redirect') : 'dashboard');
  212. if (config('v2board.app_url')) {
  213. $location = config('v2board.app_url') . $redirect;
  214. } else {
  215. $location = url($redirect);
  216. }
  217. return redirect()->to($location)->send();
  218. }
  219. if ($request->input('verify')) {
  220. $key = CacheKey::get('TEMP_TOKEN', $request->input('verify'));
  221. $userId = Cache::get($key);
  222. if (!$userId) {
  223. abort(500, __('Token error'));
  224. }
  225. $user = User::find($userId);
  226. if (!$user) {
  227. abort(500, __('The user does not '));
  228. }
  229. if ($user->banned) {
  230. abort(500, __('Your account has been suspended'));
  231. }
  232. Cache::forget($key);
  233. $authService = new AuthService($user);
  234. return response([
  235. 'data' => $authService->generateAuthData($request)
  236. ]);
  237. }
  238. }
  239. public function getQuickLoginUrl(Request $request)
  240. {
  241. $authorization = $request->input('auth_data') ?? $request->header('authorization');
  242. if (!$authorization) abort(403, '未登录或登陆已过期');
  243. $user = AuthService::decryptAuthData($authorization);
  244. if (!$user) abort(403, '未登录或登陆已过期');
  245. $code = Helper::guid();
  246. $key = CacheKey::get('TEMP_TOKEN', $code);
  247. Cache::put($key, $user['id'], 60);
  248. $redirect = '/#/login?verify=' . $code . '&redirect=' . ($request->input('redirect') ? $request->input('redirect') : 'dashboard');
  249. if (config('v2board.app_url')) {
  250. $url = config('v2board.app_url') . $redirect;
  251. } else {
  252. $url = url($redirect);
  253. }
  254. return response([
  255. 'data' => $url
  256. ]);
  257. }
  258. public function forget(AuthForget $request)
  259. {
  260. if ((string)Cache::get(CacheKey::get('EMAIL_VERIFY_CODE', $request->input('email'))) !== (string)$request->input('email_code')) {
  261. abort(500, __('Incorrect email verification code'));
  262. }
  263. $user = User::where('email', $request->input('email'))->first();
  264. if (!$user) {
  265. abort(500, __('This email is not registered in the system'));
  266. }
  267. $user->password = password_hash($request->input('password'), PASSWORD_DEFAULT);
  268. $user->password_algo = NULL;
  269. $user->password_salt = NULL;
  270. if (!$user->save()) {
  271. abort(500, __('Reset failed'));
  272. }
  273. Cache::forget(CacheKey::get('EMAIL_VERIFY_CODE', $request->input('email')));
  274. return response([
  275. 'data' => true
  276. ]);
  277. }
  278. }