From 98d564d74e9a03603a6e26d82194953dd5024ab4 Mon Sep 17 00:00:00 2001 From: Rob Sutter Date: Tue, 24 Oct 2023 21:01:50 +0200 Subject: [PATCH] Add handling for password only connections (#1685) --- src/lib/connect/index.ts | 4 ++++ test/mqtt.ts | 8 ++++++++ 2 files changed, 12 insertions(+) diff --git a/src/lib/connect/index.ts b/src/lib/connect/index.ts index 27f0cb3a1..2c6d21c7a 100644 --- a/src/lib/connect/index.ts +++ b/src/lib/connect/index.ts @@ -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 } } diff --git a/test/mqtt.ts b/test/mqtt.ts index 77df013e6..fa0b76eed 100644 --- a/test/mqtt.ts +++ b/test/mqtt.ts @@ -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')