From 9ea26dfc3c7c7e7144a90f858b2b7f22cb44add8 Mon Sep 17 00:00:00 2001 From: Robert Kaiser Date: Thu, 15 Dec 2016 16:50:25 +0100 Subject: [PATCH] merge DB config into normal settings - this is another part of bug 415 --- app/authsystem.inc.php | 12 ++++-------- app/authutils.php-class | 24 ++++++++++++------------ etc/kairo/auth_db.json | 5 ----- etc/kairo/auth_settings.json | 4 ++++ 4 files changed, 20 insertions(+), 25 deletions(-) delete mode 100644 etc/kairo/auth_db.json diff --git a/app/authsystem.inc.php b/app/authsystem.inc.php index 59d65cc..ac5469b 100644 --- a/app/authsystem.inc.php +++ b/app/authsystem.inc.php @@ -13,13 +13,6 @@ // error reporting (for testing) ini_set('display_errors', 1); error_reporting(E_ALL); -// Read DB settings -$dbdata = json_decode(file_get_contents('/etc/kairo/auth_db.json'), true); -if (!is_array($dbdata)) { trigger_error('DB configuration not found', E_USER_ERROR); } -$settings = json_decode(file_get_contents('/etc/kairo/auth_settings.json'), true); -if (!is_array($settings)) { trigger_error('Auth settings not found', E_USER_ERROR); } -$settings['dbdata'] = $dbdata; - // Extended DOM document class require_once(__DIR__.'/../php-utility-classes/classes/document.php-class'); // Class for sending emails @@ -30,11 +23,14 @@ require_once(__DIR__.'/../vendor/autoload.php'); require_once(__DIR__.'/authutils.php-class'); // Instantiate server utils. try { - $utils = new AuthUtils($settings); + $utils = new AuthUtils(); $db = $utils->db; + $settings = $utils->settings; } catch (Exception $e) { $utils = null; + print('Failed to set up utilities: '.$e->getMessage()); + exit(1); } $utils->setUpL10n(); diff --git a/app/authutils.php-class b/app/authutils.php-class index 690d5e7..3148164 100755 --- a/app/authutils.php-class +++ b/app/authutils.php-class @@ -7,12 +7,11 @@ class AuthUtils { // KaiRo.at authentication utilities PHP class // This class contains helper functions for the authentication system. // - // function __construct($settings) + // function __construct() // CONSTRUCTOR - // Settings are an associative array with a numeric pwd_cost field and an array pwd_nonces field. // // public $settings - // Ab array of settings for the auth server website. + // An array of settings for the auth server website. // // public $db // A PDO database object for interaction. @@ -108,17 +107,18 @@ class AuthUtils { // Append a login form for the given session to the given DOM element, possibly prefilling the email from the given user info array. // The optional $addfields parameter is an array of name=>value pairs of hidden fields to add to the form. - function __construct($settings) { + function __construct() { // *** constructor *** - $this->settings = $settings; - $this->db = new PDO($this->settings['dbdata']['dsn'], $this->settings['dbdata']['username'], $this->settings['dbdata']['password']); + $this->settings = json_decode(@file_get_contents('/etc/kairo/auth_settings.json'), true); + if (!is_array($this->settings)) { throw new ErrorException('Authentication system settings not found', 0); } + $this->db = new PDO('mysql:dbname='.$this->settings['db_name'].';host='.$this->settings['db_host'], $this->settings['db_username'], $this->settings['db_password']); $this->db->exec("SET time_zone='+00:00';"); // Execute directly on PDO object, set session to UTC to make our gmdate() values match correctly. // For debugging, potentially add |robert\.box\.kairo\.at to that regex temporarily. $this->running_on_localhost = preg_match('/^((.+\.)?localhost|127\.0\.0\.\d+)$/', $_SERVER['SERVER_NAME']); - if (array_key_exists('pwd_cost', $settings)) { + if (array_key_exists('pwd_cost', $this->settings)) { $this->pwd_cost = $this->settings['pwd_cost']; } - if (array_key_exists('pwd_nonces', $settings)) { + if (array_key_exists('pwd_nonces', $this->settings)) { $this->pwd_nonces = $this->settings['pwd_nonces']; } } @@ -428,10 +428,10 @@ class AuthUtils { function getOAuthServer() { // Simple server based on https://bshaffer.github.io/oauth2-server-php-docs/cookbook - - // dbata needs to be set and be an associative array with the members 'dsn', 'username', and 'password'. - // dsn is the Data Source Name for your database, for exmaple "mysql:dbname=my_oauth2_db;host=localhost" - $oauth2_storage = new OAuth2\Storage\Pdo($this->settings['dbdata']); + $dbdata = array('dsn' => 'mysql:dbname='.$this->settings['db_name'].';host='.$this->settings['db_host'], + 'username' => $this->settings['db_username'], + 'password' => $this->settings['db_password']); + $oauth2_storage = new OAuth2\Storage\Pdo($dbdata); // Set configuration $oauth2_config = array( diff --git a/etc/kairo/auth_db.json b/etc/kairo/auth_db.json deleted file mode 100644 index d3c75b4..0000000 --- a/etc/kairo/auth_db.json +++ /dev/null @@ -1,5 +0,0 @@ -{ -"dsn": "mysql:dbname=example_auth;host=localhost", -"username": "example_auth", -"password": "" -} diff --git a/etc/kairo/auth_settings.json b/etc/kairo/auth_settings.json index 9f2197b..6294f26 100644 --- a/etc/kairo/auth_settings.json +++ b/etc/kairo/auth_settings.json @@ -1,4 +1,8 @@ { +"db_name": "example_auth", +"db_host": "localhost", +"db_username": "example_auth", +"db_password": "", "pwd_cost": 10, "pwd_nonces": [ "" -- 2.43.0