2013-06-28 13:00:30 -04:00
|
|
|
<?php
|
2016-02-08 09:41:00 -05:00
|
|
|
/* Icinga Web 2 | (c) 2013 Icinga Development Team | GPLv2+ */
|
2013-06-28 13:00:30 -04:00
|
|
|
|
2015-04-21 06:51:31 -04:00
|
|
|
namespace Icinga\Authentication\User;
|
2013-06-28 13:00:30 -04:00
|
|
|
|
2015-05-04 06:15:05 -04:00
|
|
|
use Exception;
|
2015-07-16 10:16:55 -04:00
|
|
|
use Icinga\Data\Inspectable;
|
|
|
|
|
use Icinga\Data\Inspection;
|
2015-05-11 10:00:24 -04:00
|
|
|
use Icinga\Data\Filter\Filter;
|
2014-06-02 09:52:58 -04:00
|
|
|
use Icinga\Exception\AuthenticationException;
|
2015-05-04 06:15:05 -04:00
|
|
|
use Icinga\Repository\DbRepository;
|
|
|
|
|
use Icinga\User;
|
2017-11-20 14:19:28 -05:00
|
|
|
use PDO;
|
2013-06-28 13:00:30 -04:00
|
|
|
|
2015-07-16 10:16:55 -04:00
|
|
|
class DbUserBackend extends DbRepository implements UserBackendInterface, Inspectable
|
2013-08-15 08:16:34 -04:00
|
|
|
{
|
2013-08-15 08:58:08 -04:00
|
|
|
/**
|
2015-05-04 06:15:05 -04:00
|
|
|
* The query columns being provided
|
|
|
|
|
*
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
|
|
|
|
protected $queryColumns = array(
|
|
|
|
|
'user' => array(
|
2015-05-05 03:34:23 -04:00
|
|
|
'user' => 'name COLLATE utf8_general_ci',
|
2015-05-04 06:15:05 -04:00
|
|
|
'user_name' => 'name',
|
|
|
|
|
'is_active' => 'active',
|
|
|
|
|
'created_at' => 'UNIX_TIMESTAMP(ctime)',
|
|
|
|
|
'last_modified' => 'UNIX_TIMESTAMP(mtime)'
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
|
2015-05-11 10:00:24 -04:00
|
|
|
/**
|
|
|
|
|
* The statement columns being provided
|
|
|
|
|
*
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
|
|
|
|
protected $statementColumns = array(
|
|
|
|
|
'user' => array(
|
|
|
|
|
'password' => 'password_hash',
|
|
|
|
|
'created_at' => 'ctime',
|
|
|
|
|
'last_modified' => 'mtime'
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
|
2015-05-05 03:34:23 -04:00
|
|
|
/**
|
|
|
|
|
* The columns which are not permitted to be queried
|
|
|
|
|
*
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
2015-08-13 08:06:27 -04:00
|
|
|
protected $blacklistedQueryColumns = array('user');
|
2015-05-05 03:34:23 -04:00
|
|
|
|
2015-08-13 11:05:13 -04:00
|
|
|
/**
|
|
|
|
|
* The search columns being provided
|
|
|
|
|
*
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
|
|
|
|
protected $searchColumns = array('user');
|
|
|
|
|
|
2015-05-04 06:15:05 -04:00
|
|
|
/**
|
|
|
|
|
* The default sort rules to be applied on a query
|
2013-11-27 04:58:43 -05:00
|
|
|
*
|
2015-05-04 06:15:05 -04:00
|
|
|
* @var array
|
2013-11-27 04:58:43 -05:00
|
|
|
*/
|
2015-05-04 06:15:05 -04:00
|
|
|
protected $sortRules = array(
|
|
|
|
|
'user_name' => array(
|
2015-05-04 09:55:36 -04:00
|
|
|
'columns' => array(
|
2015-05-21 07:53:18 -04:00
|
|
|
'is_active desc',
|
|
|
|
|
'user_name'
|
2015-05-04 09:55:36 -04:00
|
|
|
)
|
2015-05-04 06:15:05 -04:00
|
|
|
)
|
|
|
|
|
);
|
2013-11-27 04:58:43 -05:00
|
|
|
|
2015-05-11 10:00:24 -04:00
|
|
|
/**
|
2015-05-29 05:32:15 -04:00
|
|
|
* The value conversion rules to apply on a query or statement
|
2015-05-11 10:00:24 -04:00
|
|
|
*
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
2015-05-29 05:32:15 -04:00
|
|
|
protected $conversionRules = array(
|
|
|
|
|
'user' => array(
|
|
|
|
|
'password'
|
|
|
|
|
)
|
|
|
|
|
);
|
2015-05-11 10:00:24 -04:00
|
|
|
|
2015-05-04 06:15:05 -04:00
|
|
|
/**
|
|
|
|
|
* Initialize this database user backend
|
|
|
|
|
*/
|
|
|
|
|
protected function init()
|
2013-11-27 04:58:43 -05:00
|
|
|
{
|
2015-05-04 06:15:05 -04:00
|
|
|
if (! $this->ds->getTablePrefix()) {
|
|
|
|
|
$this->ds->setTablePrefix('icingaweb_');
|
|
|
|
|
}
|
2013-11-27 04:58:43 -05:00
|
|
|
}
|
|
|
|
|
|
2015-08-13 11:05:13 -04:00
|
|
|
/**
|
|
|
|
|
* Initialize this repository's filter columns
|
|
|
|
|
*
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
protected function initializeFilterColumns()
|
|
|
|
|
{
|
|
|
|
|
$userLabel = t('Username') . ' ' . t('(Case insensitive)');
|
|
|
|
|
return array(
|
|
|
|
|
$userLabel => 'user',
|
|
|
|
|
t('Username') => 'user_name',
|
|
|
|
|
t('Active') => 'is_active',
|
2016-12-13 07:46:01 -05:00
|
|
|
t('Created at') => 'created_at',
|
|
|
|
|
t('Last modified') => 'last_modified'
|
2015-08-13 11:05:13 -04:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2014-10-08 04:26:12 -04:00
|
|
|
/**
|
2015-05-11 10:00:24 -04:00
|
|
|
* Insert a table row with the given data
|
|
|
|
|
*
|
2017-11-20 14:19:28 -05:00
|
|
|
* @param string $table
|
|
|
|
|
* @param array $bind
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
2015-05-11 10:00:24 -04:00
|
|
|
*/
|
2017-12-14 11:23:37 -05:00
|
|
|
public function insert($table, array $bind, array $types = array())
|
2015-05-11 10:00:24 -04:00
|
|
|
{
|
2015-10-16 06:36:47 -04:00
|
|
|
$this->requireTable($table);
|
2015-05-13 03:15:18 -04:00
|
|
|
$bind['created_at'] = date('Y-m-d H:i:s');
|
|
|
|
|
$this->ds->insert(
|
|
|
|
|
$this->prependTablePrefix($table),
|
|
|
|
|
$this->requireStatementColumns($table, $bind),
|
|
|
|
|
array(
|
|
|
|
|
'active' => PDO::PARAM_INT,
|
|
|
|
|
'password_hash' => PDO::PARAM_LOB
|
|
|
|
|
)
|
|
|
|
|
);
|
2015-05-11 10:00:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Update table rows with the given data, optionally limited by using a filter
|
2014-10-08 04:26:12 -04:00
|
|
|
*
|
2015-05-11 10:00:24 -04:00
|
|
|
* @param string $table
|
2015-05-13 03:15:18 -04:00
|
|
|
* @param array $bind
|
2015-05-11 10:00:24 -04:00
|
|
|
* @param Filter $filter
|
|
|
|
|
*/
|
2017-12-14 11:23:37 -05:00
|
|
|
public function update($table, array $bind, Filter $filter = null, array $types = array())
|
2015-05-11 10:00:24 -04:00
|
|
|
{
|
2015-10-16 06:36:47 -04:00
|
|
|
$this->requireTable($table);
|
2015-05-13 03:15:18 -04:00
|
|
|
$bind['last_modified'] = date('Y-m-d H:i:s');
|
2015-05-11 10:00:24 -04:00
|
|
|
if ($filter) {
|
2015-06-01 09:03:08 -04:00
|
|
|
$filter = $this->requireFilter($table, $filter);
|
2015-05-11 10:00:24 -04:00
|
|
|
}
|
|
|
|
|
|
2015-05-13 03:15:18 -04:00
|
|
|
$this->ds->update(
|
|
|
|
|
$this->prependTablePrefix($table),
|
|
|
|
|
$this->requireStatementColumns($table, $bind),
|
|
|
|
|
$filter,
|
|
|
|
|
array(
|
|
|
|
|
'active' => PDO::PARAM_INT,
|
|
|
|
|
'password_hash' => PDO::PARAM_LOB
|
|
|
|
|
)
|
|
|
|
|
);
|
2015-05-11 10:00:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Hash and return the given password
|
|
|
|
|
*
|
|
|
|
|
* @param string $value
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
2014-10-08 04:26:12 -04:00
|
|
|
*/
|
2015-05-11 10:00:24 -04:00
|
|
|
protected function persistPassword($value)
|
2014-10-08 04:26:12 -04:00
|
|
|
{
|
2018-05-08 03:06:19 -04:00
|
|
|
return password_hash($value, PASSWORD_DEFAULT);
|
2014-10-08 04:26:12 -04:00
|
|
|
}
|
|
|
|
|
|
2013-08-28 04:16:18 -04:00
|
|
|
/**
|
2014-11-04 09:52:09 -05:00
|
|
|
* Fetch the hashed password for the given user
|
2014-10-30 10:40:07 -04:00
|
|
|
*
|
2014-11-04 09:52:09 -05:00
|
|
|
* @param string $username The name of the user
|
2014-10-30 10:40:07 -04:00
|
|
|
*
|
2014-11-04 09:52:09 -05:00
|
|
|
* @return string
|
2014-10-30 10:40:07 -04:00
|
|
|
*/
|
2014-11-04 09:52:09 -05:00
|
|
|
protected function getPasswordHash($username)
|
2014-10-30 10:40:07 -04:00
|
|
|
{
|
2015-05-04 06:15:05 -04:00
|
|
|
if ($this->ds->getDbType() === 'pgsql') {
|
2015-01-19 10:43:19 -05:00
|
|
|
// Since PostgreSQL version 9.0 the default value for bytea_output is 'hex' instead of 'escape'
|
2015-05-13 03:16:24 -04:00
|
|
|
$columns = array('password_hash' => 'ENCODE(password_hash, \'escape\')');
|
2015-01-19 10:43:19 -05:00
|
|
|
} else {
|
2015-05-13 03:16:24 -04:00
|
|
|
$columns = array('password_hash');
|
2015-01-19 10:43:19 -05:00
|
|
|
}
|
2015-03-06 05:03:45 -05:00
|
|
|
|
2020-06-19 03:23:06 -04:00
|
|
|
$nameColumn = 'name';
|
|
|
|
|
if ($this->ds->getDbType() === 'mysql') {
|
|
|
|
|
$username = strtolower($username);
|
|
|
|
|
$nameColumn = 'BINARY LOWER(name)';
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-13 03:16:24 -04:00
|
|
|
$query = $this->ds->select()
|
|
|
|
|
->from($this->prependTablePrefix('user'), $columns)
|
2020-06-19 03:23:06 -04:00
|
|
|
->where($nameColumn, $username)
|
2015-05-13 03:16:24 -04:00
|
|
|
->where('active', true);
|
2019-12-11 04:15:05 -05:00
|
|
|
|
2015-05-13 03:16:24 -04:00
|
|
|
$statement = $this->ds->getDbAdapter()->prepare($query->getSelectQuery());
|
|
|
|
|
$statement->execute();
|
|
|
|
|
$statement->bindColumn(1, $lob, PDO::PARAM_LOB);
|
|
|
|
|
$statement->fetch(PDO::FETCH_BOUND);
|
2015-03-06 05:03:45 -05:00
|
|
|
if (is_resource($lob)) {
|
|
|
|
|
$lob = stream_get_contents($lob);
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-04 06:15:05 -04:00
|
|
|
return $this->ds->getDbType() === 'pgsql' ? pg_unescape_bytea($lob) : $lob;
|
2014-10-30 10:40:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2015-05-04 06:15:05 -04:00
|
|
|
* Authenticate the given user
|
2013-08-28 04:16:18 -04:00
|
|
|
*
|
2014-03-06 08:07:33 -05:00
|
|
|
* @param User $user
|
|
|
|
|
* @param string $password
|
2013-08-15 08:16:34 -04:00
|
|
|
*
|
2015-05-04 06:15:05 -04:00
|
|
|
* @return bool True on success, false on failure
|
2014-10-30 10:40:07 -04:00
|
|
|
*
|
2015-05-04 06:15:05 -04:00
|
|
|
* @throws AuthenticationException In case authentication is not possible due to an error
|
2013-06-28 13:00:30 -04:00
|
|
|
*/
|
2014-03-03 11:21:17 -05:00
|
|
|
public function authenticate(User $user, $password)
|
2013-07-25 10:47:43 -04:00
|
|
|
{
|
2014-03-28 09:45:03 -04:00
|
|
|
try {
|
2018-05-08 03:06:19 -04:00
|
|
|
return password_verify(
|
2017-11-20 14:19:28 -05:00
|
|
|
$password,
|
|
|
|
|
$this->getPasswordHash($user->getUsername())
|
|
|
|
|
);
|
2014-03-28 09:45:03 -04:00
|
|
|
} catch (Exception $e) {
|
2014-06-02 09:52:58 -04:00
|
|
|
throw new AuthenticationException(
|
2014-08-22 04:59:52 -04:00
|
|
|
'Failed to authenticate user "%s" against backend "%s". An exception was thrown:',
|
|
|
|
|
$user->getUsername(),
|
|
|
|
|
$this->getName(),
|
2014-06-02 09:52:58 -04:00
|
|
|
$e
|
2014-03-28 09:45:03 -04:00
|
|
|
);
|
|
|
|
|
}
|
2013-06-28 13:00:30 -04:00
|
|
|
}
|
|
|
|
|
|
2015-07-16 10:16:55 -04:00
|
|
|
/**
|
|
|
|
|
* Inspect this object to gain extended information about its health
|
|
|
|
|
*
|
|
|
|
|
* @return Inspection The inspection result
|
|
|
|
|
*/
|
|
|
|
|
public function inspect()
|
|
|
|
|
{
|
|
|
|
|
$insp = new Inspection('Db User Backend');
|
|
|
|
|
$insp->write($this->ds->inspect());
|
|
|
|
|
try {
|
2017-02-24 09:29:05 -05:00
|
|
|
$insp->write(sprintf('%s active users', $this->select()->where('is_active', true)->count()));
|
2015-07-16 10:16:55 -04:00
|
|
|
} catch (Exception $e) {
|
|
|
|
|
$insp->error(sprintf('Query failed: %s', $e->getMessage()));
|
|
|
|
|
}
|
|
|
|
|
return $insp;
|
|
|
|
|
}
|
2014-08-22 04:59:52 -04:00
|
|
|
}
|