mirror of
https://github.com/hardillb/node-red-alexa-home-skill-web.git
synced 2025-12-18 08:10:03 +01:00
Added Mosquitto Auth support
This commit is contained in:
32
index.js
32
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();
|
||||
|
||||
@@ -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);
|
||||
|
||||
11
models/topics.js
Normal file
11
models/topics.js
Normal file
@@ -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);
|
||||
@@ -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",
|
||||
|
||||
@@ -39,6 +39,7 @@
|
||||
<nav class="side-menu">
|
||||
<ul class="nav nav-pills nav-stacked">
|
||||
<li><a href="#createAccount">Create Account</a></li>
|
||||
<li><a href="#linkAccount">Link Account</a></li>
|
||||
<li><a href="#installNodes">Install Nodes</a></li>
|
||||
<li><a href="#configNodes">Configure Nodes</a></li>
|
||||
</ul>
|
||||
@@ -51,6 +52,8 @@
|
||||
<p>Before you start the first thing that needs to be done is to create an account on this site.
|
||||
You can do this by clicking <a href="/newuser">here</a></p>
|
||||
<p></p>
|
||||
<h2 id="linkAccount">Link Account</h2>
|
||||
<p>Once you have an account you need to add the Skill to your Alexa powered device</p>
|
||||
<h2 id="installNodes">Install Nodes</h2>
|
||||
<p>There are serveral ways to install new nodes into Node-RED</p>
|
||||
<p>The simplest is to use the installer built into the Node-RED admin UI.</p>
|
||||
|
||||
@@ -4,6 +4,9 @@
|
||||
<label for="username">Username</label>
|
||||
<input type="text" id="username"/>
|
||||
<br>
|
||||
<label for="email">Email</label>
|
||||
<input type="email" id="email"/>
|
||||
<br>
|
||||
<label for="password">Password:</label>
|
||||
<input type="password" id="password"/>
|
||||
<br>
|
||||
@@ -17,20 +20,21 @@
|
||||
button.onclick = function(){
|
||||
var username = document.getElementById('username').value;
|
||||
var password = document.getElementById('password').value;
|
||||
var passwordAgain = document.getElementById('passwordAgain').value;
|
||||
var passwordAgain = document.getElementById('passwordAgain').value;
|
||||
|
||||
if (password !== passwordAgain) {
|
||||
alert("Passwords don't match");
|
||||
return;
|
||||
}
|
||||
|
||||
var params = "username=" + username + "&password=" + password;
|
||||
if (password !== passwordAgain) {
|
||||
alert("Passwords don't match");
|
||||
return;
|
||||
}
|
||||
var email = document.getElementById('email').value;
|
||||
var params = "username=" + encodeURIComponent(username)
|
||||
+ "&password=" + encodeURIComponent(password)
|
||||
+ "&email=" + encodeURIComponent(email);
|
||||
xhr.open('POST', '/newUser',true);
|
||||
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
|
||||
xhr.onreadystatechange = function () {
|
||||
if( xhr.readyState == 4 && xhr.status == 201) {
|
||||
//new user created
|
||||
//forward to 2FA addition page
|
||||
window.location = '/';
|
||||
} else if (xhr.readyState == 4 && xhr.status == 400) {
|
||||
//show error
|
||||
|
||||
Reference in New Issue
Block a user