From 6853f106712b89b891a27e9984bfb5273548a796 Mon Sep 17 00:00:00 2001 From: Mike Bremford Date: Wed, 18 Aug 2021 10:02:09 +0100 Subject: [PATCH 1/2] Add username from CONNECT to client after authenticate succes --- docs/Client.md | 9 +++++++++ lib/handlers/connect.js | 4 +++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/docs/Client.md b/docs/Client.md index a0c11c2f..713b028d 100644 --- a/docs/Client.md +++ b/docs/Client.md @@ -72,6 +72,15 @@ It is available only after `CONNACK (rc=0)`, otherwise it is `null` in cases: - after `CONNACK (rc!=0)` response - `connectionError` raised by aedes +## client.username + +- `` __Default__: `null` + +Client username, specified by CONNECT packet. + +It is available only after a successful call to `aedes.authenticate()`, where it +will be set to the username specifed by the client, or `null` if no username was supplied. + ## client.clean - `` __Default__: `true` diff --git a/lib/handlers/connect.js b/lib/handlers/connect.js index 14865f36..e35c3c92 100644 --- a/lib/handlers/connect.js +++ b/lib/handlers/connect.js @@ -98,9 +98,10 @@ function init (client, packet, done) { function authenticate (arg, done) { const client = this.client client.pause() + let username = this.username; client.broker.authenticate( client, - this.packet.username, + username, this.packet.password, negate) @@ -113,6 +114,7 @@ function authenticate (arg, done) { } if (!err && successful) { client._authorized = true + client.username = username || null; return done() } From 582ee500828f05a403cb62800cdc5afc3e54796e Mon Sep 17 00:00:00 2001 From: Mike Bremford Date: Wed, 18 Aug 2021 10:54:03 +0100 Subject: [PATCH 2/2] Check username is undefined before setting it --- lib/handlers/connect.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/handlers/connect.js b/lib/handlers/connect.js index e35c3c92..5a42ce96 100644 --- a/lib/handlers/connect.js +++ b/lib/handlers/connect.js @@ -114,7 +114,10 @@ function authenticate (arg, done) { } if (!err && successful) { client._authorized = true - client.username = username || null; + if (typeof(client.username) == "undefined") { + // check it's undefined, it could have been set in preConnect + client.username = username || null; + } return done() }