mirror of
https://github.com/hardillb/node-red-alexa-home-skill-web.git
synced 2025-12-20 17:20:50 +01:00
Fixed Token auth
This commit is contained in:
30
index.js
30
index.js
@@ -1,5 +1,6 @@
|
|||||||
var fs = require('fs');
|
var fs = require('fs');
|
||||||
var url = require('url');
|
var url = require('url');
|
||||||
|
var mqtt = require('mqtt');
|
||||||
var http = require('http');
|
var http = require('http');
|
||||||
var https = require('https');
|
var https = require('https');
|
||||||
var flash = require('connect-flash');
|
var flash = require('connect-flash');
|
||||||
@@ -19,6 +20,25 @@ var port = (process.env.VCAP_APP_PORT || process.env.PORT ||3000);
|
|||||||
var host = (process.env.VCAP_APP_HOST || '0.0.0.0');
|
var host = (process.env.VCAP_APP_HOST || '0.0.0.0');
|
||||||
var mongo_url = (process.env.MONGO_URL || 'mongodb://localhost/users');
|
var mongo_url = (process.env.MONGO_URL || 'mongodb://localhost/users');
|
||||||
|
|
||||||
|
var mqtt_url = (process.env.MQTT_URL || 'mqtt://localhost:1883');
|
||||||
|
var mqtt_user = (process.env.MQTT_USER || undefined);
|
||||||
|
var mqtt_password = (process.env.MQTT_PASSWORD || undefined);
|
||||||
|
console.log(mqtt_url);
|
||||||
|
|
||||||
|
var mqttOptions = {
|
||||||
|
keepAlive: 10,
|
||||||
|
clean: true,
|
||||||
|
clientId: 'webApp_' + Math.random().toString(16).substr(2, 8)
|
||||||
|
};
|
||||||
|
|
||||||
|
if (mqtt_user) {
|
||||||
|
mqttOptions.username = mqtt_user;
|
||||||
|
mqttOptions.password = mqtt_password;
|
||||||
|
}
|
||||||
|
|
||||||
|
var mqttClient = mqtt.connect(mqtt_url, mqttOptions);
|
||||||
|
|
||||||
|
|
||||||
if (process.env.VCAP_SERVICES) {
|
if (process.env.VCAP_SERVICES) {
|
||||||
var services = JSON.parse(process.env.VCAP_SERVICES);
|
var services = JSON.parse(process.env.VCAP_SERVICES);
|
||||||
|
|
||||||
@@ -101,7 +121,7 @@ var accessTokenStrategy = new PassportOAuthBearer(function(token, done) {
|
|||||||
oauthModels.AccessToken.findOne({ token: token }).populate('user').populate('grant').exec(function(error, token) {
|
oauthModels.AccessToken.findOne({ token: token }).populate('user').populate('grant').exec(function(error, token) {
|
||||||
if (token && token.active && token.grant.active && token.user) {
|
if (token && token.active && token.grant.active && token.user) {
|
||||||
done(null, token.user, { scope: token.scope });
|
done(null, token.user, { scope: token.scope });
|
||||||
} else if (!error) {p
|
} else if (!error) {
|
||||||
done(null, false);
|
done(null, false);
|
||||||
} else {
|
} else {
|
||||||
done(error);
|
done(error);
|
||||||
@@ -289,6 +309,14 @@ app.post('/api/v1/command',
|
|||||||
function(req,res,next){
|
function(req,res,next){
|
||||||
console.log(req.user.username);
|
console.log(req.user.username);
|
||||||
console.log(req.body);
|
console.log(req.body);
|
||||||
|
var topic = req.user.username + "/" + req.body.payload.appliance.applianceId;
|
||||||
|
var message = JSON.stringify(req.body);
|
||||||
|
try{
|
||||||
|
mqttClient.publish(topic,message);
|
||||||
|
} catch (err) {
|
||||||
|
|
||||||
|
}
|
||||||
|
res.status(200).send();
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ var GrantCodeSchema = new Schema({
|
|||||||
return uid(24);
|
return uid(24);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
user: { type: Schema.Types.ObjectId, ref: 'User' },
|
user: { type: Schema.Types.ObjectId, ref: 'Account' },
|
||||||
application: { type: Schema.Types.ObjectId, ref: 'Application' },
|
application: { type: Schema.Types.ObjectId, ref: 'Application' },
|
||||||
scope: [ { type: String } ],
|
scope: [ { type: String } ],
|
||||||
active: { type: Boolean, default: true }
|
active: { type: Boolean, default: true }
|
||||||
@@ -29,7 +29,7 @@ var AccessTokenSchema = new Schema({
|
|||||||
return uid(124);
|
return uid(124);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
user: { type: Schema.Types.ObjectId, ref: 'User' },
|
user: { type: Schema.Types.ObjectId, ref: 'Account' },
|
||||||
application: { type: Schema.Types.ObjectId, ref: 'Application' },
|
application: { type: Schema.Types.ObjectId, ref: 'Application' },
|
||||||
grant: { type: Schema.Types.ObjectId, ref: 'GrantCode' },
|
grant: { type: Schema.Types.ObjectId, ref: 'GrantCode' },
|
||||||
scope: [ { type: String }],
|
scope: [ { type: String }],
|
||||||
|
|||||||
@@ -24,6 +24,7 @@
|
|||||||
"mongoose": "^4.6.5",
|
"mongoose": "^4.6.5",
|
||||||
"mongoose-sequence": "^3.1.0",
|
"mongoose-sequence": "^3.1.0",
|
||||||
"morgan": "^1.7.0",
|
"morgan": "^1.7.0",
|
||||||
|
"mqtt": "^2.0.1",
|
||||||
"oauth2orize": "^1.5.1",
|
"oauth2orize": "^1.5.1",
|
||||||
"passport": "^0.3.2",
|
"passport": "^0.3.2",
|
||||||
"passport-http-bearer": "^1.0.1",
|
"passport-http-bearer": "^1.0.1",
|
||||||
|
|||||||
@@ -2,8 +2,8 @@
|
|||||||
<div class="container main-content">
|
<div class="container main-content">
|
||||||
<div id="register">
|
<div id="register">
|
||||||
<label style="width: 75px" for="username">Username:</label>
|
<label style="width: 75px" for="username">Username:</label>
|
||||||
<input type="text" id="username"/>
|
<input type="text" onchange="validate()" id="username"/>
|
||||||
<span>(can contain any utf-8 chars except /, + or #)</span>
|
<span>(can contain any utf-8 chars except /, + or # and must not start with $)</span>
|
||||||
<br>
|
<br>
|
||||||
<label style="width: 75px" for="email">Email:</label>
|
<label style="width: 75px" for="email">Email:</label>
|
||||||
<input type="email" id="email"/>
|
<input type="email" id="email"/>
|
||||||
@@ -45,6 +45,16 @@
|
|||||||
|
|
||||||
xhr.send(params);
|
xhr.send(params);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
function validate() {
|
||||||
|
var data = $('#username').val();
|
||||||
|
console.log(data);
|
||||||
|
if (data.indexOf('/') > 0 || data.indexOf('#') > 0 || data.indexOf('+') > 0) {
|
||||||
|
alert("invalid username");
|
||||||
|
} else if (data.indexOf('$') == 0) {
|
||||||
|
alert("invalid username");
|
||||||
|
}
|
||||||
|
}
|
||||||
</script>
|
</script>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
Reference in New Issue
Block a user