From 99b8c31d3a74808f2553adadd8b43f440bd45b2e Mon Sep 17 00:00:00 2001 From: Brian Peacock Date: Thu, 8 May 2014 19:24:41 -0500 Subject: [PATCH] Added the ability to add users --- lib/config.js | 11 ++++++++--- lib/index.js | 11 +++++++---- lib/static/favicon.ico | Bin 0 -> 7094 bytes lib/users.js | 32 ++++++++++++++++++++++++++++++++ 4 files changed, 47 insertions(+), 7 deletions(-) create mode 100644 lib/static/favicon.ico create mode 100644 lib/users.js diff --git a/lib/config.js b/lib/config.js index 9efca141e..77558c9bc 100644 --- a/lib/config.js +++ b/lib/config.js @@ -1,7 +1,8 @@ var assert = require('assert') , crypto = require('crypto') , minimatch = require('minimatch') - , utils = require('./utils') + , users = require('./users') + , utils = require('./utils'); // [[a, [b, c]], d] -> [a, b, c, d] function flatten(array) { @@ -147,8 +148,12 @@ Config.prototype.get_package_setting = function(package, setting) { } Config.prototype.authenticate = function(user, password) { - if (this.users[user] == null) return false - return crypto.createHash('sha1').update(password).digest('hex') === this.users[user].password + if(!users.users[user]) { + return false; + } + else { + return crypto.createHash('sha1').update(password).digest('hex') === users.users[user].password + } } module.exports = Config diff --git a/lib/index.js b/lib/index.js index db907efbd..b3b238f28 100644 --- a/lib/index.js +++ b/lib/index.js @@ -16,6 +16,7 @@ var express = require('express') , localList = require('./local-list') , search = require('./search') , _ = require('underscore') + , users = require('./users') , marked = require('marked'); function match(regexp) { @@ -209,10 +210,12 @@ module.exports = function(config_hash) { }) app.put('/-/user/:org_couchdb_user', function(req, res, next) { - res.status(409) - return res.send({ - error: 'registration is not implemented', - }) + users.add(req.body, function(err) { + res.status(409) + return res.send({ + error: 'registration is not implemented', + }) + }); }) app.put('/-/user/:org_couchdb_user/-rev/*', function(req, res, next) { diff --git a/lib/static/favicon.ico b/lib/static/favicon.ico new file mode 100644 index 0000000000000000000000000000000000000000..9e0d4eef78c9c01027fb8a88af38c17628e34889 GIT binary patch literal 7094 zcmeHLI}XAy40Q@fj1>bj6Y33M%n3LL$KVKv%fQIaDVRA#u@eWiRY0^ARe)zH@h@)N zm$Z^iWGEwR$x$&UTC$YqUf)iLHf$?cwLEq_3{81vnt9fo%d(`^`a*~Ol}^W7(+wR6 z00AHX1b_e#00JLIAgfXP&1&IU>hbk#RWqJOQRf@RVdHf-!-ng@@UuiiKmZ5; z0U#gXT)GJTKs4hWlUmH+?% literal 0 HcmV?d00001 diff --git a/lib/users.js b/lib/users.js new file mode 100644 index 000000000..a19a41b1f --- /dev/null +++ b/lib/users.js @@ -0,0 +1,32 @@ +var fs = require('fs') + , crypto = require('crypto') + , usersPath = './users.json'; + +var Users = function() { + if(fs.existsSync(usersPath)) { + this.users = JSON.parse(fs.readFileSync(usersPath, 'utf8')); + } + else { + this.users = {}; + } +}; + +Users.prototype = { + add: function(params, callback) { + //Hash the Password + params.password = crypto.createHash('sha1').update(params.password).digest('hex'); + + //Save + this.users[params.name] = params; + this.sync(callback); + }, + remove: function(name, callback) { + delete this.users[name]; + this.sync(callback); + }, + sync: function(callback) { + fs.writeFile(usersPath, JSON.stringify(this.users), callback); + } +}; + +module.exports = new Users();