diff --git a/db/reset_admin_password.js b/db/reset_admin_password.js new file mode 100755 index 0000000..81d5608 --- /dev/null +++ b/db/reset_admin_password.js @@ -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); + }); + }); +});