Permalink
Browse files

auth.js: added updateUsers and call to process.on('SIGHUP', function(…

…){})

Allows it to reread the htpasswd file whenever a SIGHUP is sent
  • Loading branch information...
phoebesimon committed May 30, 2012
1 parent 718d61a commit b1eefc32caf7c261a30558d9665ad262d0c839b7
Showing with 24 additions and 10 deletions.
  1. +24 −10 lib/web/auth.js
View
@@ -60,6 +60,9 @@ AuthDB.prototype._validateMD5 = function(password, hash) {
};
+AuthDB.prototype.updateUsers = function(users) {
+ this.users = users;
+}
function sha1crypt(password) {
return '{SHA}' + crypto.createHash('sha1').update(password).digest('base64');
@@ -136,20 +139,31 @@ function md5crypt(password, salt, magic) {
exports.loadDBFromFile = function(filePath) {
- var contents = fs.readFileSync(filePath, 'utf8'),
+
+ var getUsers = function(filePath) {
+ var contents = fs.readFileSync(filePath, 'utf8'),
users = {},
lines, both, i;
- lines = contents.split('\n').map(function(line) {
- return line.trim();
- });
+ lines = contents.split('\n').map(function(line) {
+ return line.trim();
+ });
- for (i = 0; i < lines.length; i++) {
- if (lines[i] && lines[i].indexOf('#') !== 0) {
- both = lines[i].split(':');
- users[both[0]] = both[1];
+ for (i = 0; i < lines.length; i++) {
+ if (lines[i] && lines[i].indexOf('#') !== 0) {
+ both = lines[i].split(':');
+ users[both[0]] = both[1];
+ }
}
- }
+ return users;
+ };
+
+ users = getUsers(filePath);
+ myAuthDB = new AuthDB(users);
+ process.on('SIGHUP', function(){
+ users = getUsers(filePath);
+ myAuthDB.updateUsers(users);
+ });
- return new AuthDB(users);
+ return myAuthDB;
};

0 comments on commit b1eefc3

Please sign in to comment.