Skip to content

Commit

Permalink
Fix topic to channel parsing (#381)
Browse files Browse the repository at this point in the history
Signed-off-by: Sava Radoš <srados@bitsfactory.com>
  • Loading branch information
Sava R authored and drasko committed Sep 4, 2018
1 parent 902630f commit 970c1c8
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions mqtt/mqtt.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,16 @@ nats.subscribe('channel.*', function (msg) {

aedes.authorizePublish = function (client, packet, publish) {
// Topics are in the form `channels/<channel_id>/messages`
var channel = packet.topic.split('/')[1],
var channel = /^channels\/(.+?)\/messages$/.exec(packet.topic);
if (!channel) {
logger.warn('unknown topic');
publish(4); // Bad username or password
return;
}
var channelId = channel[1],
accessReq = {
token: client.password,
chanID: Number(channel)
chanID: Number(channelId)
},
onAuthorize = function (err, res) {
var rawMsg;
Expand All @@ -84,11 +90,11 @@ aedes.authorizePublish = function (client, packet, publish) {

rawMsg = message.RawMessage.encode({
Publisher: client.thingId,
Channel: channel,
Channel: channelId,
Protocol: 'mqtt',
Payload: packet.payload
});
nats.publish('channel.' + channel, rawMsg);
nats.publish('channel.' + channelId, rawMsg);

// Set empty topic for packet so that it won't be published two times.
packet.topic = '';
Expand Down

0 comments on commit 970c1c8

Please sign in to comment.