From cbf413f04e9c92c9685529276b73049b473895d7 Mon Sep 17 00:00:00 2001 From: Christian <6939810+chkr1011@users.noreply.github.com> Date: Sat, 23 Jul 2022 15:33:13 +0200 Subject: [PATCH] Squashed commit of the following: 1125-expose-http-context-in-validateconnectionasync commit 353c9671a04a273798b0b3775be5f0764bfd7edf Author: Christian <6939810+chkr1011@users.noreply.github.com> Date: Sat Jul 23 15:33:01 2022 +0200 Remove usage of obsolete stuff. commit 1f3c35685c7a65453c37a37e8c54a2ca4305ff6d Author: Christian <6939810+chkr1011@users.noreply.github.com> Date: Sat Jul 23 15:30:54 2022 +0200 Update ValidatingConnectionEventArgs.cs commit a5e1b8938974b5b57e95ec41d74634ca130a8ad5 Merge: 8878efee 11249d00 Author: Christian <6939810+chkr1011@users.noreply.github.com> Date: Sat Jul 23 15:30:30 2022 +0200 Merge branch 'master' into 1125-expose-http-context-in-validateconnectionasync commit 8878efee0da3c67d90bb7fb6946453f62329cd8e Author: Christian <6939810+chkr1011@users.noreply.github.com> Date: Sat Jul 23 15:30:03 2022 +0200 Expose channel adapter when validating connections. --- .github/workflows/ReleaseNotes.md | 1 + MQTTnet.sln.DotSettings | 5 ++++- Samples/Server/Server_Simple_Samples.cs | 2 +- Source/MQTTnet.TestApp/ServerTest.cs | 2 +- Source/MQTTnet.Tests/Server/Security_Tests.cs | 4 ++-- .../Events/ValidatingConnectionEventArgs.cs | 17 +++++++++++------ .../Internal/MqttClientSessionsManager.cs | 10 +++++----- 7 files changed, 25 insertions(+), 16 deletions(-) diff --git a/.github/workflows/ReleaseNotes.md b/.github/workflows/ReleaseNotes.md index 7104a49f..67631049 100644 --- a/.github/workflows/ReleaseNotes.md +++ b/.github/workflows/ReleaseNotes.md @@ -1,3 +1,4 @@ * [Client] Added support for passing the local endpoint which should be used (network card). * [Client] Exposed _PackageIdentifier_ in _MqttApplicationMessageReceivedEventArgs_ (thanks to @koepalex, #1466). * [Server] Added a new event (_ClientAcknowledgedPublishPacketAsync_) which is fired whenever a client acknowledges a PUBLISH packet (QoS 1 and 2, #487). +* [Server] Exposed channel adapter (HTTP context etc.) in connection validation (#1125). diff --git a/MQTTnet.sln.DotSettings b/MQTTnet.sln.DotSettings index 504fc3a2..87d205d0 100644 --- a/MQTTnet.sln.DotSettings +++ b/MQTTnet.sln.DotSettings @@ -29,7 +29,7 @@ OnDifferentLines OnDifferentLines <?xml version="1.0" encoding="utf-16"?> -<Patterns xmlns="urn:schemas-jetbrains-com:member-reordering-patterns"> +<Patterns xmlns="urn:schemas-jetbrains-com:member-reordering-patterns"> <TypePattern DisplayName="Non-reorderable types"> <TypePattern.Match> <Or> @@ -226,6 +226,9 @@ </Entry> </TypePattern> </Patterns> + // Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. True True True diff --git a/Samples/Server/Server_Simple_Samples.cs b/Samples/Server/Server_Simple_Samples.cs index e4d63eb4..0ea4b6b7 100644 --- a/Samples/Server/Server_Simple_Samples.cs +++ b/Samples/Server/Server_Simple_Samples.cs @@ -138,7 +138,7 @@ public static class Server_Simple_Samples e.ReasonCode = MqttConnectReasonCode.ClientIdentifierNotValid; } - if (e.Username != "ValidUser") + if (e.UserName != "ValidUser") { e.ReasonCode = MqttConnectReasonCode.BadUserNameOrPassword; } diff --git a/Source/MQTTnet.TestApp/ServerTest.cs b/Source/MQTTnet.TestApp/ServerTest.cs index 341b3dda..e71f9761 100644 --- a/Source/MQTTnet.TestApp/ServerTest.cs +++ b/Source/MQTTnet.TestApp/ServerTest.cs @@ -118,7 +118,7 @@ namespace MQTTnet.TestApp { if (e.ClientId == "SpecialClient") { - if (e.Username != "USER" || e.Password != "PASS") + if (e.UserName != "USER" || e.Password != "PASS") { e.ReasonCode = MqttConnectReasonCode.BadUserNameOrPassword; } diff --git a/Source/MQTTnet.Tests/Server/Security_Tests.cs b/Source/MQTTnet.Tests/Server/Security_Tests.cs index 7d2336b0..0e144df8 100644 --- a/Source/MQTTnet.Tests/Server/Security_Tests.cs +++ b/Source/MQTTnet.Tests/Server/Security_Tests.cs @@ -38,7 +38,7 @@ namespace MQTTnet.Tests.Server testEnvironment.Server.ValidatingConnectionAsync += eventArgs => { - if (eventArgs.Username == "SECRET") + if (eventArgs.UserName == "SECRET") { eventArgs.ReasonCode = MqttConnectReasonCode.Success; } @@ -147,7 +147,7 @@ namespace MQTTnet.Tests.Server server.ValidatingConnectionAsync += e => { - if (e.Username != "UserName1") + if (e.UserName != "UserName1") { e.ReasonCode = MqttConnectReasonCode.BadUserNameOrPassword; } diff --git a/Source/MQTTnet/Server/Events/ValidatingConnectionEventArgs.cs b/Source/MQTTnet/Server/Events/ValidatingConnectionEventArgs.cs index 8a5b48b6..eaa916e1 100644 --- a/Source/MQTTnet/Server/Events/ValidatingConnectionEventArgs.cs +++ b/Source/MQTTnet/Server/Events/ValidatingConnectionEventArgs.cs @@ -17,13 +17,12 @@ namespace MQTTnet.Server { public sealed class ValidatingConnectionEventArgs : EventArgs { - readonly IMqttChannelAdapter _clientAdapter; readonly MqttConnectPacket _connectPacket; public ValidatingConnectionEventArgs(MqttConnectPacket connectPacket, IMqttChannelAdapter clientAdapter) { _connectPacket = connectPacket ?? throw new ArgumentNullException(nameof(connectPacket)); - _clientAdapter = clientAdapter ?? throw new ArgumentNullException(nameof(clientAdapter)); + ChannelAdapter = clientAdapter ?? throw new ArgumentNullException(nameof(clientAdapter)); } /// @@ -44,6 +43,12 @@ namespace MQTTnet.Server /// public string AuthenticationMethod => _connectPacket.AuthenticationMethod; + /// + /// Gets the channel adapter. This can be a _MqttConnectionContext_ (used in ASP.NET), a _MqttChannelAdapter_ (used for + /// TCP or WebSockets) or a custom implementation. + /// + public IMqttChannelAdapter ChannelAdapter { get; } + /// /// Gets or sets a value indicating whether clean sessions are used or not. /// When a client connects to a broker it can connect using either a non persistent connection (clean session) or a @@ -56,7 +61,7 @@ namespace MQTTnet.Server /// public bool? CleanSession => _connectPacket.CleanSession; - public X509Certificate2 ClientCertificate => _clientAdapter.ClientCertificate; + public X509Certificate2 ClientCertificate => ChannelAdapter.ClientCertificate; /// /// Gets the client identifier. @@ -64,9 +69,9 @@ namespace MQTTnet.Server /// public string ClientId => _connectPacket.ClientId; - public string Endpoint => _clientAdapter.Endpoint; + public string Endpoint => ChannelAdapter.Endpoint; - public bool IsSecureConnection => _clientAdapter.IsSecureConnection; + public bool IsSecureConnection => ChannelAdapter.IsSecureConnection; /// /// Gets or sets the keep alive period. @@ -85,7 +90,7 @@ namespace MQTTnet.Server public string Password => Encoding.UTF8.GetString(RawPassword ?? PlatformAbstractionLayer.EmptyByteArray); - public MqttProtocolVersion ProtocolVersion => _clientAdapter.PacketFormatterAdapter.ProtocolVersion; + public MqttProtocolVersion ProtocolVersion => ChannelAdapter.PacketFormatterAdapter.ProtocolVersion; public byte[] RawPassword => _connectPacket.Password; diff --git a/Source/MQTTnet/Server/Internal/MqttClientSessionsManager.cs b/Source/MQTTnet/Server/Internal/MqttClientSessionsManager.cs index 7d2e47ec..cea0e381 100644 --- a/Source/MQTTnet/Server/Internal/MqttClientSessionsManager.cs +++ b/Source/MQTTnet/Server/Internal/MqttClientSessionsManager.cs @@ -623,25 +623,25 @@ namespace MQTTnet.Server async Task ValidateConnection(MqttConnectPacket connectPacket, IMqttChannelAdapter channelAdapter) { - var context = new ValidatingConnectionEventArgs(connectPacket, channelAdapter) + var eventArgs = new ValidatingConnectionEventArgs(connectPacket, channelAdapter) { SessionItems = new ConcurrentDictionary() }; - await _eventContainer.ValidatingConnectionEvent.InvokeAsync(context).ConfigureAwait(false); + await _eventContainer.ValidatingConnectionEvent.InvokeAsync(eventArgs).ConfigureAwait(false); // Check the client ID and set a random one if supported. if (string.IsNullOrEmpty(connectPacket.ClientId) && channelAdapter.PacketFormatterAdapter.ProtocolVersion == MqttProtocolVersion.V500) { - connectPacket.ClientId = context.AssignedClientIdentifier; + connectPacket.ClientId = eventArgs.AssignedClientIdentifier; } if (string.IsNullOrEmpty(connectPacket.ClientId)) { - context.ReasonCode = MqttConnectReasonCode.ClientIdentifierNotValid; + eventArgs.ReasonCode = MqttConnectReasonCode.ClientIdentifierNotValid; } - return context; + return eventArgs; } } } \ No newline at end of file