@@ -1,4 +1,5 @@ | |||||
using System; | using System; | ||||
using System.Text; | |||||
using System.Threading.Tasks; | using System.Threading.Tasks; | ||||
using IronPython.Runtime; | using IronPython.Runtime; | ||||
using Microsoft.Extensions.Logging; | using Microsoft.Extensions.Logging; | ||||
@@ -22,6 +23,12 @@ namespace MQTTnet.Server.Mqtt | |||||
{ | { | ||||
try | try | ||||
{ | { | ||||
string passwordString = null; | |||||
if (context.Password != null) | |||||
{ | |||||
passwordString = Encoding.UTF8.GetString(context.Password); | |||||
} | |||||
var pythonContext = new PythonDictionary | var pythonContext = new PythonDictionary | ||||
{ | { | ||||
{ "client_id", context.ClientId }, | { "client_id", context.ClientId }, | ||||
@@ -29,6 +36,7 @@ namespace MQTTnet.Server.Mqtt | |||||
{ "is_secure_connection", context.IsSecureConnection }, | { "is_secure_connection", context.IsSecureConnection }, | ||||
{ "username", context.Username }, | { "username", context.Username }, | ||||
{ "password", context.Password }, | { "password", context.Password }, | ||||
{ "password_string", passwordString }, | |||||
{ "result", PythonConvert.Pythonfy(context.ReturnCode) } | { "result", PythonConvert.Pythonfy(context.ReturnCode) } | ||||
}; | }; | ||||
@@ -29,7 +29,7 @@ def on_validate_client_connection(context): | |||||
context["result"] = "connection_refused_not_authorized" | context["result"] = "connection_refused_not_authorized" | ||||
return | return | ||||
if context["password"] != "secret": | |||||
if context["password_string"] != "secret": | |||||
context["result"] = "connection_refused_not_authorized" | context["result"] = "connection_refused_not_authorized" | ||||
print(context) | print(context) | ||||
@@ -2,7 +2,7 @@ | |||||
{ | { | ||||
public interface IMqttClientCredentials | public interface IMqttClientCredentials | ||||
{ | { | ||||
string Password { get; } | |||||
string Username { get; } | string Username { get; } | ||||
byte[] Password { get; } | |||||
} | } | ||||
} | } |
@@ -4,6 +4,6 @@ | |||||
{ | { | ||||
public string Username { get; set; } | public string Username { get; set; } | ||||
public string Password { get; set; } | |||||
public byte[] Password { get; set; } | |||||
} | } | ||||
} | } |
@@ -1,5 +1,6 @@ | |||||
using System; | using System; | ||||
using System.Linq; | using System.Linq; | ||||
using System.Text; | |||||
using MQTTnet.Client.ExtendedAuthenticationExchange; | using MQTTnet.Client.ExtendedAuthenticationExchange; | ||||
using MQTTnet.Formatter; | using MQTTnet.Formatter; | ||||
@@ -116,6 +117,18 @@ namespace MQTTnet.Client.Options | |||||
} | } | ||||
public MqttClientOptionsBuilder WithCredentials(string username, string password = null) | public MqttClientOptionsBuilder WithCredentials(string username, string password = null) | ||||
{ | |||||
byte[] passwordBuffer = null; | |||||
if (password != null) | |||||
{ | |||||
passwordBuffer = Encoding.UTF8.GetBytes(password); | |||||
} | |||||
return WithCredentials(username, passwordBuffer); | |||||
} | |||||
public MqttClientOptionsBuilder WithCredentials(string username, byte[] password = null) | |||||
{ | { | ||||
_options.Credentials = new MqttClientCredentials | _options.Credentials = new MqttClientCredentials | ||||
{ | { | ||||
@@ -293,7 +293,7 @@ namespace MQTTnet.Formatter.V3 | |||||
if (passwordFlag) | if (passwordFlag) | ||||
{ | { | ||||
packet.Password = body.ReadStringWithLengthPrefix(); | |||||
packet.Password = body.ReadWithLengthPrefix(); | |||||
} | } | ||||
ValidateConnectPacket(packet); | ValidateConnectPacket(packet); | ||||
@@ -208,7 +208,7 @@ namespace MQTTnet.Formatter.V5 | |||||
if (passwordFlag) | if (passwordFlag) | ||||
{ | { | ||||
packet.Password = body.ReadStringWithLengthPrefix(); | |||||
packet.Password = body.ReadWithLengthPrefix(); | |||||
} | } | ||||
return packet; | return packet; | ||||
@@ -6,7 +6,7 @@ | |||||
public string Username { get; set; } | public string Username { get; set; } | ||||
public string Password { get; set; } | |||||
public byte[] Password { get; set; } | |||||
public ushort KeepAlivePeriod { get; set; } | public ushort KeepAlivePeriod { get; set; } | ||||
@@ -23,13 +23,14 @@ | |||||
public override string ToString() | public override string ToString() | ||||
{ | { | ||||
var password = Password; | |||||
if (!string.IsNullOrEmpty(password)) | |||||
var passwordText = string.Empty; | |||||
if (Password != null) | |||||
{ | { | ||||
password = "****"; | |||||
passwordText = "****"; | |||||
} | } | ||||
return string.Concat("Connect: [ClientId=", ClientId, "] [Username=", Username, "] [Password=", password, "] [KeepAlivePeriod=", KeepAlivePeriod, "] [CleanSession=", CleanSession, "]"); | |||||
return string.Concat("Connect: [ClientId=", ClientId, "] [Username=", Username, "] [Password=", passwordText, "] [KeepAlivePeriod=", KeepAlivePeriod, "] [CleanSession=", CleanSession, "]"); | |||||
} | } | ||||
} | } | ||||
} | } |
@@ -1,10 +1,11 @@ | |||||
using MQTTnet.Protocol; | |||||
using System.Text; | |||||
using MQTTnet.Protocol; | |||||
namespace MQTTnet.Server | namespace MQTTnet.Server | ||||
{ | { | ||||
public class MqttConnectionValidatorContext | public class MqttConnectionValidatorContext | ||||
{ | { | ||||
public MqttConnectionValidatorContext(string clientId, string username, string password, MqttApplicationMessage willMessage, string endpoint, bool isSecureConnection) | |||||
public MqttConnectionValidatorContext(string clientId, string username, byte[] password, MqttApplicationMessage willMessage, string endpoint, bool isSecureConnection) | |||||
{ | { | ||||
ClientId = clientId; | ClientId = clientId; | ||||
Username = username; | Username = username; | ||||
@@ -18,7 +19,7 @@ namespace MQTTnet.Server | |||||
public string Username { get; } | public string Username { get; } | ||||
public string Password { get; } | |||||
public byte[] Password { get; } | |||||
public MqttApplicationMessage WillMessage { get; } | public MqttApplicationMessage WillMessage { get; } | ||||
@@ -23,7 +23,7 @@ namespace MQTTnet.Tests | |||||
var p = new MqttConnectPacket | var p = new MqttConnectPacket | ||||
{ | { | ||||
ClientId = "XYZ", | ClientId = "XYZ", | ||||
Password = "PASS", | |||||
Password = Encoding.UTF8.GetBytes("PASS"), | |||||
Username = "USER", | Username = "USER", | ||||
KeepAlivePeriod = 123, | KeepAlivePeriod = 123, | ||||
CleanSession = true | CleanSession = true | ||||
@@ -38,7 +38,7 @@ namespace MQTTnet.Tests | |||||
var p = new MqttConnectPacket | var p = new MqttConnectPacket | ||||
{ | { | ||||
ClientId = "XYZ", | ClientId = "XYZ", | ||||
Password = "PASS", | |||||
Password = Encoding.UTF8.GetBytes("PASS"), | |||||
Username = "USER", | Username = "USER", | ||||
KeepAlivePeriod = 123, | KeepAlivePeriod = 123, | ||||
CleanSession = true | CleanSession = true | ||||
@@ -53,7 +53,7 @@ namespace MQTTnet.Tests | |||||
var p = new MqttConnectPacket | var p = new MqttConnectPacket | ||||
{ | { | ||||
ClientId = "XYZ", | ClientId = "XYZ", | ||||
Password = "PASS", | |||||
Password = Encoding.UTF8.GetBytes("PASS"), | |||||
Username = "USER", | Username = "USER", | ||||
KeepAlivePeriod = 123, | KeepAlivePeriod = 123, | ||||
CleanSession = true, | CleanSession = true, | ||||
@@ -75,7 +75,7 @@ namespace MQTTnet.Tests | |||||
var p = new MqttConnectPacket | var p = new MqttConnectPacket | ||||
{ | { | ||||
ClientId = "XYZ", | ClientId = "XYZ", | ||||
Password = "PASS", | |||||
Password = Encoding.UTF8.GetBytes("PASS"), | |||||
Username = "USER", | Username = "USER", | ||||
KeepAlivePeriod = 123, | KeepAlivePeriod = 123, | ||||
CleanSession = true | CleanSession = true | ||||
@@ -90,7 +90,7 @@ namespace MQTTnet.Tests | |||||
var p = new MqttConnectPacket | var p = new MqttConnectPacket | ||||
{ | { | ||||
ClientId = "XYZ", | ClientId = "XYZ", | ||||
Password = "PASS", | |||||
Password = Encoding.UTF8.GetBytes("PASS"), | |||||
Username = "USER", | Username = "USER", | ||||
KeepAlivePeriod = 123, | KeepAlivePeriod = 123, | ||||
CleanSession = true, | CleanSession = true, | ||||
@@ -62,7 +62,7 @@ namespace MQTTnet.TestApp.NetCore | |||||
public class RandomPassword : IMqttClientCredentials | public class RandomPassword : IMqttClientCredentials | ||||
{ | { | ||||
public string Password => Guid.NewGuid().ToString(); | |||||
public byte[] Password => Guid.NewGuid().ToByteArray(); | |||||
public string Username => "the_static_user"; | public string Username => "the_static_user"; | ||||
} | } | ||||
@@ -28,7 +28,8 @@ namespace MQTTnet.TestApp.NetCore | |||||
{ | { | ||||
if (p.ClientId == "SpecialClient") | if (p.ClientId == "SpecialClient") | ||||
{ | { | ||||
if (p.Username != "USER" || p.Password != "PASS") | |||||
var password = Encoding.UTF8.GetString(p.Password); | |||||
if (p.Username != "USER" || password != "PASS") | |||||
{ | { | ||||
p.ReturnCode = MqttConnectReturnCode.ConnectionRefusedBadUsernameOrPassword; | p.ReturnCode = MqttConnectReturnCode.ConnectionRefusedBadUsernameOrPassword; | ||||
} | } | ||||
@@ -138,7 +138,7 @@ namespace MQTTnet.TestApp.UniversalWindows | |||||
options.Credentials = new MqttClientCredentials | options.Credentials = new MqttClientCredentials | ||||
{ | { | ||||
Username = User.Text, | Username = User.Text, | ||||
Password = Password.Text | |||||
Password = Encoding.UTF8.GetBytes(Password.Text) | |||||
}; | }; | ||||
} | } | ||||
@@ -539,7 +539,7 @@ namespace MQTTnet.TestApp.UniversalWindows | |||||
//... | //... | ||||
} | } | ||||
client.UseApplicationMessageReceivedHandler(Handler); | |||||
client.UseApplicationMessageReceivedHandler(e => Handler(e)); | |||||
// Subscribe after connect | // Subscribe after connect | ||||
@@ -601,7 +601,7 @@ namespace MQTTnet.TestApp.UniversalWindows | |||||
Credentials = new MqttClientCredentials | Credentials = new MqttClientCredentials | ||||
{ | { | ||||
Username = "bud", | Username = "bud", | ||||
Password = "%spencer%" | |||||
Password = Encoding.UTF8.GetBytes("%spencer%") | |||||
}, | }, | ||||
ChannelOptions = new MqttClientTcpOptions | ChannelOptions = new MqttClientTcpOptions | ||||
{ | { | ||||
@@ -633,7 +633,9 @@ namespace MQTTnet.TestApp.UniversalWindows | |||||
return; | return; | ||||
} | } | ||||
if (c.Password != "mySecretPassword") | |||||
var password = Encoding.UTF8.GetString(c.Password); | |||||
if (password != "mySecretPassword") | |||||
{ | { | ||||
c.ReturnCode = MqttConnectReturnCode.ConnectionRefusedBadUsernameOrPassword; | c.ReturnCode = MqttConnectReturnCode.ConnectionRefusedBadUsernameOrPassword; | ||||
return; | return; | ||||
@@ -717,7 +719,8 @@ namespace MQTTnet.TestApp.UniversalWindows | |||||
return; | return; | ||||
} | } | ||||
if (c.Password != "mySecretPassword") | |||||
var password = Encoding.UTF8.GetString(c.Password); | |||||
if (password != "mySecretPassword") | |||||
{ | { | ||||
c.ReturnCode = MqttConnectReturnCode.ConnectionRefusedBadUsernameOrPassword; | c.ReturnCode = MqttConnectReturnCode.ConnectionRefusedBadUsernameOrPassword; | ||||
return; | return; | ||||