From 29918fb45475f9bb20ff6cb0757054d6a6e1fbb6 Mon Sep 17 00:00:00 2001 From: Gerardo Date: Wed, 25 Oct 2017 15:56:29 +0200 Subject: [PATCH] Password Provider for dynamic password --- .../IManagedMqttClientOptions.cs | 2 + .../ManagedClient/ManagedMqttClient.cs | 1 + .../ManagedClient/ManagedMqttClientOptions.cs | 3 + .../ManagedClientTest.cs | 55 +++++++++++++++++- Tests/MQTTnet.TestApp.NetCore/messages.bin | Bin 474 -> 0 bytes 5 files changed, 59 insertions(+), 2 deletions(-) delete mode 100644 Tests/MQTTnet.TestApp.NetCore/messages.bin diff --git a/MQTTnet.Core/ManagedClient/IManagedMqttClientOptions.cs b/MQTTnet.Core/ManagedClient/IManagedMqttClientOptions.cs index 298de9a..c812ea9 100644 --- a/MQTTnet.Core/ManagedClient/IManagedMqttClientOptions.cs +++ b/MQTTnet.Core/ManagedClient/IManagedMqttClientOptions.cs @@ -10,5 +10,7 @@ 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 6b2e1dd..0d37fe7 100644 --- a/MQTTnet.Core/ManagedClient/ManagedMqttClient.cs +++ b/MQTTnet.Core/ManagedClient/ManagedMqttClient.cs @@ -277,6 +277,7 @@ 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 2e24fd0..45b2af3 100644 --- a/MQTTnet.Core/ManagedClient/ManagedMqttClientOptions.cs +++ b/MQTTnet.Core/ManagedClient/ManagedMqttClientOptions.cs @@ -10,5 +10,8 @@ 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 b54de06..05dd0cf 100644 --- a/Tests/MQTTnet.TestApp.NetCore/ManagedClientTest.cs +++ b/Tests/MQTTnet.TestApp.NetCore/ManagedClientTest.cs @@ -7,6 +7,9 @@ using MQTTnet.Core.Packets; using MQTTnet.Core.Protocol; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; +using System.IO; +using Newtonsoft.Json; +using System.Collections.Generic; namespace MQTTnet.TestApp.NetCore { @@ -22,18 +25,32 @@ namespace MQTTnet.TestApp.NetCore services.GetService() .AddConsole(); + ClientRetainedMessageHandler ms = new ClientRetainedMessageHandler(); + Func func = delegate (ManagedMqttClientOptions managedMqttClientOptions) + { + return "password"; + }; var options = new ManagedMqttClientOptions { ClientOptions = new MqttClientTcpOptions { Server = "broker.hivemq.com", - ClientId = "MQTTnetManagedClientTest" + ClientId = "MQTTnetManagedClientTest", + Password = "pippo", }, - AutoReconnectDelay = TimeSpan.FromSeconds(1) + AutoReconnectDelay = TimeSpan.FromSeconds(1), + Storage = ms, + PasswordProvider = o => + { + //o.ClientOptions.Password = GetPassword(); + return o.ClientOptions.Password; + } }; + + try { var managedClient = services.GetRequiredService(); @@ -59,5 +76,39 @@ namespace MQTTnet.TestApp.NetCore Console.WriteLine(e); } } + + + public static string GetPassword() + { + return "password"; + } + + + public class ClientRetainedMessageHandler : IManagedMqttClientStorage + { + private const string Filename = @"RetainedMessages.json"; + + public Task SaveQueuedMessagesAsync(IList messages) + { + File.WriteAllText(Filename, JsonConvert.SerializeObject(messages)); + return Task.FromResult(0); + } + + public Task> LoadQueuedMessagesAsync() + { + IList retainedMessages; + if (File.Exists(Filename)) + { + var json = File.ReadAllText(Filename); + retainedMessages = JsonConvert.DeserializeObject>(json); + } + else + { + retainedMessages = new List(); + } + + return Task.FromResult(retainedMessages); + } + } } } diff --git a/Tests/MQTTnet.TestApp.NetCore/messages.bin b/Tests/MQTTnet.TestApp.NetCore/messages.bin deleted file mode 100644 index 59505fa898386f36e09b72ce0a44d1f4a8c2c083..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 474 zcmbu6K}*9h7=|;~xeomiy<5U|9z948JLty5uuCajRz5~BP0IHrLj6_yhngrm7DV4b zLJoP}CkdM{#y&&h8gqXWrrq{M2m_7_Aotk!d$Sltn}J>gu;M# zD0QCg{Yk6#2?k-`z!b)7okaL$@;Z=%j+a(zXkDNX-+%#BEq_+zj;pE~>E(O0XO!K8 zfUdVFaR^eOMMlaNh~zoAjU4`?bX`w_^=+eqGtC-OACNZhKi5wfD@NLWqgvJRk<)Y$ S8?KpuZ?4~vtpCA}^t&%d=9cgP