Subversion Repositories oidplus

Rev

Rev 566 | Rev 570 | 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() {
567 daniel-mar 105
                if (OIDplus::authUtils()->forceAllLoggedOut()) {
106
                        return array();
107
                } else {
108
                        return $this->getAuthContentStore()->loggedInRaList();
109
                }
2 daniel-mar 110
        }
111
 
566 daniel-mar 112
        public function isRaLoggedIn($email) {
113
                return $this->getAuthContentStore()->isRaLoggedIn($email);
2 daniel-mar 114
        }
115
 
14 daniel-mar 116
        // Admin authentication functions
2 daniel-mar 117
 
566 daniel-mar 118
        public function adminLogin() {
119
                return $this->getAuthContentStore()->adminLogin();
2 daniel-mar 120
        }
121
 
566 daniel-mar 122
        public function adminLogout() {
123
                return $this->getAuthContentStore()->adminLogout();
2 daniel-mar 124
        }
125
 
566 daniel-mar 126
        public function adminCheckPassword($password) {
421 daniel-mar 127
                $passwordData = OIDplus::baseConfig()->getValue('ADMIN_PASSWORD', '');
128
                if (empty($passwordData)) {
360 daniel-mar 129
                        throw new OIDplusException(_L('No admin password set in %1','userdata/baseconfig/config.inc.php'));
261 daniel-mar 130
                }
456 daniel-mar 131
 
421 daniel-mar 132
                if (strpos($passwordData, '$') !== false) {
456 daniel-mar 133
                        if ($passwordData[0] == '$') {
134
                                // Version 3: BCrypt
135
                                return password_verify($password, $passwordData);
136
                        } else {
457 daniel-mar 137
                                // Version 2: SHA3-512 with salt
456 daniel-mar 138
                                list($s_salt, $hash) = explode('$', $passwordData, 2);
139
                        }
421 daniel-mar 140
                } else {
456 daniel-mar 141
                        // Version 1: SHA3-512 without salt
421 daniel-mar 142
                        $s_salt = '';
143
                        $hash = $passwordData;
144
                }
145
                return strcmp(sha3_512($s_salt.$password, true), base64_decode($hash)) === 0;
2 daniel-mar 146
        }
147
 
566 daniel-mar 148
        public function isAdminLoggedIn() {
567 daniel-mar 149
                if (OIDplus::authUtils()->forceAllLoggedOut()) {
150
                        return false;
151
                } else {
152
                        return $this->getAuthContentStore()->isAdminLoggedIn();
153
                }
2 daniel-mar 154
        }
155
 
310 daniel-mar 156
        // Authentication keys for validating arguments (e.g. sent by mail)
2 daniel-mar 157
 
158
        public static function makeAuthKey($data) {
424 daniel-mar 159
                $data = OIDplus::baseConfig()->getValue('SERVER_SECRET') . '/AUTHKEY/' . $data;
421 daniel-mar 160
                $calc_authkey = sha3_512($data, false);
2 daniel-mar 161
                return $calc_authkey;
162
        }
163
 
164
        public static function validateAuthKey($data, $auth_key) {
421 daniel-mar 165
                return strcmp(self::makeAuthKey($data), $auth_key) === 0;
2 daniel-mar 166
        }
167
 
329 daniel-mar 168
        // "Veto" functions to force logout state
169
 
170
        public static function forceAllLoggedOut() {
171
                if (isset($_SERVER['SCRIPT_FILENAME']) && (basename($_SERVER['SCRIPT_FILENAME']) == 'sitemap.php')) {
172
                        // The sitemap may not contain any confidential information,
173
                        // even if the user is logged in, because the admin could
174
                        // accidentally copy-paste the sitemap to a
175
                        // search engine control panel while they are logged in
176
                        return true;
177
                } else {
178
                        return false;
179
                }
180
        }
427 daniel-mar 181
 
424 daniel-mar 182
        // CSRF functions
427 daniel-mar 183
 
424 daniel-mar 184
        private $enable_csrf = true;
427 daniel-mar 185
 
424 daniel-mar 186
        public function enableCSRF() {
187
                $this->enable_csrf = true;
188
        }
427 daniel-mar 189
 
424 daniel-mar 190
        public function disableCSRF() {
191
                $this->enable_csrf = false;
192
        }
427 daniel-mar 193
 
424 daniel-mar 194
        public function genCSRFToken() {
564 daniel-mar 195
                return bin2hex(self::getRandomBytes(64));
424 daniel-mar 196
        }
427 daniel-mar 197
 
424 daniel-mar 198
        public function checkCSRF() {
427 daniel-mar 199
                if (!$this->enable_csrf) return;
564 daniel-mar 200
                if (!isset($_REQUEST['csrf_token']) || !isset($_COOKIE['csrf_token']) || ($_REQUEST['csrf_token'] !== $_COOKIE['csrf_token'])) {
201
                        throw new OIDplusException(_L('Wrong CSRF Token'));
424 daniel-mar 202
                }
203
        }
329 daniel-mar 204
 
421 daniel-mar 205
        // Generate RA passwords
206
 
461 daniel-mar 207
        public static function raGeneratePassword($password): OIDplusRAAuthInfo {
453 daniel-mar 208
                $def_method = OIDplus::config()->getValue('default_ra_auth_method');
209
 
210
                $plugins = OIDplus::getAuthPlugins();
211
                foreach ($plugins as $plugin) {
212
                        if (basename($plugin->getPluginDirectory()) === $def_method) {
213
                                return $plugin->generate($password);
214
                        }
215
                }
216
                throw new OIDplusException(_L('Default RA auth method/plugin "%1" not found',$def_method));
421 daniel-mar 217
        }
218
 
219
        // Generate admin password
220
 
221
        /* Nothing here; the admin password will be generated in setup_base.js */
222
 
223
}