Skip to content

Commit

Permalink
Merge pull request #62 from GavinDmello/master
Browse files Browse the repository at this point in the history
Handling for other return codes
  • Loading branch information
mcollina authored Aug 3, 2016
2 parents be13dff + 75ffb68 commit 7798db8
Show file tree
Hide file tree
Showing 3 changed files with 203 additions and 4 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,21 @@ instance.authenticate = function (client, username, password, callback) {
callback(null, username === 'matteo')
}
```
Other return codes can passed as follows :-

```js
instance.authenticate = function (client, username, password, callback) {
var error = new Error('Auth error')
error.returnCode = 1
callback(error, null)
}
```
The return code values and their responses which can be passed are given below :-

* `1` - Unacceptable protocol version
* `2` - Identifier rejected
* `3` - Server unavailable
* `4` - Bad user name or password

-------------------------------------------------------
<a name="authorizePublish"></a>
Expand Down
16 changes: 12 additions & 4 deletions lib/handlers/connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,18 @@ function authenticate (arg, done) {
client.broker.registerClient(client)
return done()
} else if (err) {
write(client, {
cmd: 'connack',
returnCode: 4
}, client.close.bind(client, done))
if (err.returnCode && (err.returnCode >= 1 && err.returnCode <= 3)) {
write(client, {
cmd: 'connack',
returnCode: err.returnCode
}, client.close.bind(client, done))
} else {
// If errorCode is 4 or not a number
write(client, {
cmd: 'connack',
returnCode: 4
}, client.close.bind(client, done))
}
} else {
write(client, {
cmd: 'connack',
Expand Down
176 changes: 176 additions & 0 deletions test/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,182 @@ test('authenticate errors', function (t) {
})
})

test('authentication error when return code 1 (unacceptable protocol version) is passed', function (t) {
t.plan(5)

var s = setup()

s.broker.authenticate = function (client, username, password, cb) {
t.ok(client instanceof Client, 'client is there')
t.equal(username, 'my username', 'username is there')
t.deepEqual(password, new Buffer('my pass'), 'password is there')
var error = new Error('Auth error')
error.returnCode = 1
cb(error, null)
}

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

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

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

test('authentication error when return code 2 (identifier rejected) is passed', function (t) {
t.plan(5)

var s = setup()

s.broker.authenticate = function (client, username, password, cb) {
t.ok(client instanceof Client, 'client is there')
t.equal(username, 'my username', 'username is there')
t.deepEqual(password, new Buffer('my pass'), 'password is there')
var error = new Error('Auth error')
error.returnCode = 2
cb(error, null)
}

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

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

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

test('authentication error when return code 3 (Server unavailable) is passed', function (t) {
t.plan(5)

var s = setup()

s.broker.authenticate = function (client, username, password, cb) {
t.ok(client instanceof Client, 'client is there')
t.equal(username, 'my username', 'username is there')
t.deepEqual(password, new Buffer('my pass'), 'password is there')
var error = new Error('Auth error')
error.returnCode = 3
cb(error, null)
}

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

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

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

test('authentication error when non numeric return code is passed', function (t) {
t.plan(5)

var s = setup()

s.broker.authenticate = function (client, username, password, cb) {
t.ok(client instanceof Client, 'client is there')
t.equal(username, 'my username', 'username is there')
t.deepEqual(password, new Buffer('my pass'), 'password is there')
var error = new Error('Non numeric error codes')
error.returnCode = 'return Code'
cb(error, null)
}

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

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

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

test('authorize publish', function (t) {
t.plan(3)

Expand Down

0 comments on commit 7798db8

Please sign in to comment.