-
Notifications
You must be signed in to change notification settings - Fork 0
/
userdb.js
35 lines (28 loc) · 1014 Bytes
/
userdb.js
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
var hash = require('./hash.js');
var fs = require('fs'), ini = require('ini');
var defaultCfgPath = './usrperm.cfg';
exports.usrExits = function (name) {
var cfgtxt = fs.readFileSync(defaultCfgPath, 'utf-8');
var config = ini.parse(cfgtxt);
return config.hasOwnProperty(name);
};
exports.updateUsrPass = function (name, passwd) {
fs.closeSync(fs.openSync(defaultCfgPath, 'a')) // touch
var cfgtxt = fs.readFileSync(defaultCfgPath, 'utf-8');
var config = ini.parse(cfgtxt);
var salt = hash.genSalt();
if (config[name] == undefined) {
console.log('username does not exist, creating...');
config[name] = {};
}
config[name]['hash'] = hash.hashPasswd(name, passwd, salt);
config[name]['salt'] = salt;
config[name]['perm'] = ['default'];
fs.writeFileSync(defaultCfgPath, ini.stringify(config));
};
exports.getUser = function (name) {
var cfgtxt = fs.readFileSync(defaultCfgPath, 'utf-8');
if (!exports.usrExits(name))
throw Error('User does not exists.');
return ini.parse(cfgtxt)[name];
};