2013-06-28 19:00:30 +02:00
|
|
|
<?php
|
2015-02-04 10:46:36 +01:00
|
|
|
/* Icinga Web 2 | (c) 2013-2015 Icinga Development Team | GPLv2+ */
|
2013-06-28 19:00:30 +02:00
|
|
|
|
2015-04-21 12:51:31 +02:00
|
|
|
namespace Icinga\Authentication\User;
|
2013-06-28 19:00:30 +02:00
|
|
|
|
2015-05-04 12:15:05 +02:00
|
|
|
use Exception;
|
2014-11-04 15:52:09 +01:00
|
|
|
use PDO;
|
2014-06-02 15:52:58 +02:00
|
|
|
use Icinga\Exception\AuthenticationException;
|
2015-05-04 12:15:05 +02:00
|
|
|
use Icinga\Repository\DbRepository;
|
|
|
|
use Icinga\User;
|
2013-06-28 19:00:30 +02:00
|
|
|
|
2015-05-04 12:15:05 +02:00
|
|
|
class DbUserBackend extends DbRepository implements UserBackendInterface
|
2013-08-15 14:16:34 +02:00
|
|
|
{
|
2014-10-30 15:40:07 +01:00
|
|
|
/**
|
|
|
|
* The algorithm to use when hashing passwords
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
const HASH_ALGORITHM = '$1$'; // MD5
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The length of the salt to use when hashing a password
|
|
|
|
*
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
const SALT_LENGTH = 12; // 12 is required by MD5
|
|
|
|
|
2013-08-15 14:58:08 +02:00
|
|
|
/**
|
2015-05-04 12:15:05 +02:00
|
|
|
* The query columns being provided
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $queryColumns = array(
|
|
|
|
'user' => array(
|
2015-05-05 09:34:23 +02:00
|
|
|
'user' => 'name COLLATE utf8_general_ci',
|
2015-05-04 12:15:05 +02:00
|
|
|
'user_name' => 'name',
|
|
|
|
'is_active' => 'active',
|
|
|
|
'created_at' => 'UNIX_TIMESTAMP(ctime)',
|
|
|
|
'last_modified' => 'UNIX_TIMESTAMP(mtime)'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2015-05-05 09:34:23 +02:00
|
|
|
/**
|
|
|
|
* The columns which are not permitted to be queried
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $filterColumns = array('user');
|
|
|
|
|
2015-05-04 12:15:05 +02:00
|
|
|
/**
|
|
|
|
* The default sort rules to be applied on a query
|
2013-11-27 10:58:43 +01:00
|
|
|
*
|
2015-05-04 12:15:05 +02:00
|
|
|
* @var array
|
2013-11-27 10:58:43 +01:00
|
|
|
*/
|
2015-05-04 12:15:05 +02:00
|
|
|
protected $sortRules = array(
|
|
|
|
'user_name' => array(
|
2015-05-04 15:55:36 +02:00
|
|
|
'columns' => array(
|
|
|
|
'user_name asc',
|
|
|
|
'is_active desc'
|
|
|
|
)
|
2015-05-04 12:15:05 +02:00
|
|
|
)
|
|
|
|
);
|
2013-11-27 10:58:43 +01:00
|
|
|
|
2015-05-04 12:15:05 +02:00
|
|
|
/**
|
|
|
|
* Initialize this database user backend
|
|
|
|
*/
|
|
|
|
protected function init()
|
2013-11-27 10:58:43 +01:00
|
|
|
{
|
2015-05-04 12:15:05 +02:00
|
|
|
if (! $this->ds->getTablePrefix()) {
|
|
|
|
$this->ds->setTablePrefix('icingaweb_');
|
|
|
|
}
|
2013-11-27 10:58:43 +01:00
|
|
|
}
|
|
|
|
|
2014-10-08 10:26:12 +02:00
|
|
|
/**
|
|
|
|
* Add a new user
|
|
|
|
*
|
|
|
|
* @param string $username The name of the new user
|
|
|
|
* @param string $password The new user's password
|
|
|
|
* @param bool $active Whether the user is active
|
|
|
|
*/
|
|
|
|
public function addUser($username, $password, $active = true)
|
|
|
|
{
|
2014-11-04 15:52:09 +01:00
|
|
|
$passwordHash = $this->hashPassword($password);
|
|
|
|
|
2015-05-04 12:15:05 +02:00
|
|
|
$stmt = $this->ds->getDbAdapter()->prepare(
|
2014-11-04 12:42:39 +01:00
|
|
|
'INSERT INTO icingaweb_user VALUES (:name, :active, :password_hash, now(), DEFAULT);'
|
2014-10-08 10:26:12 +02:00
|
|
|
);
|
2014-11-04 15:52:09 +01:00
|
|
|
$stmt->bindParam(':name', $username, PDO::PARAM_STR);
|
|
|
|
$stmt->bindParam(':active', $active, PDO::PARAM_INT);
|
|
|
|
$stmt->bindParam(':password_hash', $passwordHash, PDO::PARAM_LOB);
|
|
|
|
$stmt->execute();
|
2014-10-08 10:26:12 +02:00
|
|
|
}
|
|
|
|
|
2013-08-28 10:16:18 +02:00
|
|
|
/**
|
2014-11-04 15:52:09 +01:00
|
|
|
* Fetch the hashed password for the given user
|
2014-10-30 15:40:07 +01:00
|
|
|
*
|
2014-11-04 15:52:09 +01:00
|
|
|
* @param string $username The name of the user
|
2014-10-30 15:40:07 +01:00
|
|
|
*
|
2014-11-04 15:52:09 +01:00
|
|
|
* @return string
|
2014-10-30 15:40:07 +01:00
|
|
|
*/
|
2014-11-04 15:52:09 +01:00
|
|
|
protected function getPasswordHash($username)
|
2014-10-30 15:40:07 +01:00
|
|
|
{
|
2015-05-04 12:15:05 +02:00
|
|
|
if ($this->ds->getDbType() === 'pgsql') {
|
2015-01-19 16:43:19 +01:00
|
|
|
// Since PostgreSQL version 9.0 the default value for bytea_output is 'hex' instead of 'escape'
|
2015-05-04 12:15:05 +02:00
|
|
|
$stmt = $this->ds->getDbAdapter()->prepare(
|
2015-01-19 16:43:19 +01:00
|
|
|
'SELECT ENCODE(password_hash, \'escape\') FROM icingaweb_user WHERE name = :name AND active = 1'
|
|
|
|
);
|
|
|
|
} else {
|
2015-05-04 12:15:05 +02:00
|
|
|
$stmt = $this->ds->getDbAdapter()->prepare(
|
2015-01-19 16:43:19 +01:00
|
|
|
'SELECT password_hash FROM icingaweb_user WHERE name = :name AND active = 1'
|
|
|
|
);
|
|
|
|
}
|
2015-03-06 11:03:45 +01:00
|
|
|
|
2014-11-04 15:52:09 +01:00
|
|
|
$stmt->execute(array(':name' => $username));
|
|
|
|
$stmt->bindColumn(1, $lob, PDO::PARAM_LOB);
|
|
|
|
$stmt->fetch(PDO::FETCH_BOUND);
|
2015-03-06 11:03:45 +01:00
|
|
|
if (is_resource($lob)) {
|
|
|
|
$lob = stream_get_contents($lob);
|
|
|
|
}
|
|
|
|
|
2015-05-04 12:15:05 +02:00
|
|
|
return $this->ds->getDbType() === 'pgsql' ? pg_unescape_bytea($lob) : $lob;
|
2014-10-30 15:40:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-05-04 12:15:05 +02:00
|
|
|
* Authenticate the given user
|
2013-08-28 10:16:18 +02:00
|
|
|
*
|
2014-03-06 14:07:33 +01:00
|
|
|
* @param User $user
|
|
|
|
* @param string $password
|
2013-08-15 14:16:34 +02:00
|
|
|
*
|
2015-05-04 12:15:05 +02:00
|
|
|
* @return bool True on success, false on failure
|
2014-10-30 15:40:07 +01:00
|
|
|
*
|
2015-05-04 12:15:05 +02:00
|
|
|
* @throws AuthenticationException In case authentication is not possible due to an error
|
2013-06-28 19:00:30 +02:00
|
|
|
*/
|
2014-03-03 17:21:17 +01:00
|
|
|
public function authenticate(User $user, $password)
|
2013-07-25 16:47:43 +02:00
|
|
|
{
|
2014-03-28 14:45:03 +01:00
|
|
|
try {
|
2014-11-04 15:52:09 +01:00
|
|
|
$passwordHash = $this->getPasswordHash($user->getUsername());
|
|
|
|
$passwordSalt = $this->getSalt($passwordHash);
|
|
|
|
$hashToCompare = $this->hashPassword($password, $passwordSalt);
|
|
|
|
return $hashToCompare === $passwordHash;
|
2014-03-28 14:45:03 +01:00
|
|
|
} catch (Exception $e) {
|
2014-06-02 15:52:58 +02:00
|
|
|
throw new AuthenticationException(
|
2014-08-22 10:59:52 +02:00
|
|
|
'Failed to authenticate user "%s" against backend "%s". An exception was thrown:',
|
|
|
|
$user->getUsername(),
|
|
|
|
$this->getName(),
|
2014-06-02 15:52:58 +02:00
|
|
|
$e
|
2014-03-28 14:45:03 +01:00
|
|
|
);
|
|
|
|
}
|
2013-06-28 19:00:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-10-30 15:40:07 +01:00
|
|
|
* Extract salt from the given password hash
|
2013-07-25 16:47:43 +02:00
|
|
|
*
|
2014-10-30 15:40:07 +01:00
|
|
|
* @param string $hash The hashed password
|
2013-08-13 18:08:21 +02:00
|
|
|
*
|
2014-10-30 15:40:07 +01:00
|
|
|
* @return string
|
2013-06-28 19:00:30 +02:00
|
|
|
*/
|
2014-10-30 15:40:07 +01:00
|
|
|
protected function getSalt($hash)
|
2013-07-25 16:47:43 +02:00
|
|
|
{
|
2014-11-04 13:03:36 +01:00
|
|
|
return substr($hash, strlen(self::HASH_ALGORITHM), self::SALT_LENGTH);
|
2013-06-28 19:00:30 +02:00
|
|
|
}
|
|
|
|
|
2014-10-08 10:26:12 +02:00
|
|
|
/**
|
|
|
|
* Return a random salt
|
|
|
|
*
|
|
|
|
* The returned salt is safe to be used for hashing a user's password
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function generateSalt()
|
|
|
|
{
|
2014-11-11 10:19:09 +01:00
|
|
|
return openssl_random_pseudo_bytes(self::SALT_LENGTH);
|
2014-10-08 10:26:12 +02:00
|
|
|
}
|
|
|
|
|
2013-11-27 10:58:43 +01:00
|
|
|
/**
|
2014-03-03 17:21:17 +01:00
|
|
|
* Hash a password
|
2013-11-27 10:58:43 +01:00
|
|
|
*
|
2014-10-30 15:40:07 +01:00
|
|
|
* @param string $password
|
|
|
|
* @param string $salt
|
2013-11-27 10:58:43 +01:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2014-10-30 15:40:07 +01:00
|
|
|
protected function hashPassword($password, $salt = null)
|
|
|
|
{
|
|
|
|
return crypt($password, self::HASH_ALGORITHM . ($salt !== null ? $salt : $this->generateSalt()));
|
2013-11-27 10:58:43 +01:00
|
|
|
}
|
2014-08-22 10:59:52 +02:00
|
|
|
}
|