2016-09-11 01:58:06 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* plugin_database
|
|
|
|
|
*
|
2016-09-11 06:22:37 +02:00
|
|
|
* @method validate uses authentication plugins to check if a user is authorized to login
|
|
|
|
|
* @method get_domain used to get the domain name from the URL or username and then sets both domain_name and domain_uuid
|
2016-09-11 01:58:06 +02:00
|
|
|
*/
|
|
|
|
|
class plugin_database {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Define variables and their scope
|
|
|
|
|
*/
|
|
|
|
|
public $debug;
|
|
|
|
|
public $domain_name;
|
|
|
|
|
public $domain_uuid;
|
|
|
|
|
public $user_uuid;
|
2016-09-11 06:22:37 +02:00
|
|
|
public $contact_uuid;
|
2016-09-11 01:58:06 +02:00
|
|
|
public $username;
|
|
|
|
|
public $password;
|
|
|
|
|
public $key;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* database checks the local database to authenticate the user or key
|
|
|
|
|
* @return array [authorized] => true or false
|
|
|
|
|
*/
|
|
|
|
|
function database() {
|
|
|
|
|
|
2019-09-02 23:57:18 +02:00
|
|
|
//set the default status
|
|
|
|
|
$user_authorized = false;
|
2016-09-11 01:58:06 +02:00
|
|
|
|
|
|
|
|
//check the username and password if they don't match then redirect to the login
|
2020-10-27 02:22:46 +01:00
|
|
|
$sql = "select u.user_uuid, u.contact_uuid, u.username, u.password, u.salt, u.api_key, d.domain_name ";
|
|
|
|
|
$sql .= "from v_users as u, v_domains as d ";
|
|
|
|
|
$sql .= "where u.domain_uuid = d.domain_uuid ";
|
2016-09-11 01:58:06 +02:00
|
|
|
if (strlen($this->key) > 30) {
|
2020-10-27 02:22:46 +01:00
|
|
|
$sql .= "and u.api_key = :api_key ";
|
2019-09-02 23:57:18 +02:00
|
|
|
$parameters['api_key'] = $this->key;
|
2016-09-11 01:58:06 +02:00
|
|
|
}
|
|
|
|
|
else {
|
2020-10-27 02:22:46 +01:00
|
|
|
$sql .= "and lower(u.username) = lower(:username) ";
|
2019-09-02 23:57:18 +02:00
|
|
|
$parameters['username'] = $this->username;
|
2016-09-11 01:58:06 +02:00
|
|
|
}
|
2020-03-06 09:09:12 +01:00
|
|
|
if ($_SESSION["users"]["unique"]["text"] === "global") {
|
2019-09-19 04:09:11 +02:00
|
|
|
//unique username - global (example: email address)
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
//unique username - per domain
|
2020-10-27 02:22:46 +01:00
|
|
|
$sql .= "and u.domain_uuid = :domain_uuid ";
|
2019-09-02 23:57:18 +02:00
|
|
|
$parameters['domain_uuid'] = $this->domain_uuid;
|
2016-09-11 01:58:06 +02:00
|
|
|
}
|
|
|
|
|
$sql .= "and (user_enabled = 'true' or user_enabled is null) ";
|
2019-09-02 23:57:18 +02:00
|
|
|
$database = new database;
|
|
|
|
|
$row = $database->select($sql, $parameters, 'row');
|
2020-03-06 09:09:12 +01:00
|
|
|
if (is_array($row) && @sizeof($row) !== 0) {
|
2016-09-11 20:45:56 +02:00
|
|
|
|
2019-09-02 23:57:18 +02:00
|
|
|
//get the domain uuid when users are unique globally
|
2020-03-06 09:09:12 +01:00
|
|
|
if ($_SESSION["users"]["unique"]["text"] === "global" && $row["domain_uuid"] !== $this->domain_uuid) {
|
2019-09-02 23:57:18 +02:00
|
|
|
//set the domain_uuid
|
|
|
|
|
$this->domain_uuid = $row["domain_uuid"];
|
2020-10-27 02:22:46 +01:00
|
|
|
$this->domain_name = $row["domain_name"];
|
2016-09-11 01:58:06 +02:00
|
|
|
|
2019-09-02 23:57:18 +02:00
|
|
|
//set the domain session variables
|
|
|
|
|
$_SESSION["domain_uuid"] = $this->domain_uuid;
|
|
|
|
|
$_SESSION["domain_name"] = $this->domain_name;
|
2016-09-11 01:58:06 +02:00
|
|
|
|
2019-09-02 23:57:18 +02:00
|
|
|
//set the setting arrays
|
|
|
|
|
$domain = new domains();
|
|
|
|
|
$domain->db = $db;
|
|
|
|
|
$domain->set();
|
|
|
|
|
}
|
2016-09-11 01:58:06 +02:00
|
|
|
|
2019-09-02 23:57:18 +02:00
|
|
|
//set the user_uuid
|
|
|
|
|
$this->user_uuid = $row['user_uuid'];
|
|
|
|
|
$this->contact_uuid = $row['contact_uuid'];
|
2016-09-11 01:58:06 +02:00
|
|
|
|
2020-03-06 09:09:12 +01:00
|
|
|
//validate the password
|
|
|
|
|
$valid_password = false;
|
|
|
|
|
if (isset($this->key) && strlen($this->key) > 30 && $this->key === $row["api_key"]) {
|
|
|
|
|
$valid_password = true;
|
2019-09-02 23:57:18 +02:00
|
|
|
}
|
2020-03-06 09:09:12 +01:00
|
|
|
else if (substr($row["password"], 0, 1) === '$') {
|
|
|
|
|
if (isset($this->password) && strlen($this->password) > 0) {
|
|
|
|
|
if (password_verify($this->password, $row["password"])) {
|
|
|
|
|
$valid_password = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-09-02 23:57:18 +02:00
|
|
|
}
|
2019-09-19 04:09:11 +02:00
|
|
|
else {
|
2020-03-06 09:09:12 +01:00
|
|
|
//deprecated - compare the password provided by the user with the one in the database
|
|
|
|
|
if (md5($row["salt"].$this->password) === $row["password"]) {
|
|
|
|
|
$row["password"] = crypt($this->password, '$1$'.$password_salt.'$');
|
|
|
|
|
$valid_password = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//check to to see if the the password hash needs to be updated
|
|
|
|
|
if ($valid_password) {
|
|
|
|
|
//set the password hash cost
|
|
|
|
|
$options = array('cost' => 10);
|
|
|
|
|
|
|
|
|
|
//check if a newer hashing algorithm is available or the cost has changed
|
|
|
|
|
if (password_needs_rehash($row["password"], PASSWORD_DEFAULT, $options)) {
|
|
|
|
|
|
|
|
|
|
//build user insert array
|
|
|
|
|
$array['users'][0]['user_uuid'] = $this->user_uuid;
|
|
|
|
|
$array['users'][0]['domain_uuid'] = $this->domain_uuid;
|
|
|
|
|
$array['users'][0]['password'] = password_hash($this->password, PASSWORD_DEFAULT, $options);
|
|
|
|
|
$array['users'][0]['salt'] = null;
|
|
|
|
|
|
|
|
|
|
//build user group insert array
|
|
|
|
|
$array['user_groups'][0]['user_group_uuid'] = uuid();
|
|
|
|
|
$array['user_groups'][0]['domain_uuid'] = $this->domain_uuid;
|
|
|
|
|
$array['user_groups'][0]['group_name'] = 'user';
|
|
|
|
|
$array['user_groups'][0]['user_uuid'] = $this->user_uuid;
|
|
|
|
|
|
|
|
|
|
//grant temporary permissions
|
|
|
|
|
$p = new permissions;
|
|
|
|
|
$p->add('user_edit', 'temp');
|
|
|
|
|
|
|
|
|
|
//execute insert
|
|
|
|
|
$database = new database;
|
|
|
|
|
$database->app_name = 'authentication';
|
|
|
|
|
$database->app_uuid = 'a8a12918-69a4-4ece-a1ae-3932be0e41f1';
|
|
|
|
|
$database->save($array);
|
|
|
|
|
unset($array);
|
|
|
|
|
|
|
|
|
|
//revoke temporary permissions
|
|
|
|
|
$p->delete('user_edit', 'temp');
|
|
|
|
|
|
|
|
|
|
}
|
2019-09-19 04:09:11 +02:00
|
|
|
}
|
2016-09-11 01:58:06 +02:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//result array
|
|
|
|
|
$result["plugin"] = "database";
|
|
|
|
|
$result["domain_name"] = $this->domain_name;
|
|
|
|
|
$result["username"] = $this->username;
|
|
|
|
|
if ($this->debug) {
|
|
|
|
|
$result["password"] = $this->password;
|
|
|
|
|
}
|
|
|
|
|
$result["user_uuid"] = $this->user_uuid;
|
|
|
|
|
$result["domain_uuid"] = $this->domain_uuid;
|
2019-02-19 21:12:41 +01:00
|
|
|
$result["contact_uuid"] = $this->contact_uuid;
|
2016-09-11 01:58:06 +02:00
|
|
|
$result["sql"] = $sql;
|
2020-03-06 09:09:12 +01:00
|
|
|
if ($valid_password) {
|
2019-09-19 04:09:11 +02:00
|
|
|
$result["authorized"] = "true";
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
$result["authorized"] = "false";
|
|
|
|
|
}
|
2016-09-11 01:58:06 +02:00
|
|
|
return $result;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-19 04:09:11 +02:00
|
|
|
?>
|