Commit ce0624cf by Chavee Issariyapat

auth with auth service

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