merge DB config into normal settings - this is another part of bug 415
authorRobert Kaiser <kairo@kairo.at>
Thu, 15 Dec 2016 15:50:25 +0000 (16:50 +0100)
committerRobert Kaiser <kairo@kairo.at>
Thu, 15 Dec 2016 15:50:25 +0000 (16:50 +0100)
app/authsystem.inc.php
app/authutils.php-class
etc/kairo/auth_db.json [deleted file]
etc/kairo/auth_settings.json

index 59d65cc4df0f04161fb1d9a3ac31d7ac090273b4..ac5469b416743c35f12dc1a1be66e9d829b5466d 100644 (file)
 // 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();
index 690d5e7a82e832f2ea6f720e9a396b59551f011f..3148164d22f0135b08dd7f31d3fae65b39b20946 100755 (executable)
@@ -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 (file)
index d3c75b4..0000000
+++ /dev/null
@@ -1,5 +0,0 @@
-{
-"dsn": "mysql:dbname=example_auth;host=localhost",
-"username": "example_auth",
-"password": ""
-}
index 9f2197bd0f56e65e14b1daa27446b467130ca3dc..6294f26e634947210426089af58fda705e3c3833 100644 (file)
@@ -1,4 +1,8 @@
 {
+"db_name": "example_auth",
+"db_host": "localhost",
+"db_username": "example_auth",
+"db_password": "",
 "pwd_cost": 10,
 "pwd_nonces": [
  ""