2013-06-28 19:00:30 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
|
|
|
/**
|
|
|
|
* This file is part of Icinga 2 Web.
|
|
|
|
*
|
|
|
|
* Icinga 2 Web - Head for multiple monitoring backends.
|
|
|
|
* Copyright (C) 2013 Icinga Development Team
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*
|
|
|
|
* @copyright 2013 Icinga Development Team <info@icinga.org>
|
|
|
|
* @license http://www.gnu.org/licenses/gpl-2.0.txt GPL, version 2
|
|
|
|
* @author Icinga Development Team <info@icinga.org>
|
|
|
|
*/
|
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
|
|
|
|
|
|
|
namespace Icinga\Authentication\Backend;
|
|
|
|
|
2013-08-23 15:04:00 +02:00
|
|
|
use Zend_Db;
|
2013-08-15 14:16:34 +02:00
|
|
|
use \Icinga\User;
|
|
|
|
use \Icinga\Authentication\UserBackend;
|
|
|
|
use \Icinga\Authentication\Credentials;
|
|
|
|
use \Icinga\Authentication;
|
|
|
|
use \Icinga\Application\Logger;
|
2013-06-28 19:00:30 +02:00
|
|
|
|
|
|
|
/**
|
2013-07-29 11:42:31 +02:00
|
|
|
* User authentication backend (@see Icinga\Authentication\UserBackend) for
|
|
|
|
* authentication of users via an SQL database. The credentials needed to access
|
|
|
|
* the database are configurable via the application.ini
|
|
|
|
*
|
|
|
|
* See the UserBackend class (@see Icinga\Authentication\UserBackend) for
|
|
|
|
* usage information
|
2013-06-28 19:00:30 +02:00
|
|
|
*/
|
2013-08-15 14:16:34 +02:00
|
|
|
class DbUserBackend implements UserBackend
|
|
|
|
{
|
2013-08-15 14:58:08 +02:00
|
|
|
/**
|
|
|
|
* Mapping of all table column names
|
|
|
|
*/
|
|
|
|
|
2013-08-23 15:04:00 +02:00
|
|
|
const USER_NAME_COLUMN = 'username';
|
2013-08-15 14:58:08 +02:00
|
|
|
|
|
|
|
const SALT_COLUMN = 'salt';
|
|
|
|
|
|
|
|
const PASSWORD_COLUMN = 'password';
|
|
|
|
|
|
|
|
const ACTIVE_COLUMN = 'active';
|
|
|
|
|
2013-07-26 15:01:52 +02:00
|
|
|
/**
|
|
|
|
* The database connection that will be used for fetching users
|
|
|
|
*
|
|
|
|
* @var \Zend_Db
|
|
|
|
*/
|
|
|
|
private $db = null;
|
2013-06-28 19:00:30 +02:00
|
|
|
|
2013-07-26 15:01:52 +02:00
|
|
|
/**
|
2013-08-13 18:08:21 +02:00
|
|
|
* The name of the user table
|
2013-07-26 15:01:52 +02:00
|
|
|
*
|
|
|
|
* @var String
|
|
|
|
*/
|
2013-08-13 18:08:21 +02:00
|
|
|
private $userTable = "account";
|
2013-08-23 15:04:00 +02:00
|
|
|
|
2013-06-28 19:00:30 +02:00
|
|
|
/**
|
2013-07-26 15:57:37 +02:00
|
|
|
* Create a DbUserBackend
|
2013-07-25 16:47:43 +02:00
|
|
|
*
|
2013-08-13 18:08:21 +02:00
|
|
|
* @param Zend_Db The database that provides the authentication data
|
2013-06-28 19:00:30 +02:00
|
|
|
*/
|
2013-08-13 18:08:21 +02:00
|
|
|
public function __construct($database)
|
2013-07-25 16:47:43 +02:00
|
|
|
{
|
2013-08-13 18:08:21 +02:00
|
|
|
$this->db = $database;
|
2013-07-26 15:01:52 +02:00
|
|
|
|
2013-08-13 18:08:21 +02:00
|
|
|
/*
|
|
|
|
* Test if the connection is available
|
|
|
|
*/
|
|
|
|
$this->db->getConnection();
|
2013-06-28 19:00:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-07-26 15:57:37 +02:00
|
|
|
* Check if the user identified by the given credentials is available
|
2013-07-25 16:47:43 +02:00
|
|
|
*
|
2013-08-23 15:04:00 +02:00
|
|
|
* @param Credentials $credentials The login credentials
|
2013-08-15 14:16:34 +02:00
|
|
|
*
|
2013-08-23 15:04:00 +02:00
|
|
|
* @return boolean True when the username is known and currently active.
|
2013-06-28 19:00:30 +02:00
|
|
|
*/
|
|
|
|
public function hasUsername(Credentials $credential)
|
|
|
|
{
|
2013-07-26 15:01:52 +02:00
|
|
|
if ($this->db === null) {
|
|
|
|
Logger::warn('Ignoring hasUsername in database as no connection is available');
|
|
|
|
return false;
|
|
|
|
}
|
2013-06-28 19:00:30 +02:00
|
|
|
$user = $this->getUserByName($credential->getUsername());
|
2013-08-23 15:04:00 +02:00
|
|
|
return isset($user);
|
2013-06-28 19:00:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-07-25 16:47:43 +02:00
|
|
|
* Authenticate a user with the given credentials
|
|
|
|
*
|
2013-08-23 15:04:00 +02:00
|
|
|
* @param Credentials $credentials The login credentials
|
2013-08-15 14:16:34 +02:00
|
|
|
*
|
2013-08-23 15:04:00 +02:00
|
|
|
* @return User|null The authenticated user or Null.
|
2013-06-28 19:00:30 +02:00
|
|
|
*/
|
2013-07-25 16:47:43 +02:00
|
|
|
public function authenticate(Credentials $credential)
|
|
|
|
{
|
2013-07-26 15:01:52 +02:00
|
|
|
if ($this->db === null) {
|
|
|
|
Logger::warn('Ignoring database authentication as no connection is available');
|
|
|
|
return null;
|
|
|
|
}
|
2013-06-28 19:00:30 +02:00
|
|
|
$this->db->getConnection();
|
2013-08-23 15:04:00 +02:00
|
|
|
try {
|
|
|
|
$salt = $this->getUserSalt($credential->getUsername());
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
Logger::error($e->getMessage());
|
|
|
|
return null;
|
|
|
|
}
|
2013-06-28 19:00:30 +02:00
|
|
|
$res = $this->db
|
|
|
|
->select()->from($this->userTable)
|
2013-08-15 14:58:08 +02:00
|
|
|
->where(self::USER_NAME_COLUMN.' = ?', $credential->getUsername())
|
|
|
|
->where(self::ACTIVE_COLUMN. ' = ?', true)
|
2013-08-15 14:16:34 +02:00
|
|
|
->where(
|
2013-08-15 14:58:08 +02:00
|
|
|
self::PASSWORD_COLUMN. ' = ?',
|
|
|
|
hash_hmac(
|
|
|
|
'sha256',
|
2013-08-23 15:04:00 +02:00
|
|
|
$salt,
|
2013-08-15 14:58:08 +02:00
|
|
|
$credential->getPassword()
|
|
|
|
)
|
2013-08-15 14:16:34 +02:00
|
|
|
)
|
2013-06-28 19:00:30 +02:00
|
|
|
->query()->fetch();
|
2013-08-23 15:04:00 +02:00
|
|
|
if ($res !== false) {
|
2013-06-28 19:00:30 +02:00
|
|
|
return $this->createUserFromResult($res);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-07-26 15:57:37 +02:00
|
|
|
* Fetch the users salt from the database
|
2013-07-25 16:47:43 +02:00
|
|
|
*
|
2013-08-23 15:04:00 +02:00
|
|
|
* @param $username The user whose salt should be fetched.
|
2013-08-13 18:08:21 +02:00
|
|
|
*
|
2013-08-23 15:04:00 +02:00
|
|
|
* @return String|null Returns the salt-string or Null, when the user does not exist.
|
2013-06-28 19:00:30 +02:00
|
|
|
*/
|
2013-07-25 16:47:43 +02:00
|
|
|
private function getUserSalt($username)
|
|
|
|
{
|
2013-06-28 19:00:30 +02:00
|
|
|
$this->db->getConnection();
|
|
|
|
$res = $this->db->select()
|
2013-08-15 14:58:08 +02:00
|
|
|
->from($this->userTable, self::SALT_COLUMN)
|
|
|
|
->where(self::USER_NAME_COLUMN.' = ?', $username)
|
2013-06-28 19:00:30 +02:00
|
|
|
->query()->fetch();
|
2013-08-23 15:04:00 +02:00
|
|
|
if ($res !== false) {
|
|
|
|
return $res->{self::SALT_COLUMN};
|
|
|
|
} else {
|
|
|
|
throw new \Exception('No Salt found for user "' . $username . '"');
|
|
|
|
}
|
2013-06-28 19:00:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-07-26 12:58:21 +02:00
|
|
|
* Fetch the user information from the database
|
2013-07-25 16:47:43 +02:00
|
|
|
*
|
2013-08-23 15:04:00 +02:00
|
|
|
* @param $username The name of the user.
|
2013-08-13 18:08:21 +02:00
|
|
|
*
|
2013-08-23 15:04:00 +02:00
|
|
|
* @return User|null Returns the user object, or null when the user does not exist.
|
2013-06-28 19:00:30 +02:00
|
|
|
*/
|
2013-07-25 16:47:43 +02:00
|
|
|
private function getUserByName($username)
|
|
|
|
{
|
2013-07-26 15:01:52 +02:00
|
|
|
if ($this->db === null) {
|
|
|
|
Logger::warn('Ignoring getUserByName as no database connection is available');
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
$this->db->getConnection();
|
|
|
|
$res = $this->db->
|
|
|
|
select()->from($this->userTable)
|
2013-08-15 14:58:08 +02:00
|
|
|
->where(self::USER_NAME_COLUMN.' = ?', $username)
|
|
|
|
->where(self::ACTIVE_COLUMN.' = ?', true)
|
2013-07-26 15:01:52 +02:00
|
|
|
->query()->fetch();
|
2013-08-23 15:04:00 +02:00
|
|
|
if ($res !== false) {
|
|
|
|
return $this->createUserFromResult($res);
|
2013-07-26 15:01:52 +02:00
|
|
|
}
|
2013-08-23 15:04:00 +02:00
|
|
|
return null;
|
2013-07-26 15:01:52 +02:00
|
|
|
} catch (\Zend_Db_Statement_Exception $exc) {
|
|
|
|
Logger::error("Could not fetch users from db : %s ", $exc->getMessage());
|
2013-06-28 19:00:30 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-07-26 15:57:37 +02:00
|
|
|
* Create a new instance of User from a query result
|
2013-07-25 16:47:43 +02:00
|
|
|
*
|
2013-08-23 15:04:00 +02:00
|
|
|
* @param $result The query result containing the user row
|
2013-08-15 14:16:34 +02:00
|
|
|
*
|
2013-08-23 15:04:00 +02:00
|
|
|
* @return User The created instance of User.
|
2013-06-28 19:00:30 +02:00
|
|
|
*/
|
2013-08-23 15:04:00 +02:00
|
|
|
private function createUserFromResult($resultRow)
|
2013-07-25 16:47:43 +02:00
|
|
|
{
|
2013-06-28 19:00:30 +02:00
|
|
|
$usr = new User(
|
2013-08-23 15:04:00 +02:00
|
|
|
$resultRow->{self::USER_NAME_COLUMN}
|
2013-08-15 14:16:34 +02:00
|
|
|
);
|
2013-06-28 19:00:30 +02:00
|
|
|
return $usr;
|
|
|
|
}
|
2013-07-26 15:57:37 +02:00
|
|
|
}
|