pub struct Auth {
pub props: Option<Properties>,
/* private fields */
}
Expand description
MQTT v5.0 AUTH packet representation
The AUTH packet is sent from the Client to the Server or from the Server to the Client as part of an extended authentication exchange, such as challenge/response authentication. It is also used by the Client or Server to perform re-authentication during a connection.
The AUTH packet enables enhanced authentication flows beyond the basic username/password mechanism provided in the CONNECT packet. It supports:
- SASL-based authentication mechanisms
- Challenge/response authentication flows
- Re-authentication during an active connection
- Custom authentication methods
§Packet Structure
The AUTH packet consists of:
- Fixed Header (1 byte): Packet type (0xF0) and flags
- Variable Header:
- Reason Code (1 byte, optional)
- Properties (variable length, optional if reason code present)
§Authentication Flow
Enhanced authentication is a challenge/response style authentication that can extend beyond the initial connection. The flow typically involves:
- Client sends CONNECT with Authentication Method property
- Server responds with CONNACK containing Continue Authentication reason code
- Multiple AUTH packet exchanges between client and server
- Final CONNACK with Success or failure reason code
§Re-authentication
Either the Client or Server can initiate re-authentication at any time during the connection by sending an AUTH packet with a reason code of Re-authenticate.
§Examples
use mqtt_protocol_core::mqtt;
use mqtt_protocol_core::mqtt::result_code::AuthReasonCode;
use mqtt_protocol_core::mqtt::packet::Properties;
use mqtt_protocol_core::mqtt::packet::Property;
// Create an AUTH packet for continue authentication
let auth = mqtt::packet::v5_0::Auth::builder()
.reason_code(AuthReasonCode::ContinueAuthentication)
.props(Properties::from_vec(vec![
Property::AuthenticationMethod("SCRAM-SHA-256".into()),
Property::AuthenticationData(b"challenge_data".to_vec()),
]))
.build()
.unwrap();
// Create an AUTH packet for re-authentication
let reauth = mqtt::packet::v5_0::Auth::builder()
.reason_code(AuthReasonCode::ReAuthenticate)
.build()
.unwrap();
Fields§
§props: Option<Properties>
MQTT v5.0 properties for the AUTH packet
Valid properties for AUTH packets include:
- Authentication Method: Specifies the authentication method being used
- Authentication Data: Contains method-specific authentication data
- Reason String: Human-readable string describing the reason
- User Property: Application-specific key-value pairs
Implementations§
Source§impl Auth
impl Auth
Sourcepub fn props(&self) -> &Option<Properties>
pub fn props(&self) -> &Option<Properties>
MQTT v5.0 properties for the AUTH packet
Valid properties for AUTH packets include:
- Authentication Method: Specifies the authentication method being used
- Authentication Data: Contains method-specific authentication data
- Reason String: Human-readable string describing the reason
- User Property: Application-specific key-value pairs
Source§impl Auth
impl Auth
Sourcepub fn builder() -> AuthBuilder
pub fn builder() -> AuthBuilder
Create a new AuthBuilder for constructing AUTH packets
Returns a builder instance that can be used to configure and construct an AUTH packet with the desired properties and reason code.
§Returns
AuthBuilder
- A new builder instance
§Examples
use mqtt_protocol_core::mqtt;
use mqtt_protocol_core::mqtt::result_code::AuthReasonCode;
let auth = mqtt::packet::v5_0::Auth::builder()
.reason_code(AuthReasonCode::ContinueAuthentication)
.build()
.unwrap();
Sourcepub fn packet_type() -> PacketType
pub fn packet_type() -> PacketType
Get the packet type for AUTH packets
Returns the fixed packet type identifier for AUTH packets as defined in the MQTT v5.0 specification.
§Returns
PacketType::Auth
- The AUTH packet type constant
Sourcepub fn reason_code(&self) -> Option<AuthReasonCode>
pub fn reason_code(&self) -> Option<AuthReasonCode>
Get the reason code from the AUTH packet
Extracts and parses the reason code from the packet buffer. The reason code indicates the purpose or result of the authentication operation.
§Returns
Some(AuthReasonCode)
- The parsed reason code if present and validNone
- If no reason code is present in the packet
§Examples
use mqtt_protocol_core::mqtt;
use mqtt_protocol_core::mqtt::result_code::AuthReasonCode;
let auth = mqtt::packet::v5_0::Auth::builder()
.reason_code(AuthReasonCode::Success)
.build()
.unwrap();
assert_eq!(auth.reason_code(), Some(AuthReasonCode::Success));
Sourcepub fn size(&self) -> usize
pub fn size(&self) -> usize
Calculate the total size of the AUTH packet in bytes
Returns the complete size of the packet including the fixed header, remaining length field, reason code (if present), property length (if present), and all properties.
§Returns
usize
- Total packet size in bytes
§Examples
use mqtt_protocol_core::mqtt;
let auth = mqtt::packet::v5_0::Auth::builder().build().unwrap();
let size = auth.size();
println!("AUTH packet size: {} bytes", size);
Sourcepub fn to_buffers(&self) -> Vec<IoSlice<'_>>
pub fn to_buffers(&self) -> Vec<IoSlice<'_>>
Create IoSlice buffers for efficient network I/O
Returns a vector of IoSlice
objects that can be used for vectored I/O
operations, allowing zero-copy writes to network sockets. The buffers
represent the complete AUTH packet in wire format.
§Returns
A vector of IoSlice
objects for vectored I/O operations
§Examples
use mqtt_protocol_core::mqtt;
let auth = mqtt::packet::v5_0::Auth::builder().build().unwrap();
let buffers = auth.to_buffers();
// Use with vectored write: socket.write_vectored(&buffers)?;
Sourcepub fn to_continuous_buffer(&self) -> Vec<u8> ⓘ
pub fn to_continuous_buffer(&self) -> Vec<u8> ⓘ
Create a continuous buffer containing the complete packet data
Returns a vector containing all packet bytes in a single continuous buffer.
This method provides an alternative to to_buffers()
for no-std environments
where vectored I/O is not available.
The returned buffer contains the complete AUTH packet serialized according to the MQTT v5.0 protocol specification, including fixed header, remaining length, optional reason code, and optional properties.
§Returns
A vector containing the complete packet data
§Examples
use mqtt_protocol_core::mqtt;
let auth = mqtt::packet::v5_0::Auth::builder().build().unwrap();
let buffer = auth.to_continuous_buffer();
// buffer contains all packet bytes
Sourcepub fn parse(data: &[u8]) -> Result<(Self, usize), MqttError>
pub fn parse(data: &[u8]) -> Result<(Self, usize), MqttError>
Parse an AUTH packet from raw bytes
Parses the variable header portion of an AUTH packet from the provided byte buffer. This function expects the fixed header to have already been parsed and removed.
The parsing process validates:
- Reason code validity (if present)
- Property structure and content
- Required property relationships (e.g., Authentication Data requires Authentication Method)
- Protocol compliance for reason code and property combinations
§Parameters
data
- Byte slice containing the AUTH packet variable header (excluding fixed header)
§Returns
Ok((Auth, usize))
- Successfully parsed AUTH packet and number of bytes consumedErr(MqttError::MalformedPacket)
- If the packet structure is invalidErr(MqttError::ProtocolError)
- If the packet violates MQTT protocol rules
§Protocol Rules
- If reason code is not Success, Authentication Method property is required
- Authentication Data property requires Authentication Method property
- Properties must not contain duplicates (except User Property)
- Only specific properties are allowed in AUTH packets
§Examples
use mqtt_protocol_core::mqtt;
let data = &[0x00, 0x00]; // Minimal AUTH packet: Success reason code + empty properties
let (auth_packet, consumed) = mqtt::packet::v5_0::Auth::parse(data)?;
assert_eq!(consumed, 2);
Trait Implementations§
Source§impl Debug for Auth
Debug implementation for AUTH packets
impl Debug for Auth
Debug implementation for AUTH packets
Provides debug output for AUTH packets by delegating to the Display
implementation.
This ensures consistent formatting between debug and display representations,
which is useful for logging and debugging scenarios.
The debug output is identical to the display output, showing the packet in JSON format with all relevant fields.
§Examples
use mqtt_protocol_core::mqtt;
use mqtt_protocol_core::mqtt::result_code::AuthReasonCode;
let auth = mqtt::packet::v5_0::Auth::builder()
.reason_code(AuthReasonCode::Success)
.build()
.unwrap();
println!("{:?}", auth);
// Outputs: {"type":"AUTH","reason_code":"Success","props":{}}
Source§impl Display for Auth
Display implementation for AUTH packets
impl Display for Auth
Display implementation for AUTH packets
Provides human-readable string representation of AUTH packets using JSON format.
This implementation leverages the Serialize
trait to create consistent,
structured output suitable for logging and debugging.
If serialization fails, an error message is returned instead of panicking.
§Examples
use mqtt_protocol_core::mqtt;
use mqtt_protocol_core::mqtt::result_code::AuthReasonCode;
let auth = mqtt::packet::v5_0::Auth::builder()
.reason_code(AuthReasonCode::ContinueAuthentication)
.build()
.unwrap();
println!("{}", auth);
// Outputs: {"type":"AUTH","reason_code":"ContinueAuthentication","props":{}}
Source§impl<PacketIdType> From<Auth> for GenericPacket<PacketIdType>where
PacketIdType: IsPacketId + Serialize,
impl<PacketIdType> From<Auth> for GenericPacket<PacketIdType>where
PacketIdType: IsPacketId + Serialize,
Source§fn from(v: Auth) -> GenericPacket<PacketIdType>
fn from(v: Auth) -> GenericPacket<PacketIdType>
Source§impl GenericPacketDisplay for Auth
GenericPacketDisplay implementation for AUTH packets
impl GenericPacketDisplay for Auth
GenericPacketDisplay implementation for AUTH packets
Implements the generic packet display interface that provides standardized formatting capabilities for AUTH packets. This trait enables consistent display and debug output across different packet types.
Source§impl GenericPacketTrait for Auth
GenericPacketTrait implementation for AUTH packets
impl GenericPacketTrait for Auth
GenericPacketTrait implementation for AUTH packets
Implements the common packet interface that allows AUTH packets to be used polymorphically with other MQTT packet types. This trait provides standardized methods for packet size calculation and buffer generation.
Source§impl PacketKind for Auth
PacketKind
implementation for v5.0 AUTH packet (v5.0 exclusive)
impl PacketKind for Auth
PacketKind
implementation for v5.0 AUTH packet (v5.0 exclusive)
Source§const IS_CONNECT: bool = false
const IS_CONNECT: bool = false
true
if this is a CONNECT packetSource§const IS_CONNACK: bool = false
const IS_CONNACK: bool = false
true
if this is a CONNACK packetSource§const IS_PUBLISH: bool = false
const IS_PUBLISH: bool = false
true
if this is a PUBLISH packetSource§const IS_PUBCOMP: bool = false
const IS_PUBCOMP: bool = false
true
if this is a PUBCOMP packetSource§const IS_SUBSCRIBE: bool = false
const IS_SUBSCRIBE: bool = false
true
if this is a SUBSCRIBE packetSource§const IS_UNSUBSCRIBE: bool = false
const IS_UNSUBSCRIBE: bool = false
true
if this is an UNSUBSCRIBE packetSource§const IS_UNSUBACK: bool = false
const IS_UNSUBACK: bool = false
true
if this is an UNSUBACK packetSource§const IS_PINGREQ: bool = false
const IS_PINGREQ: bool = false
true
if this is a PINGREQ packetSource§const IS_PINGRESP: bool = false
const IS_PINGRESP: bool = false
true
if this is a PINGRESP packetSource§const IS_DISCONNECT: bool = false
const IS_DISCONNECT: bool = false
true
if this is a DISCONNECT packetSource§impl Serialize for Auth
Serialize implementation for AUTH packets
impl Serialize for Auth
Serialize implementation for AUTH packets
Provides JSON serialization support for AUTH packets, enabling conversion to structured data formats for logging, debugging, and inter-process communication.
The serialized format includes:
type
: Always “AUTH” for packet type identificationreason_code
: The authentication reason code (if present)props
: The MQTT v5.0 properties (if present)
§Examples
use mqtt_protocol_core::mqtt;
use mqtt_protocol_core::mqtt::result_code::AuthReasonCode;
use serde_json;
let auth = mqtt::packet::v5_0::Auth::builder()
.reason_code(AuthReasonCode::Success)
.build()
.unwrap();
let json = serde_json::to_string(&auth).unwrap();
// Results in: {"type":"AUTH","reason_code":"Success","props":{}}
Source§impl<PacketIdType> TryInto<Auth> for GenericPacket<PacketIdType>where
PacketIdType: IsPacketId + Serialize,
impl<PacketIdType> TryInto<Auth> for GenericPacket<PacketIdType>where
PacketIdType: IsPacketId + Serialize,
impl Eq for Auth
impl SendableRole<Any> for Auth
impl SendableRole<Client> for Auth
impl SendableRole<Server> for Auth
impl StructuralPartialEq for Auth
Auto Trait Implementations§
impl Freeze for Auth
impl RefUnwindSafe for Auth
impl Send for Auth
impl Sync for Auth
impl Unpin for Auth
impl UnwindSafe for Auth
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.