Commit 8b9e2a85 by Chavee Issariyapat

rewrite some functions

parent f78abcd2
...@@ -26,8 +26,6 @@ module.exports = function (options = {}) { ...@@ -26,8 +26,6 @@ module.exports = function (options = {}) {
var cachekey = 'pub:' + req.body.client_id + ':' + req.body.username + ':' + topic; var cachekey = 'pub:' + req.body.client_id + ':' + req.body.username + ':' + topic;
var authstatus = cache.get(cachekey); var authstatus = cache.get(cachekey);
console.log(req.body)
// checkRole // checkRole
var checkRealDB = require('./checkClientRole').checkRealDB var checkRealDB = require('./checkClientRole').checkRealDB
checkRealDB(req.body.username, (err, decoded) => { checkRealDB(req.body.username, (err, decoded) => {
...@@ -35,34 +33,41 @@ module.exports = function (options = {}) { ...@@ -35,34 +33,41 @@ module.exports = function (options = {}) {
// Set Response // Set Response
var isShadow = require('./utils/getTopic').isShadow var isShadow = require('./utils/getTopic').isShadow
if (isShadow(topic)) { if (isShadow(topic)) {
var _ftopic = require('./utils/getTopic').getTopic(topic, req.body.client_id) // get topic where concat with groupID var getGroupID = require('./utils/getGroupID').getGroupID
var response = { getGroupID(req.body.username, req.body.client_id, (_id) => { // get groupID
'result': 'ok', var _ftopic = require('./utils/getTopic').rewriteTopic(topic, 'pub', _id, req.body.client_id) // get topic where concat with groupID
'modifiers': { var response = {
'topic': _ftopic, 'result': 'ok',
'qos': 0, 'modifiers': {
'retain': false 'topic': _ftopic,
'qos': 0,
'retain': false
}
} }
} console.log(response)
console.log(response)
if (typeof (authstatus) == 'undefined') { if (typeof (authstatus) == 'undefined') {
// cache missed // cache missed
authCheck(req.body.client_id, req.body.username, topic, function (result) { authCheck(req.body.client_id, req.body.username, topic, function (result) {
cache.set(cachekey, result); cache.set(cachekey, result);
res.send(response);
});
}
else {
res.send(response); res.send(response);
}); }
} next();
else {
res.send(response);
} })
next();
} }
else { else {
var getGroupID = require('./utils/getGroupID').getGroupID var getGroupID = require('./utils/getGroupID').getGroupID
getGroupID(req.body.username, req.body.client_id, (_id) => { // get groupID getGroupID(req.body.username, req.body.client_id, (_id) => { // get groupID
// console.log(_id) // console.log(_id)
var _ftopic = require('./utils/getTopic').getTopic(topic, _id) // get topic where concat with groupID var _ftopic = require('./utils/getTopic').rewriteTopic(topic, 'pub',_id, req.body.client_id) // get topic where concat with groupID
var response = { var response = {
'result': 'ok', 'result': 'ok',
'modifiers': { 'modifiers': {
......
...@@ -31,8 +31,6 @@ module.exports = function (options = {}) { ...@@ -31,8 +31,6 @@ module.exports = function (options = {}) {
var cachekey = 'sub:' + req.body.client_id + ':' + req.body.username + ':' + topic; var cachekey = 'sub:' + req.body.client_id + ':' + req.body.username + ':' + topic;
var authstatus = cache.get(cachekey); var authstatus = cache.get(cachekey);
console.log(req.body)
// checkRole // checkRole
var checkRealDB = require('./checkClientRole').checkRealDB var checkRealDB = require('./checkClientRole').checkRealDB
checkRealDB(req.body.username, (err, decoded) => { checkRealDB(req.body.username, (err, decoded) => {
...@@ -40,34 +38,40 @@ module.exports = function (options = {}) { ...@@ -40,34 +38,40 @@ module.exports = function (options = {}) {
// Set Response // Set Response
var isShadow = require('./utils/getTopic').isShadow var isShadow = require('./utils/getTopic').isShadow
if (isShadow(topic)) { if (isShadow(topic)) {
var _ftopic = require('./utils/getTopic').getTopic(topic, req.body.client_id) // get topic where concat with groupID
var _topic = [{ // setTopic for response
'topic': _ftopic,
'qos': 0
}]
var response = {
'result': 'ok',
'topics': _topic
}
console.log(response)
if (typeof (authstatus) == 'undefined') { var getGroupID = require('./utils/getGroupID').getGroupID
// cache missed getGroupID(req.body.username, req.body.client_id, (_id) => { // get groupID
authCheck(req.body.client_id, req.body.username, topic, function (result) { var _ftopic = require('./utils/getTopic').rewriteTopic(topic, 'sub', _id, req.body.client_id) // get topic where concat with groupID
cache.set(cachekey, result);
var _topic = [{ // setTopic for response
'topic': _ftopic,
'qos': 0
}]
var response = {
'result': 'ok',
'topics': _topic
}
console.log(response)
if (typeof (authstatus) == 'undefined') {
// cache missed
authCheck(req.body.client_id, req.body.username, topic, function (result) {
cache.set(cachekey, result);
res.send(response);
});
}
else {
res.send(response); res.send(response);
}); }
} next();
else {
res.send(response); })
}
next();
} }
else { else {
var getGroupID = require('./utils/getGroupID').getGroupID var getGroupID = require('./utils/getGroupID').getGroupID
getGroupID(req.body.username, req.body.client_id, (_id) => { // get groupID getGroupID(req.body.username, req.body.client_id, (_id) => { // get groupID
// console.log(_id) // console.log(_id)
var _ftopic = require('./utils/getTopic').getTopic(topic, _id) // get topic where concat with groupID var _ftopic = require('./utils/getTopic').rewriteTopic(topic, 'sub', _id, req.body.client_id) // get topic where concat with groupID
var _topic = [{ // setTopic for response var _topic = [{ // setTopic for response
'topic': _ftopic, 'topic': _ftopic,
'qos': 0 'qos': 0
...@@ -93,7 +97,9 @@ module.exports = function (options = {}) { ...@@ -93,7 +97,9 @@ module.exports = function (options = {}) {
} }
} }
else { else {
console.log('>>>>>>>>>>>>>>>>>>>')
console.log(decoded) console.log(decoded)
console.log('>>>>>>>>>>>>>>>>>>>')
if (decoded.role === 'realtimedb') { if (decoded.role === 'realtimedb') {
var response = { var response = {
'result': 'ok' 'result': 'ok'
......
...@@ -2,9 +2,9 @@ process.env["NODE_ENV"] = "development"; ...@@ -2,9 +2,9 @@ process.env["NODE_ENV"] = "development";
process.env["NODE_CONFIG_DIR"] = __dirname + "/config/"; process.env["NODE_CONFIG_DIR"] = __dirname + "/config/";
var auth_on_register_debug = process.env["AUTH_ON_REGISTER_DEBUG"] || false; var auth_on_register_debug = process.env["AUTH_ON_REGISTER_DEBUG"] || false;
var auth_on_publish_debug = process.env["AUTH_ON_PUBLISH_DEBUG"] || false; var auth_on_publish_debug = process.env["AUTH_ON_PUBLISH_DEBUG"] || true;
var auth_on_subscribe_debug = process.env["AUTH_ON_SUBSCRIBE_DEBUG"] || false; var auth_on_subscribe_debug = process.env["AUTH_ON_SUBSCRIBE_DEBUG"] || true;
var on_publish_debug = process.env["ON_PUBLISH_DEBUG"] || false; var on_publish_debug = process.env["ON_PUBLISH_DEBUG"] || true;
var restify = require('restify'); var restify = require('restify');
var auth_on_register = require('./auth_on_register')({ debug: auth_on_register_debug }); var auth_on_register = require('./auth_on_register')({ debug: auth_on_register_debug });
......
var modify = require('./utils/modify');
function on_deliver(req, res, next) { function on_deliver(req, res, next) {
var topics = beDesireTopic(req.body.topic)
console.log('----on_deliver-----') console.log('----on_deliver-----')
console.log(req.body) var topics = modify.removeTempWordInTopic(req.body.topic)
console.log(topics) console.log(req.body)
// console.log(topics)
var response = { var response = {
'result': 'ok', 'result': 'ok',
'modifiers': { 'modifiers': {
"topic": topics "topic": topics
} }
} }
console.log(response);
res.send(response) res.send(response)
} }
module.exports.on_deliver = on_deliver module.exports.on_deliver = on_deliver
...@@ -21,13 +24,12 @@ function hasGroup(topics) { ...@@ -21,13 +24,12 @@ function hasGroup(topics) {
} }
module.exports.hasGroup = hasGroup module.exports.hasGroup = hasGroup
function beDesireTopic(topics) { // function beDesireTopic(topics) {
if (hasGroup(topics)) { // if (hasGroup(topics)) {
return joinTopic(topics) // return joinTopic(topics)
} // }
else return topics // else return topics
} // }
module.exports.beDesireTopic = beDesireTopic
function joinTopic(topics) { function joinTopic(topics) {
var topic = topics.split('/!')[1] var topic = topics.split('/!')[1]
......
...@@ -10,7 +10,7 @@ function on_unsubscribe(req, res, next) { ...@@ -10,7 +10,7 @@ function on_unsubscribe(req, res, next) {
var getGroupID = require('./utils/getGroupID').getGroupID var getGroupID = require('./utils/getGroupID').getGroupID
getGroupID(req.body.username, req.body.client_id, (_id) => { // get groupID getGroupID(req.body.username, req.body.client_id, (_id) => { // get groupID
// console.log(_id) // console.log(_id)
var _ftopic = require('./utils/getTopic').getTopic(topic, _id) // get topic where concat with groupID var _ftopic = require('./utils/getTopic').rewriteTopic(topic, 'unsub', _id, req.body.client_id) // get topic where concat with groupID
var response = { var response = {
"result": "ok", "result": "ok",
"topics": [_ftopic] "topics": [_ftopic]
......
function getTopic(topic, id) { // function getTopic(topic, id) {
var _topic = classifiedTopic(topic) + '/!' + id + topic.split(classifiedTopic(topic))[1] // //var _topic = classifiedTopic(topic) + '/!' + id + topic.split(classifiedTopic(topic))[1]
return _topic // var _topic = topicPrefix(topic) + id + topic.split(topicPrefix(topic))[1]
} // return _topic
module.exports.getTopic = getTopic // }
// module.exports.getTopic = getTopic
function classifiedTopic(topic) {
if (topic.indexOf('@msg') !== -1) { // function topicPrefix(topic) {
return '@msg' // if (topic.indexOf('@msg') !== -1) {
} else if (topic.indexOf('@shadow/updated') !== -1) { // return '@msg/!'
return '@shadow/updated' // } else if (topic.indexOf('@shadow/updated') !== -1) {
} else if (topic.indexOf('@shadow/update') !== -1) { // return '@shadow/updated/!'
return '@shadow/update' // } else if (topic.indexOf('@shadow/update') !== -1) {
} else if (topic.indexOf('@shadow/get') !== -1) { // return '@shadow/update/'
return '@shadow/get' // } else if (topic.indexOf('@shadow/get') !== -1) {
} else if (topic.indexOf('@client') !== -1) { // return '@shadow/get/!'
return '@client' // } else if (topic.indexOf('@client') !== -1) {
// return '@client/'
// }
// }
var modify = require('./modify');
function rewriteTopic(topic, op, groupid, clientid) {
var chunk = topic.split('/', 2);
switch (chunk[0]) {
case '@msg' :
return modify.insertWordIntoTopic(topic, '!'+clientid, 1);
case '@shadow' :
if (chunk[1] == 'write')
return modify.insertWordIntoTopic(topic, ''+clientid, 2);
else if (chunk[1] == 'set')
return modify.insertWordIntoTopic(topic, ''+groupid, 2);
else if (chunk[1] == 'changed') {
if (op == 'pub') {
if ( chunk.length > 2 )
return modify.insertWordIntoTopic(topic, '!'+groupid, 2);
else if ( chunk.length == 2 )
return modify.insertWordIntoTopic(topic, '!'+clientid, 2);
else return "";
}
else {
if ( chunk.length > 2 )
return modify.insertWordIntoTopic(topic, '!'+groupid, 2);
else if ( chunk.length == 2 )
return modify.insertWordIntoTopic(topic, '!'+clientid, 2);
else return "";
}
}
else return "";
case '@private' :
return modify.insertWordIntoTopic(topic, '!'+clientid, 1);
} }
} }
module.exports.rewriteTopic = rewriteTopic
function isShadow(topic) { function isShadow(topic) {
if (topic.indexOf('@shadow') !== -1) { if (topic.indexOf('@shadow') !== -1) {
return true return true
......
module.exports.insertWordIntoTopic = insertWordIntoTopic
module.exports.removeTempWordInTopic = removeTempWordInTopic
/*
Ex: insertWordIntoTopic('@shadow/home/temp', '!GROUP', 1)
expected return : "@shadow/!GROUP/home/temp"
*/
function insertWordIntoTopic(topic, word, pos) {
var a = topic.split('/');
a.splice(pos, 0, word);
// console.log('>>> insertWordIntoTopic('+topic +','+ word +','+ pos +');');
// console.log(a.join('/'));
return a.join('/');
}
/*
Ex : removeTempWordInTopic('@shadow/!GROUP/home/temp')
expected return : "@shadowhome/temp"
*/
function removeTempWordInTopic(topic) {
var a = topic.split('/');
var k=0, l=a.length;
while (k<l) {
if (a[k].substr(0,1)=='!') {
a.splice(k,1);
l--;
}
else k++;
}
return a.join('/');
}
// Sample
// var str = insertWordIntoTopic('@shadow/home/temp', '!GROUP', 0);
// console.log(str);
// console.log(removeTempWordInTopic(str));
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment