1
0

ResetPasswordController.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. namespace App\Http\Controllers\Auth;
  3. use App\Http\Controllers\Controller;
  4. use Illuminate\Foundation\Auth\ResetsPasswords;
  5. use Illuminate\Support\Facades\Password;
  6. use Illuminate\Http\Request;
  7. use App\Services\BouncerService;
  8. use Illuminate\Validation\Rules;
  9. class ResetPasswordController extends Controller
  10. {
  11. /*
  12. |--------------------------------------------------------------------------
  13. | Password Reset Controller
  14. |--------------------------------------------------------------------------
  15. |
  16. | This controller is responsible for handling password reset requests
  17. | and uses a simple trait to include this behavior. You're free to
  18. | explore this trait and override any methods you wish to tweak.
  19. |
  20. */
  21. use ResetsPasswords;
  22. /**
  23. * Where to redirect users after resetting their password.
  24. *
  25. * @var string
  26. */
  27. protected $redirectTo = '/i/web';
  28. /**
  29. * Create a new controller instance.
  30. *
  31. * @return void
  32. */
  33. public function __construct()
  34. {
  35. $this->middleware('guest');
  36. }
  37. /**
  38. * Get the password reset validation rules.
  39. *
  40. * @return array
  41. */
  42. protected function rules()
  43. {
  44. usleep(random_int(100000, 3000000));
  45. if(config('captcha.enabled')) {
  46. return [
  47. 'token' => 'required',
  48. 'email' => 'required|email',
  49. 'password' => ['required', 'confirmed', 'max:72', Rules\Password::defaults()],
  50. 'h-captcha-response' => ['required' ,'filled', 'captcha']
  51. ];
  52. }
  53. return [
  54. 'token' => 'required',
  55. 'email' => 'required|email',
  56. 'password' => ['required', 'confirmed', 'max:72', Rules\Password::defaults()],
  57. ];
  58. }
  59. /**
  60. * Get the password reset validation error messages.
  61. *
  62. * @return array
  63. */
  64. protected function validationErrorMessages()
  65. {
  66. return [
  67. 'password.max' => 'Passwords should not exceed 72 characters.',
  68. 'h-captcha-response.required' => 'Failed to validate the captcha.',
  69. 'h-captcha-response.filled' => 'Failed to validate the captcha.',
  70. 'h-captcha-response.captcha' => 'Failed to validate the captcha.',
  71. ];
  72. }
  73. /**
  74. * Display the password reset view for the given token.
  75. *
  76. * If no token is present, display the link request form.
  77. *
  78. * @param \Illuminate\Http\Request $request
  79. * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
  80. */
  81. public function showResetForm(Request $request)
  82. {
  83. if(config('pixelfed.bouncer.cloud_ips.ban_logins')) {
  84. abort_if(BouncerService::checkIp($request->ip()), 404);
  85. }
  86. usleep(random_int(100000, 300000));
  87. $token = $request->route()->parameter('token');
  88. return view('auth.passwords.reset')->with(
  89. ['token' => $token, 'email' => $request->email]
  90. );
  91. }
  92. public function reset(Request $request)
  93. {
  94. if(config('pixelfed.bouncer.cloud_ips.ban_logins')) {
  95. abort_if(BouncerService::checkIp($request->ip()), 404);
  96. }
  97. $request->validate($this->rules(), $this->validationErrorMessages());
  98. // Here we will attempt to reset the user's password. If it is successful we
  99. // will update the password on an actual user model and persist it to the
  100. // database. Otherwise we will parse the error and return the response.
  101. $response = $this->broker()->reset(
  102. $this->credentials($request), function ($user, $password) {
  103. $this->resetPassword($user, $password);
  104. }
  105. );
  106. // If the password was successfully reset, we will redirect the user back to
  107. // the application's home authenticated view. If there is an error we can
  108. // redirect them back to where they came from with their error message.
  109. return $response == Password::PASSWORD_RESET
  110. ? $this->sendResetResponse($request, $response)
  111. : $this->sendResetFailedResponse($request, $response);
  112. }
  113. /**
  114. * Get the password reset credentials from the request.
  115. *
  116. * @param \Illuminate\Http\Request $request
  117. * @return array
  118. */
  119. protected function credentials(Request $request)
  120. {
  121. return $request->only(
  122. 'email', 'password', 'password_confirmation', 'token'
  123. );
  124. }
  125. /**
  126. * Get the response for a failed password reset.
  127. *
  128. * @param \Illuminate\Http\Request $request
  129. * @param string $response
  130. * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\JsonResponse
  131. */
  132. protected function sendResetFailedResponse(Request $request, $response)
  133. {
  134. if ($request->wantsJson()) {
  135. throw ValidationException::withMessages(['email' => [trans($response)]]);
  136. }
  137. return redirect()->back()
  138. ->withInput($request->only('email'))
  139. ->withErrors(['email' => [trans($response)]]);
  140. }
  141. }