Commit ce0624cf by Chavee Issariyapat

auth with auth service

parent 241d16c4
var config = require('config');
// https://github.com/isaacs/node-lru-cache // https://github.com/isaacs/node-lru-cache
var LRU = require("lru-cache") , var LRU = require("lru-cache") ,
cache = LRU({ cache = LRU({
...@@ -6,9 +7,8 @@ var LRU = require("lru-cache") , ...@@ -6,9 +7,8 @@ var LRU = require("lru-cache") ,
}); });
// for testing // for testing
function authPublish(client_id, username, topic) { function authCheck(client_id, username, topic, callback) {
if (!client_id || !username || !topic) return false; callback(true);
else return true;
} }
module.exports = function(options={}) { module.exports = function(options={}) {
...@@ -24,36 +24,29 @@ module.exports = function(options={}) { ...@@ -24,36 +24,29 @@ module.exports = function(options={}) {
var topic = req.body.topic; var topic = req.body.topic;
var cachekey = req.body.client_id +':'+ req.body.username+':'+topic; var cachekey = 'pub:'+req.body.client_id +':'+ req.body.username+':'+topic;
var authstatus = cache.get(cachekey);
if (typeof(authstatus)=='undefined') { if (typeof(authstatus)=='undefined') {
// cache missed // cache missed
if (authPublish(req.body.client_id, req.body.username, topic)) { authCheck(req.body.client_id, req.body.username, topic, function(result) {
authstatus = true; cache.set(cachekey, result);
cache.set(cachekey, authstatus); res.send({
} "result": result?"ok":"no"
else { });
authstatus = false; });
cache.set(cachekey, authstatus);
}
}
else {
// cache hit
}
if (authstatus) {
out = {
"result": "ok"
}
} }
else { else {
out = { res.send({
"result": "no" "result": authstatus?"ok":"no"
} });
} }
res.send(out);
next(); next();
} }
else {
res.send({
"result": "no"
});
}
} }
} }
var config = require('config');
// https://github.com/isaacs/node-lru-cache // https://github.com/isaacs/node-lru-cache
var LRU = require("lru-cache") , var LRU = require("lru-cache") ,
cache = LRU({ cache = LRU({
...@@ -5,51 +6,48 @@ var LRU = require("lru-cache") , ...@@ -5,51 +6,48 @@ var LRU = require("lru-cache") ,
maxAge: 1000 * 60 * 5 maxAge: 1000 * 60 * 5
}); });
var authclient = require('seneca')({log: 'silent'})
.client({port: config.get('authserv_port'), host: config.get('authserv_host')});
// for testing // for testing
function authByUserPasswd(client_id, username, password) { function authCheck(client_id, token, password, callback) {
if (!client_id || !username || !password) return false; authclient.act({role:'auth',cmd:'token',action:'info',token: token}, function(err, res) {
else return true; console.log(res);
if (res && res.data) {
callback(true);
}
else {
callback(false);
}
});
} }
module.exports = function(options={}) { module.exports = function(options={}) {
return function(req, res, next) { return function(req, res, next) {
if (req.header('vernemq-hook') == 'auth_on_register') { if (req.header('vernemq-hook') == 'auth_on_register' && req && req.body) {
if (options.debug) { var cachekey = 'auth:'+req.body.client_id +':'+ req.body.username +':'+ req.body.password;
console.log(req.body); var authstatus = cache.get(cachekey);
}
var out, authstatus;
var cachekey = req.body.client_id +':'+ req.body.username +':'+ req.body.password;
authstatus = cache.get(cachekey);
if (typeof(authstatus)=='undefined') { if (typeof(authstatus)=='undefined') {
// cache missed // cache missed
if (authByUserPasswd(req.body.client_id, req.body.username, req.body.password)) { authCheck(req.body.client_id, req.body.username, req.body.password, function(result) {
authstatus = true; cache.set(cachekey, result);
cache.set(cachekey, authstatus); res.send({
} "result": result?"ok":"no"
else { });
authstatus = false; });
cache.set(cachekey, authstatus);
}
}
else {
// cache hit
}
if (authstatus) {
out = {
"result": "ok"
}
} }
else { else {
out = { res.send({
"result": "no" "result": authstatus?"ok":"no"
} });
} }
res.send(out);
next(); next();
} }
else {
res.send({
"result": "no"
});
}
} }
} }
var config = require('config');
// https://github.com/isaacs/node-lru-cache // https://github.com/isaacs/node-lru-cache
var LRU = require("lru-cache") , var LRU = require("lru-cache") ,
cache = LRU({ cache = LRU({
...@@ -6,9 +7,8 @@ var LRU = require("lru-cache") , ...@@ -6,9 +7,8 @@ var LRU = require("lru-cache") ,
}); });
// for testing // for testing
function authSubscribe(client_id, username, topic) { function authCheck(client_id, username, topic, callback) {
if (!client_id || !username || !topic) return false; callback(true);
else return true;
} }
module.exports = function(options={}) { module.exports = function(options={}) {
...@@ -24,36 +24,29 @@ module.exports = function(options={}) { ...@@ -24,36 +24,29 @@ module.exports = function(options={}) {
var topic = req.body.topic; var topic = req.body.topic;
var cachekey = req.body.client_id +':'+ req.body.username+':'+topic; var cachekey = 'sub:'+req.body.client_id +':'+ req.body.username+':'+topic;
var authstatus = cache.get(cachekey);
if (typeof(authstatus)=='undefined') { if (typeof(authstatus)=='undefined') {
// cache missed // cache missed
if (authSubscribe(req.body.client_id, req.body.username, topic)) { authCheck(req.body.client_id, req.body.username, topic, function(result) {
authstatus = true; cache.set(cachekey, result);
cache.set(cachekey, authstatus); res.send({
} "result": result?"ok":"no"
else { });
authstatus = false; });
cache.set(cachekey, authstatus);
}
}
else {
// cache hit
}
if (authstatus) {
out = {
"result": "ok"
}
} }
else { else {
out = { res.send({
"result": "no" "result": authstatus?"ok":"no"
} });
} }
res.send(out);
next(); next();
} }
else {
res.send({
"result": "no"
});
}
} }
} }
{
"authserv_host" : "AUTH_SERVICE_HOST",
"authserv_port" : "AUTH_SERVICE_PORT"
}
{
"authserv_host" : "203.154.135.231",
"authserv_port" : 31088
}
process.env["NODE_ENV"] = "development";
process.env["NODE_CONFIG_DIR"] = __dirname + "/config/";
var restify = require('restify'); var restify = require('restify');
var auth_on_register = require('./auth_on_register')({debug:true}); var auth_on_register = require('./auth_on_register')({debug:true});
var auth_on_publish = require('./auth_on_publish')({debug:true}); var auth_on_publish = require('./auth_on_publish')({debug:true});
......
...@@ -9,7 +9,9 @@ ...@@ -9,7 +9,9 @@
"author": "", "author": "",
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"config": "^1.30.0",
"lru-cache": "^4.1.3", "lru-cache": "^4.1.3",
"restify": "^7.1.1" "restify": "^7.1.1",
"seneca": "^3.6.0"
} }
} }
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