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,57 +25,24 @@ module.exports = function (options = {}) { ...@@ -25,57 +25,24 @@ 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') {
// checkRole cache.set(cachekey, true); // cache missed
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);
var 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);
});
}
else {
res.send(response);
}
next(); next();
})
} }
else { else {
var getGroupID = require('./utils/getGroupID').getGroupID var output = {};
getGroupID(req.body.username, req.body.client_id, (_id) => { // get groupID var _id = require('./utils/getGroupID').getGroupID(req.body.username, req.body.client_id)
// console.log(_id) var _ftopic = require('./utils/getTopic').rewriteTopic(topic, 'pub', _id, req.body.client_id, output); // 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 response = {
var response = {
'result': 'ok', 'result': 'ok',
'modifiers': { 'modifiers': {
'topic': _ftopic, 'topic': _ftopic,
...@@ -83,47 +50,15 @@ module.exports = function (options = {}) { ...@@ -83,47 +50,15 @@ module.exports = function (options = {}) {
'retain': false 'retain': false
} }
} }
console.log(response) if (output.verb == 'get' || output.verb == 'read') {
response.modifiers.payload = Buffer.from(req.body.client_id).toString('base64');
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 {
console.log(decoded)
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); res.send(response);
}
next(); next();
} }
} }
})
}
else { else {
res.send({ res.send({ "result": "no" });
"result": "no"
});
next(); next();
} }
} }
......
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,23 +8,18 @@ var LRU = require("lru-cache"), ...@@ -10,23 +8,18 @@ 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
getRole(token, (role) => {
if (role === false) {
authclient.act({role:'auth',cmd:'token',action:'info',token: token}, function(err, res) {
if (debug) { if (debug) {
console.log("res ------>\n"); console.log("res ------>\n");
console.log(res); console.log(res);
...@@ -48,16 +41,8 @@ function authCheck(client_id, token, password, callback) { ...@@ -48,16 +41,8 @@ function authCheck(client_id, token, password, callback) {
callback(false); callback(false);
} }
}); });
} }
else callback(true)
})
} }
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);
// checkRole
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 getGroupID = require('./utils/getGroupID').getGroupID
getGroupID(req.body.username, req.body.client_id, (_id) => { // get 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
'topic': _ftopic,
'qos': 0
}]
var response = {
'result': 'ok',
'topics': _topic
}
console.log(response)
if (typeof (authstatus) == 'undefined') { if (typeof (authstatus) == 'undefined') {
// cache missed cache.set(cachekey, true); // cache missed
authCheck(req.body.client_id, req.body.username, topic, function (result) {
cache.set(cachekey, result);
res.send(response);
});
} }
else { var response
var decoded = require('./checkClientRole').checkRealDB(req.body.username)
if (decoded) {
if (decoded.role === 'realtimedb') response = { 'result': 'ok' }
else response = { 'result': 'no' }
res.send(response); res.send(response);
}
next(); next();
})
} }
else { else {
var getGroupID = require('./utils/getGroupID').getGroupID var _id = require('./utils/getGroupID').getGroupID(req.body.username, req.body.client_id)
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 _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
}] }]
var response = { response = {
'result': 'ok', 'result': 'ok',
'topics': _topic '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); res.send(response);
});
}
else {
res.send(response);
}
next();
})
}
}
else {
console.log(decoded)
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(); 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)
} }
else { catch(e) {
// console.log(decoded) return false
cb(decoded)
} }
})
} }
module.exports.getRole = getRole
function signRole(req, res) { function signRole(req, res) {
var secret = 'nexpie' var secret = 'nexpie'
...@@ -40,44 +42,3 @@ function signRole(req, res) { ...@@ -40,44 +42,3 @@ function signRole(req, res) {
res.send('role not complete') res.send('role not complete')
} }
} }
\ No newline at end of file
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) else response = { 'result': 'no' }
res.send(response); res.send(response);
}) next()
} }
else { else {
console.log(decoded) var _id = require('./utils/getGroupID').getGroupID(token, client_id)
if (decoded.role === 'realtimedb') { if (_id) {
var response = { var _ftopic = require('./utils/getTopic').rewriteTopic(topic, 'unsub', _id, client_id)
'result': 'ok' response = {
"result": "ok",
"topics": [_ftopic]
} }
console.log(response)
res.send(response);
} }
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) {
...@@ -64,12 +43,3 @@ function rewriteTopic(topic, op, groupid, clientid, output) { ...@@ -64,12 +43,3 @@ 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