moscajs / mosca

MQTT broker as a module
mosca.io
3.2k stars 513 forks source link

username in authorizepublish #781

Open timoschuetz opened 5 years ago

timoschuetz commented 5 years ago

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?

danclive commented 5 years ago

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