mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-15 00:15:15 +08:00
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:
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -44,6 +43,12 @@ namespace MQTTnet.Server
|
||||
/// </summary>
|
||||
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>
|
||||
/// 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
|
||||
/// </summary>
|
||||
public bool? CleanSession => _connectPacket.CleanSession;
|
||||
|
||||
public X509Certificate2 ClientCertificate => _clientAdapter.ClientCertificate;
|
||||
public X509Certificate2 ClientCertificate => ChannelAdapter.ClientCertificate;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the client identifier.
|
||||
@@ -64,9 +69,9 @@ namespace MQTTnet.Server
|
||||
/// </summary>
|
||||
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>
|
||||
/// 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;
|
||||
|
||||
|
||||
@@ -623,25 +623,25 @@ namespace MQTTnet.Server
|
||||
|
||||
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>()
|
||||
};
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user