mirror of
https://github.com/jambonz/jambonz-api-server.git
synced 2026-07-24 13:02:16 +00:00
add script to initialize/reset admin password to default
This commit is contained in:
Executable
+62
@@ -0,0 +1,62 @@
|
||||
#!/usr/bin/env node
|
||||
const {getMysqlConnection} = require('../lib/db');
|
||||
const crypto = require('crypto');
|
||||
const uuidv4 = require('uuid/v4');
|
||||
const sqlInsert = `INSERT into users
|
||||
(user_sid, name, hashed_password, salt)
|
||||
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, [
|
||||
sid,
|
||||
'admin',
|
||||
passwordHash,
|
||||
salt
|
||||
], (err) => {
|
||||
if (err) return console.log(err, 'Error inserting admin user');
|
||||
console.log('successfully reset admin password');
|
||||
conn.release();
|
||||
process.exit(0);
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user