Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.2] reset the attempts on lockout to avoid race condition re-lockout #13283

Merged
merged 4 commits into from
Apr 25, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/Illuminate/Cache/RateLimiter.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ public function tooManyAttempts($key, $maxAttempts, $decayMinutes = 1)
if ($this->attempts($key) > $maxAttempts) {
$this->cache->add($key.':lockout', time() + ($decayMinutes * 60), $decayMinutes);

$this->resetAttempts($key);

return true;
}

Expand Down Expand Up @@ -72,6 +74,17 @@ public function attempts($key)
return $this->cache->get($key, 0);
}

/**
* Reset the number of attempts for the given key.
*
* @param string $key
* @return mixed
*/
public function resetAttempts($key)
{
return $this->cache->forget($key);
}

/**
* Get the number of retries left for the given key.
*
Expand All @@ -94,7 +107,7 @@ public function retriesLeft($key, $maxAttempts)
*/
public function clear($key)
{
$this->cache->forget($key);
$this->resetAttempts($key);

$this->cache->forget($key.':lockout');
}
Expand Down
1 change: 1 addition & 0 deletions tests/Cache/CacheRateLimiterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public function testTooManyAttemptsReturnsTrueIfMaxAttemptsExceeded()
$cache->shouldReceive('get')->once()->with('key', 0)->andReturn(10);
$cache->shouldReceive('has')->once()->with('key:lockout')->andReturn(false);
$cache->shouldReceive('add')->once()->with('key:lockout', m::type('int'), 1);
$cache->shouldReceive('forget')->once()->with('key');
$rateLimiter = new RateLimiter($cache);

$this->assertTrue($rateLimiter->tooManyAttempts('key', 1, 1));
Expand Down