From 2d03c267e4f1f4be7abf7519c7ea476c7640c7c5 Mon Sep 17 00:00:00 2001 From: Christian Kratky Date: Mon, 27 May 2019 19:57:36 +0200 Subject: [PATCH] Expose binary password to MqttConnectionValidatorContext. --- .../MQTTnet.Server/Mqtt/MqttConnectionValidator.cs | 9 +-------- .../Server/MqttConnectionValidatorContext.cs | 14 +++++++++++--- Tests/MQTTnet.TestApp.NetCore/ServerTest.cs | 3 +-- .../MainPage.xaml.cs | 7 ++----- 4 files changed, 15 insertions(+), 18 deletions(-) diff --git a/Source/MQTTnet.Server/Mqtt/MqttConnectionValidator.cs b/Source/MQTTnet.Server/Mqtt/MqttConnectionValidator.cs index f6b3340e..fd27d767 100644 --- a/Source/MQTTnet.Server/Mqtt/MqttConnectionValidator.cs +++ b/Source/MQTTnet.Server/Mqtt/MqttConnectionValidator.cs @@ -1,5 +1,4 @@ using System; -using System.Text; using System.Threading.Tasks; using IronPython.Runtime; using Microsoft.Extensions.Logging; @@ -23,12 +22,6 @@ namespace MQTTnet.Server.Mqtt { try { - string passwordString = null; - if (context.Password != null) - { - passwordString = Encoding.UTF8.GetString(context.Password); - } - var pythonContext = new PythonDictionary { { "client_id", context.ClientId }, @@ -36,7 +29,7 @@ namespace MQTTnet.Server.Mqtt { "is_secure_connection", context.IsSecureConnection }, { "username", context.Username }, { "password", context.Password }, - { "password_string", passwordString }, + { "raw_password", context.RawPassword }, { "result", PythonConvert.Pythonfy(context.ReturnCode) } }; diff --git a/Source/MQTTnet/Server/MqttConnectionValidatorContext.cs b/Source/MQTTnet/Server/MqttConnectionValidatorContext.cs index cba71fb3..a00cffcc 100644 --- a/Source/MQTTnet/Server/MqttConnectionValidatorContext.cs +++ b/Source/MQTTnet/Server/MqttConnectionValidatorContext.cs @@ -5,11 +5,17 @@ namespace MQTTnet.Server { public class MqttConnectionValidatorContext { - public MqttConnectionValidatorContext(string clientId, string username, byte[] password, MqttApplicationMessage willMessage, string endpoint, bool isSecureConnection) + public MqttConnectionValidatorContext( + string clientId, + string username, + byte[] password, + MqttApplicationMessage willMessage, + string endpoint, + bool isSecureConnection) { ClientId = clientId; Username = username; - Password = password; + RawPassword = password; WillMessage = willMessage; Endpoint = endpoint; IsSecureConnection = isSecureConnection; @@ -19,7 +25,9 @@ namespace MQTTnet.Server public string Username { get; } - public byte[] Password { get; } + public string Password => Encoding.UTF8.GetString(RawPassword ?? new byte[0]); + + public byte[] RawPassword { get; } public MqttApplicationMessage WillMessage { get; } diff --git a/Tests/MQTTnet.TestApp.NetCore/ServerTest.cs b/Tests/MQTTnet.TestApp.NetCore/ServerTest.cs index 621d7c59..bd626719 100644 --- a/Tests/MQTTnet.TestApp.NetCore/ServerTest.cs +++ b/Tests/MQTTnet.TestApp.NetCore/ServerTest.cs @@ -28,8 +28,7 @@ namespace MQTTnet.TestApp.NetCore { if (p.ClientId == "SpecialClient") { - var password = Encoding.UTF8.GetString(p.Password); - if (p.Username != "USER" || password != "PASS") + if (p.Username != "USER" || p.Password != "PASS") { p.ReturnCode = MqttConnectReturnCode.ConnectionRefusedBadUsernameOrPassword; } diff --git a/Tests/MQTTnet.TestApp.UniversalWindows/MainPage.xaml.cs b/Tests/MQTTnet.TestApp.UniversalWindows/MainPage.xaml.cs index f5b1c587..94abc418 100644 --- a/Tests/MQTTnet.TestApp.UniversalWindows/MainPage.xaml.cs +++ b/Tests/MQTTnet.TestApp.UniversalWindows/MainPage.xaml.cs @@ -633,9 +633,7 @@ namespace MQTTnet.TestApp.UniversalWindows return; } - var password = Encoding.UTF8.GetString(c.Password); - - if (password != "mySecretPassword") + if (c.Password != "mySecretPassword") { c.ReturnCode = MqttConnectReturnCode.ConnectionRefusedBadUsernameOrPassword; return; @@ -719,8 +717,7 @@ namespace MQTTnet.TestApp.UniversalWindows return; } - var password = Encoding.UTF8.GetString(c.Password); - if (password != "mySecretPassword") + if (c.Password != "mySecretPassword") { c.ReturnCode = MqttConnectReturnCode.ConnectionRefusedBadUsernameOrPassword; return;