aboutsummaryrefslogtreecommitdiffhomepage
path: root/admin
blob: 755680ff910b65f6f34d6647daa943b253e9f9c8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
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 <username>', 'New username')
    .option('-p --password <password>', 'New password')
    .action(addUser);

program.command('user-edit')
    .description('Edit local user')
    .option('-u --username <username>', 'Username')
    .option('-p --password <password>', 'New password')
    .action(editUser);

program.command('user-del')
    .description('Delete local user')
    .option('-u --username <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);
    }
}