ThrottlesLogins.php 3.1 KB

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