Permalink
Browse files

Added couch facade, now connects to couch server specified by ENV var…

…s - issue still with delete
  • Loading branch information...
1 parent e0c10dc commit e8889ba3b4665b6a1ebed12a84000b1ce5704e04 @cliftonc committed Mar 12, 2011
Showing with 153 additions and 31 deletions.
  1. +8 −0 dummyuser.json
  2. +3 −0 lib/base64.js
  3. +81 −0 lib/couch.facade.js
  4. +2 −2 lib/emailer.js
  5. +7 −2 lib/token.registry.js
  6. +52 −27 server.js
View
@@ -0,0 +1,8 @@
+{
+ "_id": "org.couchdb.user:test",
+ "name": "test",
+ "password_sha": "24e8e07c23d8ae85108468ec4814b2f0fa84edde",
+ "salt": "78f67e252351a56d6e1e6df9ba005239",
+ "roles": [],
+ "type": "user"
+}
View
@@ -0,0 +1,3 @@
+
+exports.encode = function (str) { return new Buffer(str).toString("base64") }
+exports.decode = function (str) { return new Buffer(str, "base64").toString() }
View
@@ -0,0 +1,81 @@
+/**
+ * Facade for CouchDB rest calls for user, only implement what is requried
+ *
+ * Configuration from ENV
+ * COUCH_HOST=localhost
+ * COUCH_PORT=5984
+ * USERDB=_users
+ * ADMIN=adminuser:adminpass
+ */
+
+var base64 = require('./base64'), http = require('http');
+
+exports.validateUser = function(username,callback) {
+
+ /*
+ * Construct a simple http get based on the env params, assume 'normal' CouchDB Setup?
+ */
+ var options = http.request({ host: process.env.COUCH_HOST,
+ port: process.env.COUCH_PORT,
+ path: "/" + process.env.USERDB + "/org.couchdb.user:" + username,
+ method: "GET"
+ });
+
+ var req = http.request(options, function(res) {
+ res.on('data', function (chunk) {
+ var data = JSON.parse(chunk);
+ if(res.statusCode == 200) {
+ if(data.error) {
+ // We got an error for some reason
+ callback(false);
+ } else {
+ callback(true,data._rev);
+ }
+ } else {
+ callback(false);
+ }
+
+ });
+ });
+
+ req.end();
+
+};
+
+exports.deleteUser = function(token,callback) {
+
+ /*
+ * Construct a simple http get based on the env params, assume 'normal' CouchDB Setup?
+ */
+ var options = http.request({
+ host: process.env.COUCH_HOST,
+ port: process.env.COUCH_PORT,
+ path: "/" + process.env.USERDB + "/org.couchdb.user:" + token.username + '?rev=' + token.revision,
+ headers: {'authorization':'Basic ' + base64.encode(process.env.ADMIN)},
+ method: "DELETE"
+ });
+
+ console.log(options);
+
+ /**
+ * Make the request
+ */
+ var req = http.request(options, function(res) {
+ res.on('data', function (chunk) {
+
+ console.log("Data: " + chunk);
+ if(chunk) {
+ if(JSON.parse(chunk).error) {
+ callback(false);
+ } else {
+ callback(res.statusCode == 200 ? true : false);
+ }
+ } else {
+ callback(res.statusCode == 200 ? true : false);
+ }
+ });
+ });
+
+ req.end();
+
+};
View
@@ -13,8 +13,8 @@ exports.Mailer = {
port: 465,
ssl: true,
use_authentication: true,
- user: "<USER>@gmail.com",
- pass: "<PASSWORD"
+ user: "clifton.cunningham@gmail.com",
+ pass: "f0rtr3ss"
}
email.send_mail({
@@ -10,13 +10,18 @@ exports.TokenRegistry = {
createToken: function(username, email, state, expires) {
var token = uuid.generate();
- this._currentTokens[token] = { username: username, email: email, expires: expires, state: state };
+ this._currentTokens[token] = { username: username, email: email, expires: expires, state: state, revision: '' };
return token;
},
removeToken: function(token) {
if (token in this._currentTokens)
delete this._currentTokens[token];
},
+ setRevision: function(token, revision) {
+ if (token in this._currentTokens) {
+ this._currentTokens[token].revision = revision;
+ }
+ },
setState: function(token, state) {
if (token in this._currentTokens) {
this._currentTokens[token].state = state;
@@ -30,7 +35,7 @@ exports.TokenRegistry = {
getCurrent: function() {
var ret = {};
for (var idx in this._currentTokens) {
- ret[idx] = ({ username: this._currentTokens[idx].username, email: this._currentTokens[idx].email, expires: this._currentTokens[idx].expires, state: this._currentTokens[idx].state });
+ ret[idx] = ({ username: this._currentTokens[idx].username, email: this._currentTokens[idx].email, expires: this._currentTokens[idx].expires, state: this._currentTokens[idx].state, revision: this._currentTokens[idx].revision });
}
return ret;
},
View
@@ -10,11 +10,18 @@ var express = require('express'),
validatorMixin = require('./lib/validator.mixin'),
mailer = require('./lib/emailer').Mailer,
check = require('validator').check,
- sanitize = require('validator').sanitize;
+ sanitize = require('validator').sanitize,
+ couch = require('./lib/couch.facade');
/**
* Initial configuration of the Express server
- */
+ *
+ * Configuration from ENV
+ * COUCH=localhost:5984
+ * USERDB=_users
+ * ADMIN=adminuser:adminpass
+ *
+ **/
var app = express.createServer();
app.use(express.bodyParser());
app.use(express.methodOverride());
@@ -48,7 +55,7 @@ app.configure('development', function() {
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
app.set('baseUrl','localhost:3000');
- app.set('helpEmail','<EMAIL>@gmail.com');
+ app.set('helpEmail','clifton.cunningham@gmail.com');
// Enable list in dev mode
app.get('/list', function(req,res,next) {
@@ -62,7 +69,7 @@ app.configure('development', function() {
*/
app.configure('production', function() {
app.set('baseUrl','localhost:3000');
- app.set('helpEmail','<EMAIL>@gmail.com');
+ app.set('helpEmail','clifton.cunningham@gmail.com');
app.use(express.errorHandler({ dumpExceptions: false, showStack: false }));
});
@@ -107,25 +114,40 @@ app.post('/reset', function(req,res,next) {
* TODO: WE SHOULD MAKE SURE THE USER IS IN NPM AT THIS POINT
* account details are in token.username / token.email
*/
+ couch.validateUser(req.params.username,function(isValid,revision) {
+
+ if(isValid) {
+
+ console.log("Revision: " + revision);
+
+ // We need to store the revision to delete it
+ tokenRegistry.setRevision(tokenId,revision);
+
+ // SMTP settings for the emailer are in lib/emailer.js
+ mailer.sendMail({
+ tokenId:tokenId,
+ email:req.params.email,
+ username:req.params.username,
+ baseUrl:app.set('baseUrl'),
+ helpEmail:app.set('helpEmail')
+ }, function(err,result) {
+ if(!err) {
+ responseData = {message:'Your request has been submitted, if your details are valid you will receive an email with further instructions.'};
+ res.render("reset",{locals:responseData});
+ } else {
+ responseData = {message:'There was a problem sending you an email:<br/><pre class="code">' + err + '</pre>This is probably because we have misconfigured something on the backend, please try again later.'};
+ res.render("reset",{locals:responseData});
+ }
+ });
+
+ } else {
+ responseData = {message:'That username was not found in the repository!'};
+ res.render("reset",{locals:responseData});
+ }
+
+ });
+
- // SMTP settings for the emailer are in lib/emailer.js
- mailer.sendMail({
- tokenId:tokenId,
- email:req.params.email,
- username:req.params.username,
- baseUrl:app.set('baseUrl'),
- helpEmail:app.set('helpEmail')
- }, function(err,result) {
-
- if(!err) {
- responseData = {message:'Your request has been submitted, if your details are valid you will receive an email with further instructions.'};
- res.render("reset",{locals:responseData});
- } else {
- responseData = {message:'There was a problem sending you an email:<br/><pre class="code">' + err + '</pre>This is probably because we have misconfigured something on the backend, please try again later.'};
- res.render("reset",{locals:responseData});
- }
-
- });
@@ -138,7 +160,6 @@ app.post('/reset', function(req,res,next) {
});
-
/**
* Confirmation via the email link
*/
@@ -152,13 +173,17 @@ app.get('/confirm/:tokenId', function(req,res,next) {
* TODO: IF YOU GET TO THIS POINT YOU CAN NOW RESET THE ACCOUNT
* account details are in token.username / token.email
*/
-
- // Clean up
- tokenRegistry.removeToken(req.params.tokenId);
-
+ couch.deleteUser(token,function(isValid) {
+ if(isValid) {
+ tokenRegistry.removeToken(req.params.tokenId);
+ } else {
+ err = {message:'Unable to delete the user from the repository'};
+ }
+ });
};
res.render("confirm",{locals:{err:err,token:token}});
+
});
});

0 comments on commit e8889ba

Please sign in to comment.