From ef9e976bb908850a183a6614b0a647f95e47c2bd Mon Sep 17 00:00:00 2001 From: Ben Hardill Date: Mon, 31 Oct 2016 22:07:14 +0000 Subject: [PATCH] Added Mosquitto Auth support --- index.js | 32 +++++++++++++++++++++++++------- models/account.js | 6 +++++- models/topics.js | 11 +++++++++++ package.json | 1 + static/docs/docs.html | 3 +++ views/pages/register.ejs | 20 ++++++++++++-------- 6 files changed, 57 insertions(+), 16 deletions(-) create mode 100644 models/topics.js diff --git a/index.js b/index.js index 974df3d..5d8cbf6 100644 --- a/index.js +++ b/index.js @@ -15,10 +15,6 @@ var PassportOAuthBearer = require('passport-http-bearer'); var oauthServer = require('./oauth'); -var Account = require('./models/account'); -var oauthModels = require('./models/oauth'); -var Devices = require('./models/devices'); - var port = (process.env.VCAP_APP_PORT || process.env.PORT ||3000); var host = (process.env.VCAP_APP_HOST || '0.0.0.0'); var mongo_url = (process.env.MONGO_URL || 'mongodb://localhost/users'); @@ -36,6 +32,13 @@ if (process.env.VCAP_SERVICES) { } } +mongoose.connect(mongo_url); + +var Account = require('./models/account'); +var oauthModels = require('./models/oauth'); +var Devices = require('./models/devices'); +var Topics = require('./models/topics'); + var app_id = 'https://localhost:' + port; if (process.env.VCAP_APPLICATION) { @@ -107,8 +110,6 @@ var accessTokenStrategy = new PassportOAuthBearer(function(token, done) { passport.use(accessTokenStrategy); -mongoose.connect(mongo_url); - app.get('/', function(req,res){ res.render('pages/index'); }); @@ -125,12 +126,29 @@ app.get('/newuser', function(req,res){ }); app.post('/newuser', function(req,res){ - Account.register(new Account({ username : req.body.username }), req.body.password, function(err, account) { + Account.register(new Account({ username : req.body.username, email: req.body.email, mqttPass: "foo" }), req.body.password, function(err, account) { if (err) { console.log(err); return res.status(400).send(err.message); } + var topics = new Topics({topics: [account.username+'/#']}); + topics.save(function(err){ + if (!err){ + var s = Buffer.from(account.salt, 'hex').toString('base64'); + var h = Buffer.from(account.hash, 'hex').toString(('base64')); + + var mqttPass = "PBKDF2$sha256$25000$" + s + "$" + h; + + Account.update( + {username: account.username}, + {$set: {mqttPass: mqttPass, topics: topics._id}}, + { multi: false }, + function(err, count){} + ); + } + }); + passport.authenticate('local')(req, res, function () { console.log("created new user %s", req.body.username); res.status(201).send(); diff --git a/models/account.js b/models/account.js index 3577372..9b9ef0e 100644 --- a/models/account.js +++ b/models/account.js @@ -4,7 +4,11 @@ var passportLocalMongoose = require('passport-local-mongoose'); var Account = new Schema({ username: String, - password: String + password: String, + email: String, + mqttPass: { type: String, default: '' }, + superuser: { type: Number, default: 0}, + topics: { type: Number} }); Account.plugin(passportLocalMongoose); diff --git a/models/topics.js b/models/topics.js new file mode 100644 index 0000000..1827cd2 --- /dev/null +++ b/models/topics.js @@ -0,0 +1,11 @@ +var mongoose = require('mongoose'); +var Schema = mongoose.Schema; +var AutoIncrement = require('mongoose-sequence'); + +var Topics = new Schema({ + _id: Number, + topics: {type: [String]} +},{ _id: false }); + +Topics.plugin(AutoIncrement, {inc_field: '_id'}); +module.exports = mongoose.model('Topics', Topics); \ No newline at end of file diff --git a/package.json b/package.json index 41560e4..6b8e06a 100644 --- a/package.json +++ b/package.json @@ -21,6 +21,7 @@ "express": "^4.14.0", "express-session": "^1.14.1", "mongoose": "^4.6.5", + "mongoose-sequence": "^3.1.0", "morgan": "^1.7.0", "oauth2orize": "^1.5.1", "passport": "^0.3.2", diff --git a/static/docs/docs.html b/static/docs/docs.html index 657c5d2..9d9598b 100644 --- a/static/docs/docs.html +++ b/static/docs/docs.html @@ -39,6 +39,7 @@