use std::sync::Arc;
use mqtt_format::v3::{
connect_return::MConnectReturnCode, qos::MQualityOfService,
subscription_request::MSubscriptionRequest,
};
use crate::server::ClientId;
#[derive(Debug, thiserror::Error)]
pub enum LoginError {
#[error("The given password did not match")]
InvalidPassword,
}
impl LoginError {
pub fn as_rejection_code(&self) -> MConnectReturnCode {
match self {
LoginError::InvalidPassword => MConnectReturnCode::BadUsernamePassword,
}
}
}
#[async_trait::async_trait]
pub trait LoginHandler: Send + Sync + 'static {
async fn allow_login(
&self,
client_id: Arc<ClientId>,
username: Option<&str>,
password: Option<&[u8]>,
) -> Result<(), LoginError>;
}
pub struct AllowAllLogins;
#[async_trait::async_trait]
impl LoginHandler for AllowAllLogins {
async fn allow_login(
&self,
_client_id: Arc<ClientId>,
_username: Option<&str>,
_password: Option<&[u8]>,
) -> Result<(), LoginError> {
Ok(())
}
}
#[async_trait::async_trait]
pub trait SubscriptionHandler: Send + Sync + 'static {
async fn allow_subscription(
&self,
client_id: Arc<ClientId>,
subscription: MSubscriptionRequest<'_>,
) -> Option<MQualityOfService>;
}
pub struct AllowAllSubscriptions;
#[async_trait::async_trait]
impl SubscriptionHandler for AllowAllSubscriptions {
async fn allow_subscription(
&self,
_client_id: Arc<ClientId>,
subscription: MSubscriptionRequest<'_>,
) -> Option<MQualityOfService> {
Some(subscription.qos)
}
}