use async_trait::async_trait;
use dashmap::DashMap;
use std::sync::Arc;
use crate::core::token::Token;
use crate::error::{GarrisonError, GarrisonResult};
use crate::session::GarrisonSession;
#[async_trait]
pub trait SwitchToGuard: Send + Sync {
async fn check(&self, original_login_id: &str, target_login_id: &str) -> GarrisonResult<()>;
}
pub struct DenyAllSwitchToGuard;
#[async_trait]
pub trait AuthLogic: Send + Sync {
async fn login(&self, id: &str, params: Option<&str>) -> GarrisonResult<String>;
async fn logout(&self, token: &str) -> GarrisonResult<()>;
async fn is_login(&self, token: &str) -> GarrisonResult<bool>;
async fn get_login_id(&self, token: &str) -> GarrisonResult<Option<String>>;
async fn verify_token(&self, token: &str) -> GarrisonResult<String>;
async fn switch_to(&self, _token: &str, _target_login_id: &str) -> GarrisonResult<()> {
Err(GarrisonError::NotImplemented(format!(
"core-switch-to-not-implemented::{}",
std::any::type_name::<Self>()
)))
}
async fn renew_to_equivalent(&self, _token: &str) -> GarrisonResult<String> {
Err(GarrisonError::NotImplemented(format!(
"core-renew-not-implemented::{}",
std::any::type_name::<Self>()
)))
}
}
pub struct AuthLogicDefault {
session: Arc<GarrisonSession>,
token_handler: Arc<dyn Token>,
timeout: i64,
remember_me_enabled: bool,
remember_me_timeout: i64,
seed_primary_amr: bool,
switch_to_guard: Arc<dyn SwitchToGuard>,
renew_locks: Arc<DashMap<String, Arc<tokio::sync::Mutex<()>>>>,
}
mod default;
mod guards;
#[cfg(test)]
pub(super) use default::parse_remember_me_param;
#[cfg(test)]
mod mock;
#[cfg(test)]
mod tests;