From d1d62be6f23c8812bb3f4d024fec95ace28a55fb Mon Sep 17 00:00:00 2001 From: reality Date: Sat, 27 Jul 2013 17:19:53 +0000 Subject: [PATCH] basic skellington for web login stuff [#538] --- modules/web/strings.js | 5 +++ modules/web/web.js | 71 +++++++++++++++++++++++++++++++++++++++--- 2 files changed, 72 insertions(+), 4 deletions(-) create mode 100644 modules/web/strings.js diff --git a/modules/web/strings.js b/modules/web/strings.js new file mode 100644 index 0000000..9cf6415 --- /dev/null +++ b/modules/web/strings.js @@ -0,0 +1,5 @@ +{ + "web_pass_set": { + "en": "Congratulations, your account is now set up to log into the web interface!" + } +} diff --git a/modules/web/web.js b/modules/web/web.js index 75d3c08..8841947 100644 --- a/modules/web/web.js +++ b/modules/web/web.js @@ -1,6 +1,10 @@ var express = require('express'), + passport = require('passport'), + passHash = require('password-hash'), + flash = require('connect-flash'), _ = require('underscore')._, - fs = require('fs'); + fs = require('fs'), + LocalStrategy = require('passport-local').Strategy; var webInterface = function(dbot) { this.config = dbot.config.modules.web; @@ -9,6 +13,39 @@ var webInterface = function(dbot) { this.app.use(express.static(this.pub)); this.app.set('view engine', 'jade'); + this.app.use(express.cookieParser()); + this.app.use(express.methodOverride()); + this.app.use(express.session({ 'secret': 'wat' })); + this.app.use(flash()); + + this.app.use(passport.initialize()); + this.app.use(passport.session()); + this.app.use(app.router); + + passport.use(new LocalStrategy(function(username, pass, callback) { + var splitUser = username.split('@'), + server = splitUser[1], + username = splitUser[0]; + + dbot.api.users.resolveUser(server, username, function(user) { + if(user) { + this.api.getWebUser(user.id, function(webUser) { + if(webUser) { + var hash = passHash.generate(pass); + if(webUser.password === hash) { + return callback(null, user); + } else { + return callback(null, false, { 'message': 'Incorrect password.' }); + } + } else { + return callback(null, false, { 'message': 'Use ~setwebpass to set up your account for web login.' }); + } + }); + } else { + return callback(null, false, { 'message': 'Unknown user' }); + } + }.bind(this)); + }.bind(this)); var server = this.app.listen(this.config.webPort); @@ -44,15 +81,16 @@ var webInterface = function(dbot) { } }); - console.log(indexModules); - - // TODO: get list of loaded modules this.app.get('/', function(req, res) { res.render('index', { 'name': dbot.config.name, 'routes': indexModules }); }); + + this.app.get('/login', function(req, res) { + + }); }.bind(this); this.onDestroy = function() { @@ -67,6 +105,31 @@ var webInterface = function(dbot) { } else { return 'http://' + this.config.webHost + ':' + this.config.webPort + '/' + path; } + }, + + 'getWebUser': function(id) { + this.db.read('web_users', id, function(err, webUser) { + if(!err) callback(webUser); + }); + } + }; + + this.commands = { + '~setwebpassword': function(event) { + var newPass = event.input[1]; + this.api.getWebUser(event.rUser.id, function(webUser) { + if(!webUser) { + webUser = { + 'id': event.rUser.id, + 'password': false + } + } + webUser.password = passHash.generate(newPass); + + this.db.save('web_users', webUser.id, webUser, function(result) { + event.reply(dbot.t('web_pass_set')); + }); + }); } }; };