Commit 80fff711 by zCaesar

refractor code 1st time

parent 61bbce57
...@@ -7,9 +7,9 @@ var LRU = require("lru-cache"), ...@@ -7,9 +7,9 @@ var LRU = require("lru-cache"),
}); });
// for testing // for testing
function authCheck(client_id, username, topic, callback) { // function authCheck(client_id, username, topic, callback) {
callback(true); // callback(true);
} // }
module.exports = function (options = {}) { module.exports = function (options = {}) {
return function (req, res, next) { return function (req, res, next) {
...@@ -25,106 +25,41 @@ module.exports = function (options = {}) { ...@@ -25,106 +25,41 @@ 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);
if (typeof (authstatus) === 'undefined') {
cache.set(cachekey, true); // cache missed
}
// checkRole var response
var checkRealDB = require('./checkClientRole').checkRealDB;
checkRealDB(req.body.username, (err, decoded) => {
if (err) {
// Set Response
var isShadow = require('./utils/getTopic').isShadow
if (isShadow(topic)) {
var output = {};
var getGroupID = require('./utils/getGroupID').getGroupID
getGroupID(req.body.username, req.body.client_id, (_id) => { // get groupID
var _ftopic = require('./utils/getTopic').rewriteTopic(topic, 'pub', _id, req.body.client_id, output); // get topic where concat with groupID
var response = {
'result': 'ok',
'modifiers': {
'topic': _ftopic,
'qos': 0,
'retain': false
}
}
if (output.verb == 'get' || output.verb == 'read') {
response.modifiers.payload = Buffer.from(req.body.client_id).toString('base64');
}
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);
}
next();
})
}
else {
var getGroupID = require('./utils/getGroupID').getGroupID
getGroupID(req.body.username, req.body.client_id, (_id) => { // get groupID
// console.log(_id)
var _ftopic = require('./utils/getTopic').rewriteTopic(topic, 'pub',_id, req.body.client_id) // get topic where concat with groupID
var response = {
'result': 'ok',
'modifiers': {
'topic': _ftopic,
'qos': 0,
'retain': false
}
}
console.log(response)
if (typeof (authstatus) == 'undefined') { var decoded = require('./checkClientRole').checkRealDB(req.body.username)
// cache missed if (decoded) {
authCheck(req.body.client_id, req.body.username, topic, function (result) { if (decoded.role === 'realtimedb') response = { 'result': 'ok' }
cache.set(cachekey, result); else response = { 'result': 'no' }
res.send(response); res.send(response);
}); next();
} }
else { else {
res.send(response); var output = {};
} var _id = require('./utils/getGroupID').getGroupID(req.body.username, req.body.client_id)
next(); var _ftopic = require('./utils/getTopic').rewriteTopic(topic, 'pub', _id, req.body.client_id, output); // get topic where concat with groupID
}) response = {
'result': 'ok',
'modifiers': {
'topic': _ftopic,
'qos': 0,
'retain': false
} }
} }
else { if (output.verb == 'get' || output.verb == 'read') {
console.log(decoded) response.modifiers.payload = Buffer.from(req.body.client_id).toString('base64');
if (decoded.role === 'realtimedb') {
var response = {
'result': 'ok'
}
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);
}
next();
}
} }
}) res.send(response);
next();
}
} }
else { else {
res.send({ res.send({ "result": "no" });
"result": "no"
});
next(); next();
} }
} }
} }
\ No newline at end of file
var config = require('config'); var config = require('config');
var checkRealDB = require('./checkClientRole').checkRealDB
var getRole = require('./checkClientRole').getRole
// https://github.com/isaacs/node-lru-cache // https://github.com/isaacs/node-lru-cache
var LRU = require("lru-cache"), var LRU = require("lru-cache"),
...@@ -10,54 +8,41 @@ var LRU = require("lru-cache"), ...@@ -10,54 +8,41 @@ var LRU = require("lru-cache"),
}); });
var debug = false; var debug = false;
var authclient = require('seneca')({log: 'silent'}) var authclient = require('seneca')({ log: 'silent' })
.client({port: config.get('authserv_port'), host: config.get('authserv_host')}); .client({ port: config.get('authserv_port'), host: config.get('authserv_host') });
function authCheck(client_id, token, password, callback) { function authCheck(client_id, token, password, callback) {
checkRealDB(token, (err, decoded) => { if (require('./checkClientRole').checkRealDB(token)) { // auth realtimedb by token
/* callback(true)
* decode.scope }
* decode.iat else {
* decode.exp if (require('./checkClientRole').getRole(token)) callback(true) // auth client device by token
*/ else {
if (err) { authclient.act({ role: 'auth', cmd: 'token', action: 'info', token: token }, function (err, res) { // auth client device by query from db
if (debug) {
getRole(token, (role) => { console.log("res ------>\n");
if (role === false) { console.log(res);
}
authclient.act({role:'auth',cmd:'token',action:'info',token: token}, function(err, res) {
if (debug) {
console.log("res ------>\n");
console.log(res);
}
if (res && res.data) { if (res && res.data) {
try { try {
var jdata = JSON.parse(res.data); var jdata = JSON.parse(res.data);
if (jdata && jdata.code == 200) { if (jdata && jdata.code == 200) {
callback(true);
}
else callback(false);
} catch (e) {
callback(false);
}
callback(true); callback(true);
} }
else { else callback(false);
callback(false); } catch (e) {
} callback(false);
}); }
callback(true);
} }
else callback(true) else {
}) callback(false);
} }
else { });
callback(true)
} }
}
})
} }
module.exports = function (options = {}) { module.exports = function (options = {}) {
......
...@@ -9,9 +9,9 @@ var LRU = require("lru-cache"), ...@@ -9,9 +9,9 @@ var LRU = require("lru-cache"),
var debug = false; var debug = false;
// for testing // for testing
function authCheck(client_id, token, topic, callback) { // function authCheck(client_id, token, topic, callback) {
callback(true); // callback(true);
} // }
module.exports = function (options = {}) { module.exports = function (options = {}) {
...@@ -30,99 +30,36 @@ module.exports = function (options = {}) { ...@@ -30,99 +30,36 @@ 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);
if (typeof (authstatus) == 'undefined') {
cache.set(cachekey, true); // cache missed
}
var response
var decoded = require('./checkClientRole').checkRealDB(req.body.username)
// checkRole if (decoded) {
var checkRealDB = require('./checkClientRole').checkRealDB if (decoded.role === 'realtimedb') response = { 'result': 'ok' }
checkRealDB(req.body.username, (err, decoded) => { else response = { 'result': 'no' }
if (err) { res.send(response);
// Set Response next();
var isShadow = require('./utils/getTopic').isShadow }
if (isShadow(topic)) { else {
var _id = require('./utils/getGroupID').getGroupID(req.body.username, req.body.client_id)
var getGroupID = require('./utils/getGroupID').getGroupID var _ftopic = require('./utils/getTopic').rewriteTopic(topic, 'sub', _id, req.body.client_id) // get topic where concat with groupID
getGroupID(req.body.username, req.body.client_id, (_id) => { // get groupID var _topic = [{ // setTopic for response
var _ftopic = require('./utils/getTopic').rewriteTopic(topic, 'sub', _id, req.body.client_id) // get topic where concat with groupID 'topic': _ftopic,
'qos': 0
var _topic = [{ // setTopic for response }]
'topic': _ftopic, response = {
'qos': 0 'result': 'ok',
}] 'topics': _topic
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);
}
next();
})
}
else {
var getGroupID = require('./utils/getGroupID').getGroupID
getGroupID(req.body.username, req.body.client_id, (_id) => { // get groupID
// console.log(_id)
var _ftopic = require('./utils/getTopic').rewriteTopic(topic, 'sub', _id, 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') {
// cache missed
authCheck(req.body.client_id, req.body.username, topic, function (result) {
cache.set(cachekey, result);
res.send(response);
});
}
else {
res.send(response);
}
next();
})
}
} }
else { res.send(response);
console.log(decoded) next();
if (decoded.role === 'realtimedb') { }
var response = {
'result': 'ok'
}
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);
}
next();
}
}
})
} }
else { else {
res.send({ res.send({ "result": "no" });
"result": "no"
});
next(); next();
} }
} }
} }
var jwt = require('jsonwebtoken') var jwt = require('jsonwebtoken')
var config = require('config') var config = require('config')
var getExp = require('./utils/getDates').getExp
function checkRealDB(token, cb) { module.exports.checkRealDB = checkRealDB
module.exports.getRole = getRole
module.exports.signRole = signRole
function checkRealDB(token) {
const verifyOptions = { const verifyOptions = {
algorithms: ['RS256'] algorithms: ['RS256']
}; };
jwt.verify(token, config.get('pubca'), verifyOptions, (err, decoded) => { try {
cb(err, decoded) return jwt.verify(token, config.get('pubca'), verifyOptions)
}) }
catch(e) {
return false
}
} }
module.exports.checkRealDB = checkRealDB
function getRole(token, cb) { function getRole(token) {
var secret = 'nexpie' var secret = 'nexpie'
jwt.verify(token, secret, (err, decoded) => { try {
if (err) { return jwt.verify(token, secret)
// console.log(err) }
cb(false) catch(e) {
} return false
else { }
// console.log(decoded)
cb(decoded)
}
})
} }
module.exports.getRole = getRole
function signRole(req, res) { function signRole(req, res) {
var secret = 'nexpie' var secret = 'nexpie'
...@@ -39,45 +41,4 @@ function signRole(req, res) { ...@@ -39,45 +41,4 @@ function signRole(req, res) {
else { else {
res.send('role not complete') res.send('role not complete')
} }
}
module.exports.signRole = signRole
function getExp(expires) {
var exp
if (expires.endsWith('y')) {
exp = getYears(expires.split('y')[0])
}
else if (expires.endsWith('m')) {
exp = getMonths(expires.split('m')[0])
}
else if (expires.endsWith('d')) {
exp = expires.split('d')[0] + 'd'
}
else if (expires.endsWith('h')) {
exp = expires.split('h')[0] + 'h'
}
else if (expires.endsWith('mi')) {
exp = getMinutes(expires.split('mi')[0])
}
else if (expires.endsWith('s')) {
exp = getSeconds(expires.split('s')[0])
}
else exp = getYears(10)
return exp
}
function getMonths(d) {
return (d * 30) + 'd'
}
function getYears(m) {
return (m * 30 * 12) + 'd'
}
function getMinutes(mi) {
return (mi * 1000 * 60) + 'ms'
}
function getSeconds(ms) {
return (ms * 1000) + 'ms'
} }
\ No newline at end of file
...@@ -16,21 +16,6 @@ function on_deliver(req, res, next) { ...@@ -16,21 +16,6 @@ function on_deliver(req, res, next) {
} }
module.exports.on_deliver = on_deliver module.exports.on_deliver = on_deliver
function hasGroup(topics) {
if (topics.indexOf('!') > -1) {
return true
}
else return false
}
module.exports.hasGroup = hasGroup
// function beDesireTopic(topics) {
// if (hasGroup(topics)) {
// return joinTopic(topics)
// }
// else return topics
// }
function joinTopic(topics) { function joinTopic(topics) {
var topic = topics.split('/!')[1] var topic = topics.split('/!')[1]
return topics.split('/!')[0] + topic.substring(topic.indexOf('/'), topic.length) return topics.split('/!')[0] + topic.substring(topic.indexOf('/'), topic.length)
......
...@@ -2,33 +2,31 @@ function on_unsubscribe(req, res, next) { ...@@ -2,33 +2,31 @@ function on_unsubscribe(req, res, next) {
console.log('------on unsubscribe-------') console.log('------on unsubscribe-------')
console.log(req.body) console.log(req.body)
var topic = req.body.topics[0]; var topic = req.body.topics[0];
// checkRole var token = req.body.username
var checkRealDB = require('./checkClientRole').checkRealDB var client_id = req.body.client_id
checkRealDB(req.body.username, (err, decoded) => { var response
if (err) {
// Set Response var decoded = require('./checkClientRole').checkRealDB(token)
var getGroupID = require('./utils/getGroupID').getGroupID if (decoded) {
getGroupID(req.body.username, req.body.client_id, (_id) => { // get groupID if (decoded.role === 'realtimedb') {
// console.log(_id) response = { 'result': 'ok' }
var _ftopic = require('./utils/getTopic').rewriteTopic(topic, 'unsub', _id, req.body.client_id) // get topic where concat with groupID
var response = {
"result": "ok",
"topics": [_ftopic]
}
console.log(response)
res.send(response);
})
} }
else { else response = { 'result': 'no' }
console.log(decoded) res.send(response);
if (decoded.role === 'realtimedb') { next()
var response = { }
'result': 'ok' else {
} var _id = require('./utils/getGroupID').getGroupID(token, client_id)
console.log(response) if (_id) {
res.send(response); var _ftopic = require('./utils/getTopic').rewriteTopic(topic, 'unsub', _id, client_id)
response = {
"result": "ok",
"topics": [_ftopic]
} }
} }
}) else response = { 'result': 'no' }
res.send(response);
next()
}
} }
module.exports.on_unsubscribe = on_unsubscribe module.exports.on_unsubscribe = on_unsubscribe
\ No newline at end of file
module.exports.getExp = getExp
function getExp(expires) {
var exp
if (expires.endsWith('y')) {
exp = getYears(expires.split('y')[0])
}
else if (expires.endsWith('m')) {
exp = getMonths(expires.split('m')[0])
}
else if (expires.endsWith('d')) {
exp = expires.split('d')[0] + 'd'
}
else if (expires.endsWith('h')) {
exp = expires.split('h')[0] + 'h'
}
else if (expires.endsWith('mi')) {
exp = getMinutes(expires.split('mi')[0])
}
else if (expires.endsWith('s')) {
exp = getSeconds(expires.split('s')[0])
}
else exp = getYears(10)
return exp
}
function getMonths(d) {
return (d * 30) + 'd'
}
function getYears(m) {
return (m * 30 * 12) + 'd'
}
function getMinutes(mi) {
return (mi * 1000 * 60) + 'ms'
}
function getSeconds(ms) {
return (ms * 1000) + 'ms'
}
\ No newline at end of file
var getRole = require('../checkClientRole').getRole var getRole = require('../checkClientRole').getRole
function getGroupID(token, id, cb) { function getGroupID(token, client_id) {
checkID(token, id, (groupId) => { var role = getRole(token)
// console.log(groupId) if (role) {
cb(groupId)
})
}
module.exports.getGroupID = getGroupID
function checkID(token, id, cb) {
// console.log(token)
getRole(token, (role) => {
// console.log(role)
if (role.hasOwnProperty('groupId')) { if (role.hasOwnProperty('groupId')) {
cb(role.groupId) return role.groupId
} }
else { else {
doDB(id, (groupId) => { return doDB(client_id)
// console.log(groupId)
cb(groupId)
})
} }
}) }
else return role
} }
module.exports.getGroupID = getGroupID
function doDB(id, cb) { function doDB(client_id) {
var groupId = 'test' var groupId = 'test'
cb(groupId) return groupId
} }
\ No newline at end of file
// function getTopic(topic, id) {
// //var _topic = classifiedTopic(topic) + '/!' + id + topic.split(classifiedTopic(topic))[1]
// var _topic = topicPrefix(topic) + id + topic.split(topicPrefix(topic))[1]
// return _topic
// }
// module.exports.getTopic = getTopic
// function topicPrefix(topic) {
// if (topic.indexOf('@msg') !== -1) {
// return '@msg/!'
// } else if (topic.indexOf('@shadow/updated') !== -1) {
// return '@shadow/updated/!'
// } else if (topic.indexOf('@shadow/update') !== -1) {
// return '@shadow/update/'
// } else if (topic.indexOf('@shadow/get') !== -1) {
// return '@shadow/get/!'
// } else if (topic.indexOf('@client') !== -1) {
// return '@client/'
// }
// }
var modify = require('./modify'); var modify = require('./modify');
function rewriteTopic(topic, op, groupid, clientid, output) { function rewriteTopic(topic, op, groupid, clientid, output) {
...@@ -63,13 +42,4 @@ function rewriteTopic(topic, op, groupid, clientid, output) { ...@@ -63,13 +42,4 @@ function rewriteTopic(topic, op, groupid, clientid, output) {
} }
} }
module.exports.rewriteTopic = rewriteTopic module.exports.rewriteTopic = rewriteTopic
\ No newline at end of file
function isShadow(topic) {
if (topic.indexOf('@shadow') !== -1) {
return true
}
else return false
}
module.exports.isShadow = isShadow
\ No newline at end of file
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