Subversion Repositories oidplus

Rev

Rev 14 | 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.         // RA authentification functions
  23.  
  24.         public static function raLogin($email) {
  25.                 if (strpos($email, '|') !== false) return;
  26.  
  27.                 $ses = new OIDplusSessionHandler(OIDPLUS_SESSION_SECRET);
  28.                 $list = $ses->getValue('oidplus_logged_in');
  29.                 if (is_null($list)) $list = '';
  30.  
  31.                 $ary = ($list == '') ? array() : explode('|', $list);
  32.                 if (!in_array($email, $ary)) $ary[] = $email;
  33.                 $list = implode('|', $ary);
  34.  
  35.                 $ses->setValue('oidplus_logged_in', $list);
  36.                 unset($ses);
  37.         }
  38.  
  39.         public static function raLogout($email) {
  40.                 $ses = new OIDplusSessionHandler(OIDPLUS_SESSION_SECRET);
  41.                 $list = $ses->getValue('oidplus_logged_in');
  42.                 if (is_null($list)) $list = '';
  43.  
  44.                 $ary = ($list == '') ? array() : explode('|', $list);
  45.                 $key = array_search($email, $ary);
  46.                 if ($key !== false) unset($ary[$key]);
  47.                 $list = implode('|', $ary);
  48.  
  49.                 $ses->setValue('oidplus_logged_in', $list);
  50.                 unset($ses);
  51.         }
  52.  
  53.         public static function raLogoutAll() {
  54.                 $ses = new OIDplusSessionHandler(OIDPLUS_SESSION_SECRET);
  55.                 $ses->setValue('oidplus_logged_in', '');
  56.                 unset($ses);
  57.         }
  58.  
  59.         public static function loggedInRaList() {
  60.                 $ses = new OIDplusSessionHandler(OIDPLUS_SESSION_SECRET);
  61.                 $list = $ses->getValue('oidplus_logged_in');
  62.                 if (is_null($list)) $list = '';
  63.                 return ($list == '') ? array() : explode('|', $list);
  64.         }
  65.  
  66.         public static function isRaLoggedIn($email) {
  67.                 return in_array($email, self::loggedInRaList());
  68.         }
  69.  
  70.         // Admin authentification functions
  71.  
  72.         public static function adminLogin() {
  73.                 $ses = new OIDplusSessionHandler(OIDPLUS_SESSION_SECRET);
  74.                 $ses->setValue('oidplus_admin_logged_in', '1');
  75.                 unset($ses);
  76.         }
  77.  
  78.         public static function adminLogout() {
  79.                 $ses = new OIDplusSessionHandler(OIDPLUS_SESSION_SECRET);
  80.                 $ses->setValue('oidplus_admin_logged_in', '');
  81.                 unset($ses);
  82.         }
  83.  
  84.         public static function adminCheckPassword($password) {
  85.                 $calc_authkey = bin2hex(version_compare(PHP_VERSION, '7.1.0') >= 0 ? hash('sha3-512', $password, true) : bb\Sha3\Sha3::hash($password, 512, true));
  86.                 return $calc_authkey == bin2hex(base64_decode(OIDPLUS_ADMIN_PASSWORD));
  87.         }
  88.  
  89.         public static function isAdminLoggedIn() {
  90.                 $ses = new OIDplusSessionHandler(OIDPLUS_SESSION_SECRET);
  91.                 return $ses->getValue('oidplus_admin_logged_in') == '1';
  92.         }
  93.  
  94.         // Action.php auth arguments
  95.  
  96.         public static function makeAuthKey($data) {
  97.                 $calc_authkey = bin2hex(version_compare(PHP_VERSION, '7.1.0') >= 0 ? hash('sha3-512', $data, true) : bb\Sha3\Sha3::hash($data, 512, true));
  98.                 return $calc_authkey;
  99.         }
  100.  
  101.         public static function validateAuthKey($data, $auth_key) {
  102.                 return self::makeAuthKey($data) == $auth_key;
  103.         }
  104.  
  105. }
  106.