merge of features from hosted branch (#7)

major merge of features from the hosted branch that was created temporarily during the initial launch of jambonz.org
This commit is contained in:
Dave Horton
2021-06-17 15:56:21 -04:00
committed by GitHub
parent ab7c69c0e8
commit ed51d8b13f
105 changed files with 10330 additions and 1601 deletions
+35 -60
View File
@@ -1,71 +1,46 @@
#!/usr/bin/env node
const {getMysqlConnection} = require('../lib/db');
const crypto = require('crypto');
console.log('reset_admin_password');
const {promisePool} = require('../lib/db');
const uuidv4 = require('uuid/v4');
const {generateHashedPassword} = require('../lib/utils/password-utils');
const sqlInsert = `INSERT into users
(user_sid, name, hashed_password, salt)
values (?, ?, ?, ?)
(user_sid, name, email, hashed_password, force_change, provider, email_validated)
values (?, ?, ?, ?, ?, ?, ?)
`;
const sqlChangeAdminToken = `UPDATE api_keys set token = ?
WHERE account_sid IS NULL
AND service_provider_sid IS NULL`;
const sqlQueryAccount = 'SELECT * from accounts LIMIT 1';
const sqlAddAccountAdminToken = `INSERT into api_keys (api_key_sid, token, account_sid)
VALUES (?, ?, ?)`;
/**
* generates random string of characters i.e salt
* @function
* @param {number} length - Length of the random string.
*/
const genRandomString = (len) => {
return crypto.randomBytes(Math.ceil(len / 2))
.toString('hex') /** convert to hexadecimal format */
.slice(0, len); /** return required number of characters */
};
/**
* hash password with sha512.
* @function
* @param {string} password - List of required fields.
* @param {string} salt - Data to be validated.
*/
const sha512 = function(password, salt) {
const hash = crypto.createHmac('sha512', salt); /** Hashing algorithm sha512 */
hash.update(password);
var value = hash.digest('hex');
return {
salt:salt,
passwordHash:value
};
};
const saltHashPassword = (userpassword) => {
var salt = genRandomString(16); /** Gives us salt of length 16 */
return sha512(userpassword, salt);
};
/* reset admin password */
getMysqlConnection((err, conn) => {
if (err) return console.log(err, 'Error connecting to database');
/* delete admin user if it exists */
conn.query('DELETE from users where name = "admin"', (err) => {
if (err) return console.log(err, 'Error removing admin user');
const {salt, passwordHash} = saltHashPassword('admin');
const sid = uuidv4();
conn.query(sqlInsert, [
const doIt = async() => {
const passwordHash = await generateHashedPassword('admin');
const sid = uuidv4();
await promisePool.execute('DELETE from users where name = "admin"');
await promisePool.execute(sqlInsert,
[
sid,
'admin',
'joe@foo.bar',
passwordHash,
salt
], (err) => {
if (err) return console.log(err, 'Error inserting admin user');
console.log('successfully reset admin password');
const uuid = uuidv4();
conn.query(sqlChangeAdminToken, [uuid], (err) => {
if (err) return console.log(err, 'Error updating admin token');
console.log('successfully changed admin tokens');
conn.release();
process.exit(0);
});
});
});
});
1,
'local',
1
]
);
/* reset admin token */
const uuid = uuidv4();
await promisePool.query(sqlChangeAdminToken, [uuid]);
/* create admin token for single account */
const api_key_sid = uuidv4();
const token = uuidv4();
const [r] = await promisePool.query(sqlQueryAccount);
await promisePool.execute(sqlAddAccountAdminToken, [api_key_sid, token, r[0].account_sid]);
process.exit(0);
};
doIt();