This repository has been archived by the owner on Feb 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
90 lines (76 loc) · 2.45 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/env node
const mosca = require('mosca');
const Promise = require('promise');
const bcrypt = require('bcryptjs');
const MongoClient = require('mongodb').MongoClient;
// windston logger
// debug file present on /debug
const logger = require('./logger.js');
const settings = require('./settings');
const server = new mosca.Server(settings.mosca);
const mysql = settings.mysql;
// when a client is connected
server.on('clientConnected', function(client) {
logger.info('Client connected: '+ client.id);
});
// when a client is disconnected
server.on('clientDisconnected', function(client) {
logger.info('Client disconnected: '+ client.id);
});
var published = function(packet, client) {
logger.info('Client published: '+ packet.payload);
MongoClient.connect(settings.mongo.url, { useNewUrlParser: true }, function (err, db) {
if (err) throw err;
var dbo = db.db(settings.mongo.db);
// Here we can change the packet
packet.timestamp = Date.now() / 1000 | 0;
dbo.collection(settings.mongo.col).insertOne(packet, function(err, res) {
if (err) throw err;
db.close();
});
});
};
var authenticate = function(client, username, password, callback) {
var promise = new Promise(function(resolve,reject){
mysql.query('SELECT password FROM users WHERE email = ?',[username], function (error, results, fields) {
if(error){
logger.error("MySQL error: "+error);
reject();return;
}
if(results.length>0){
// async method to check the password
(async () =>{
bcrypt.compare(password.toString(), results[0].password.toString(), function(err, res) {
if (err) throw err;
if(res) {
logger.info("User "+username+" logged in...");
resolve();
} else {
// Wrong Password
logger.warn("Wrong password...");
reject();
}
});
})();
} else{
// User doesn't exist
logger.warn("User doesn't exist...");
reject();
}
});
});
promise.then(function(){
// Authenticate
callback(null,true);
}).catch(function(){
// Reject
callback(null,false);
});
};
server.on('ready', function(){
// Override the authenticate function
server.authenticate = authenticate;
// Override the published function
server.published = published;
logger.info('Mosca server is up and running on port: '+settings.mosca.port+'...');
});