Subversion Repositories oidplus

Rev

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