MqttConnectionValidatorContext.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Security.Cryptography.X509Certificates;
  4. using System.Text;
  5. using MQTTnet.Adapter;
  6. using MQTTnet.Formatter;
  7. using MQTTnet.Packets;
  8. using MQTTnet.Protocol;
  9. namespace MQTTnet.Server
  10. {
  11. public class MqttConnectionValidatorContext
  12. {
  13. private readonly MqttConnectPacket _connectPacket;
  14. private readonly IMqttChannelAdapter _clientAdapter;
  15. public MqttConnectionValidatorContext(MqttConnectPacket connectPacket, IMqttChannelAdapter clientAdapter, IDictionary<object, object> sessionItems)
  16. {
  17. _connectPacket = connectPacket;
  18. _clientAdapter = clientAdapter ?? throw new ArgumentNullException(nameof(clientAdapter));
  19. SessionItems = sessionItems;
  20. }
  21. public string ClientId => _connectPacket.ClientId;
  22. public string Endpoint => _clientAdapter.Endpoint;
  23. public bool IsSecureConnection => _clientAdapter.IsSecureConnection;
  24. public X509Certificate2 ClientCertificate => _clientAdapter.ClientCertificate;
  25. public MqttProtocolVersion ProtocolVersion => _clientAdapter.PacketFormatterAdapter.ProtocolVersion;
  26. public string Username => _connectPacket?.Username;
  27. public byte[] RawPassword => _connectPacket?.Password;
  28. public string Password => Encoding.UTF8.GetString(RawPassword ?? new byte[0]);
  29. public MqttApplicationMessage WillMessage => _connectPacket?.WillMessage;
  30. public bool? CleanSession => _connectPacket?.CleanSession;
  31. public ushort? KeepAlivePeriod => _connectPacket?.KeepAlivePeriod;
  32. public List<MqttUserProperty> UserProperties => _connectPacket?.Properties?.UserProperties;
  33. public byte[] AuthenticationData => _connectPacket?.Properties?.AuthenticationData;
  34. public string AuthenticationMethod => _connectPacket?.Properties?.AuthenticationMethod;
  35. public uint? MaximumPacketSize => _connectPacket?.Properties?.MaximumPacketSize;
  36. public ushort? ReceiveMaximum => _connectPacket?.Properties?.ReceiveMaximum;
  37. public ushort? TopicAliasMaximum => _connectPacket?.Properties?.TopicAliasMaximum;
  38. public bool? RequestProblemInformation => _connectPacket?.Properties?.RequestProblemInformation;
  39. public bool? RequestResponseInformation => _connectPacket?.Properties?.RequestResponseInformation;
  40. public uint? SessionExpiryInterval => _connectPacket?.Properties?.SessionExpiryInterval;
  41. public uint? WillDelayInterval => _connectPacket?.Properties?.WillDelayInterval;
  42. /// <summary>
  43. /// Gets or sets a key/value collection that can be used to share data within the scope of this session.
  44. /// </summary>
  45. public IDictionary<object, object> SessionItems { get; }
  46. /// <summary>
  47. /// This is used for MQTTv3 only.
  48. /// </summary>
  49. [Obsolete("Use ReasonCode instead. It is MQTTv5 only but will be converted to a valid ReturnCode.")]
  50. public MqttConnectReturnCode ReturnCode
  51. {
  52. get => new MqttConnectReasonCodeConverter().ToConnectReturnCode(ReasonCode);
  53. set => ReasonCode = new MqttConnectReasonCodeConverter().ToConnectReasonCode(value);
  54. }
  55. /// <summary>
  56. /// This is used for MQTTv5 only. When a MQTTv3 client connects the enum value must be one which is
  57. /// also supported in MQTTv3. Otherwise the connection attempt will fail because not all codes can be
  58. /// converted properly.
  59. /// </summary>
  60. public MqttConnectReasonCode ReasonCode { get; set; } = MqttConnectReasonCode.Success;
  61. public List<MqttUserProperty> ResponseUserProperties { get; set; }
  62. public byte[] ResponseAuthenticationData { get; set; }
  63. public string AssignedClientIdentifier { get; set; }
  64. public string ReasonString { get; set; }
  65. }
  66. }