Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

matthewknill's avatar

Throttle login attempts hierarchically

Throttle login hierachically

I'm using the default Laravel 9 LoginRequest and I'm wondering what is the most succinct way of rate limiting based on the following constraints:

  • Max attempts per 10 minutes: 3
  • Max attempts per hour: 10
  • Max attempts per day: 20

Unfortunately, I haven't found a great deal of information on doing it within the LoginRequest class. I have seen it done succinctly using the configureRateLimiting in the RouteServiceProvider class, however, this would not throw the validation exception. Here is the code I have currently:

    /**
     * The rate limit has been breached
     *
     * @return mixed
     */
    protected function breachRateLimit()
    {
        event(new Lockout($this));

        $seconds = RateLimiter::availableIn($this->throttleKey());

        throw ValidationException::withMessages([
            'email' => trans('auth.throttle', [
                'seconds' => $seconds,
                'minutes' => ceil($seconds / 60),
                'hours' => ceil($seconds / 3600),
            ]),
        ]);
    }

    /**
     * Ensure the login request is not rate limited.
     *
     * @throws \Illuminate\Validation\ValidationException
     */
    public function ensureIsNotRateLimited(): void
    {
        // Rate limit to 5 attempts every 10 minutes
        if (!RateLimiter::attempt($this->throttleKey(), 5, function () {}, 600)) {
            $this->breachRateLimit();
        }

        // Rate limit to 10 attempts every hour
        if (!RateLimiter::attempt($this->throttleKey(), 10, function () {}, 3600)) {
            $this->breachRateLimit();
        }

        // Rate limit to 20 attempts per day
        if (!RateLimiter::attempt($this->throttleKey(), 20, function () {}, 86400)) {
            $this->breachRateLimit();
        }
    }

The rest of the LoginRequest class is identical to stock Laravel 9. I would expect that the above would work, however, it only seems to work for the 5 attempts every 10 minutes and seems to ignore the others.

0 likes
1 reply
matthewknill's avatar
matthewknill
OP
Best Answer
Level 1

The solution is to use multiple throttle keys as follows:

    /**
     * @return \Illuminate\Config\Repository|\Illuminate\Contracts\Foundation\Application|mixed
     */
    private function getRateLimits()
    {
        $passwordRateLimit = config('auth.password_rate_limit', [60 => 5]);
        krsort($passwordRateLimit);
        return $passwordRateLimit;
    }

    /**
     * Increment the rate limiter
     *
     * @return mixed
     */
    protected function failLogin()
    {
        foreach (array_keys($this->getRateLimits()) as $decaySeconds) {
            RateLimiter::hit($this->throttleKey($decaySeconds), $decaySeconds);
        }

        throw ValidationException::withMessages([
            'email' => trans('auth.failed'),
        ]);
    }

// authenticate function

    /**
     * Ensure the login request is not rate limited.
     *
     * @throws \Illuminate\Validation\ValidationException
     */
    public function ensureIsNotRateLimited(): void
    {
        foreach ($this->getRateLimits() as $decaySeconds => $maxAttempts) {
            if (RateLimiter::tooManyAttempts($this->throttleKey($decaySeconds), $maxAttempts)) {
                $seconds = RateLimiter::availableIn($this->throttleKey($decaySeconds));

                event(new Lockout($this));

                throw ValidationException::withMessages([
                    'email' => trans('auth.throttle', [
                        'seconds' => $seconds,
                    ]),
                ]);
            }
        }
    }

    /**
     * Get the rate limiting throttle key for the request.
     */
    public function throttleKey($interval = ''): string
    {
        return Str::transliterate(Str::lower($this->input('email')).'|'.$this->ip().'|'.$interval);
    }

Then in config/auth.php, configure as required:

    /*
    |--------------------------------------------------------------------------
    | Login Throttling
    |--------------------------------------------------------------------------
    |
    | Here you can define how many logins a user can attempt before
    | being throttled. The key is the decay in seconds and the value is
    | amount of attempts.
    |
    */
    'password_rate_limit' => [
        120=> 5,
        3600 => 15,
        86400 => 30,
    ]

Please or to participate in this conversation.