icingaweb2/library/Icinga/Authentication/Backend/DbUserBackend.php

181 lines
4.8 KiB
PHP
Raw Normal View History

<?php
// {{{ICINGA_LICENSE_HEADER}}}
// {{{ICINGA_LICENSE_HEADER}}}
namespace Icinga\Authentication\Backend;
use Icinga\Authentication\UserBackend;
use Icinga\Data\Db\DbConnection;
use Icinga\User;
use Icinga\Exception\AuthenticationException;
use Exception;
use Zend_Db_Expr;
use Zend_Db_Select;
use Icinga\Exception\IcingaException;
class DbUserBackend extends UserBackend
{
/**
* Connection to the database
*
* @var DbConnection
*/
2014-10-08 10:26:12 +02:00
protected $conn;
public function __construct(DbConnection $conn)
{
$this->conn = $conn;
}
/**
* Test whether the given user exists
*
* @param User $user
*
* @return bool
*/
public function hasUser(User $user)
{
2014-03-06 14:07:33 +01:00
$select = new Zend_Db_Select($this->conn->getConnection());
$row = $select->from('account', array(new Zend_Db_Expr(1)))
->where('username = ?', $user->getUsername())
2014-03-06 14:07:33 +01:00
->query()->fetchObject();
return ($row !== false) ? true : false;
}
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)
{
$passwordSalt = $this->generateSalt();
$hashedPassword = $this->hashPassword($password, $passwordSalt);
$stmt = $this->conn->getDbAdapter()->prepare(
'INSERT INTO account VALUES (:username, :salt, :password, :active);'
);
$stmt->execute(array(
':active' => $active,
':username' => $username,
':salt' => $passwordSalt,
':password' => $hashedPassword
));
}
/**
2014-03-28 14:45:03 +01:00
* Authenticate the given user and return true on success, false on failure and null on error
*
2014-03-06 14:07:33 +01:00
* @param User $user
* @param string $password
*
2014-03-28 14:45:03 +01:00
* @return bool|null
* @throws AuthenticationException
*/
public function authenticate(User $user, $password)
{
2014-03-28 14:45:03 +01:00
try {
$salt = $this->getSalt($user->getUsername());
if ($salt === null) {
return false;
}
if ($salt === '') {
throw new IcingaException(
'Cannot find salt for user %s',
$user->getUsername()
);
2014-03-28 14:45:03 +01:00
}
2014-03-06 14:07:33 +01:00
2014-03-28 14:45:03 +01:00
$select = new Zend_Db_Select($this->conn->getConnection());
$row = $select->from('account', array(new Zend_Db_Expr(1)))
->where('username = ?', $user->getUsername())
->where('active = ?', true)
->where('password = ?', $this->hashPassword($password, $salt))
->query()->fetchObject();
2014-03-06 14:07:33 +01:00
2014-03-28 14:45:03 +01:00
return ($row !== false) ? true : false;
} catch (Exception $e) {
throw new AuthenticationException(
'Failed to authenticate user "%s" against backend "%s". An exception was thrown:',
$user->getUsername(),
$this->getName(),
$e
2014-03-28 14:45:03 +01:00
);
}
}
/**
* Get salt by username
*
* @param string $username
*
* @return string|null
*/
2014-10-08 10:26:12 +02:00
protected function getSalt($username)
{
2014-03-06 14:07:33 +01:00
$select = new Zend_Db_Select($this->conn->getConnection());
$row = $select->from('account', array('salt'))->where('username = ?', $username)->query()->fetchObject();
return ($row !== false) ? $row->salt : null;
}
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()
{
return bin2hex(openssl_random_pseudo_bytes(32));
2014-10-08 10:26:12 +02:00
}
/**
* Hash a password
*
* @param string $password
* @param string $salt
*
* @return string
*/
2014-10-08 10:26:12 +02:00
protected function hashPassword($password, $salt) {
return hash_hmac('sha256', $password, $salt);
}
/**
* Get the number of users available
*
* @return int
*/
public function count()
{
2014-03-06 14:07:33 +01:00
$select = new Zend_Db_Select($this->conn->getConnection());
$row = $select->from(
'account',
array('count' => 'COUNT(*)')
)->query()->fetchObject();
return ($row !== false) ? $row->count : 0;
}
/**
* Return the names of all available users
*
* @return array
*/
public function listUsers()
{
$query = $this->conn->select()->from('account', array('username'));
$users = array();
foreach ($query->fetchAll() as $row) {
$users[] = $row->username;
}
return $users;
}
}