Rev 564 | Rev 567 | 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 |
||
511 | daniel-mar | 5 | * Copyright 2019 - 2021 Daniel Marschall, ViaThinkSoft |
2 | daniel-mar | 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 | |||
511 | daniel-mar | 20 | if (!defined('INSIDE_OIDPLUS')) die(); |
21 | |||
2 | daniel-mar | 22 | class OIDplusAuthUtils { |
23 | |||
566 | daniel-mar | 24 | // Useful functions |
25 | |||
457 | daniel-mar | 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 | |||
465 | daniel-mar | 32 | if (function_exists('mcrypt_create_iv')) { |
466 | daniel-mar | 33 | $a = bin2hex(mcrypt_create_iv($len, MCRYPT_DEV_URANDOM)); |
465 | daniel-mar | 34 | if ($a) return $a; |
35 | } |
||
36 | |||
457 | daniel-mar | 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 | |||
566 | daniel-mar | 51 | // Content provider |
2 | daniel-mar | 52 | |
566 | daniel-mar | 53 | protected function getAuthContentStore() { |
54 | static $contentProvider = null; |
||
2 | daniel-mar | 55 | |
566 | daniel-mar | 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 | } |
||
2 | daniel-mar | 66 | |
566 | daniel-mar | 67 | return $contentProvider; |
2 | daniel-mar | 68 | } |
69 | |||
566 | daniel-mar | 70 | // RA authentication functions |
2 | daniel-mar | 71 | |
566 | daniel-mar | 72 | public function raLogin($email) { |
73 | return $this->getAuthContentStore()->raLogin($email); |
||
74 | } |
||
2 | daniel-mar | 75 | |
566 | daniel-mar | 76 | public function raLogout($email) { |
77 | return $this->getAuthContentStore()->raLogout($email); |
||
2 | daniel-mar | 78 | } |
79 | |||
566 | daniel-mar | 80 | public function raCheckPassword($ra_email, $password) { |
329 | daniel-mar | 81 | $ra = new OIDplusRA($ra_email); |
453 | daniel-mar | 82 | |
459 | daniel-mar | 83 | $authInfo = $ra->getAuthInfo(); |
453 | daniel-mar | 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) { |
||
459 | daniel-mar | 90 | if ($plugin->verify($authInfo, $password)) return true; |
453 | daniel-mar | 91 | } |
92 | |||
93 | return false; |
||
329 | daniel-mar | 94 | } |
95 | |||
566 | daniel-mar | 96 | public function raNumLoggedIn() { |
97 | return $this->getAuthContentStore()->raNumLoggedIn(); |
||
85 | daniel-mar | 98 | } |
99 | |||
566 | daniel-mar | 100 | public function raLogoutAll() { |
101 | return $this->getAuthContentStore()->raLogoutAll(); |
||
2 | daniel-mar | 102 | } |
103 | |||
566 | daniel-mar | 104 | public function loggedInRaList() { |
105 | return $this->getAuthContentStore()->loggedInRaList(); |
||
2 | daniel-mar | 106 | } |
107 | |||
566 | daniel-mar | 108 | public function isRaLoggedIn($email) { |
109 | return $this->getAuthContentStore()->isRaLoggedIn($email); |
||
2 | daniel-mar | 110 | } |
111 | |||
14 | daniel-mar | 112 | // Admin authentication functions |
2 | daniel-mar | 113 | |
566 | daniel-mar | 114 | public function adminLogin() { |
115 | return $this->getAuthContentStore()->adminLogin(); |
||
2 | daniel-mar | 116 | } |
117 | |||
566 | daniel-mar | 118 | public function adminLogout() { |
119 | return $this->getAuthContentStore()->adminLogout(); |
||
2 | daniel-mar | 120 | } |
121 | |||
566 | daniel-mar | 122 | public function adminCheckPassword($password) { |
421 | daniel-mar | 123 | $passwordData = OIDplus::baseConfig()->getValue('ADMIN_PASSWORD', ''); |
124 | if (empty($passwordData)) { |
||
360 | daniel-mar | 125 | throw new OIDplusException(_L('No admin password set in %1','userdata/baseconfig/config.inc.php')); |
261 | daniel-mar | 126 | } |
456 | daniel-mar | 127 | |
421 | daniel-mar | 128 | if (strpos($passwordData, '$') !== false) { |
456 | daniel-mar | 129 | if ($passwordData[0] == '$') { |
130 | // Version 3: BCrypt |
||
131 | return password_verify($password, $passwordData); |
||
132 | } else { |
||
457 | daniel-mar | 133 | // Version 2: SHA3-512 with salt |
456 | daniel-mar | 134 | list($s_salt, $hash) = explode('$', $passwordData, 2); |
135 | } |
||
421 | daniel-mar | 136 | } else { |
456 | daniel-mar | 137 | // Version 1: SHA3-512 without salt |
421 | daniel-mar | 138 | $s_salt = ''; |
139 | $hash = $passwordData; |
||
140 | } |
||
141 | return strcmp(sha3_512($s_salt.$password, true), base64_decode($hash)) === 0; |
||
2 | daniel-mar | 142 | } |
143 | |||
566 | daniel-mar | 144 | public function isAdminLoggedIn() { |
145 | return $this->getAuthContentStore()->isAdminLoggedIn(); |
||
2 | daniel-mar | 146 | } |
147 | |||
310 | daniel-mar | 148 | // Authentication keys for validating arguments (e.g. sent by mail) |
2 | daniel-mar | 149 | |
150 | public static function makeAuthKey($data) { |
||
424 | daniel-mar | 151 | $data = OIDplus::baseConfig()->getValue('SERVER_SECRET') . '/AUTHKEY/' . $data; |
421 | daniel-mar | 152 | $calc_authkey = sha3_512($data, false); |
2 | daniel-mar | 153 | return $calc_authkey; |
154 | } |
||
155 | |||
156 | public static function validateAuthKey($data, $auth_key) { |
||
421 | daniel-mar | 157 | return strcmp(self::makeAuthKey($data), $auth_key) === 0; |
2 | daniel-mar | 158 | } |
159 | |||
329 | daniel-mar | 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 | } |
||
427 | daniel-mar | 173 | |
424 | daniel-mar | 174 | // CSRF functions |
427 | daniel-mar | 175 | |
424 | daniel-mar | 176 | private $enable_csrf = true; |
427 | daniel-mar | 177 | |
424 | daniel-mar | 178 | public function enableCSRF() { |
179 | $this->enable_csrf = true; |
||
180 | } |
||
427 | daniel-mar | 181 | |
424 | daniel-mar | 182 | public function disableCSRF() { |
183 | $this->enable_csrf = false; |
||
184 | } |
||
427 | daniel-mar | 185 | |
424 | daniel-mar | 186 | public function genCSRFToken() { |
564 | daniel-mar | 187 | return bin2hex(self::getRandomBytes(64)); |
424 | daniel-mar | 188 | } |
427 | daniel-mar | 189 | |
424 | daniel-mar | 190 | public function checkCSRF() { |
427 | daniel-mar | 191 | if (!$this->enable_csrf) return; |
564 | daniel-mar | 192 | if (!isset($_REQUEST['csrf_token']) || !isset($_COOKIE['csrf_token']) || ($_REQUEST['csrf_token'] !== $_COOKIE['csrf_token'])) { |
193 | throw new OIDplusException(_L('Wrong CSRF Token')); |
||
424 | daniel-mar | 194 | } |
195 | } |
||
329 | daniel-mar | 196 | |
421 | daniel-mar | 197 | // Generate RA passwords |
198 | |||
461 | daniel-mar | 199 | public static function raGeneratePassword($password): OIDplusRAAuthInfo { |
453 | daniel-mar | 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)); |
||
421 | daniel-mar | 209 | } |
210 | |||
211 | // Generate admin password |
||
212 | |||
213 | /* Nothing here; the admin password will be generated in setup_base.js */ |
||
214 | |||
215 | } |