mirror of
https://github.com/mainflux/mainflux.git
synced 2025-04-29 13:49:28 +08:00
Fix topic to channel parsing (#381)
Signed-off-by: Sava Radoš <srados@bitsfactory.com>
This commit is contained in:
parent
902630fb14
commit
970c1c86ca
14
mqtt/mqtt.js
14
mqtt/mqtt.js
@ -72,10 +72,16 @@ nats.subscribe('channel.*', function (msg) {
|
|||||||
|
|
||||||
aedes.authorizePublish = function (client, packet, publish) {
|
aedes.authorizePublish = function (client, packet, publish) {
|
||||||
// Topics are in the form `channels/<channel_id>/messages`
|
// 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 = {
|
accessReq = {
|
||||||
token: client.password,
|
token: client.password,
|
||||||
chanID: Number(channel)
|
chanID: Number(channelId)
|
||||||
},
|
},
|
||||||
onAuthorize = function (err, res) {
|
onAuthorize = function (err, res) {
|
||||||
var rawMsg;
|
var rawMsg;
|
||||||
@ -84,11 +90,11 @@ aedes.authorizePublish = function (client, packet, publish) {
|
|||||||
|
|
||||||
rawMsg = message.RawMessage.encode({
|
rawMsg = message.RawMessage.encode({
|
||||||
Publisher: client.thingId,
|
Publisher: client.thingId,
|
||||||
Channel: channel,
|
Channel: channelId,
|
||||||
Protocol: 'mqtt',
|
Protocol: 'mqtt',
|
||||||
Payload: packet.payload
|
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.
|
// Set empty topic for packet so that it won't be published two times.
|
||||||
packet.topic = '';
|
packet.topic = '';
|
||||||
|
Loading…
x
Reference in New Issue
Block a user