Skip to content

Commit

Permalink
Merge pull request #36 from pkinney/config-auth
Browse files Browse the repository at this point in the history
Add options for overriding methods in broker creation
  • Loading branch information
mcollina committed Feb 26, 2016
2 parents 1741b51 + cf4faa9 commit b118ce8
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 21 deletions.
38 changes: 17 additions & 21 deletions aedes.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,28 @@ var Packet = require('aedes-packet')
var bulk = require('bulk-write-stream')
var reusify = require('reusify')
var Client = require('./lib/client')
var _ = require('lodash')

module.exports = Aedes

var defaultOptions = {
concurrency: 100,
heartbeatInterval: 60000, // 1 minute
connectTimeout: 30000, // 30 secs
authenticate: function (client, username, password, callback) { callback(null, true) },
authorizePublish: function (client, packet, callback) { callback(null) },
authorizeSubscribe: function (client, sub, callback) { callback(null, sub) },
published: function (packet, client, callback) { callback(null) }
}

function Aedes (opts) {
var that = this

if (!(this instanceof Aedes)) {
return new Aedes(opts)
}

// TODO replace with extend
opts = opts || {}
opts.concurrency = opts.concurrency || 100
opts.heartbeatInterval = opts.heartbeatInterval || 60000 // 1 minute
opts.connectTimeout = opts.connectTimeout || 30000 // 30 secs
opts = _.defaults(opts || {}, defaultOptions)

this.id = shortid()
this.counter = 0
Expand All @@ -42,6 +49,11 @@ function Aedes (opts) {
this._series = series()
this._enqueuers = reusify(DoEnqueues)

this.authenticate = opts.authenticate
this.authorizePublish = opts.authorizePublish
this.authorizeSubscribe = opts.authorizeSubscribe
this.published = opts.published

this.clients = {}
this.brokers = {}

Expand Down Expand Up @@ -268,22 +280,6 @@ Aedes.prototype.close = function (cb) {
this._parallel(this, closeClient, Object.keys(this.clients), cb || noop)
}

Aedes.prototype.authenticate = function (client, username, password, callback) {
callback(null, true)
}

Aedes.prototype.authorizePublish = function (client, packet, callback) {
callback(null)
}

Aedes.prototype.authorizeSubscribe = function (client, sub, cb) {
cb(null, sub)
}

Aedes.prototype.published = function (packet, client, done) {
done(null)
}

function PublishState (broker, client, packet) {
this.broker = broker
this.client = client
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"fastparallel": "^2.0.0",
"fastseries": "^1.5.0",
"from2": "^2.1.0",
"lodash": "^4.5.1",
"mqemitter": "^1.0.0",
"mqtt-packet": "^4.0.0",
"pump": "^1.0.0",
Expand Down
98 changes: 98 additions & 0 deletions test/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,44 @@ test('authorize publish', function (t) {
})
})

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

var s = connect(setup(aedes({
authorizePublish: function (client, packet, cb) {
t.ok(client, 'client exists')
t.deepEqual(packet, expected, 'packet matches')
cb()
}
})))

var expected = {
cmd: 'publish',
topic: 'hello',
payload: new Buffer('world'),
qos: 0,
retain: false,
length: 12,
dup: false
}

s.broker.mq.on('hello', function (packet, cb) {
expected.brokerId = s.broker.id
expected.brokerCounter = s.broker.counter
expected.messageId = 0
delete expected.dup
delete expected.length
t.deepEqual(packet, expected, 'packet matches')
cb()
})

s.inStream.write({
cmd: 'publish',
topic: 'hello',
payload: 'world'
})
})

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

Expand Down Expand Up @@ -217,6 +255,23 @@ test('authorize subscribe', function (t) {
subscribe(t, s, 'hello', 0)
})

test('authorize subscribe from config options', function (t) {
t.plan(5)

var s = connect(setup(aedes({
authorizeSubscribe: function (client, sub, cb) {
t.ok(client, 'client exists')
t.deepEqual(sub, {
topic: 'hello',
qos: 0
}, 'topic matches')
cb(null, sub)
}
})))

subscribe(t, s, 'hello', 0)
})

test('negate subscription', function (t) {
t.plan(5)

Expand Down Expand Up @@ -317,3 +372,46 @@ test('failed authentication does not disconnect other client with same clientId'
removeEos()
})
})

test('set authentication method in config options', function (t) {
t.plan(6)

var s = setup(aedes({
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')
cb(null, false)
}
}))

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.equal(s.broker.connectedClients, 0, 'no connected clients')
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
})
})

0 comments on commit b118ce8

Please sign in to comment.