$dbdata = json_decode(file_get_contents('/etc/kairo/auth_db.json'), true);
if (!is_array($dbdata)) { trigger_error('DB configuration not found', E_USER_ERROR); }
-$pwd_options = array('cost' => 10);
-
// Extended DOM document class
require_once('../kairo/include/cbsm/util/document.php-class');
// Class for sending emails
// KaiRo.at authentication utilities PHP class
// This class contains helper functions for the authentication system.
//
+ // private static $pwd_cost
+ // Store cost parameter for use with PHP password_hash function.
+ //
// static function checkPasswordConstraints($new_password, $user_email)
// Check password constraints and return an array of error messages (empty if all constraints are met).
//
// static function verifyTimeCode($timecode_to_verify, $session, [$validity_minutes])
// Verify a given time-based code and return true if it's valid or false if it's not.
// See createTimeCode() documentation for the session and validity paramerters.
+ //
+ // static function pwdHash($new_password)
+ // Return a hash for the given password.
+ //
+ // static function pwdVerify($password_to_verify, $user)
+ // Return true if the password verifies against the pwdhash field of the user, false if not.
+ //
+ // static function pwdNeedsRehash($user)
+ // Return true if the pwdhash field of the user uses an outdated standard and needs to be rehashed.
+
+ private static $pwd_cost = 10;
static function checkPasswordConstraints($new_password, $user_email) {
$errors = array();
static function verifyTimeCode($timecode_to_verify, $session, $validity_minutes = 10) {
if (preg_match('/^(\d+)\.\d+$/', $timecode_to_verify, $regs)) {
- return ($timecode_to_verify === AuthUtils::createTimeCode($session, $regs[1], $validity_minutes));
+ return ($timecode_to_verify === self::createTimeCode($session, $regs[1], $validity_minutes));
}
return false;
}
+
+ static function pwdHash($new_password) {
+ return password_hash($new_password, PASSWORD_DEFAULT, array('cost' => self::$pwd_cost));
+ }
+
+ static function pwdVerify($password_to_verify, $userdata) {
+ return password_verify($password_to_verify, $userdata['pwdhash']));
+ }
+
+ static function pwdNeedsRehash($userdata) {
+ return password_needs_rehash($userdata['pwdhash'], PASSWORD_DEFAULT, array('cost' => self::$pwd_cost));
+ }
}
?>
$user = $result->fetch(PDO::FETCH_ASSOC);
if ($user['id'] && array_key_exists('pwd', $_POST)) {
// existing user, check password
- if (($user['status'] == 'ok') && password_verify(@$_POST['pwd'], $user['pwdhash'])) {
+ if (($user['status'] == 'ok') && AuthUtils::pwdVerify(@$_POST['pwd'], $user)) {
// Check if a newer hashing algorithm is available
// or the cost has changed
- if (password_needs_rehash($user['pwdhash'], PASSWORD_DEFAULT, $pwd_options)) {
+ if (AuthUtils::pwdNeedsRehash($user)) {
// If so, create a new hash, and replace the old one
- $newHash = password_hash($_POST['pwd'], PASSWORD_DEFAULT, $pwd_options);
+ $newHash = AuthUtils::pwdHash($_POST['pwd']);
$result = $db->prepare('UPDATE `auth_users` SET `pwdhash` = :pwdhash WHERE `id` = :userid;');
if (!$result->execute(array(':pwdhash' => $newHash, ':userid' => $user['id']))) {
// XXXlog: Failed to update user hash!
if (!count($errors)) {
// Put user into the DB
if (!$user['id']) {
- $newHash = password_hash($_POST['pwd'], PASSWORD_DEFAULT, $pwd_options);
+ $newHash = AuthUtils::pwdHash($_POST['pwd']);
$vcode = AuthUtils::createVerificationCode();
$result = $db->prepare('INSERT INTO `auth_users` (`email`, `pwdhash`, `status`, `verify_hash`) VALUES (:email, :pwdhash, \'unverified\', :vcode);');
if (!$result->execute(array(':email' => $_POST['email'], ':pwdhash' => $newHash, ':vcode' => $vcode))) {
}
$errors += AuthUtils::checkPasswordConstraints(strval($_POST['pwd']), $user['email']);
if (!count($errors)) {
- $newHash = password_hash($_POST['pwd'], PASSWORD_DEFAULT, $pwd_options);
+ $newHash = AuthUtils::pwdHash($_POST['pwd']);
$result = $db->prepare('UPDATE `auth_users` SET `pwdhash` = :pwdhash, `verify_hash` = \'\' WHERE `id` = :userid;');
if (!$result->execute(array(':pwdhash' => $newHash, ':userid' => $session['user']))) {
// XXXlog: Password reset failure!