From 738b1d82fd22dd0aa455cd6affaae6b2a94ebb41 Mon Sep 17 00:00:00 2001 From: Johannes Date: Thu, 17 Nov 2016 12:51:23 +0100 Subject: Add local user admin tool --- admin | 105 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100755 admin diff --git a/admin b/admin new file mode 100755 index 0000000..755680f --- /dev/null +++ b/admin @@ -0,0 +1,105 @@ +#!/usr/bin/env node + +'use strict'; + +var program = require('commander'), + safe = require('safetydance'), + bcrypt = require('bcryptjs'), + path = require('path'); + +function exit(error) { + if (error) console.error(error); + process.exit(error ? 1 : 0); +} + +var LOCAL_AUTH_FILE = path.resolve(process.env.LOCAL_AUTH_FILE || './.users.json'); + +console.log('Using local auth file: ', LOCAL_AUTH_FILE); + +function addUser(options) { + if (!options.username) exit('missing --username'); + if (!options.password) exit('missing --password'); + + var users = safe.require(LOCAL_AUTH_FILE); + if (!users) users = {}; + if (users[options.username]) exit('User already exists'); + + bcrypt.hash(options.password, 8, function(error, hash) { + users[options.username] = { username: options.username, passwordHash: hash }; + safe.fs.writeFileSync(LOCAL_AUTH_FILE, JSON.stringify(users, null, 4)); + + console.log('Done.'); + }); +} + +function editUser(options) { + if (!options.username) exit('missing --username'); + if (!options.password) exit('missing --password'); + + var users = safe.require(LOCAL_AUTH_FILE); + if (!users) users = {}; + if (!users[options.username]) exit('No such user'); + + bcrypt.hash(options.password, 8, function(error, hash) { + users[options.username] = { username: options.username, passwordHash: hash }; + safe.fs.writeFileSync(LOCAL_AUTH_FILE, JSON.stringify(users, null, 4)); + + console.log('Done.'); + }); +} + +function delUser(options) { + if (!options.username) exit('missing --username'); + + var users = safe.require(LOCAL_AUTH_FILE); + if (!users) exit('No such user'); + + if (!users[options.username]) exit('No such user'); + + delete users[options.username]; + safe.fs.writeFileSync(LOCAL_AUTH_FILE, JSON.stringify(users, null, 4)); + + console.log('Done.'); +} + +function listUsers() { + var users = safe.require(LOCAL_AUTH_FILE); + if (!users) exit('No users. Use `admin user-add` to add some.'); + + console.log(users); +} + +program.version('0.1.0'); + +program.command('user-add') + .description('Add local user') + .option('-u --username ', 'New username') + .option('-p --password ', 'New password') + .action(addUser); + +program.command('user-edit') + .description('Edit local user') + .option('-u --username ', 'Username') + .option('-p --password ', 'New password') + .action(editUser); + +program.command('user-del') + .description('Delete local user') + .option('-u --username ', 'Username') + .action(delUser); + +program.command('users') + .description('List local users') + .action(listUsers); + +program.parse(process.argv); + +if (!process.argv.slice(2).length) { + program.outputHelp(); +} else { // https://github.com/tj/commander.js/issues/338 + var knownCommand = program.commands.some(function (command) { return command._name === process.argv[2]; }); + if (!knownCommand) { + console.error('Unknown command: ' + process.argv[2]); + process.exit(1); + } +} -- cgit v1.2.3