Subversion Repositories oidplus

Rev

Rev 457 | Rev 461 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. <?php
  2.  
  3. /*
  4.  * OIDplus 2.0
  5.  * Copyright 2019 Daniel Marschall, ViaThinkSoft
  6.  *
  7.  * Licensed under the Apache License, Version 2.0 (the "License");
  8.  * you may not use this file except in compliance with the License.
  9.  * You may obtain a copy of the License at
  10.  *
  11.  *     http://www.apache.org/licenses/LICENSE-2.0
  12.  *
  13.  * Unless required by applicable law or agreed to in writing, software
  14.  * distributed under the License is distributed on an "AS IS" BASIS,
  15.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16.  * See the License for the specific language governing permissions and
  17.  * limitations under the License.
  18.  */
  19.  
  20. class OIDplusAuthUtils {
  21.  
  22.         public static function getRandomBytes($len) {
  23.                 if (function_exists('openssl_random_pseudo_bytes')) {
  24.                         $a = openssl_random_pseudo_bytes($len);
  25.                         if ($a) return $a;
  26.                 }
  27.  
  28.                 if (function_exists('random_bytes')) {
  29.                         $a = random_bytes($len);
  30.                         if ($a) return $a;
  31.                 }
  32.  
  33.                 // Fallback to non-secure RNG
  34.                 $a = '';
  35.                 while (strlen($a) < $len*2) {
  36.                         $a .= sha1(uniqid(mt_rand(), true));
  37.                 }
  38.                 $a = substr($a, 0, $len*2);
  39.                 return hex2bin($a);
  40.         }
  41.  
  42.         // RA authentication functions
  43.  
  44.         public static function raLogin($email) {
  45.                 if (strpos($email, '|') !== false) return;
  46.  
  47.                 $ses = OIDplus::sesHandler();
  48.                 $list = $ses->getValue('oidplus_logged_in');
  49.                 if (is_null($list)) $list = '';
  50.  
  51.                 $ary = ($list == '') ? array() : explode('|', $list);
  52.                 if (!in_array($email, $ary)) $ary[] = $email;
  53.                 $list = implode('|', $ary);
  54.  
  55.                 $ses->setValue('oidplus_logged_in', $list);
  56.         }
  57.  
  58.         public static function raLogout($email) {
  59.                 $ses = OIDplus::sesHandler();
  60.                 $list = $ses->getValue('oidplus_logged_in');
  61.                 if (is_null($list)) $list = '';
  62.  
  63.                 $ary = ($list == '') ? array() : explode('|', $list);
  64.                 $key = array_search($email, $ary);
  65.                 if ($key !== false) unset($ary[$key]);
  66.                 $list = implode('|', $ary);
  67.  
  68.                 $ses->setValue('oidplus_logged_in', $list);
  69.  
  70.                 if (($list == '') && (!self::isAdminLoggedIn())) {
  71.                         // Nobody logged in anymore. Destroy session cookie to make GDPR people happy
  72.                         $ses->destroySession();
  73.                 }
  74.         }
  75.  
  76.         public static function raCheckPassword($ra_email, $password) {
  77.                 $ra = new OIDplusRA($ra_email);
  78.  
  79.                 $authInfo = $ra->getAuthInfo();
  80.  
  81.                 $plugins = OIDplus::getAuthPlugins();
  82.                 if (count($plugins) == 0) {
  83.                         throw new OIDplusException(_L('No RA authentication plugins found'));
  84.                 }
  85.                 foreach ($plugins as $plugin) {
  86.                         if ($plugin->verify($authInfo, $password)) return true;
  87.                 }
  88.  
  89.                 return false;
  90.         }
  91.  
  92.         public static function raNumLoggedIn() {
  93.                 return count(self::loggedInRaList());
  94.         }
  95.  
  96.         public static function raLogoutAll() {
  97.                 $ses = OIDplus::sesHandler();
  98.                 $ses->setValue('oidplus_logged_in', '');
  99.         }
  100.  
  101.         public static function loggedInRaList() {
  102.                 if (self::forceAllLoggedOut()) {
  103.                         return array();
  104.                 }
  105.  
  106.                 $ses = OIDplus::sesHandler();
  107.                 $list = $ses->getValue('oidplus_logged_in');
  108.                 if (is_null($list)) $list = '';
  109.  
  110.                 $res = array();
  111.                 foreach (array_unique(explode('|',$list)) as $ra_email) {
  112.                         if ($ra_email == '') continue;
  113.                         $res[] = new OIDplusRA($ra_email);
  114.                 }
  115.                 return $res;
  116.         }
  117.  
  118.         public static function isRaLoggedIn($email) {
  119.                 foreach (self::loggedInRaList() as $ra) {
  120.                         if ($email == $ra->raEmail()) return true;
  121.                 }
  122.                 return false;
  123.         }
  124.  
  125.         // Admin authentication functions
  126.  
  127.         public static function adminLogin() {
  128.                 $ses = OIDplus::sesHandler();
  129.                 $ses->setValue('oidplus_admin_logged_in', '1');
  130.         }
  131.  
  132.         public static function adminLogout() {
  133.                 $ses = OIDplus::sesHandler();
  134.                 $ses->setValue('oidplus_admin_logged_in', '0');
  135.  
  136.                 if (self::raNumLoggedIn() == 0) {
  137.                         // Nobody logged in anymore. Destroy session cookie to make GDPR people happy
  138.                         $ses->destroySession();
  139.                 }
  140.         }
  141.  
  142.         public static function adminCheckPassword($password) {
  143.                 $passwordData = OIDplus::baseConfig()->getValue('ADMIN_PASSWORD', '');
  144.                 if (empty($passwordData)) {
  145.                         throw new OIDplusException(_L('No admin password set in %1','userdata/baseconfig/config.inc.php'));
  146.                 }
  147.  
  148.                 if (strpos($passwordData, '$') !== false) {
  149.                         if ($passwordData[0] == '$') {
  150.                                 // Version 3: BCrypt
  151.                                 return password_verify($password, $passwordData);
  152.                         } else {
  153.                                 // Version 2: SHA3-512 with salt
  154.                                 list($s_salt, $hash) = explode('$', $passwordData, 2);
  155.                         }
  156.                 } else {
  157.                         // Version 1: SHA3-512 without salt
  158.                         $s_salt = '';
  159.                         $hash = $passwordData;
  160.                 }
  161.                 return strcmp(sha3_512($s_salt.$password, true), base64_decode($hash)) === 0;
  162.         }
  163.  
  164.         public static function isAdminLoggedIn() {
  165.                 if (self::forceAllLoggedOut()) {
  166.                         return false;
  167.                 }
  168.                 $ses = OIDplus::sesHandler();
  169.                 return $ses->getValue('oidplus_admin_logged_in') == '1';
  170.         }
  171.  
  172.         // Authentication keys for validating arguments (e.g. sent by mail)
  173.  
  174.         public static function makeAuthKey($data) {
  175.                 $data = OIDplus::baseConfig()->getValue('SERVER_SECRET') . '/AUTHKEY/' . $data;
  176.                 $calc_authkey = sha3_512($data, false);
  177.                 return $calc_authkey;
  178.         }
  179.  
  180.         public static function validateAuthKey($data, $auth_key) {
  181.                 return strcmp(self::makeAuthKey($data), $auth_key) === 0;
  182.         }
  183.  
  184.         // "Veto" functions to force logout state
  185.  
  186.         public static function forceAllLoggedOut() {
  187.                 if (isset($_SERVER['SCRIPT_FILENAME']) && (basename($_SERVER['SCRIPT_FILENAME']) == 'sitemap.php')) {
  188.                         // The sitemap may not contain any confidential information,
  189.                         // even if the user is logged in, because the admin could
  190.                         // accidentally copy-paste the sitemap to a
  191.                         // search engine control panel while they are logged in
  192.                         return true;
  193.                 } else {
  194.                         return false;
  195.                 }
  196.         }
  197.  
  198.         // CSRF functions
  199.  
  200.         private $enable_csrf = true;
  201.  
  202.         public function enableCSRF() {
  203.                 $this->enable_csrf = true;
  204.         }
  205.  
  206.         public function disableCSRF() {
  207.                 $this->enable_csrf = false;
  208.         }
  209.  
  210.         public function genCSRFToken() {
  211.                 return bin2hex(OIDplusAuthUtils::getRandomBytes(64));
  212.         }
  213.  
  214.         public function checkCSRF() {
  215.                 if (!$this->enable_csrf) return;
  216.                 if (!isset($_REQUEST['csrf_token']) || !isset($_COOKIE['csrf_token']) || ($_REQUEST['csrf_token'] != $_COOKIE['csrf_token'])) {
  217.                         throw new Exception(_L('Wrong CSRF Token'));
  218.                 }
  219.         }
  220.  
  221.         // Generate RA passwords
  222.  
  223.         public static function raGeneratePassword($password): OIDInfoRAAuthInfo {
  224.                 $def_method = OIDplus::config()->getValue('default_ra_auth_method');
  225.  
  226.                 $plugins = OIDplus::getAuthPlugins();
  227.                 foreach ($plugins as $plugin) {
  228.                         if (basename($plugin->getPluginDirectory()) === $def_method) {
  229.                                 return $plugin->generate($password);
  230.                         }
  231.                 }
  232.                 throw new OIDplusException(_L('Default RA auth method/plugin "%1" not found',$def_method));
  233.         }
  234.  
  235.         // Generate admin password
  236.  
  237.         /* Nothing here; the admin password will be generated in setup_base.js */
  238.  
  239. }
  240.