use crate::core::permission::PermissionChecker;
use crate::dao::GarrisonDao;
use crate::error::GarrisonResult;
#[cfg(feature = "listener")]
use crate::listener::GarrisonListenerManager;
use crate::plugin::GarrisonPluginManager;
use crate::stp::GarrisonInterface;
#[cfg(any(
feature = "sms-rate-limit",
feature = "firewall-ratelimit",
feature = "firewall-bruteforce",
feature = "firewall-ddos",
feature = "firewall",
feature = "oauth2-server"
))]
use crate::strategy::hooks::{GarrisonFirewallCheckHook, LoginContext};
use async_trait::async_trait;
use std::collections::HashMap;
use std::sync::Arc;
#[cfg(feature = "security-extra")]
pub mod alert;
#[cfg(feature = "device-binding")]
pub mod device_binding;
#[cfg(feature = "firewall")]
pub mod firewall;
#[cfg(any(
feature = "sms-rate-limit",
feature = "firewall-ratelimit",
feature = "firewall-bruteforce",
feature = "firewall-ddos",
feature = "firewall",
feature = "oauth2-server"
))]
pub mod hooks;
pub mod rate_limiter_backend;
pub mod registry;
mod default;
#[cfg(any(
feature = "sms-rate-limit",
feature = "firewall-ratelimit",
feature = "firewall-bruteforce",
feature = "firewall-ddos",
feature = "firewall",
feature = "oauth2-server"
))]
pub use hooks::{
GarrisonFirewallCheckHookDefault, LoginContext as FirewallLoginContext, BRUTE_FORCE_THRESHOLD,
BRUTE_FORCE_WINDOW, LOGIN_FREQUENCY_THRESHOLD, LOGIN_FREQUENCY_WINDOW,
};
pub use registry::{
DefaultFirewallStrategy, DefaultLoginHandler, DefaultLogoutHandler, DefaultPermissionHandler,
DefaultSessionCreator, DefaultTokenGenerator, FirewallStrategy, LoginHandler, LogoutHandler,
PermissionHandler, SessionCreator, Strategy, TokenGenerator,
};
#[async_trait]
pub trait GarrisonPermissionStrategy: Send + Sync {
async fn invalidate_login_cache(&self, _login_id: &str) -> GarrisonResult<()> {
Ok(())
}
async fn get_permission_list(&self, login_id: &str) -> GarrisonResult<Vec<String>>;
async fn get_role_list(&self, login_id: &str) -> GarrisonResult<Vec<String>>;
async fn check_permission(&self, login_id: &str, permission: &str) -> GarrisonResult<bool>;
async fn check_permission_in_tenant(
&self,
tenant_id: i64,
login_id: &str,
permission: &str,
) -> GarrisonResult<bool>;
async fn check_role(&self, login_id: &str, role: &str) -> GarrisonResult<bool>;
async fn check_role_in_tenant(
&self,
tenant_id: i64,
login_id: &str,
role: &str,
) -> GarrisonResult<bool>;
async fn check_role_any(&self, login_id: &str, roles: &[&str]) -> GarrisonResult<bool>;
async fn check_role_all(&self, login_id: &str, roles: &[&str]) -> GarrisonResult<bool>;
async fn get_user_info(&self, _login_id: &str) -> GarrisonResult<Option<String>> {
Ok(None)
}
#[cfg(any(
feature = "sms-rate-limit",
feature = "firewall-ratelimit",
feature = "firewall-bruteforce",
feature = "firewall-ddos",
feature = "firewall",
feature = "oauth2-server"
))]
async fn check_login_hooks(&self, login_id: &str, ctx: &LoginContext) -> GarrisonResult<()>;
fn firewall_hook_injected(&self) -> bool {
false
}
}
pub struct GarrisonPermissionStrategyDefault {
interface: Arc<dyn GarrisonInterface>,
permission_checker: Option<Arc<dyn PermissionChecker>>,
dao: Option<Arc<dyn GarrisonDao>>,
tenant_id: Option<i64>,
login_type: String,
role_hierarchy: HashMap<String, Vec<String>>,
plugin_manager: Option<Arc<GarrisonPluginManager>>,
#[cfg(any(
feature = "sms-rate-limit",
feature = "firewall-ratelimit",
feature = "firewall-bruteforce",
feature = "firewall-ddos",
feature = "firewall",
feature = "oauth2-server"
))]
firewall_hook: Option<Arc<dyn GarrisonFirewallCheckHook>>,
#[cfg(feature = "listener")]
listener_manager: Option<Arc<GarrisonListenerManager>>,
}
#[cfg(test)]
mod mock;
#[cfg(test)]
mod tests;