Skip to content

Commit

Permalink
Add handling for password only connections (mqttjs#1685)
Browse files Browse the repository at this point in the history
  • Loading branch information
rts-rob committed Oct 24, 2023
1 parent cc48d45 commit 98d564d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/lib/connect/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@ function parseAuthOptions(opts: IClientOptions) {
if (matches) {
opts.username = matches[1]
opts.password = matches[2]
} else if (opts.auth.startsWith(':')) {
// Password only, no username provided
opts.password = opts.auth.slice(1)
} else {
// Username only, no password provided
opts.username = opts.auth
}
}
Expand Down
8 changes: 8 additions & 0 deletions test/mqtt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ describe('mqtt', () => {
c.end((err) => done(err))
})

it('should return an MqttClient with password option set', function _test(t, done) {
const c = mqtt.connect('mqtt://:pass@localhost:1883')

c.should.be.instanceOf(mqtt.MqttClient)
c.options.should.have.property('password', 'pass')
c.end((err) => done(err))
})

it('should return an MqttClient with username and password options set', function _test(t, done) {
const c = mqtt.connect('mqtt://user@localhost:1883')

Expand Down

0 comments on commit 98d564d

Please sign in to comment.