3 namespace BookStack\Access\Controllers;
5 use Illuminate\Cache\RateLimiter;
6 use Illuminate\Http\Request;
7 use Illuminate\Http\Response;
8 use Illuminate\Support\Str;
9 use Illuminate\Validation\ValidationException;
14 * Determine if the user has too many failed login attempts.
16 protected function hasTooManyLoginAttempts(Request $request): bool
18 return $this->limiter()->tooManyAttempts(
19 $this->throttleKey($request),
25 * Increment the login attempts for the user.
27 protected function incrementLoginAttempts(Request $request): void
29 $this->limiter()->hit(
30 $this->throttleKey($request),
31 $this->decayMinutes() * 60
36 * Redirect the user after determining they are locked out.
37 * @throws ValidationException
39 protected function sendLockoutResponse(Request $request): \Symfony\Component\HttpFoundation\Response
41 $seconds = $this->limiter()->availableIn(
42 $this->throttleKey($request)
45 throw ValidationException::withMessages([
46 $this->username() => [trans('auth.throttle', [
47 'seconds' => $seconds,
48 'minutes' => ceil($seconds / 60),
50 ])->status(Response::HTTP_TOO_MANY_REQUESTS);
54 * Clear the login locks for the given user credentials.
56 protected function clearLoginAttempts(Request $request): void
58 $this->limiter()->clear($this->throttleKey($request));
62 * Get the throttle key for the given request.
64 protected function throttleKey(Request $request): string
66 return Str::transliterate(Str::lower($request->input($this->username())) . '|' . $request->ip());
70 * Get the rate limiter instance.
72 protected function limiter(): RateLimiter
74 return app(RateLimiter::class);
78 * Get the maximum number of attempts to allow.
80 public function maxAttempts(): int
86 * Get the number of minutes to throttle for.
88 public function decayMinutes(): int