Skip to content

Commit

Permalink
failed authentication does not disconnect other client with same clie…
Browse files Browse the repository at this point in the history
…ntId

Closes #6.
  • Loading branch information
mcollina committed Jan 5, 2016
1 parent cf85ce2 commit c55f347
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 2 deletions.
3 changes: 1 addition & 2 deletions lib/handlers/connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ function handleConnect (client, packet, done) {
client.id = packet.clientId || uuid.v4()
client.will = packet.will

client.broker.registerClient(client)

clearTimeout(client._connectTimer)
client._connectTimer = null

Expand All @@ -61,6 +59,7 @@ function authenticate (arg, done) {

function negate (err, successful) {
if (!err && successful) {
client.broker.registerClient(client)
return done()
} else if (err) {
write(client, {
Expand Down
72 changes: 72 additions & 0 deletions test/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
var test = require('tape').test
var Client = require('../lib/client')
var helper = require('./helper')
var aedes = require('../')
var eos = require('end-of-stream')
var setup = helper.setup
var subscribe = helper.subscribe
Expand Down Expand Up @@ -244,3 +245,74 @@ test('negate subscription', function (t) {
t.equal(packet.messageId, 24)
})
})

test('failed authentication does not disconnect other client with same clientId', function (t) {
t.plan(3)

var broker = aedes()
var s = setup(broker)
var s0 = setup(broker)

broker.authenticate = function (client, username, password, cb) {
cb(null, password.toString() === 'right')
}

s0.inStream.write({
cmd: 'connect',
protocolId: 'MQTT',
protocolVersion: 4,
clean: true,
clientId: 'my-client',
username: 'my username',
password: 'right',
keepalive: 0
})

s0.outStream.on('data', function (packet) {
t.deepEqual(packet, {
cmd: 'connack',
returnCode: 0,
length: 2,
qos: 0,
retain: false,
dup: false,
topic: null,
payload: null,
sessionPresent: false
}, 'successful connack')

s.inStream.write({
cmd: 'connect',
protocolId: 'MQTT',
protocolVersion: 4,
clean: true,
clientId: 'my-client',
username: 'my username',
password: 'wrong',
keepalive: 0
})
})

var removeEos = eos(s0.outStream, function () {
t.fail('ended before time')
})

s.outStream.on('data', function (packet) {
t.deepEqual(packet, {
cmd: 'connack',
returnCode: 5,
length: 2,
qos: 0,
retain: false,
dup: false,
topic: null,
payload: null,
sessionPresent: false
}, 'unsuccessful connack')
})

eos(s.outStream, function () {
t.pass('ended')
removeEos()
})
})

0 comments on commit c55f347

Please sign in to comment.