Commit 35b5edc5 by Chavee Issariyapat

add on_publish

parent cfeb4f4a
......@@ -16,6 +16,7 @@ module.exports = function(options={}) {
if (req.header('vernemq-hook') == 'auth_on_publish' && req && req.body) {
if (options.debug) {
console.log('auth_on_publish-------------');
console.log(req.body);
}
......
......@@ -26,6 +26,12 @@ module.exports = function(options={}) {
return function(req, res, next) {
if (req.header('vernemq-hook') == 'auth_on_register' && req && req.body) {
if (options.debug) {
console.log('auth_on_register-------------');
console.log(req.body);
}
var cachekey = 'auth:'+req.body.client_id +':'+ req.body.username +':'+ req.body.password;
var authstatus = cache.get(cachekey);
if (typeof(authstatus)=='undefined') {
......
......@@ -16,6 +16,7 @@ module.exports = function(options={}) {
if (req.header('vernemq-hook') == 'auth_on_subscribe' && req && req.body) {
if (options.debug) {
console.log('auth_on_subscribe-------------');
console.log(req.body);
}
......
......@@ -5,6 +5,7 @@ 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});
const server = restify.createServer({
name: 'authplugin',
......@@ -27,6 +28,7 @@ server.get('/about', function (req, res, next) {
server.post('/authreg', auth_on_register);
server.post('/authpub', auth_on_publish);
server.post('/authsub', auth_on_subscribe);
server.post('/onpub', on_publish);
server.listen(port, function () {
console.log('%s listening at %s', server.name, server.url);
......
var config = require('config');
// https://github.com/isaacs/node-lru-cache
var LRU = require("lru-cache") ,
cache = LRU({
max: 500,
maxAge: 1000 * 60 * 5
});
module.exports = function(options={}) {
return function(req, res, next) {
if (req.header('vernemq-hook') == 'on_publish' && req && req.body) {
if (options.debug) {
console.log('on_publish-------------');
console.log(req.body);
}
var topic = req.body.topic;
var cachekey = 'pub:'+req.body.client_id +':'+ req.body.username+':'+topic;
var authstatus = cache.get(cachekey);
res.status(200);
res.send('');
next();
}
else {
res.status(200);
res.send('');
next();
}
}
}
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