Subversion Repositories oidplus

Rev

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