/**
  * Handle an incoming request.
  *
  * @param  \Illuminate\Http\Request  $request
  * @param  \Closure  $next
  * @param  int  $maxAttempts
  * @param  int  $decayMinutes
  * @return mixed
  */
 public function handle($request, Closure $next, $maxAttempts = 60, $decayMinutes = 1)
 {
     $key = $this->resolveRequestSignature($request);
     if ($this->limiter->tooManyAttempts($key, $maxAttempts, $decayMinutes)) {
         return new Response('Too Many Attempts.', 429, ['Retry-After' => $this->limiter->availableIn($key), 'X-RateLimit-Limit' => $maxAttempts, 'X-RateLimit-Remaining' => 0]);
     }
     $this->limiter->hit($key, $decayMinutes);
     return $next($request)->withHeaders(['X-RateLimit-Limit' => $maxAttempts, 'X-RateLimit-Remaining' => $maxAttempts - $this->limiter->attempts($key) + 1]);
 }
Beispiel #2
0
 /**
  * Calculate the number of remaining attempts.
  *
  * @param  string  $key
  * @param  int  $maxAttempts
  * @return int
  */
 protected function calculateRemainingAttempts($key, $maxAttempts)
 {
     return $maxAttempts - $this->limiter->attempts($key) + 1;
 }