Skip to content

Commit

Permalink
authorizeForward for QoS 0.
Browse files Browse the repository at this point in the history
  • Loading branch information
mcollina committed Apr 28, 2016
1 parent 825528a commit 0930ac4
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 18 deletions.
38 changes: 21 additions & 17 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,28 +69,32 @@ function Client (broker, conn) {
this._eos = eos(this.conn, this.close.bind(this))

this.deliver0 = function deliverQoS0 (_packet, cb) {
var packet = new Packet(_packet, broker)
packet.qos = 0
write(that, packet, cb)
}

this.deliverQoS = function deliverQoS (_packet, cb) {
var toForward = that.broker.authorizeForward(that, _packet)
if (!toForward) {
if (that.clean === false) {
that.broker.persistence.outgoingClearMessageId(that, _packet, nop)
}
// we consider this to be an error, since the packet is undefined, so there's nothing to send
return
if (toForward) {
var packet = new Packet(toForward, broker)
packet.qos = 0
write(that, packet, cb)
}
}

this.deliverQoS = function deliverQoS (_packet, cb) {
// downgrade to qos0 if requested by publish
if (toForward.qos === 0) {
that.deliver0(toForward, cb)
if (_packet.qos === 0) {
that.deliver0(_packet, cb)
} else {
var packet = new QoSPacket(toForward, that)
packet.writeCallback = cb
broker.persistence.outgoingUpdate(that, packet, writeQoS)
var toForward = that.broker.authorizeForward(that, _packet)
if (toForward) {
var packet = new QoSPacket(toForward, that)
packet.writeCallback = cb
broker.persistence.outgoingUpdate(that, packet, writeQoS)
} else if (that.clean === false) {
that.broker.persistence.outgoingClearMessageId(that, _packet, nop)
// we consider this to be an error, since the packet is undefined
// so there's nothing to send
cb()
} else {
cb()
}
}
}

Expand Down
36 changes: 35 additions & 1 deletion test/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -460,8 +460,9 @@ test('change a topic name inside authorizeForward method in QoS 1 mode', functio
t.deepEqual(packet, expected, 'packet matches')
})
})

test('prevent publish in QoS1 mode', function (t) {
t.plan(1)
t.plan(2)

var broker = aedes({
authorizeForward: function (client, packet, cb) {
Expand Down Expand Up @@ -493,3 +494,36 @@ test('prevent publish in QoS1 mode', function (t) {
})
})

test('prevent publish in QoS0 mode', function (t) {
t.plan(1)

var broker = aedes({
authorizeForward: function (client, packet, cb) {
return null
}
})

broker.on('client', function (client) {
client.subscribe({
topic: 'hello',
qos: 0
}, function (err) {
t.error(err, 'no error')

broker.publish({
topic: 'hello',
payload: new Buffer('world'),
qos: 0
}, function (err) {
t.error(err, 'no error')
})
})
})

var s = connect(setup(broker))

s.outStream.once('data', function (packet) {
t.fail('Should have not recieved this packet')
})
})

0 comments on commit 0930ac4

Please sign in to comment.