PasswordController.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Controllers;
  4. use App\Models\Config;
  5. use App\Models\User;
  6. use App\Services\Cache;
  7. use App\Services\Captcha;
  8. use App\Services\Password;
  9. use App\Services\RateLimit;
  10. use App\Utils\Hash;
  11. use App\Utils\ResponseHelper;
  12. use Exception;
  13. use Psr\Http\Client\ClientExceptionInterface;
  14. use Psr\Http\Message\ResponseInterface;
  15. use RedisException;
  16. use Slim\Http\Response;
  17. use Slim\Http\ServerRequest;
  18. use voku\helper\AntiXSS;
  19. use function strlen;
  20. final class PasswordController extends BaseController
  21. {
  22. /**
  23. * @throws Exception
  24. */
  25. public function reset(ServerRequest $request, Response $response, array $args): Response|ResponseInterface
  26. {
  27. $captcha = [];
  28. if (Config::obtain('enable_reset_password_captcha')) {
  29. $captcha = Captcha::generate();
  30. }
  31. return $response->write(
  32. $this->view()
  33. ->assign('captcha', $captcha)
  34. ->fetch('password/reset.tpl')
  35. );
  36. }
  37. /**
  38. * @throws RedisException
  39. */
  40. public function handleReset(ServerRequest $request, Response $response, array $args): ResponseInterface
  41. {
  42. if (Config::obtain('enable_reset_password_captcha')) {
  43. $ret = Captcha::verify($request->getParams());
  44. if (! $ret) {
  45. return ResponseHelper::error($response, '系统无法接受你的验证结果,请刷新页面后重试');
  46. }
  47. }
  48. $antiXss = new AntiXSS();
  49. $email = strtolower($antiXss->xss_clean($request->getParam('email')));
  50. if ($email === '') {
  51. return ResponseHelper::error($response, '未填写邮箱');
  52. }
  53. if (! RateLimit::checkEmailIpLimit($request->getServerParam('REMOTE_ADDR')) ||
  54. ! RateLimit::checkEmailAddressLimit($email)
  55. ) {
  56. return ResponseHelper::error($response, '你的请求过于频繁,请稍后再试');
  57. }
  58. $user = User::where('email', $email)->first();
  59. $msg = '如果你的账户存在于我们的数据库中,那么重置密码的链接将会发送到你账户所对应的邮箱。';
  60. if ($user !== null) {
  61. try {
  62. Password::sendResetEmail($email);
  63. } catch (ClientExceptionInterface|RedisException $e) {
  64. $msg = '邮件发送失败,请联系网站管理员。';
  65. }
  66. }
  67. return ResponseHelper::success($response, $msg);
  68. }
  69. /**
  70. * @throws Exception
  71. */
  72. public function token(ServerRequest $request, Response $response, array $args)
  73. {
  74. $antiXss = new AntiXSS();
  75. $token = $antiXss->xss_clean($args['token']);
  76. $redis = Cache::initRedis();
  77. $email = $redis->get('password_reset:' . $token);
  78. if (! $email) {
  79. return $response->withStatus(302)->withHeader('Location', '/password/reset');
  80. }
  81. return $response->write(
  82. $this->view()->fetch('password/token.tpl')
  83. );
  84. }
  85. /**
  86. * @throws RedisException
  87. */
  88. public function handleToken(ServerRequest $request, Response $response, array $args): ResponseInterface
  89. {
  90. $antiXss = new AntiXSS();
  91. $token = $antiXss->xss_clean($args['token']);
  92. $password = $request->getParam('password');
  93. $repasswd = $request->getParam('repasswd');
  94. if ($password !== $repasswd) {
  95. return ResponseHelper::error($response, '两次输入不符合');
  96. }
  97. if (strlen($password) < 8) {
  98. return ResponseHelper::error($response, '密码过短');
  99. }
  100. $redis = Cache::initRedis();
  101. $email = $redis->get('password_reset:' . $token);
  102. if (! $email) {
  103. return ResponseHelper::error($response, '链接无效');
  104. }
  105. $user = User::where('email', $email)->first();
  106. if ($user === null) {
  107. return ResponseHelper::error($response, '链接无效');
  108. }
  109. // reset password
  110. $hashPassword = Hash::passwordHash($password);
  111. $user->pass = $hashPassword;
  112. if (! $user->save()) {
  113. return ResponseHelper::error($response, '重置失败,请重试');
  114. }
  115. if (Config::obtain('enable_forced_replacement')) {
  116. $user->cleanLink();
  117. }
  118. $redis->del('password_reset:' . $token);
  119. return ResponseHelper::success($response, '重置成功');
  120. }
  121. }