aboutsummaryrefslogtreecommitdiffhomepage
path: root/admin
diff options
context:
space:
mode:
authorJohannes <johannes@cloudron.io>2016-11-17 12:51:23 +0100
committerJohannes <johannes@cloudron.io>2016-11-17 12:51:23 +0100
commit738b1d82fd22dd0aa455cd6affaae6b2a94ebb41 (patch)
tree3eca4268365bc948d13f6b737fcae0838f2047ba /admin
parentdcb2086608d38a8c5c17fcdbd7ec97155aeb9cc0 (diff)
downloadSurfer-738b1d82fd22dd0aa455cd6affaae6b2a94ebb41.tar.gz
Surfer-738b1d82fd22dd0aa455cd6affaae6b2a94ebb41.tar.zst
Surfer-738b1d82fd22dd0aa455cd6affaae6b2a94ebb41.zip
Add local user admin tool
Diffstat (limited to 'admin')
-rwxr-xr-xadmin105
1 files changed, 105 insertions, 0 deletions
diff --git a/admin b/admin
new file mode 100755
index 0000000..755680f
--- /dev/null
+++ b/admin
@@ -0,0 +1,105 @@
1#!/usr/bin/env node
2
3'use strict';
4
5var program = require('commander'),
6 safe = require('safetydance'),
7 bcrypt = require('bcryptjs'),
8 path = require('path');
9
10function exit(error) {
11 if (error) console.error(error);
12 process.exit(error ? 1 : 0);
13}
14
15var LOCAL_AUTH_FILE = path.resolve(process.env.LOCAL_AUTH_FILE || './.users.json');
16
17console.log('Using local auth file: ', LOCAL_AUTH_FILE);
18
19function addUser(options) {
20 if (!options.username) exit('missing --username');
21 if (!options.password) exit('missing --password');
22
23 var users = safe.require(LOCAL_AUTH_FILE);
24 if (!users) users = {};
25 if (users[options.username]) exit('User already exists');
26
27 bcrypt.hash(options.password, 8, function(error, hash) {
28 users[options.username] = { username: options.username, passwordHash: hash };
29 safe.fs.writeFileSync(LOCAL_AUTH_FILE, JSON.stringify(users, null, 4));
30
31 console.log('Done.');
32 });
33}
34
35function editUser(options) {
36 if (!options.username) exit('missing --username');
37 if (!options.password) exit('missing --password');
38
39 var users = safe.require(LOCAL_AUTH_FILE);
40 if (!users) users = {};
41 if (!users[options.username]) exit('No such user');
42
43 bcrypt.hash(options.password, 8, function(error, hash) {
44 users[options.username] = { username: options.username, passwordHash: hash };
45 safe.fs.writeFileSync(LOCAL_AUTH_FILE, JSON.stringify(users, null, 4));
46
47 console.log('Done.');
48 });
49}
50
51function delUser(options) {
52 if (!options.username) exit('missing --username');
53
54 var users = safe.require(LOCAL_AUTH_FILE);
55 if (!users) exit('No such user');
56
57 if (!users[options.username]) exit('No such user');
58
59 delete users[options.username];
60 safe.fs.writeFileSync(LOCAL_AUTH_FILE, JSON.stringify(users, null, 4));
61
62 console.log('Done.');
63}
64
65function listUsers() {
66 var users = safe.require(LOCAL_AUTH_FILE);
67 if (!users) exit('No users. Use `admin user-add` to add some.');
68
69 console.log(users);
70}
71
72program.version('0.1.0');
73
74program.command('user-add')
75 .description('Add local user')
76 .option('-u --username <username>', 'New username')
77 .option('-p --password <password>', 'New password')
78 .action(addUser);
79
80program.command('user-edit')
81 .description('Edit local user')
82 .option('-u --username <username>', 'Username')
83 .option('-p --password <password>', 'New password')
84 .action(editUser);
85
86program.command('user-del')
87 .description('Delete local user')
88 .option('-u --username <username>', 'Username')
89 .action(delUser);
90
91program.command('users')
92 .description('List local users')
93 .action(listUsers);
94
95program.parse(process.argv);
96
97if (!process.argv.slice(2).length) {
98 program.outputHelp();
99} else { // https://github.com/tj/commander.js/issues/338
100 var knownCommand = program.commands.some(function (command) { return command._name === process.argv[2]; });
101 if (!knownCommand) {
102 console.error('Unknown command: ' + process.argv[2]);
103 process.exit(1);
104 }
105}