Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add auth handler to public API to enable mqtt5 enhanced authn #1231

Closed
wants to merge 12 commits into from
50 changes: 50 additions & 0 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,10 @@ MqttClient.prototype._handlePacket = function (packet, done) {
this._handleConnack(packet)
done()
break
case 'auth':
this._handleAuth(packet)
done()
break
case 'pingresp':
this._handlePingresp(packet)
done()
Expand Down Expand Up @@ -1104,6 +1108,13 @@ MqttClient.prototype._sendPacket = function (packet, cb, cbStorePut) {
cbStorePut = cbStorePut || nop

if (!this.connected) {
// allow auth packets to be sent while authenticating with the broker (mqtt5 enhanced auth)
if (packet.cmd === 'auth') {
this._shiftPingInterval()
sendPacket(this, packet, cb)
return
}

debug('_sendPacket :: client not connected. Storing packet offline.')
this._storePacket(packet, cb, cbStorePut)
return
Expand Down Expand Up @@ -1263,6 +1274,45 @@ MqttClient.prototype._handleConnack = function (packet) {
}
}

MqttClient.prototype._handleAuth = function (packet) {
var options = this.options
var version = options.protocolVersion
var rc = version === 5 ? packet.reasonCode : packet.returnCode

if (version !== 5) {
var err = new Error('Protocol error: Auth packets are only supported in MQTT 5. Your version:' + version)
err.code = rc
this.emit('error', err)
return
}

var that = this
this.handleAuth(packet, function (err, packet) {
if (err) {
that.emit('error', err)
return
}

if (rc === 24) {
that.reconnecting = false
that._sendPacket(packet)
} else {
var error = new Error('Connection refused: ' + errors[rc])
err.code = rc
that.emit('error', error)
}
})
}

/**
* @param packet the packet received by the broker
* @return the auth packet to be returned to the broker
* @api public
*/
MqttClient.prototype.handleAuth = function (packet, callback) {
callback()
}

/**
* _handlePublish
*
Expand Down
11 changes: 10 additions & 1 deletion types/lib/client.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
IClientReconnectOptions
} from './client-options'
import { Store } from './store'
import { Packet, IConnectPacket, IPublishPacket, IDisconnectPacket, QoS } from 'mqtt-packet'
import { IAuthPacket, IConnectPacket, IPublishPacket, IDisconnectPacket, Packet, QoS } from 'mqtt-packet'

export interface ISubscriptionGrant {
/**
Expand Down Expand Up @@ -232,6 +232,15 @@ export declare class MqttClient extends events.EventEmitter {
*/
public handleMessage (packet: Packet, callback: PacketCallback): void

/**
* Handler to handle auth packages to implement mqtt5 enhanced auth procedures
*
* @param packet the auth packet to handle
* @param callback call when finished
* @api public
*/
public handleAuth (packet: IAuthPacket, callback: PacketCallback): void

/**
* getLastMessageId
*/
Expand Down