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.
This commit is contained in:
Christian
2022-07-23 15:33:53 +02:00
parent 11249d006f
commit cbf413f04e
7 changed files with 25 additions and 16 deletions
+1
View File
@@ -1,3 +1,4 @@
* [Client] Added support for passing the local endpoint which should be used (network card). * [Client] Added support for passing the local endpoint which should be used (network card).
* [Client] Exposed _PackageIdentifier_ in _MqttApplicationMessageReceivedEventArgs_ (thanks to @koepalex, #1466). * [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] 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).
+4 -1
View File
@@ -29,7 +29,7 @@
<s:String x:Key="/Default/CodeStyle/CodeFormatting/XmlDocFormatter/ProcessingInstructionAttributesFormat/@EntryValue">OnDifferentLines</s:String> <s:String x:Key="/Default/CodeStyle/CodeFormatting/XmlDocFormatter/ProcessingInstructionAttributesFormat/@EntryValue">OnDifferentLines</s:String>
<s:String x:Key="/Default/CodeStyle/CodeFormatting/XmlDocFormatter/TagAttributesFormat/@EntryValue">OnDifferentLines</s:String> <s:String x:Key="/Default/CodeStyle/CodeFormatting/XmlDocFormatter/TagAttributesFormat/@EntryValue">OnDifferentLines</s:String>
<s:String x:Key="/Default/CodeStyle/CSharpFileLayoutPatterns/Pattern/@EntryValue">&lt;?xml version="1.0" encoding="utf-16"?&gt; <s:String x:Key="/Default/CodeStyle/CSharpFileLayoutPatterns/Pattern/@EntryValue">&lt;?xml version="1.0" encoding="utf-16"?&gt;
&lt;Patterns xmlns="urn:schemas-jetbrains-com:member-reordering-patterns"&gt; &lt;Patterns xmlns="urn:schemas-jetbrains-com:member-reordering-patterns"&gt;
&lt;TypePattern DisplayName="Non-reorderable types"&gt; &lt;TypePattern DisplayName="Non-reorderable types"&gt;
&lt;TypePattern.Match&gt; &lt;TypePattern.Match&gt;
&lt;Or&gt; &lt;Or&gt;
@@ -226,6 +226,9 @@
&lt;/Entry&gt; &lt;/Entry&gt;
&lt;/TypePattern&gt; &lt;/TypePattern&gt;
&lt;/Patterns&gt;</s:String> &lt;/Patterns&gt;</s:String>
<s:String x:Key="/Default/CodeStyle/FileHeader/FileHeaderText/@EntryValue">// 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.</s:String>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpAttributeForSingleLineMethodUpgrade/@EntryIndexedValue">True</s:Boolean> <s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpAttributeForSingleLineMethodUpgrade/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpKeepExistingMigration/@EntryIndexedValue">True</s:Boolean> <s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpKeepExistingMigration/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpPlaceEmbeddedOnSameLineMigration/@EntryIndexedValue">True</s:Boolean> <s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ECSharpPlaceEmbeddedOnSameLineMigration/@EntryIndexedValue">True</s:Boolean>
+1 -1
View File
@@ -138,7 +138,7 @@ public static class Server_Simple_Samples
e.ReasonCode = MqttConnectReasonCode.ClientIdentifierNotValid; e.ReasonCode = MqttConnectReasonCode.ClientIdentifierNotValid;
} }
if (e.Username != "ValidUser") if (e.UserName != "ValidUser")
{ {
e.ReasonCode = MqttConnectReasonCode.BadUserNameOrPassword; e.ReasonCode = MqttConnectReasonCode.BadUserNameOrPassword;
} }
+1 -1
View File
@@ -118,7 +118,7 @@ namespace MQTTnet.TestApp
{ {
if (e.ClientId == "SpecialClient") if (e.ClientId == "SpecialClient")
{ {
if (e.Username != "USER" || e.Password != "PASS") if (e.UserName != "USER" || e.Password != "PASS")
{ {
e.ReasonCode = MqttConnectReasonCode.BadUserNameOrPassword; e.ReasonCode = MqttConnectReasonCode.BadUserNameOrPassword;
} }
@@ -38,7 +38,7 @@ namespace MQTTnet.Tests.Server
testEnvironment.Server.ValidatingConnectionAsync += eventArgs => testEnvironment.Server.ValidatingConnectionAsync += eventArgs =>
{ {
if (eventArgs.Username == "SECRET") if (eventArgs.UserName == "SECRET")
{ {
eventArgs.ReasonCode = MqttConnectReasonCode.Success; eventArgs.ReasonCode = MqttConnectReasonCode.Success;
} }
@@ -147,7 +147,7 @@ namespace MQTTnet.Tests.Server
server.ValidatingConnectionAsync += e => server.ValidatingConnectionAsync += e =>
{ {
if (e.Username != "UserName1") if (e.UserName != "UserName1")
{ {
e.ReasonCode = MqttConnectReasonCode.BadUserNameOrPassword; e.ReasonCode = MqttConnectReasonCode.BadUserNameOrPassword;
} }
@@ -17,13 +17,12 @@ namespace MQTTnet.Server
{ {
public sealed class ValidatingConnectionEventArgs : EventArgs public sealed class ValidatingConnectionEventArgs : EventArgs
{ {
readonly IMqttChannelAdapter _clientAdapter;
readonly MqttConnectPacket _connectPacket; readonly MqttConnectPacket _connectPacket;
public ValidatingConnectionEventArgs(MqttConnectPacket connectPacket, IMqttChannelAdapter clientAdapter) public ValidatingConnectionEventArgs(MqttConnectPacket connectPacket, IMqttChannelAdapter clientAdapter)
{ {
_connectPacket = connectPacket ?? throw new ArgumentNullException(nameof(connectPacket)); _connectPacket = connectPacket ?? throw new ArgumentNullException(nameof(connectPacket));
_clientAdapter = clientAdapter ?? throw new ArgumentNullException(nameof(clientAdapter)); ChannelAdapter = clientAdapter ?? throw new ArgumentNullException(nameof(clientAdapter));
} }
/// <summary> /// <summary>
@@ -44,6 +43,12 @@ namespace MQTTnet.Server
/// </summary> /// </summary>
public string AuthenticationMethod => _connectPacket.AuthenticationMethod; public string AuthenticationMethod => _connectPacket.AuthenticationMethod;
/// <summary>
/// Gets the channel adapter. This can be a _MqttConnectionContext_ (used in ASP.NET), a _MqttChannelAdapter_ (used for
/// TCP or WebSockets) or a custom implementation.
/// </summary>
public IMqttChannelAdapter ChannelAdapter { get; }
/// <summary> /// <summary>
/// Gets or sets a value indicating whether clean sessions are used or not. /// 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 /// 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
/// </summary> /// </summary>
public bool? CleanSession => _connectPacket.CleanSession; public bool? CleanSession => _connectPacket.CleanSession;
public X509Certificate2 ClientCertificate => _clientAdapter.ClientCertificate; public X509Certificate2 ClientCertificate => ChannelAdapter.ClientCertificate;
/// <summary> /// <summary>
/// Gets the client identifier. /// Gets the client identifier.
@@ -64,9 +69,9 @@ namespace MQTTnet.Server
/// </summary> /// </summary>
public string ClientId => _connectPacket.ClientId; 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;
/// <summary> /// <summary>
/// Gets or sets the keep alive period. /// Gets or sets the keep alive period.
@@ -85,7 +90,7 @@ namespace MQTTnet.Server
public string Password => Encoding.UTF8.GetString(RawPassword ?? PlatformAbstractionLayer.EmptyByteArray); 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; public byte[] RawPassword => _connectPacket.Password;
@@ -623,25 +623,25 @@ namespace MQTTnet.Server
async Task<ValidatingConnectionEventArgs> ValidateConnection(MqttConnectPacket connectPacket, IMqttChannelAdapter channelAdapter) async Task<ValidatingConnectionEventArgs> ValidateConnection(MqttConnectPacket connectPacket, IMqttChannelAdapter channelAdapter)
{ {
var context = new ValidatingConnectionEventArgs(connectPacket, channelAdapter) var eventArgs = new ValidatingConnectionEventArgs(connectPacket, channelAdapter)
{ {
SessionItems = new ConcurrentDictionary<object, object>() SessionItems = new ConcurrentDictionary<object, object>()
}; };
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. // Check the client ID and set a random one if supported.
if (string.IsNullOrEmpty(connectPacket.ClientId) && channelAdapter.PacketFormatterAdapter.ProtocolVersion == MqttProtocolVersion.V500) if (string.IsNullOrEmpty(connectPacket.ClientId) && channelAdapter.PacketFormatterAdapter.ProtocolVersion == MqttProtocolVersion.V500)
{ {
connectPacket.ClientId = context.AssignedClientIdentifier; connectPacket.ClientId = eventArgs.AssignedClientIdentifier;
} }
if (string.IsNullOrEmpty(connectPacket.ClientId)) if (string.IsNullOrEmpty(connectPacket.ClientId))
{ {
context.ReasonCode = MqttConnectReasonCode.ClientIdentifierNotValid; eventArgs.ReasonCode = MqttConnectReasonCode.ClientIdentifierNotValid;
} }
return context; return eventArgs;
} }
} }
} }