Commit 52df9500 authored by Jason Frisvold's avatar Jason Frisvold
Browse files

- Add password_hash functions (for BCRYPT) - PHP 5.3.7+ only

- Convert code to use Illuminate database ORM
- Use BCRYPT for password storage
parent 51f2d44e
......@@ -3,6 +3,7 @@
"slim/slim": "2.4.3",
"slim/views": "0.1.2",
"smarty/smarty": "3.1.21",
"illuminate/database": "4.2.9"
"illuminate/database": "4.2.9",
"ircmaxell/password-compat": "1.0.3"
}
}
......@@ -101,12 +101,16 @@
$isauthenticated = function( $role = 'user' ) {
return function() use ( $role ) {
// Globalize the phptodo variables needed
global $skynet_dbHost, $skynet_dbUser, $skynet_dbPass, $skynet_dbName,
$skynet_sessTime, $skynet_serveruri, $sqlhdlr, $smarty_data;
//global $skynet_dbHost, $skynet_dbUser, $skynet_dbPass, $skynet_dbName,
// $skynet_sessTime, $skynet_serveruri, $sqlhdlr, $smarty_data;
global $skynet_sessTime, $skynet_serveruri, $smarty_data, $sqlhdlr;
// Try and get the id, last time, and user id from the sessions
// database
$result = $sqlhdlr->table('sessions')->select('id', 'last', 'user_id')->where('phpsessid', '=', session_id())->first();
$result = $sqlhdlr->table('sessions')
->select('id', 'last', 'user_id')
->where('phpsessid', '=', session_id())
->first();
$id = $result['id'];
$last = $result['last'];
......@@ -133,9 +137,10 @@
//$sqlhdlr->query($query) or die( 'Error: ' . $sqlhdlr->error );
// Create the user object
$user_obj = new skynetUser($skynet_dbHost, $skynet_dbUser,
$skynet_dbPass, $skynet_dbName,
$user_id);
$user_obj = new skynetUser($user_id);
//$user_obj = new skynetUser($skynet_dbHost, $skynet_dbUser,
// $skynet_dbPass, $skynet_dbName,
// $user_id);
// Assign the appropriate data to the smarty $smarty_data array
$smarty_data['username'] = $user_obj->username();
......
......@@ -24,13 +24,15 @@ session_start();
function login($username, $password) {
// Globalize the phptodo variables needed
global $skynet_dbHost, $skynet_dbUser, $skynet_dbPass, $skynet_dbName,
$skynet_sessTime, $sqlhdlr;
//global $skynet_dbHost, $skynet_dbUser, $skynet_dbPass, $skynet_dbName,
// $skynet_sessTime, $sqlhdlr;
global $skynet_sessTime, $sqlhdlr;
// Create user class
$user_obj = new skynetUser($skynet_dbHost, $skynet_dbUser,
$skynet_dbPass, $skynet_dbName,
-1, $username, $password, true);
//$user_obj = new skynetUser($skynet_dbHost, $skynet_dbUser,
// $skynet_dbPass, $skynet_dbName,
// -1, $username, $password, true);
$user_obj = new skynetUser(-1, $username, $password, true);
if ($user_obj->logged_in()) {
// Regenerate the session ID (security enhancement)
......@@ -38,17 +40,29 @@ function login($username, $password) {
// Insert the user_id into the sessions database along with the
// session ID and the current time
$query = sprintf('INSERT INTO sessions (phpsessid, user_id, last) ' .
'VALUES ("%s", "%s", %d)', session_id(),
$user_obj->user_id(), time());
$sqlhdlr->query($query) or die( 'Error: ' . $sqlhdlr->error );
$sqlhdlr->table('sessions')
->insert(array(
'phpsessid' => session_id(),
'user_id' => $user_obj->user_id(),
'last' => time()
));
//$query = sprintf('INSERT INTO sessions (phpsessid, user_id, last) ' .
// 'VALUES ("%s", "%s", %d)', session_id(),
// $user_obj->user_id(), time());
//
//$sqlhdlr->query($query) or die( 'Error: ' . $sqlhdlr->error );
// Clean up any old sessions that have timed out
$query = sprintf('DELETE FROM sessions WHERE last < %d', time() -
$skynet_sessTime);
$sqlhdlr->query($query) or die( 'Error: ' . $sqlhdlr->error );
$sqlhdlr->table('sessions')
->where('last', '<', time() - $skynet_sessTime)
->delete();
//$query = sprintf('DELETE FROM sessions WHERE last < %d', time() -
// $skynet_sessTime);
//
//$sqlhdlr->query($query) or die( 'Error: ' . $sqlhdlr->error );
// Return 1 indicating a successful login
return 1;
......@@ -64,45 +78,71 @@ function login($username, $password) {
function authenticate() {
// Globalize the phptodo variables needed
global $skynet_dbHost, $skynet_dbUser, $skynet_dbPass, $skynet_dbName,
$skynet_sessTime, $sqlhdlr;
//global $skynet_dbHost, $skynet_dbUser, $skynet_dbPass, $skynet_dbName,
// $skynet_sessTime, $sqlhdlr;
global $skynet_sessTime, $smarty_data, $sqlhdlr;
// Globalize the user variables
global $smarty;
//global $smarty;
// Try and get the id, last time, and user if from the sessions database
$query = sprintf('SELECT id, last, user_id FROM sessions WHERE ' .
'phpsessid = "%s"', session_id());
$results = $sqlhdlr->table('sessions')
->select('id', 'last', 'user_id')
->where('phpsessid', session_id())
->first();
if ($result = $sqlhdlr->query($query)) {
list($id, $last, $user_id) = $result->fetch_array();
$result->close();
} else {
die( 'Error: ' . $sqlhdlr->error );
}
$id = $results['id'];
$last = $results['last'];
$user_id = $results['user_id'];
// Try and get the id, last time, and user if from the sessions database
//$query = sprintf('SELECT id, last, user_id FROM sessions WHERE ' .
// 'phpsessid = "%s"', session_id());
//
//if ($result = $sqlhdlr->query($query)) {
// list($id, $last, $user_id) = $result->fetch_array();
// $result->close();
//} else {
// die( 'Error: ' . $sqlhdlr->error );
//}
// Check to see if an id was set, and if the time is good
if ((isset($id)) && (($last + $skynet_sessTime) >= time())) {
// Good session, update the timestamp
$query = sprintf("UPDATE sessions SET last = %d WHERE id = %d", time(),
$id);
$sqlhdlr->table('sessions')
->where('id', $id)
->update(array(
'last', time()
));
$sqlhdlr->query($query) or die( 'Error: ' . $sqlhdlr->error );
//$query = sprintf("UPDATE sessions SET last = %d WHERE id = %d", time(),
// $id);
//
//$sqlhdlr->query($query) or die( 'Error: ' . $sqlhdlr->error );
// Create the user object
$user_obj = new skynetUser($skynet_dbHost, $skynet_dbUser,
$skynet_dbPass, $skynet_dbName, $user_id);
//$user_obj = new skynetUser($skynet_dbHost, $skynet_dbUser,
// $skynet_dbPass, $skynet_dbName, $user_id);
$user_obj = new skynetUser($user_id);
// Assign the username to the smarty template
$smarty->assign('username', $user_obj->username());
$smarty->assign('adminflag', $user_obj->adminflag());
array_push($smarty_data, array('username' => $user_obj->username()));
array_push($smarty_data, array('adminflag', $user_obj->adminflag()));
//$smarty->assign('username', $user_obj->username());
//$smarty->assign('adminflag', $user_obj->adminflag());
if (get_magic_quotes_gpc()) {
$smarty->assign('fullname',
stripslashes(htmlentities($user_obj->fullname(),
ENT_QUOTES)));
array_push($smarty_data,
array('fullname',
stripslashes(htmlentities($user_obj->fullname(),
ENT_QUOTES))));
//$smarty->assign('fullname',
// stripslashes(htmlentities($user_obj->fullname(),
// ENT_QUOTES)));
} else {
$smarty->assign('fullname', htmlentities($user_obj->fullname(),
ENT_QUOTES));
array_push($smarty_data, array('fullname',
htmlentities($user_obj->fullname(),
ENT_QUOTES)));
//$smarty->assign('fullname', htmlentities($user_obj->fullname(),
// ENT_QUOTES));
}
return($user_obj);
......@@ -126,8 +166,9 @@ function feed_authenticate($user_id, $feed_id, $secret) {
if ($feed->secret() == $secret) {
// Create the user object
$user_obj = new skynetUser($skynet_dbHost, $skynet_dbUser,
$skynet_dbPass, $skynet_dbName, $user_id);
$user_obj = new skynetUser($user_id);
//$user_obj = new skynetUser($skynet_dbHost, $skynet_dbUser,
// $skynet_dbPass, $skynet_dbName, $user_id);
return Array($feed, $user_obj);
} else {
......@@ -143,10 +184,14 @@ function logout() {
global $sqlhdlr;
// Try and get the id, last time, and user if from the sessions database
$query = sprintf("DELETE FROM sessions WHERE phpsessid = '%s'",
session_id());
$sqlhdlr->query($query) or die( 'Error: ' . $sqlhdlr->error );
$sqlhdlr->table('sessions')
->where('phpsessid', session_id())
->delete();
//$query = sprintf("DELETE FROM sessions WHERE phpsessid = '%s'",
// session_id());
//
//$sqlhdlr->query($query) or die( 'Error: ' . $sqlhdlr->error );
return(1);
}
......
......@@ -35,12 +35,13 @@ class skynetUser {
*/
private $user_id; // Integer
private $username; // Char(15)
private $passwordHash; // Char()
private $fullname; // Char(40)
private $email; // Char(320)
private $adminflag; // Boolean (TinyInt)
private $logged_in; // Boolean
private $sqlhdlr; // Object
//private $sqlhdlr; // Object
private $dirty_flag; // Boolean
private $pwd_dirty_flag; // Boolean
......@@ -59,17 +60,19 @@ class skynetUser {
* @param boolean $login Boolean value indicating whether this is a login
* attempt
*/
public function __construct($dbHost, $dbUser, $dbPass, $dbName, $uid = -1,
//public function __construct($dbHost, $dbUser, $dbPass, $dbName, $uid = -1,
// $username = '', $password = '', $login = false) {
public function __construct($uid = -1,
$username = '', $password = '', $login = false) {
global $sqlhdlr;
// Connect to the database server
$this->sqlhdlr = new mysqli($dbHost, $dbUser, $dbPass, $dbName);
//$this->sqlhdlr = new mysqli($dbHost, $dbUser, $dbPass, $dbName);
// Make sure we have a connection
if (mysqli_connect_errno()) {
die( '<p>Unable to connect to the database server at this time: ' .
mysqli_connect_error() . ' </p>' );
}
//if (mysqli_connect_errno()) {
// die( '<p>Unable to connect to the database server at this time: ' .
// mysqli_connect_error() . ' </p>' );
//}
// Everyone starts with a clean slate
$this->dirty_flag = false;
......@@ -97,25 +100,43 @@ class skynetUser {
$clean_password = $password;
}
// Check the database for the user and get their password
$query = sprintf('SELECT id FROM users WHERE username = "%s" ' .
'AND password = SHA1("%s")', $clean_username,
$this->sqlhdlr->real_escape_string($clean_password));
if ($result = $this->sqlhdlr->query($query)) {
// If a single row was returned then we have a match
if ($result->num_rows == 1) {
// Get the user_id from the sql query result
list($this->user_id) = $result->fetch_array();
$result->close();
// Mark the user as logged in
$this->logged_in = true;
//// Check the database for the user and get their password
//$query = sprintf('SELECT id FROM users WHERE username = "%s" ' .
// 'AND password = SHA1("%s")', $clean_username,
// $this->sqlhdlr->real_escape_string($clean_password));
$results = $sqlhdlr->table('users')
->select('id', 'password')
->where('username', $clean_username)
->first();
$this->user_id = $results['id'];
$this->passwordHash = $results['password'];
if (password_verify($clean_password, $this->passwordHash)) {
if (password_needs_rehash($this->passwordHash,
PASSWORD_BCRYPT,
['cost' => 10])) {
$this->passwordHash = password_hash($password);
$this->_update_password();
}
} else {
die( 'Error: ' . $this->sqlhdlr->error );
$this->logged_in = true;
}
// if ($result = $this->sqlhdlr->query($query)) {
// // If a single row was returned then we have a match
// if ($result->num_rows == 1) {
// // Get the user_id from the sql query result
// list($this->user_id) = $result->fetch_array();
//
// $result->close();
//
// // Mark the user as logged in
// $this->logged_in = true;
// }
// } else {
// die( 'Error: ' . $this->sqlhdlr->error );
// }
}
// If we have a UID, or we are logged in
......@@ -125,24 +146,34 @@ class skynetUser {
$this->user_id = $uid;
}
// Create the necessary sql statement
$query = sprintf('SELECT u.username, u.full_name, u.email, ' .
'u.admin FROM users AS u WHERE u.id = %d',
$this->user_id);
if ($result = $this->sqlhdlr->query($query)) {
$line = $result->fetch_array(MYSQLI_NUM);
$this->username = $line[0];
$this->fullname = $line[1];
$this->email = $line[2];
$this->adminflag = $line[3];
$result->close();
} else {
die( 'Error: ' . $this->sqlhdlr->error );
}
$results = $sqlhdlr->table('users')
->select('username', 'full_name', 'email', 'admin')
->where('id', $this->user_id)
->first();
$this->username = $results['username'];
$this->fullname = $results['full_name'];
$this->email = $results['email'];
$this->adminflag = $results['admin'];
//// Create the necessary sql statement
//$query = sprintf('SELECT u.username, u.full_name, u.email, ' .
// 'u.admin FROM users AS u WHERE u.id = %d',
// $this->user_id);
//
//if ($result = $this->sqlhdlr->query($query)) {
//
// $line = $result->fetch_array(MYSQLI_NUM);
//
// $this->username = $line[0];
// $this->fullname = $line[1];
// $this->email = $line[2];
// $this->adminflag = $line[3];
//
// $result->close();
//} else {
// die( 'Error: ' . $this->sqlhdlr->error );
//}
// New user, use defaults
} else {
......@@ -181,7 +212,7 @@ class skynetUser {
}
/**
* Set / Return the user's password
* Set the user's password
*
* @param string $password String representing the user password
*
......@@ -192,49 +223,50 @@ class skynetUser {
$skynet_pwdRegex = '/^[a-zA-Z0-9@#$%\^&\*\/]{4,15}\z/';
if (! empty($password)) {
if (preg_match($skynet_pwdRegex, $password)) {
$this->password = $password;
$this->passwordHash =
password_hash($password, PASSWORD_BCRYPT, ['cost' => 10]);
// Mark as dirty
$this->pwd_dirty_flag = true;
} else {
return null;
return true;
}
}
return $this->password;
}
/**
* Checks whether the supplied password is correct or not
*
* @param string $password String representing the user's password
*
* @return boolean True if the password is correct, false if not
*/
public function check_pwd($password = '') {
$skynet_pwdRegex = '/^[a-zA-Z0-9@#$%\^&\*\/]{4,15}\z/';
if (! empty($password) && preg_match($skynet_pwdRegex, $password)) {
// Check the password against whats stored in the database
$query = sprintf('SELECT count(*) FROM users WHERE id = %d ' .
'AND password = SHA1("%s")', $this->user_id,
$this->sqlhdlr->real_escape_string($password));
if ($result = $this->sqlhdlr->query($query)) {
list($count) = $result->fetch_array();
// If a single row was returned then we have a match
if ($count == 1) {
return true;
}
} else {
die( 'Error: ' . $this->sqlhdlr->error );
}
}
// If we get here, return a failure
return false;
}
///**
// * Checks whether the supplied password is correct or not
// *
// * @param string $password String representing the user's password
// *
// * @return boolean True if the password is correct, false if not
// */
//public function check_pwd($password = '') {
// $skynet_pwdRegex = '/^[a-zA-Z0-9@#$%\^&\*\/]{4,15}\z/';
// if (! empty($password) && preg_match($skynet_pwdRegex, $password)) {
// // Check the password against whats stored in the database
// $query = sprintf('SELECT count(*) FROM users WHERE id = %d ' .
// 'AND password = SHA1("%s")', $this->user_id,
// $this->sqlhdlr->real_escape_string($password));
//
// if ($result = $this->sqlhdlr->query($query)) {
// list($count) = $result->fetch_array();
//
// // If a single row was returned then we have a match
// if ($count == 1) {
// return true;
// }
// } else {
// die( 'Error: ' . $this->sqlhdlr->error );
// }
// }
//
// // If we get here, return a failure
// return false;
//}
/**
* Set / Return the username
*
......@@ -342,6 +374,15 @@ class skynetUser {
$this->deleteme = true;
}
/**
* Updates the user password hash in the database
*/
private function _update_password() {
$sqlhdlr->table('users')
->where('id', $this->user_id)
->update(array('password' => $this->passwordHash));
}
/**
* Deletes the user from the database if the deleteme flag is set
*/
......@@ -350,9 +391,13 @@ class skynetUser {
if ($this->deleteme) {
// Delete the entries, being careful to ensure it's
// from the right user
$query = sprintf('DELETE FROM users WHERE id = %d',
$this->user_id);
$this->sqlhdlr->query($query) or die( 'Error: ' . $this->sqlhdlr->error );
$sqlhdlr->table('users')
->where('id', $this->id)
->delete();
//$query = sprintf('DELETE FROM users WHERE id = %d',
// $this->user_id);
//$this->sqlhdlr->query($query) or die( 'Error: ' . $this->sqlhdlr->error );
}
}
......@@ -363,30 +408,43 @@ class skynetUser {
// username, fullname, password, admin flag
// User ID of -1 indicates a new user
if ($this->user_id == -1) {
$query = sprintf('INSERT INTO users (username, full_name, email, ' .
'password, admin) VALUES ("%s", "%s", "%s", ' .
'SHA1("%s"), %d)', $this->username, $this->fullname,
$this->sqlhdlr->real_escape_string($this->email),
$this->password, $this->adminflag);
$this->sqlhdlr->query($query) or die( 'Error: ' . $this->sqlhdlr->error );
$sqlhdlr->table('users')->insert(array(
'username' => $this->username,
'full_name' => $this->fullname,
'email' => $this->email,
'password' => $this->passwordHash,
'admin' => $this->adminflag
));
//$query = sprintf('INSERT INTO users (username, full_name, email, ' .
// 'password, admin) VALUES ("%s", "%s", "%s", ' .
// 'SHA1("%s"), %d)', $this->username, $this->fullname,
// $this->sqlhdlr->real_escape_string($this->email),
// $this->password, $this->adminflag);
//
//$this->sqlhdlr->query($query) or die( 'Error: ' . $this->sqlhdlr->error );
// Get the user_id
$this->user_id = $this->sqlhdlr->insert_id;
} else {
$query = sprintf('UPDATE users SET username = "%s", full_name = ' .
'"%s", email = "%s", admin = %d WHERE id = %d',
$this->username, $this->fullname,
$this->sqlhdlr->real_escape_string($this->email),
$this->adminflag, $this->user_id);
$this->sqlhdlr->query($query) or die( 'Error: ' . $this->sqlhdlr->error );
$sqlhdlr->table('users')
->where('id', $this->user_id)
->update(array(
'username' => $this->username,
'full_name' => $this->fullname,
'email' => $this->email,
'admin' => $this->adminflag
));
//$query = sprintf('UPDATE users SET username = "%s", full_name = ' .
// '"%s", email = "%s", admin = %d WHERE id = %d',
// $this->username, $this->fullname,
// $this->sqlhdlr->real_escape_string($this->email),
// $this->adminflag, $this->user_id);
//
//$this->sqlhdlr->query($query) or die( 'Error: ' . $this->sqlhdlr->error );
if ($this->pwd_dirty_flag) {
$query = sprintf('UPDATE users SET password = SHA1("%s") WHERE ' .
'id = %d', $this->password, $this->user_id);
$this->sqlhdlr->query($query) or die( 'Error: ' . $this->sqlhdlr->error );
$this->_update_password();
}
}
}
......@@ -404,7 +462,7 @@ class skynetUser {
$this->_serialize();
}
$this->sqlhdlr->close();
//$this->sqlhdlr->close();
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment