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

Adding security #550

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions src/Confide/CacheLoginThrottleService.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,14 @@ protected function parseIdentity($identity)
protected function countThrottle($identityString, $increments = 1)
{
$count = $this->app['cache']
->get('login_throttling:'.md5($identityString), 0);
->get('login_throttling:'.md5($identityString.$this->app['request']->server('REMOTE_ADDR')), 0);

$count = $count + $increments;

$ttl = $this->app['config']->get('confide::throttle_time_period');

$this->app['cache']
->put('login_throttling:'.md5($identityString), $count, $ttl);
->put('login_throttling:'.md5($identityString.$this->app['request']->server('REMOTE_ADDR')), $count, $ttl);

return $count;
}
Expand Down
21 changes: 12 additions & 9 deletions src/Confide/Confide.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,13 +235,16 @@ public function isThrottled($identity)
*/
public function forgotPassword($email)
{
$user = $this->repo->getUserByEmail($email);
if($this->passService->isThrottled()) return 'trottling';

if ($user) {
return $this->passService->requestChangePassword($user);
}
$user = $this->repo->getUserByEmail($email);

return false;
if ($user) {
return $this->passService->requestChangePassword($user);
}
$this->passService->countThrottle();

return false;
}

/**
Expand All @@ -264,16 +267,16 @@ public function destroyForgotPasswordToken($token)
*
* @return ConfideUser
*/
public function userByResetPasswordToken($token)
{
public function userByResetPasswordToken($token)
{
$email = $this->passService->getEmailByToken($token);

if ($email) {
return $this->repo->getUserByEmail($email);
return $this->repo->getUserByEmail($email);
}

return false;
}
}

/**
* Log the user out of the application.
Expand Down
98 changes: 92 additions & 6 deletions src/Confide/EloquentPasswordService.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ public function __construct($app = null)
public function requestChangePassword(RemindableInterface $user)
{
$email = $user->getReminderEmail();

$record = $this->getRecordByEmail($email);

if($record)
{
return $record;
}

$token = $this->generateToken();

$values = array(
Expand All @@ -59,6 +67,36 @@ public function requestChangePassword(RemindableInterface $user)
return $token;
}

/**
* Returns the record for email given
*
* @param string $token
*
* @return string Email.
*/
public function getRecordByEmail($email)
{
$connection = $this->getConnection();
$table = $this->getTable();

$record = $this->app['db']
->connection($connection)
->table($table)
->where('email', '=', $email)
->first();
// Check if record date is no longer valid
if($record && $record->created_at < $this->getOldestValidDate())
{
// Delete the record
$this->destroyToken($record->token);
return null;
}


return $record;
}


/**
* Returns the email associated with the given reset
* password token.
Expand All @@ -72,17 +110,24 @@ public function getEmailByToken($token)
$connection = $this->getConnection();
$table = $this->getTable();

$email = $this->app['db']
$record = $this->app['db']
->connection($connection)
->table($table)
->select('email')
->where('token', '=', $token)
->where('created_at', '>=', $this->getOldestValidDate())
->first();

$email = $this->unwrapEmail($email);

return $email;
if($record)
{
// Check if record date is valid
if($record->created_at >= $this->getOldestValidDate())
{
$email = $this->unwrapEmail($record);
return $email;
}
// Delete the record if token is no longer valid
$this->destroyToken($record->token);
}
return null;
}

/**
Expand Down Expand Up @@ -199,4 +244,45 @@ protected function getOldestValidDate()
->subHours($config->get('confide::confide.password_reset_expiration', 7))
->toDateTimeString();
}

/**
* Tells if the remote IP has reached the throttle_limit.
*
*
* @return bool True if the remote IP has reached the throttle_limit.
*/
public function isThrottled()
{

// Retuns the current count
$count = $this->countThrottle(0);

return $count >= $this->app['config']->get('confide::reset_password_throttle_limit', 4);
}

/**
* Increments the count for the remote IP by given $increments
* stores it into cache and returns the current value for remote IP
* address.
*
* @param int $increments Amount that is going to be added to the throttling attemps for the remote IP.
*
* @return int How many times that same remote IP was used.
*/
public function countThrottle($increments = 1)
{
$ip_remote = $this->app['request']->server('REMOTE_ADDR');

$count = $this->app['cache']
->get('reset_password_throttling:'.md5($ip_remote), 0);

$count = $count + $increments;

$ttl = $this->app['config']->get('confide::reset_password_throttle_time_period',15);

$this->app['cache']
->put('reset_password_throttling:'.md5($ip_remote), $count, $ttl);

return $count;
}
}
12 changes: 12 additions & 0 deletions src/config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,18 @@
*/
'password_reset_expiration' => 7, // hours

/*
|--------------------------------------------------------------------------
| Password reset Throttle
|--------------------------------------------------------------------------
|
| Defines how many wrong email failed tries from one IP may be done within
| the 'reset_password_throttle_time_period', which is in minutes.
|
*/
'reset_password_throttle_limit' => 4,
'reset_password_throttle_time_period' => 15,

/*
|--------------------------------------------------------------------------
| Signup E-mail and confirmation (true or false)
Expand Down