+ //
+ // function appendLoginForm($dom_element, $session, $user)
+ // append a login form for the given session to the given DOM element, possibly prefilling the email from the given user info array.
+
+ function __construct($settings, $db) {
+ // *** constructor ***
+ $this->db = $db;
+ $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)) {
+ $this->pwd_cost = $settings['pwd_cost'];
+ }
+ if (array_key_exists('pwd_nonces', $settings)) {
+ $this->pwd_nonces = $settings['pwd_nonces'];
+ }
+ }
+
+ public $db = null;
+ public $running_on_localhost = false;
+ public $client_reg_email_whitelist = array('kairo@kairo.at', 'com@kairo.at');
+ private $pwd_cost = 10;
+ private $pwd_nonces = array();
+
+ function log($code, $info) {
+ $result = $this->db->prepare('INSERT INTO `auth_log` (`code`, `info`, `ip_addr`) VALUES (:code, :info, :ipaddr);');
+ if (!$result->execute(array(':code' => $code, ':info' => $info, ':ipaddr' => $_SERVER['REMOTE_ADDR']))) {
+ // print($result->errorInfo()[2]);
+ }
+ }
+
+ function checkForSecureConnection() {
+ $errors = array();
+ if (($_SERVER['SERVER_PORT'] != 443) && !$this->running_on_localhost) {
+ $errors[] = _('You are not accessing this site on a secure connection, so authentication doesn\'t work.');
+ }
+ return $errors;
+ }
+
+ function sendSecurityHeaders() {
+ // Send various headers that we want to have for security resons, mostly as recommended by https://observatory.mozilla.org/
+
+ // CSP - see https://wiki.mozilla.org/Security/Guidelines/Web_Security#Content_Security_Policy
+ // Disable unsafe inline/eval, only allow loading of resources (images, fonts, scripts, etc.) from ourselves; also disable framing.
+ header('Content-Security-Policy: default-src \'none\';img-src \'self\'; script-src \'self\'; style-src \'self\'; frame-ancestors \'none\'');
+
+ // X-Content-Type-Options - see https://wiki.mozilla.org/Security/Guidelines/Web_Security#X-Content-Type-Options
+ // Prevent browsers from incorrectly detecting non-scripts as scripts
+ header('X-Content-Type-Options: nosniff');
+
+ // X-Frame-Options (for older browsers) - see https://wiki.mozilla.org/Security/Guidelines/Web_Security#X-Frame-Options
+ // Block site from being framed
+ header('X-Frame-Options: DENY');
+
+ // X-XSS-Protection (for older browsers) - see https://wiki.mozilla.org/Security/Guidelines/Web_Security#X-XSS-Protection
+ // Block pages from loading when they detect reflected XSS attacks
+ header('X-XSS-Protection: 1; mode=block');
+ }
+
+ function initSession() {
+ $session = null;
+ if (strlen(@$_COOKIE['sessionkey'])) {
+ // Fetch the session - or at least try to.
+ $result = $this->db->prepare('SELECT * FROM `auth_sessions` WHERE `sesskey` = :sesskey AND `time_expire` > :expire;');
+ $result->execute(array(':sesskey' => $_COOKIE['sessionkey'], ':expire' => gmdate('Y-m-d H:i:s')));
+ $row = $result->fetch(PDO::FETCH_ASSOC);
+ if ($row) {
+ $session = $row;
+ }
+ }
+ if (is_null($session)) {
+ // Create new session and set cookie.
+ $sesskey = $this->createSessionKey();
+ setcookie('sessionkey', $sesskey, 0, "", "", !$this->running_on_localhost, true); // Last two params are secure and httponly, secure is not set on localhost.
+ $result = $this->db->prepare('INSERT INTO `auth_sessions` (`sesskey`, `time_expire`) VALUES (:sesskey, :expire);');
+ $result->execute(array(':sesskey' => $sesskey, ':expire' => gmdate('Y-m-d H:i:s', strtotime('+5 minutes'))));
+ // After insert, actually fetch the session row from the DB so we have all values.
+ $result = $this->db->prepare('SELECT * FROM auth_sessions WHERE `sesskey` = :sesskey AND `time_expire` > :expire;');
+ $result->execute(array(':sesskey' => $sesskey, ':expire' => gmdate('Y-m-d H:i:s')));
+ $row = $result->fetch(PDO::FETCH_ASSOC);
+ if ($row) {
+ $session = $row;
+ }
+ else {
+ $this->log('session_create_failure', 'key: '.$sesskey);
+ }
+ }
+ return $session;
+ }