ThrottlesLogins.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. namespace Illuminate\Foundation\Auth;
  3. use Illuminate\Auth\Events\Lockout;
  4. use Illuminate\Cache\RateLimiter;
  5. use Illuminate\Http\Request;
  6. use Illuminate\Http\Response;
  7. use Illuminate\Support\Facades\Lang;
  8. use Illuminate\Support\Str;
  9. use Illuminate\Validation\ValidationException;
  10. trait ThrottlesLogins
  11. {
  12. /**
  13. * Determine if the user has too many failed login attempts.
  14. *
  15. * @param \Illuminate\Http\Request $request
  16. * @return bool
  17. */
  18. protected function hasTooManyLoginAttempts(Request $request)
  19. {
  20. return $this->limiter()->tooManyAttempts(
  21. $this->throttleKey($request), $this->maxAttempts()
  22. );
  23. }
  24. /**
  25. * Increment the login attempts for the user.
  26. *
  27. * @param \Illuminate\Http\Request $request
  28. * @return void
  29. */
  30. protected function incrementLoginAttempts(Request $request)
  31. {
  32. $this->limiter()->hit(
  33. $this->throttleKey($request), $this->decayMinutes() * 60
  34. );
  35. }
  36. /**
  37. * Redirect the user after determining they are locked out.
  38. *
  39. * @param \Illuminate\Http\Request $request
  40. * @return void
  41. *
  42. * @throws \Illuminate\Validation\ValidationException
  43. */
  44. protected function sendLockoutResponse(Request $request)
  45. {
  46. $seconds = $this->limiter()->availableIn(
  47. $this->throttleKey($request)
  48. );
  49. throw ValidationException::withMessages([
  50. $this->username() => [Lang::get('auth.throttle', [
  51. 'seconds' => $seconds,
  52. 'minutes' => ceil($seconds / 60),
  53. ])],
  54. ])->status(Response::HTTP_TOO_MANY_REQUESTS);
  55. }
  56. /**
  57. * Clear the login locks for the given user credentials.
  58. *
  59. * @param \Illuminate\Http\Request $request
  60. * @return void
  61. */
  62. protected function clearLoginAttempts(Request $request)
  63. {
  64. $this->limiter()->clear($this->throttleKey($request));
  65. }
  66. /**
  67. * Fire an event when a lockout occurs.
  68. *
  69. * @param \Illuminate\Http\Request $request
  70. * @return void
  71. */
  72. protected function fireLockoutEvent(Request $request)
  73. {
  74. event(new Lockout($request));
  75. }
  76. /**
  77. * Get the throttle key for the given request.
  78. *
  79. * @param \Illuminate\Http\Request $request
  80. * @return string
  81. */
  82. protected function throttleKey(Request $request)
  83. {
  84. return Str::lower($request->input($this->username())).'|'.$request->ip();
  85. }
  86. /**
  87. * Get the rate limiter instance.
  88. *
  89. * @return \Illuminate\Cache\RateLimiter
  90. */
  91. protected function limiter()
  92. {
  93. return app(RateLimiter::class);
  94. }
  95. /**
  96. * Get the maximum number of attempts to allow.
  97. *
  98. * @return int
  99. */
  100. public function maxAttempts()
  101. {
  102. return property_exists($this, 'maxAttempts') ? $this->maxAttempts : 5;
  103. }
  104. /**
  105. * Get the number of minutes to throttle for.
  106. *
  107. * @return int
  108. */
  109. public function decayMinutes()
  110. {
  111. return property_exists($this, 'decayMinutes') ? $this->decayMinutes : 1;
  112. }
  113. }