Commit 8fbdb9dc by Chavee Issariyapat

update authhook

parent b8c89812
......@@ -3,8 +3,9 @@ var config = require('config');
var LRU = require("lru-cache") ,
cache = LRU({
max: 500,
maxAge: 1000 * 60 * 5
maxAge: 1000*60*5
});
var debug = false;
var authclient = require('seneca')({log: 'silent'})
.client({port: config.get('authserv_port'), host: config.get('authserv_host')});
......@@ -13,7 +14,22 @@ var authclient = require('seneca')({log: 'silent'})
function authCheck(client_id, token, password, callback) {
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) {
try {
var jdata = JSON.parse(res.data);
if (jdata && jdata.code == 200) {
callback(true);
}
else callback(false);
} catch(e) {
callback(false);
}
callback(true);
}
else {
......@@ -23,11 +39,14 @@ function authCheck(client_id, token, password, callback) {
}
module.exports = function(options={}) {
debug = options.debug || false;
return function(req, res, next) {
if (req.header('vernemq-hook') == 'auth_on_register' && req && req.body) {
if (options.debug) {
if (debug) {
console.log('auth_on_register-------------');
console.log(req.body);
}
......
......@@ -5,6 +5,7 @@ var LRU = require("lru-cache") ,
max: 500,
maxAge: 1000 * 60 * 5
});
var debug = false;
// for testing
function authCheck(client_id, token, topic, callback) {
......@@ -12,14 +13,17 @@ function authCheck(client_id, token, topic, callback) {
}
module.exports = function(options={}) {
debug = options.debug || false;
return function(req, res, next) {
if (req.header('vernemq-hook') == 'auth_on_subscribe' && req && req.body) {
if (options.debug) {
console.log('auth_on_subscribe-------------');
console.log(req.body);
}
if (debug) {
console.log('auth_on_subscribe-------------');
console.log(req.body);
}
if (req.header('vernemq-hook') == 'auth_on_subscribe' && req && req.body) {
var authstatus;
var topic = req.body.topics[0].topic;
......
process.env["NODE_ENV"] = "development";
process.env["NODE_CONFIG_DIR"] = __dirname + "/config/";
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_subscribe_debug = process.env["AUTH_ON_SUBSCRIBE_DEBUG"] || false;
var on_publish_debug = process.env["ON_PUBLISH_DEBUG"] || false;
var restify = require('restify');
var auth_on_register = require('./auth_on_register')({debug:true});
var auth_on_publish = require('./auth_on_publish')({debug:true});
var auth_on_subscribe = require('./auth_on_subscribe')({debug:true});
var on_publish = require('./on_publish')({debug:true});
var auth_on_register = require('./auth_on_register')({debug: auth_on_register_debug});
var auth_on_publish = require('./auth_on_publish')({debug: auth_on_publish_debug});
var auth_on_subscribe = require('./auth_on_subscribe')({debug: auth_on_subscribe_debug});
var on_publish = require('./on_publish')({debug: on_publish_debug});
const server = restify.createServer({
name: 'authhook',
version: '1.0.0',
version: '1.0.1',
});
const port = 40000;
......
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