Subversion Repositories oidplus

Rev

Rev 213 | Rev 277 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 daniel-mar 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
 
112 daniel-mar 20
if (!defined('IN_OIDPLUS')) die();
21
 
2 daniel-mar 22
class OIDplusAuthUtils {
23
 
14 daniel-mar 24
        // RA authentication functions
2 daniel-mar 25
 
26
        public static function raLogin($email) {
27
                if (strpos($email, '|') !== false) return;
28
 
42 daniel-mar 29
                $ses = OIDplus::sesHandler();
2 daniel-mar 30
                $list = $ses->getValue('oidplus_logged_in');
31
                if (is_null($list)) $list = '';
32
 
33
                $ary = ($list == '') ? array() : explode('|', $list);
34
                if (!in_array($email, $ary)) $ary[] = $email;
35
                $list = implode('|', $ary);
36
 
37
                $ses->setValue('oidplus_logged_in', $list);
38
        }
39
 
40
        public static function raLogout($email) {
42 daniel-mar 41
                $ses = OIDplus::sesHandler();
2 daniel-mar 42
                $list = $ses->getValue('oidplus_logged_in');
43
                if (is_null($list)) $list = '';
44
 
45
                $ary = ($list == '') ? array() : explode('|', $list);
46
                $key = array_search($email, $ary);
47
                if ($key !== false) unset($ary[$key]);
48
                $list = implode('|', $ary);
49
 
50
                $ses->setValue('oidplus_logged_in', $list);
85 daniel-mar 51
 
179 daniel-mar 52
                if (($list == '') && (!self::isAdminLoggedIn())) {
85 daniel-mar 53
                        // Nobody logged in anymore. Destroy session cookie to make GDPR people happy
54
                        $ses->destroySession();
55
                }
2 daniel-mar 56
        }
57
 
85 daniel-mar 58
        public static function raNumLoggedIn() {
59
                $ses = OIDplus::sesHandler();
179 daniel-mar 60
 
85 daniel-mar 61
                $list = $ses->getValue('oidplus_logged_in');
62
                if (is_null($list)) return 0;
179 daniel-mar 63
 
64
                $ary = ($list == '') ? array() : explode('|', $list);
65
                return count($ary);
85 daniel-mar 66
        }
67
 
2 daniel-mar 68
        public static function raLogoutAll() {
42 daniel-mar 69
                $ses = OIDplus::sesHandler();
2 daniel-mar 70
                $ses->setValue('oidplus_logged_in', '');
71
        }
72
 
73
        public static function loggedInRaList() {
42 daniel-mar 74
                $ses = OIDplus::sesHandler();
2 daniel-mar 75
                $list = $ses->getValue('oidplus_logged_in');
76
                if (is_null($list)) $list = '';
115 daniel-mar 77
 
78
                $res = array();
79
                foreach (explode('|', $list) as $ra_email) {
80
                        if ($ra_email == '') continue;
81
                        $res[] = new OIDplusRA($ra_email);
82
                }
83
                return $res;
2 daniel-mar 84
        }
85
 
86
        public static function isRaLoggedIn($email) {
115 daniel-mar 87
                foreach (self::loggedInRaList() as $ra) {
88
                        if ($email == $ra->raEmail()) return true;
89
                }
90
                return false;
2 daniel-mar 91
        }
92
 
14 daniel-mar 93
        // Admin authentication functions
2 daniel-mar 94
 
95
        public static function adminLogin() {
42 daniel-mar 96
                $ses = OIDplus::sesHandler();
2 daniel-mar 97
                $ses->setValue('oidplus_admin_logged_in', '1');
98
        }
99
 
100
        public static function adminLogout() {
42 daniel-mar 101
                $ses = OIDplus::sesHandler();
85 daniel-mar 102
 
103
                $ses->setValue('oidplus_admin_logged_in', '0');
104
 
105
                if (self::raNumLoggedIn() == 0) {
106
                        // Nobody logged in anymore. Destroy session cookie to make GDPR people happy
107
                        $ses->destroySession();
108
                }
2 daniel-mar 109
        }
110
 
111
        public static function adminCheckPassword($password) {
261 daniel-mar 112
                $hashed = OIDplus::baseConfig()->getValue('ADMIN_PASSWORD', '');
113
                if (empty($hashed)) {
114
                        throw new OIDplusException("No admin password set in config.inc.php");
115
                }
2 daniel-mar 116
                $calc_authkey = bin2hex(version_compare(PHP_VERSION, '7.1.0') >= 0 ? hash('sha3-512', $password, true) : bb\Sha3\Sha3::hash($password, 512, true));
261 daniel-mar 117
                return $calc_authkey == bin2hex(base64_decode($hashed));
2 daniel-mar 118
        }
119
 
120
        public static function isAdminLoggedIn() {
42 daniel-mar 121
                $ses = OIDplus::sesHandler();
2 daniel-mar 122
                return $ses->getValue('oidplus_admin_logged_in') == '1';
123
        }
124
 
125
        // Action.php auth arguments
126
 
127
        public static function makeAuthKey($data) {
261 daniel-mar 128
                $data = OIDplus::baseConfig()->getValue('SERVER_SECRET') . $data;
2 daniel-mar 129
                $calc_authkey = bin2hex(version_compare(PHP_VERSION, '7.1.0') >= 0 ? hash('sha3-512', $data, true) : bb\Sha3\Sha3::hash($data, 512, true));
130
                return $calc_authkey;
131
        }
132
 
133
        public static function validateAuthKey($data, $auth_key) {
134
                return self::makeAuthKey($data) == $auth_key;
135
        }
136
 
137
}