From a9b394401b0381557710b40a5b9b7078cceb79be Mon Sep 17 00:00:00 2001 From: Christian Kratky Date: Wed, 25 Oct 2017 19:50:37 +0200 Subject: [PATCH] Add support for chaning passwords --- .../Implementations/MqttServerAdapter.cs | 3 +- .../Implementations/MqttTcpChannel.cs | 2 +- .../MQTTnet.UniversalWindows/MqttFactory.cs | 2 +- MQTTnet.Core/Client/BaseMqttClientOptions.cs | 8 ++--- MQTTnet.Core/Client/IMqttClientCredentials.cs | 8 +++++ MQTTnet.Core/Client/IMqttClientOptions.cs | 8 ++--- MQTTnet.Core/Client/MqttClient.cs | 4 +-- MQTTnet.Core/Client/MqttClientCredentials.cs | 9 +++++ MQTTnet.Core/Client/MqttClientTcpOptions.cs | 2 +- .../Client/MqttClientWebSocketOptions.cs | 2 +- .../IManagedMqttClientOptions.cs | 2 -- .../ManagedClient/ManagedMqttClient.cs | 2 -- .../ManagedClient/ManagedMqttClientOptions.cs | 3 -- .../ManagedClientTest.cs | 33 ++++++++----------- .../MainPage.xaml.cs | 10 ++++-- 15 files changed, 52 insertions(+), 46 deletions(-) create mode 100644 MQTTnet.Core/Client/IMqttClientCredentials.cs create mode 100644 MQTTnet.Core/Client/MqttClientCredentials.cs diff --git a/Frameworks/MQTTnet.NetStandard/Implementations/MqttServerAdapter.cs b/Frameworks/MQTTnet.NetStandard/Implementations/MqttServerAdapter.cs index 2d3db15..9e65715 100644 --- a/Frameworks/MQTTnet.NetStandard/Implementations/MqttServerAdapter.cs +++ b/Frameworks/MQTTnet.NetStandard/Implementations/MqttServerAdapter.cs @@ -7,10 +7,8 @@ using System.Security.Cryptography.X509Certificates; using System.Threading; using System.Threading.Tasks; using MQTTnet.Core.Adapter; -using MQTTnet.Core.Serializer; using MQTTnet.Core.Server; using Microsoft.Extensions.Logging; -using MQTTnet.Core.Client; namespace MQTTnet.Implementations { @@ -18,6 +16,7 @@ namespace MQTTnet.Implementations { private readonly ILogger _logger; private readonly IMqttCommunicationAdapterFactory _mqttCommunicationAdapterFactory; + private CancellationTokenSource _cancellationTokenSource; private Socket _defaultEndpointSocket; private Socket _tlsEndpointSocket; diff --git a/Frameworks/MQTTnet.UniversalWindows/Implementations/MqttTcpChannel.cs b/Frameworks/MQTTnet.UniversalWindows/Implementations/MqttTcpChannel.cs index 1a96f1e..03206f1 100644 --- a/Frameworks/MQTTnet.UniversalWindows/Implementations/MqttTcpChannel.cs +++ b/Frameworks/MQTTnet.UniversalWindows/Implementations/MqttTcpChannel.cs @@ -89,7 +89,7 @@ namespace MQTTnet.Implementations RawReceiveStream = ReceiveStream; } - private static Certificate LoadCertificate(BaseMqttClientOptions options) + private static Certificate LoadCertificate(MqttClientOptions options) { if (options.TlsOptions.Certificates == null || !options.TlsOptions.Certificates.Any()) { diff --git a/Frameworks/MQTTnet.UniversalWindows/MqttFactory.cs b/Frameworks/MQTTnet.UniversalWindows/MqttFactory.cs index 9761150..a3aeca9 100644 --- a/Frameworks/MQTTnet.UniversalWindows/MqttFactory.cs +++ b/Frameworks/MQTTnet.UniversalWindows/MqttFactory.cs @@ -79,7 +79,7 @@ namespace MQTTnet public MqttPacketSerializer CreateSerializer(MqttProtocolVersion protocolVersion) { - return new MqttPacketSerializer() + return new MqttPacketSerializer { ProtocolVersion = protocolVersion }; diff --git a/MQTTnet.Core/Client/BaseMqttClientOptions.cs b/MQTTnet.Core/Client/BaseMqttClientOptions.cs index ca3f82e..ea7ea7e 100644 --- a/MQTTnet.Core/Client/BaseMqttClientOptions.cs +++ b/MQTTnet.Core/Client/BaseMqttClientOptions.cs @@ -3,20 +3,18 @@ using MQTTnet.Core.Serializer; namespace MQTTnet.Core.Client { - public abstract class BaseMqttClientOptions : IMqttClientOptions + public class MqttClientOptions : IMqttClientOptions { public MqttClientTlsOptions TlsOptions { get; set; } = new MqttClientTlsOptions(); public MqttApplicationMessage WillMessage { get; set; } - public string UserName { get; set; } - - public string Password { get; set; } - public string ClientId { get; set; } = Guid.NewGuid().ToString("N"); public bool CleanSession { get; set; } = true; + public IMqttClientCredentials Credentials { get; set; } = new MqttClientCredentials(); + public TimeSpan KeepAlivePeriod { get; set; } = TimeSpan.FromSeconds(5); public TimeSpan DefaultCommunicationTimeout { get; set; } = TimeSpan.FromSeconds(10); diff --git a/MQTTnet.Core/Client/IMqttClientCredentials.cs b/MQTTnet.Core/Client/IMqttClientCredentials.cs new file mode 100644 index 0000000..c4dfc12 --- /dev/null +++ b/MQTTnet.Core/Client/IMqttClientCredentials.cs @@ -0,0 +1,8 @@ +namespace MQTTnet.Core.Client +{ + public interface IMqttClientCredentials + { + string Password { get; } + string Username { get; } + } +} \ No newline at end of file diff --git a/MQTTnet.Core/Client/IMqttClientOptions.cs b/MQTTnet.Core/Client/IMqttClientOptions.cs index 7e675ff..f805de0 100644 --- a/MQTTnet.Core/Client/IMqttClientOptions.cs +++ b/MQTTnet.Core/Client/IMqttClientOptions.cs @@ -5,14 +5,14 @@ namespace MQTTnet.Core.Client { public interface IMqttClientOptions { - bool CleanSession { get; } string ClientId { get; } + bool CleanSession { get; } + MqttApplicationMessage WillMessage { get; } + IMqttClientCredentials Credentials { get; } + TimeSpan DefaultCommunicationTimeout { get; } TimeSpan KeepAlivePeriod { get; } - string Password { get; } MqttProtocolVersion ProtocolVersion { get; } MqttClientTlsOptions TlsOptions { get; } - string UserName { get; } - MqttApplicationMessage WillMessage { get; } } } \ No newline at end of file diff --git a/MQTTnet.Core/Client/MqttClient.cs b/MQTTnet.Core/Client/MqttClient.cs index 67ffb12..e209404 100644 --- a/MQTTnet.Core/Client/MqttClient.cs +++ b/MQTTnet.Core/Client/MqttClient.cs @@ -186,8 +186,8 @@ namespace MQTTnet.Core.Client var connectPacket = new MqttConnectPacket { ClientId = _options.ClientId, - Username = _options.UserName, - Password = _options.Password, + Username = _options.Credentials?.Username, + Password = _options.Credentials?.Password, CleanSession = _options.CleanSession, KeepAlivePeriod = (ushort)_options.KeepAlivePeriod.TotalSeconds, WillMessage = willApplicationMessage diff --git a/MQTTnet.Core/Client/MqttClientCredentials.cs b/MQTTnet.Core/Client/MqttClientCredentials.cs new file mode 100644 index 0000000..085778a --- /dev/null +++ b/MQTTnet.Core/Client/MqttClientCredentials.cs @@ -0,0 +1,9 @@ +namespace MQTTnet.Core.Client +{ + public class MqttClientCredentials : IMqttClientCredentials + { + public string Username { get; set; } + + public string Password { get; set; } + } +} diff --git a/MQTTnet.Core/Client/MqttClientTcpOptions.cs b/MQTTnet.Core/Client/MqttClientTcpOptions.cs index 51d2b97..beaa506 100644 --- a/MQTTnet.Core/Client/MqttClientTcpOptions.cs +++ b/MQTTnet.Core/Client/MqttClientTcpOptions.cs @@ -1,6 +1,6 @@ namespace MQTTnet.Core.Client { - public class MqttClientTcpOptions : BaseMqttClientOptions + public class MqttClientTcpOptions : MqttClientOptions { public string Server { get; set; } diff --git a/MQTTnet.Core/Client/MqttClientWebSocketOptions.cs b/MQTTnet.Core/Client/MqttClientWebSocketOptions.cs index 522fb9b..1a3fe03 100644 --- a/MQTTnet.Core/Client/MqttClientWebSocketOptions.cs +++ b/MQTTnet.Core/Client/MqttClientWebSocketOptions.cs @@ -3,7 +3,7 @@ using System.Net; namespace MQTTnet.Core.Client { - public class MqttClientWebSocketOptions : BaseMqttClientOptions + public class MqttClientWebSocketOptions : MqttClientOptions { public string Uri { get; set; } diff --git a/MQTTnet.Core/ManagedClient/IManagedMqttClientOptions.cs b/MQTTnet.Core/ManagedClient/IManagedMqttClientOptions.cs index c812ea9..298de9a 100644 --- a/MQTTnet.Core/ManagedClient/IManagedMqttClientOptions.cs +++ b/MQTTnet.Core/ManagedClient/IManagedMqttClientOptions.cs @@ -10,7 +10,5 @@ namespace MQTTnet.Core.ManagedClient TimeSpan AutoReconnectDelay { get; } IManagedMqttClientStorage Storage { get; } - - Func PasswordProvider { get; } } } \ No newline at end of file diff --git a/MQTTnet.Core/ManagedClient/ManagedMqttClient.cs b/MQTTnet.Core/ManagedClient/ManagedMqttClient.cs index 0d37fe7..0018a58 100644 --- a/MQTTnet.Core/ManagedClient/ManagedMqttClient.cs +++ b/MQTTnet.Core/ManagedClient/ManagedMqttClient.cs @@ -6,7 +6,6 @@ using System.Threading; using System.Threading.Tasks; using MQTTnet.Core.Client; using MQTTnet.Core.Exceptions; -using MQTTnet.Core.Packets; using MQTTnet.Core.Protocol; using Microsoft.Extensions.Logging; @@ -277,7 +276,6 @@ namespace MQTTnet.Core.ManagedClient try { - _options.PasswordProvider?.Invoke(_options); await _mqttClient.ConnectAsync(_options.ClientOptions).ConfigureAwait(false); return ReconnectionResult.Reconnected; } diff --git a/MQTTnet.Core/ManagedClient/ManagedMqttClientOptions.cs b/MQTTnet.Core/ManagedClient/ManagedMqttClientOptions.cs index 45b2af3..2e24fd0 100644 --- a/MQTTnet.Core/ManagedClient/ManagedMqttClientOptions.cs +++ b/MQTTnet.Core/ManagedClient/ManagedMqttClientOptions.cs @@ -10,8 +10,5 @@ namespace MQTTnet.Core.ManagedClient public TimeSpan AutoReconnectDelay { get; set; } = TimeSpan.FromSeconds(5); public IManagedMqttClientStorage Storage { get; set; } - - public Func PasswordProvider { get; set; } - } } diff --git a/Tests/MQTTnet.TestApp.NetCore/ManagedClientTest.cs b/Tests/MQTTnet.TestApp.NetCore/ManagedClientTest.cs index 05dd0cf..587b847 100644 --- a/Tests/MQTTnet.TestApp.NetCore/ManagedClientTest.cs +++ b/Tests/MQTTnet.TestApp.NetCore/ManagedClientTest.cs @@ -3,7 +3,6 @@ using System.Threading.Tasks; using MQTTnet.Core; using MQTTnet.Core.Client; using MQTTnet.Core.ManagedClient; -using MQTTnet.Core.Packets; using MQTTnet.Core.Protocol; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; @@ -25,32 +24,21 @@ namespace MQTTnet.TestApp.NetCore services.GetService() .AddConsole(); - ClientRetainedMessageHandler ms = new ClientRetainedMessageHandler(); - Func func = delegate (ManagedMqttClientOptions managedMqttClientOptions) - { - return "password"; - }; - + var ms = new ClientRetainedMessageHandler(); + var options = new ManagedMqttClientOptions { ClientOptions = new MqttClientTcpOptions { Server = "broker.hivemq.com", ClientId = "MQTTnetManagedClientTest", - Password = "pippo", + Credentials = new RandomPassword() }, AutoReconnectDelay = TimeSpan.FromSeconds(1), - Storage = ms, - PasswordProvider = o => - { - //o.ClientOptions.Password = GetPassword(); - return o.ClientOptions.Password; - } + Storage = ms }; - - try { var managedClient = services.GetRequiredService(); @@ -78,11 +66,18 @@ namespace MQTTnet.TestApp.NetCore } - public static string GetPassword() + public class RandomPassword : IMqttClientCredentials { - return "password"; - } + public string Password + { + get + { + return Guid.NewGuid().ToString(); // The random password. + } + } + public string Username => "the_static_user"; + } public class ClientRetainedMessageHandler : IManagedMqttClientStorage { diff --git a/Tests/MQTTnet.TestApp.UniversalWindows/MainPage.xaml.cs b/Tests/MQTTnet.TestApp.UniversalWindows/MainPage.xaml.cs index 993e227..68dd42a 100644 --- a/Tests/MQTTnet.TestApp.UniversalWindows/MainPage.xaml.cs +++ b/Tests/MQTTnet.TestApp.UniversalWindows/MainPage.xaml.cs @@ -42,7 +42,7 @@ namespace MQTTnet.TestApp.UniversalWindows private async void Connect(object sender, RoutedEventArgs e) { - BaseMqttClientOptions options = null; + MqttClientOptions options = null; if (UseTcp.IsChecked == true) { options = new MqttClientTcpOptions @@ -64,8 +64,12 @@ namespace MQTTnet.TestApp.UniversalWindows throw new InvalidOperationException(); } - options.UserName = User.Text; - options.Password = Password.Text; + options.Credentials = new MqttClientCredentials + { + Username = User.Text, + Password = Password.Text + }; + options.ClientId = ClientId.Text; options.TlsOptions.UseTls = UseTls.IsChecked == true; options.TlsOptions.IgnoreCertificateChainErrors = true;