use super::property::check_property_type_list;
use super::{Properties, PropertyType, ReasonCode};
use crate::{
ByteArray, DecodeError, DecodePacket, EncodeError, EncodePacket, FixedHeader, Packet,
PacketType, VarIntError,
};
#[allow(clippy::module_name_repetitions)]
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct AuthPacket {
reason_code: ReasonCode,
properties: Properties,
}
impl AuthPacket {
#[must_use]
pub fn new() -> Self {
Self::default()
}
pub fn set_reason_code(&mut self, code: ReasonCode) -> &mut Self {
self.reason_code = code;
self
}
#[must_use]
pub const fn reason_code(&self) -> ReasonCode {
self.reason_code
}
pub fn properties_mut(&mut self) -> &mut Properties {
&mut self.properties
}
#[must_use]
pub const fn properties(&self) -> &Properties {
&self.properties
}
}
pub const AUTH_REASONS: &[ReasonCode] = &[
ReasonCode::Success,
ReasonCode::ContinueAuthentication,
ReasonCode::ReAuthenticate,
];
pub const AUTH_PROPERTIES: &[PropertyType] = &[
PropertyType::AuthenticationMethod,
PropertyType::AuthenticationData,
PropertyType::ReasonString,
PropertyType::UserProperty,
];
impl EncodePacket for AuthPacket {
fn encode(&self, buf: &mut Vec<u8>) -> Result<usize, EncodeError> {
let old_len = buf.len();
let remaining_length = ReasonCode::bytes() + self.properties.bytes();
let fixed_header = FixedHeader::new(PacketType::PingRequest, remaining_length)?;
fixed_header.encode(buf)?;
self.reason_code.encode(buf)?;
self.properties.encode(buf)?;
Ok(buf.len() - old_len)
}
}
impl DecodePacket for AuthPacket {
fn decode(ba: &mut ByteArray) -> Result<Self, DecodeError> {
let fixed_header = FixedHeader::decode(ba)?;
if fixed_header.packet_type() != PacketType::Auth {
return Err(DecodeError::InvalidPacketType);
}
if fixed_header.remaining_length() == 0 {
return Ok(Self::default());
}
let reason_code = ReasonCode::decode(ba)?;
if !AUTH_REASONS.contains(&reason_code) {
log::error!("Invalid reason code: {:?}", reason_code);
return Err(DecodeError::InvalidReasonCode);
}
let properties = Properties::decode(ba)?;
if let Err(property_type) = check_property_type_list(properties.props(), AUTH_PROPERTIES) {
log::error!(
"v5/AuthPacket: property type {:?} cannot be used in properties!",
property_type
);
return Err(DecodeError::InvalidPropertyType);
}
Ok(Self {
reason_code,
properties,
})
}
}
impl Packet for AuthPacket {
fn packet_type(&self) -> PacketType {
PacketType::Auth
}
fn bytes(&self) -> Result<usize, VarIntError> {
let remaining_length = ReasonCode::bytes() + self.properties.bytes();
let fixed_header = FixedHeader::new(PacketType::PingRequest, remaining_length)?;
Ok(fixed_header.bytes() + remaining_length)
}
}