mosca icon indicating copy to clipboard operation
mosca copied to clipboard

username in authorizepublish

Open timoschuetz opened this issue 6 years ago • 1 comments

Hello,

I'm currently working on a application with authorization and I want only one user to be allowed to publish on his channel, like it is given in the example:https://github.com/mcollina/mosca/wiki/Authentication-&-Authorization The problem is in the client object there is no user value, object.

var authorizePublish = function(client, topic, payload, callback) { console.log(client.user); if(topic.split('/')[0] == '$SYS'){callback(null, true)}else{ if(topic.split('/')[1] == 'device'){ if(topic.split('/')[2] == client.user){ //that the part that is not working callback(null, true); } callback(null, false); }else{callback(null, false)} } }

Here is the Client

var mqtt = require('mqtt');

var options = { port: 1883, clientId: 'mqttjs_1337', username: '123', password: '1234' };

var client = mqtt.connect('mqtt://localhost', options);

var json = { 'temperature': '42' }

client.on('connect', function () { client.publish('/device/123', JSON.stringify(json)); setTimeout(function() { process.exit(); }, 3000); })

client.on('message', function (topic, message) { // message is Buffer console.log(message.toString()) client.end() })

How can I access the username in the authorizepublish?

timoschuetz avatar Jan 23 '19 10:01 timoschuetz

l already create pull requests #775, but there has been no merge.

danclive avatar Jan 27 '19 07:01 danclive