Subversion Repositories oidplus

Rev

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