Subversion Repositories oidplus

Rev

Rev 456 | Go to most recent revision | View as "text/javascript" | Blame | Last modification | View Log | RSS feed

  1. /*
  2.  * OIDplus 2.0
  3.  * Copyright 2019 - 2021 Daniel Marschall, ViaThinkSoft
  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.  
  18. min_password_length = 10; // see also setup/includes/setup_base.js
  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.  
  25. var bCryptWorker = null;
  26. var g_prevBcryptPw = null;
  27. var g_last_admPwdHash = null;
  28. var g_last_pwComment = null;
  29.  
  30. function rehash_admin_pwd() {
  31.         var error = "";
  32.  
  33.         if (document.getElementById('admin_password').value.length == 0) {
  34.                 document.getElementById('config').innerHTML = "";
  35.                 return;
  36.         }
  37.  
  38.         if (document.getElementById('admin_password').value.length < min_password_length) {
  39.                 error += _L("Password is too short. Need at least %1 characters",min_password_length)+"<br>";
  40.         }
  41.  
  42.         if (document.getElementById('admin_password').value != document.getElementById('admin_password2').value) {
  43.                 error += _L("Passwords do not match")+"<br>";
  44.         }
  45.  
  46.         if (error != "") {
  47.                 document.getElementById('config').innerHTML = error;
  48.         } else {
  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.                 }
  77.         }
  78. }