pub mod builder;
pub mod builtin;
pub mod executor;
pub mod registry;
pub use builder::FlowBuilder;
pub use registry::{FlowRegistration, FlowRegistry};
use std::collections::HashMap;
use crate::error::{GarrisonError, GarrisonResult};
#[derive(Debug, Clone)]
pub enum AuthStep {
Login {
credential_type: String,
},
Mfa {
credential_type: Option<String>,
},
SocialProvider {
provider: String,
},
SsoServer {
server_id: String,
},
RequiredAction {
action: String,
},
Conditional {
condition: AuthCondition,
if_step: Box<AuthStep>,
else_step: Option<Box<AuthStep>>,
},
SubFlow {
flow_name: String,
},
}
#[derive(Debug, Clone)]
pub enum AuthCondition {
HasCredential(String),
IsLocked,
IpWhitelisted,
Custom(String),
}
#[derive(Debug, Clone, Default)]
pub struct IpWhitelist {
cidrs: Vec<ipnetwork::IpNetwork>,
}
impl IpWhitelist {
pub fn parse(items: &[String]) -> GarrisonResult<Self> {
let mut cidrs = Vec::with_capacity(items.len());
for item in items {
let net = ipnetwork::IpNetwork::try_from(item.trim()).map_err(|e| {
GarrisonError::InvalidParam(format!("ip-whitelist-parse-failed::{item}::{e}"))
})?;
cidrs.push(net);
}
Ok(Self { cidrs })
}
pub fn contains(&self, ip: &str) -> bool {
match ip.parse::<std::net::IpAddr>() {
Ok(addr) => self.cidrs.iter().any(|net| net.contains(addr)),
Err(_) => false,
}
}
pub fn is_empty(&self) -> bool {
self.cidrs.is_empty()
}
}
#[async_trait::async_trait]
pub trait CustomConditionEvaluator: Send + Sync {
async fn evaluate(&self, ctx: &AuthContext) -> GarrisonResult<bool>;
}
#[derive(Debug, Clone)]
pub struct AuthenticationFlow {
pub name: String,
pub steps: Vec<AuthStep>,
pub allow_skip: bool,
}
#[derive(Clone)]
pub struct AuthContext {
pub input: String,
pub user_id: Option<String>,
pub tenant_id: Option<String>,
pub ip: String,
pub completed_steps: Vec<usize>,
pub extras: HashMap<String, String>,
}
impl std::fmt::Debug for AuthContext {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("AuthContext")
.field("input", &"<redacted>")
.field("user_id", &self.user_id)
.field("tenant_id", &self.tenant_id)
.field("ip", &self.ip)
.field("completed_steps", &self.completed_steps)
.field("extras", &self.extras)
.finish()
}
}
#[derive(Clone)]
pub enum AuthResult {
Success {
login_id: String,
token: String,
},
Failed {
reason: String,
step: usize,
},
Pending {
completed_step: usize,
next_step: usize,
challenge: String,
},
ChallengeRequired {
challenge_type: String,
message: String,
},
}
impl std::fmt::Debug for AuthResult {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
fn mask_token(token: &str) -> String {
let prefix_len = 4;
if token.chars().count() > prefix_len {
let prefix: String = token.chars().take(prefix_len).collect();
format!("{prefix}<redacted>")
} else {
"<redacted>".to_string()
}
}
match self {
AuthResult::Success { login_id, token } => f
.debug_struct("AuthResult")
.field("variant", &"Success")
.field("login_id", login_id)
.field("token", &mask_token(token))
.finish(),
AuthResult::Failed { reason, step } => f
.debug_struct("AuthResult")
.field("variant", &"Failed")
.field("reason", reason)
.field("step", step)
.finish(),
AuthResult::Pending {
completed_step,
next_step,
challenge,
} => f
.debug_struct("AuthResult")
.field("variant", &"Pending")
.field("completed_step", completed_step)
.field("next_step", next_step)
.field("challenge", challenge)
.finish(),
AuthResult::ChallengeRequired {
challenge_type,
message,
} => f
.debug_struct("AuthResult")
.field("variant", &"ChallengeRequired")
.field("challenge_type", challenge_type)
.field("message", message)
.finish(),
}
}
}
#[cfg(test)]
mod tests;