Remember me improves

This commit is contained in:
Diego Najar 2017-11-08 00:00:48 +01:00
parent 33a8d69faa
commit ce4fadf280
5 changed files with 38 additions and 18 deletions

View File

@ -47,14 +47,7 @@ function checkRememberMe()
return false; return false;
} }
if (!Cookie::isset(REMEMBER_COOKIE_USERNAME) || !Cookie::isset(REMEMBER_COOKIE_TOKEN)) { if ($Login->verifyUserByRemember()) {
return false;
}
$username = Cookie::get(REMEMBER_COOKIE_USERNAME);
$token = Cookie::get(REMEMBER_COOKIE_TOKEN);
if ($Login->verifyUserByRemember($username, $token)) {
$Security->generateTokenCSRF(); $Security->generateTokenCSRF();
Redirect::page('dashboard'); Redirect::page('dashboard');
return true; return true;

View File

@ -12,6 +12,10 @@
<input name="password" class="uk-width-1-1 uk-form-large" placeholder="<?php $L->p('Password') ?>" type="password"> <input name="password" class="uk-width-1-1 uk-form-large" placeholder="<?php $L->p('Password') ?>" type="password">
</div> </div>
<div class="uk-form-row">
<label><input type="checkbox" name="remember"> Remember me</label>
</div>
<div class="uk-form-row"> <div class="uk-form-row">
<button type="submit" class="uk-width-1-1 uk-button uk-button-primary uk-button-large"><?php $Language->p('Login') ?></button> <button type="submit" class="uk-width-1-1 uk-button uk-button-primary uk-button-large"><?php $Language->p('Login') ?></button>
</div> </div>

View File

@ -207,6 +207,7 @@ include(PATH_HELPERS.'paginator.class.php');
include(PATH_HELPERS.'image.class.php'); include(PATH_HELPERS.'image.class.php');
include(PATH_HELPERS.'tcp.class.php'); include(PATH_HELPERS.'tcp.class.php');
include(PATH_HELPERS.'dom.class.php'); include(PATH_HELPERS.'dom.class.php');
include(PATH_HELPERS.'cookie.class.php');
if (file_exists(PATH_KERNEL.'bludit.pro.php')) { if (file_exists(PATH_KERNEL.'bludit.pro.php')) {
include(PATH_KERNEL.'bludit.pro.php'); include(PATH_KERNEL.'bludit.pro.php');

View File

@ -5,7 +5,7 @@ class Cookie {
public static function get($key) public static function get($key)
{ {
if (isset($_COOKIE[$key])) { if (isset($_COOKIE[$key])) {
return $_COOKIE[$name]; return $_COOKIE[$key];
} }
return false; return false;
} }

View File

@ -61,6 +61,20 @@ class Login {
// Set the token on the cookies // Set the token on the cookies
Cookie::set(REMEMBER_COOKIE_USERNAME, $username, REMEMBER_COOKIE_EXPIRE_IN_DAYS); Cookie::set(REMEMBER_COOKIE_USERNAME, $username, REMEMBER_COOKIE_EXPIRE_IN_DAYS);
Cookie::set(REMEMBER_COOKIE_TOKEN, $token, REMEMBER_COOKIE_EXPIRE_IN_DAYS); Cookie::set(REMEMBER_COOKIE_TOKEN, $token, REMEMBER_COOKIE_EXPIRE_IN_DAYS);
Log::set(__METHOD__.LOG_SEP.'Cookies seted for Remember Me.');
}
public function invalidateRememberMe()
{
// Invalidate all tokens on the user databases
$this->dbUsers->invalidateAllRememberTokens();
// Destroy the cookies
Cookie::set(REMEMBER_COOKIE_USERNAME, '', -1);
Cookie::set(REMEMBER_COOKIE_TOKEN, '', -1);
unset($_COOKIE[REMEMBER_COOKIE_USERNAME]);
unset($_COOKIE[REMEMBER_COOKIE_TOKEN]);
} }
// Check if the username and the password are valid // Check if the username and the password are valid
@ -101,11 +115,16 @@ class Login {
return false; return false;
} }
// Verified Remember Token // Check if the user has the cookies and the correct token
// If valid log in the user public function verifyUserByRemember()
// If not valid invalidate all remember me tokens
public function verifyUserByRemember($username, $token)
{ {
if (!Cookie::isset(REMEMBER_COOKIE_USERNAME) || !Cookie::isset(REMEMBER_COOKIE_TOKEN)) {
return false;
}
$username = Cookie::get(REMEMBER_COOKIE_USERNAME);
$token = Cookie::get(REMEMBER_COOKIE_TOKEN);
$username = Sanitize::html($username); $username = Sanitize::html($username);
$token = Sanitize::html($token); $token = Sanitize::html($token);
@ -113,20 +132,21 @@ class Login {
$token = trim($token); $token = trim($token);
if (empty($username) || empty($token)) { if (empty($username) || empty($token)) {
$this->dbUsers->invalidateAllRememberTokens(); $this->invalidateRememberMe();
Log::set(__METHOD__.LOG_SEP.'Username or Token empty. Username: '.$username.' - Token: '.$token); Log::set(__METHOD__.LOG_SEP.'Username or Token empty. Username: '.$username.' - Token: '.$token);
return false; return false;
} }
if ($username !== $this->getByRememberToken($token)) { if ($username !== $this->dbUsers->getByRememberToken($token)) {
$this->dbUsers->invalidateAllRememberTokens(); $this->invalidateRememberMe();
Log::set(__METHOD__.LOG_SEP.'The user has different token or the token doesnt exist.'); Log::set(__METHOD__.LOG_SEP.'The user has different token or the token doesn\'t exist.');
return false; return false;
} }
// Validate user and login // Validate user and login
$user = $this->dbUsers->getDb($username); $user = $this->dbUsers->getDb($username);
$this->setLogin($username, $user['role']); $this->setLogin($username, $user['role']);
Log::set(__METHOD__.LOG_SEP.'User authenticated via Remember Me.');
return true; return true;
} }
@ -141,6 +161,8 @@ class Login {
public function logout() public function logout()
{ {
return Session::destroy(); $this->invalidateRememberMe();
Session::destroy();
return true;
} }
} }