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