use crate::common::error::Result;
use crate::common::device::DeviceInfo;
use async_trait::async_trait;
#[derive(Debug, Clone)]
pub struct AuthResult {
pub authenticated: bool,
pub user_id: Option<String>,
pub error_message: Option<String>,
pub user_metadata: Option<std::collections::HashMap<String, String>>,
}
impl AuthResult {
pub fn success(user_id: Option<String>) -> Self {
Self {
authenticated: true,
user_id,
error_message: None,
user_metadata: None,
}
}
pub fn success_with_metadata(
user_id: Option<String>,
metadata: std::collections::HashMap<String, String>,
) -> Self {
Self {
authenticated: true,
user_id,
error_message: None,
user_metadata: Some(metadata),
}
}
pub fn failure(error_message: String) -> Self {
Self {
authenticated: false,
user_id: None,
error_message: Some(error_message),
user_metadata: None,
}
}
}
#[async_trait]
pub trait Authenticator: Send + Sync {
async fn authenticate(
&self,
token: &str,
connection_id: &str,
device_info: Option<&DeviceInfo>,
metadata: Option<&std::collections::HashMap<String, Vec<u8>>>,
) -> Result<AuthResult>;
async fn is_authenticated(&self, connection_id: &str) -> Result<bool> {
let _ = connection_id;
Ok(true)
}
async fn revoke_authentication(&self, connection_id: &str) -> Result<()> {
let _ = connection_id;
Ok(())
}
}