Subversion Repositories oidplus

Rev

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

Rev Author Line No. Line
149 daniel-mar 1
/*
2
 * OIDplus 2.0
511 daniel-mar 3
 * Copyright 2019 - 2021 Daniel Marschall, ViaThinkSoft
149 daniel-mar 4
 *
5
 * Licensed under the Apache License, Version 2.0 (the "License");
6
 * you may not use this file except in compliance with the License.
7
 * You may obtain a copy of the License at
8
 *
9
 *     http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 */
17
 
456 daniel-mar 18
min_password_length = 10; // see also setup/includes/setup_base.js
149 daniel-mar 19
 
20
function hexToBase64(str) {
21
        return btoa(String.fromCharCode.apply(null,
22
                    str.replace(/\r|\n/g, "").replace(/([\da-fA-F]{2}) ?/g, "0x$1 ").replace(/ +$/, "").split(" ")));
23
}
24
 
456 daniel-mar 25
var bCryptWorker = null;
26
var g_prevBcryptPw = null;
27
var g_last_admPwdHash = null;
28
var g_last_pwComment = null;
29
 
149 daniel-mar 30
function rehash_admin_pwd() {
150 daniel-mar 31
        var error = "";
32
 
33
        if (document.getElementById('admin_password').value.length == 0) {
34
                document.getElementById('config').innerHTML = "";
35
                return;
36
        }
37
 
149 daniel-mar 38
        if (document.getElementById('admin_password').value.length < min_password_length) {
360 daniel-mar 39
                error += _L("Password is too short. Need at least %1 characters",min_password_length)+"<br>";
150 daniel-mar 40
        }
41
 
42
        if (document.getElementById('admin_password').value != document.getElementById('admin_password2').value) {
360 daniel-mar 43
                error += _L("Passwords do not match")+"<br>";
150 daniel-mar 44
        }
45
 
46
        if (error != "") {
47
                document.getElementById('config').innerHTML = error;
149 daniel-mar 48
        } else {
456 daniel-mar 49
                var pw = document.getElementById('admin_password').value;
50
 
51
                if (pw != g_prevBcryptPw) {
52
                        // sync call to calculate SHA3
53
                        var admPwdHash = hexToBase64(sha3_512(pw))
54
                        var pwComment = 'salted, base64 encoded SHA3-512 hash';
55
                        document.getElementById('config').innerHTML = 'OIDplus::baseConfig()->setValue(\'ADMIN_PASSWORD\',    \'' + admPwdHash + '\'); // '+pwComment+'<br>';
56
                        g_last_admPwdHash = admPwdHash;
57
                        g_last_pwComment = pwComment;
58
 
59
                        // "async" call to calculate bcrypt (via web-worker)
60
                        if (bCryptWorker != null) {
61
                                g_prevBcryptPw = null;
62
                                bCryptWorker.terminate();
63
                        }
64
                        bCryptWorker = new Worker('setup/bcrypt_worker.js');
65
                        bCryptWorker.postMessage(pw);
66
                        bCryptWorker.onmessage = function (event) {
67
                                var admPwdHash = event.data;
68
                                var pwComment = 'bcrypt encoded hash';
69
                                document.getElementById('config').innerHTML = 'OIDplus::baseConfig()->setValue(\'ADMIN_PASSWORD\',    \'' + admPwdHash + '\'); // '+pwComment+'<br>';
70
                                g_last_admPwdHash = admPwdHash;
71
                                g_last_pwComment = pwComment;
72
                                g_prevBcryptPw = pw;
73
                        };
74
                } else {
75
                        document.getElementById('config').innerHTML = 'OIDplus::baseConfig()->setValue(\'ADMIN_PASSWORD\',    \'' + g_last_admPwdHash + '\'); // '+g_last_pwComment+'<br>';
76
                }
149 daniel-mar 77
        }
360 daniel-mar 78
}