use super::registry::FlowRegistry;
use super::{
AuthCondition, AuthContext, AuthResult, AuthStep, AuthenticationFlow, CustomConditionEvaluator,
IpWhitelist,
};
use crate::account::credential::{Credential, CredentialModel, CredentialRepository};
use crate::account::lockout::UserLockoutStrategy;
use crate::account::policy::PasswordPolicyEngine;
use crate::error::{GarrisonError, GarrisonResult};
use crate::loc;
use crate::stp::{GarrisonLogicDefault, LoginParams, MfaLogic, SessionLogic};
use crate::strategy::firewall::{FirewallContext, GarrisonFirewallStrategy};
use async_trait::async_trait;
use std::collections::HashMap;
use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;
const MAX_FLOW_DEPTH: usize = 10;
pub trait CredentialBuilder: Send + Sync {
fn build(&self, model: CredentialModel) -> GarrisonResult<Box<dyn Credential>>;
}
#[async_trait]
pub trait SocialProviderResolver: Send + Sync {
async fn resolve_login_id(
&self,
provider: &str,
code: &str,
state: &str,
) -> GarrisonResult<String>;
}
#[async_trait]
pub trait SsoServerResolver: Send + Sync {
async fn validate_and_get_login_id(
&self,
server_id: &str,
ticket: &str,
client_id: i64,
) -> GarrisonResult<String>;
}
pub struct AuthExecutor {
logic: Arc<GarrisonLogicDefault>,
credential_repo: Arc<dyn CredentialRepository>,
policy_engine: Option<Arc<PasswordPolicyEngine>>,
lockout: Option<Arc<UserLockoutStrategy>>,
registry: Arc<FlowRegistry>,
ip_whitelist: Option<IpWhitelist>,
custom_evaluators: HashMap<String, Arc<dyn CustomConditionEvaluator>>,
}
#[derive(Debug)]
enum StepOutcome {
Success { token: Option<String> },
Failed(String),
ChallengeRequired {
challenge_type: String,
message: String,
},
}
impl AuthExecutor {
pub fn new(
logic: Arc<GarrisonLogicDefault>,
credential_repo: Arc<dyn CredentialRepository>,
policy_engine: Option<Arc<PasswordPolicyEngine>>,
lockout: Option<Arc<UserLockoutStrategy>>,
registry: Arc<FlowRegistry>,
) -> Self {
Self {
logic,
credential_repo,
policy_engine,
lockout,
registry,
ip_whitelist: None,
custom_evaluators: HashMap::new(),
}
}
pub fn with_ip_whitelist(mut self, whitelist: IpWhitelist) -> Self {
self.ip_whitelist = Some(whitelist);
self
}
pub fn with_custom_evaluator(
mut self,
name: impl Into<String>,
evaluator: Arc<dyn CustomConditionEvaluator>,
) -> Self {
self.custom_evaluators.insert(name.into(), evaluator);
self
}
pub fn policy_engine(&self) -> Option<&Arc<PasswordPolicyEngine>> {
self.policy_engine.as_ref()
}
pub fn lockout(&self) -> Option<&Arc<UserLockoutStrategy>> {
self.lockout.as_ref()
}
pub async fn execute(
&self,
flow: &AuthenticationFlow,
ctx: &mut AuthContext,
) -> GarrisonResult<AuthResult> {
self.execute_inner(flow, ctx, None, None, None, None, 0)
.await
}
pub async fn execute_with_builder(
&self,
flow: &AuthenticationFlow,
ctx: &mut AuthContext,
builder: &dyn CredentialBuilder,
) -> GarrisonResult<AuthResult> {
self.execute_inner(flow, ctx, Some(builder), None, None, None, 0)
.await
}
pub async fn execute_with_full(
&self,
flow: &AuthenticationFlow,
ctx: &mut AuthContext,
builder: &dyn CredentialBuilder,
social_resolver: &dyn SocialProviderResolver,
sso_resolver: &dyn SsoServerResolver,
) -> GarrisonResult<AuthResult> {
self.execute_inner(
flow,
ctx,
Some(builder),
Some(social_resolver),
Some(sso_resolver),
None,
0,
)
.await
}
#[cfg(feature = "metrics-prometheus")]
pub async fn execute_with_metrics(
&self,
flow: &AuthenticationFlow,
ctx: &mut AuthContext,
builder: &dyn CredentialBuilder,
metrics: &crate::account::metrics::AccountMetrics,
) -> GarrisonResult<AuthResult> {
self.execute_inner(flow, ctx, Some(builder), None, None, Some(metrics), 0)
.await
}
async fn execute_inner(
&self,
flow: &AuthenticationFlow,
ctx: &mut AuthContext,
builder: Option<&dyn CredentialBuilder>,
social_resolver: Option<&dyn SocialProviderResolver>,
sso_resolver: Option<&dyn SsoServerResolver>,
metrics: Option<&crate::account::metrics::AccountMetrics>,
depth: usize,
) -> GarrisonResult<AuthResult> {
#[cfg(feature = "metrics-prometheus")]
let flow_start = std::time::Instant::now();
#[cfg(feature = "metrics-prometheus")]
let flow_name = flow.name.clone();
if flow.steps.is_empty() {
let result = Ok(AuthResult::Success {
login_id: ctx.user_id.clone().unwrap_or_default(),
token: String::new(),
});
#[cfg(feature = "metrics-prometheus")]
if let Some(m) = metrics {
m.observe_authflow_execute(&flow_name, flow_start.elapsed());
}
return result;
}
let mut token: Option<String> = None;
let mut early_result: Option<GarrisonResult<AuthResult>> = None;
for (index, step) in flow.steps.iter().enumerate() {
let outcome = self
.execute_step(
step,
ctx,
builder,
social_resolver,
sso_resolver,
metrics,
depth,
)
.await?;
if let Some(result) = self.handle_step_outcome(
outcome,
index,
&flow.steps,
flow.allow_skip,
ctx,
&mut token,
) {
early_result = Some(result);
break;
}
}
#[cfg(feature = "metrics-prometheus")]
if let Some(m) = metrics {
m.observe_authflow_execute(&flow_name, flow_start.elapsed());
}
if let Some(result) = early_result {
return result;
}
Ok(AuthResult::Success {
login_id: ctx.user_id.clone().unwrap_or_default(),
token: token.unwrap_or_default(),
})
}
fn handle_step_outcome(
&self,
outcome: StepOutcome,
index: usize,
steps: &[AuthStep],
allow_skip: bool,
ctx: &mut AuthContext,
token: &mut Option<String>,
) -> Option<GarrisonResult<AuthResult>> {
match outcome {
StepOutcome::Success { token: step_token } => {
ctx.completed_steps.push(index);
if let Some(t) = step_token {
*token = Some(t);
}
if let Some(pause_str) = ctx.extras.get("pause_after_step") {
if pause_str.parse::<usize>().ok() == Some(index) && index + 1 < steps.len() {
let challenge = self.step_challenge(&steps[index + 1]);
return Some(Ok(AuthResult::Pending {
completed_step: index,
next_step: index + 1,
challenge,
}));
}
}
None
},
StepOutcome::Failed(reason) => {
if allow_skip {
return None;
}
Some(Ok(AuthResult::Failed {
reason,
step: index,
}))
},
StepOutcome::ChallengeRequired {
challenge_type,
message,
} => Some(Ok(AuthResult::ChallengeRequired {
challenge_type,
message,
})),
}
}
fn execute_step<'a>(
&'a self,
step: &'a AuthStep,
ctx: &'a mut AuthContext,
builder: Option<&'a dyn CredentialBuilder>,
social_resolver: Option<&'a dyn SocialProviderResolver>,
sso_resolver: Option<&'a dyn SsoServerResolver>,
metrics: Option<&'a crate::account::metrics::AccountMetrics>,
depth: usize,
) -> Pin<Box<dyn Future<Output = GarrisonResult<StepOutcome>> + Send + 'a>> {
Box::pin(async move {
match step {
AuthStep::Login { credential_type } => {
self.execute_login(credential_type, ctx, builder, metrics)
.await
},
AuthStep::Mfa { credential_type } => {
self.execute_mfa(credential_type.as_ref(), ctx, builder, metrics)
.await
},
AuthStep::Conditional {
condition,
if_step,
else_step,
} => {
let condition_result = self.evaluate_condition(condition, ctx).await?;
if condition_result {
self.execute_step(
if_step,
ctx,
builder,
social_resolver,
sso_resolver,
metrics,
depth,
)
.await
} else if let Some(else_step) = else_step {
self.execute_step(
else_step,
ctx,
builder,
social_resolver,
sso_resolver,
metrics,
depth,
)
.await
} else {
Ok(StepOutcome::Success { token: None })
}
},
AuthStep::SubFlow { flow_name } => {
self.execute_subflow(
flow_name,
ctx,
builder,
social_resolver,
sso_resolver,
metrics,
depth,
)
.await
},
AuthStep::SocialProvider { provider } => {
self.execute_social(provider, ctx, social_resolver).await
},
AuthStep::SsoServer { server_id } => {
self.execute_sso(server_id, ctx, sso_resolver).await
},
AuthStep::RequiredAction { .. } => Ok(StepOutcome::Failed(
"authflow-required-action-not-implemented".to_string(),
)),
}
})
}
async fn execute_login(
&self,
credential_type: &str,
ctx: &mut AuthContext,
builder: Option<&dyn CredentialBuilder>,
metrics: Option<&crate::account::metrics::AccountMetrics>,
) -> GarrisonResult<StepOutcome> {
#[cfg(not(feature = "metrics-prometheus"))]
let _ = metrics;
if let Some(result) = self.check_login_lockout(ctx).await? {
return Ok(result);
}
let builder = match builder {
Some(b) => b,
None => {
return Ok(StepOutcome::Failed(loc!(
"authflow-login-needs-builder",
""
)));
},
};
let user_id = match &ctx.user_id {
Some(id) => id.clone(),
None => {
return Ok(StepOutcome::Failed(loc!(
"authflow-login-needs-user-id",
""
)));
},
};
let creds = self
.credential_repo
.find_by_user_and_type(&user_id, credential_type)
.await?;
if creds.is_empty() {
return Ok(StepOutcome::Failed(loc!(
"authflow-credential-not-found",
"",
("arg0", credential_type)
)));
}
let cred = builder.build(creds[0].clone())?;
#[cfg(feature = "metrics-prometheus")]
let verify_start = std::time::Instant::now();
let verified = cred.verify(&ctx.input).await?;
#[cfg(feature = "metrics-prometheus")]
if let Some(m) = metrics {
m.observe_credential_verify(credential_type, verify_start.elapsed());
}
self.handle_login_result(verified, &user_id).await
}
async fn check_login_lockout(&self, ctx: &AuthContext) -> GarrisonResult<Option<StepOutcome>> {
if let Some(lockout) = &self.lockout {
if let Some(user_id) = &ctx.user_id {
let fw_ctx = FirewallContext::new(&ctx.ip).with_login_id(user_id.clone());
if let Err(e) = lockout.check(&fw_ctx).await {
let err_str = e.to_string();
return Ok(Some(StepOutcome::Failed(loc!(
"authflow-user-locked",
"",
("arg0", &err_str)
))));
}
}
}
Ok(None)
}
async fn handle_login_result(
&self,
verified: bool,
user_id: &str,
) -> GarrisonResult<StepOutcome> {
if verified {
let session_token = self.logic.login(user_id, &LoginParams::default()).await?;
if let Some(lockout) = &self.lockout {
if let Err(e) = lockout.record_success(user_id).await {
tracing::warn!(user_id = %user_id, error = %e, "lockout record_success failed");
}
}
Ok(StepOutcome::Success {
token: Some(session_token),
})
} else {
if let Some(lockout) = &self.lockout {
if let Err(e) = lockout.record_failure(user_id).await {
tracing::warn!(user_id = %user_id, error = %e, "lockout record_failure failed");
}
}
Ok(StepOutcome::Failed(loc!(
"authflow-credential-verify-failed",
""
)))
}
}
async fn execute_mfa(
&self,
credential_type: Option<&String>,
ctx: &mut AuthContext,
builder: Option<&dyn CredentialBuilder>,
metrics: Option<&crate::account::metrics::AccountMetrics>,
) -> GarrisonResult<StepOutcome> {
#[cfg(not(feature = "metrics-prometheus"))]
let _ = metrics;
match credential_type {
None => {
self.logic.check_safe().await?;
Ok(StepOutcome::Success { token: None })
},
Some(cred_type) => {
if ctx.input.is_empty() {
return Ok(StepOutcome::ChallengeRequired {
challenge_type: cred_type.clone(),
message: loc!(
"authflow-enter-verification-code",
"",
("arg0", &**cred_type)
),
});
}
let builder = match builder {
Some(b) => b,
None => {
return Ok(StepOutcome::Failed(loc!("authflow-mfa-needs-builder", "")));
},
};
let user_id = match &ctx.user_id {
Some(id) => id.clone(),
None => {
return Ok(StepOutcome::Failed(loc!("authflow-mfa-needs-user-id", "")));
},
};
let creds = self
.credential_repo
.find_by_user_and_type(&user_id, cred_type)
.await?;
if creds.is_empty() {
return Ok(StepOutcome::Failed(loc!(
"authflow-credential-not-found",
"",
("arg0", &**cred_type)
)));
}
let cred = builder.build(creds[0].clone())?;
#[cfg(feature = "metrics-prometheus")]
let verify_start = std::time::Instant::now();
let verified = cred.verify(&ctx.input).await?;
#[cfg(feature = "metrics-prometheus")]
if let Some(m) = metrics {
m.observe_credential_verify(cred_type, verify_start.elapsed());
}
if verified {
Ok(StepOutcome::Success { token: None })
} else {
Ok(StepOutcome::Failed(loc!(
"authflow-verify-failed",
"",
("arg0", &**cred_type)
)))
}
},
}
}
async fn execute_subflow(
&self,
flow_name: &str,
ctx: &mut AuthContext,
builder: Option<&dyn CredentialBuilder>,
social_resolver: Option<&dyn SocialProviderResolver>,
sso_resolver: Option<&dyn SsoServerResolver>,
metrics: Option<&crate::account::metrics::AccountMetrics>,
depth: usize,
) -> GarrisonResult<StepOutcome> {
if depth >= MAX_FLOW_DEPTH {
let depth_str = MAX_FLOW_DEPTH.to_string();
return Ok(StepOutcome::Failed(loc!(
"authflow-max-depth-exceeded",
"",
("arg0", depth_str.as_str())
)));
}
let sub_flow = match self.registry.get(flow_name) {
Some(f) => f,
None => {
return Ok(StepOutcome::Failed(loc!(
"authflow-subflow-not-found",
"",
("arg0", flow_name)
)));
},
};
let result = self
.execute_inner(
sub_flow,
ctx,
builder,
social_resolver,
sso_resolver,
metrics,
depth + 1,
)
.await?;
match result {
AuthResult::Success { token, .. } => Ok(StepOutcome::Success {
token: if token.is_empty() { None } else { Some(token) },
}),
AuthResult::Failed { reason, .. } => {
let reason_str = reason;
Ok(StepOutcome::Failed(loc!(
"authflow-subflow-failed",
"",
("arg0", flow_name),
("arg1", &reason_str)
)))
},
AuthResult::Pending { .. } => Ok(StepOutcome::Failed(loc!(
"authflow-subflow-pending",
"",
("arg0", flow_name)
))),
AuthResult::ChallengeRequired {
challenge_type,
message,
} => Ok(StepOutcome::ChallengeRequired {
challenge_type,
message,
}),
}
}
async fn execute_social(
&self,
provider: &str,
ctx: &mut AuthContext,
social_resolver: Option<&dyn SocialProviderResolver>,
) -> GarrisonResult<StepOutcome> {
let resolver = match social_resolver {
Some(r) => r,
None => {
return Ok(StepOutcome::Failed(loc!(
"authflow-social-needs-resolver",
""
)));
},
};
if ctx.input.is_empty() {
return Ok(StepOutcome::ChallengeRequired {
challenge_type: format!("social:{}", provider),
message: loc!("authflow-complete-social-auth", "", ("arg0", provider)),
});
}
let state = ctx.extras.get("state").cloned().unwrap_or_default();
let login_id = match resolver
.resolve_login_id(provider, &ctx.input, &state)
.await
{
Ok(id) => id,
Err(e) => {
let err_str = e.to_string();
return Ok(StepOutcome::Failed(loc!(
"authflow-social-login-failed",
"",
("arg0", provider),
("arg1", &err_str)
)));
},
};
let token = self.logic.login(&login_id, &LoginParams::default()).await?;
ctx.user_id = Some(login_id.clone());
Ok(StepOutcome::Success { token: Some(token) })
}
async fn execute_sso(
&self,
server_id: &str,
ctx: &mut AuthContext,
sso_resolver: Option<&dyn SsoServerResolver>,
) -> GarrisonResult<StepOutcome> {
let resolver = match sso_resolver {
Some(r) => r,
None => {
return Ok(StepOutcome::Failed(loc!("authflow-sso-needs-resolver", "")));
},
};
if ctx.input.is_empty() {
return Ok(StepOutcome::ChallengeRequired {
challenge_type: format!("sso:{}", server_id),
message: loc!("authflow-complete-sso-auth", "", ("arg0", server_id)),
});
}
let client_id: i64 = match ctx.extras.get("client_id") {
Some(s) => s.parse().unwrap_or_else(|e| {
tracing::warn!(
server_id,
raw = %s,
error = %e,
"SSO client_id parse failed, falling back to default value 0"
);
0
}),
None => 0,
};
let login_id = match resolver
.validate_and_get_login_id(server_id, &ctx.input, client_id)
.await
{
Ok(id) => id,
Err(e) => {
let err_str = e.to_string();
return Ok(StepOutcome::Failed(loc!(
"authflow-sso-verify-failed",
"",
("arg0", &err_str)
)));
},
};
let token = self.logic.login(&login_id, &LoginParams::default()).await?;
ctx.user_id = Some(login_id.clone());
Ok(StepOutcome::Success { token: Some(token) })
}
async fn evaluate_condition(
&self,
condition: &AuthCondition,
ctx: &AuthContext,
) -> GarrisonResult<bool> {
match condition {
AuthCondition::HasCredential(cred_type) => {
if let Some(user_id) = &ctx.user_id {
let creds = self
.credential_repo
.find_by_user_and_type(user_id, cred_type)
.await?;
Ok(!creds.is_empty())
} else {
Ok(false)
}
},
AuthCondition::IsLocked => {
if let (Some(lockout), Some(user_id)) = (&self.lockout, &ctx.user_id) {
let fw_ctx = FirewallContext::new(&ctx.ip).with_login_id(user_id.clone());
Ok(lockout.check(&fw_ctx).await.is_err())
} else {
Ok(false)
}
},
AuthCondition::IpWhitelisted => match &self.ip_whitelist {
Some(whitelist) => Ok(whitelist.contains(&ctx.ip)),
None => {
tracing::debug!("authflow-ip-whitelist-not-configured (ip={})", ctx.ip);
Ok(false)
},
},
AuthCondition::Custom(name) => {
match self.custom_evaluators.get(name) {
Some(evaluator) => evaluator.evaluate(ctx).await,
None => Err(GarrisonError::InvalidParam(format!(
"authflow-unknown-custom-condition::{name}"
))),
}
},
}
}
fn step_challenge(&self, step: &AuthStep) -> String {
match step {
AuthStep::Login { credential_type } => {
loc!("authflow-step-enter", "", ("arg0", &**credential_type))
},
AuthStep::Mfa { credential_type } => match credential_type {
Some(ct) => loc!("authflow-step-enter-code", "", ("arg0", &**ct)),
None => loc!("authflow-step-complete-mfa", "").to_string(),
},
AuthStep::SocialProvider { provider } => {
loc!("authflow-step-complete-social", "", ("arg0", &**provider))
},
AuthStep::SsoServer { server_id } => {
loc!("authflow-step-complete-sso", "", ("arg0", server_id))
},
AuthStep::RequiredAction { action } => {
loc!("authflow-step-complete-action", "", ("arg0", &**action))
},
AuthStep::Conditional { .. } => loc!("authflow-step-complete-condition", ""),
AuthStep::SubFlow { flow_name } => {
loc!("authflow-step-complete-subflow", "", ("arg0", &**flow_name))
},
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::account::authflow::FlowBuilder;
use crate::account::credential::CredentialType;
use crate::config::GarrisonConfig;
use crate::dao::tests::MockDao;
use crate::dao::GarrisonDao;
use crate::error::GarrisonError;
use crate::session::GarrisonSession;
use crate::stp::GarrisonInterface;
use crate::strategy::GarrisonPermissionStrategyDefault;
use std::collections::HashMap;
struct MockCredential {
verify_result: bool,
}
#[async_trait]
impl Credential for MockCredential {
fn credential_type(&self) -> CredentialType {
"mock"
}
fn to_model(&self) -> CredentialModel {
CredentialModel {
id: "mock".to_string(),
user_id: "mock".to_string(),
credential_type: "mock".to_string(),
secret_data: String::new(),
label: None,
created_at: 0,
enabled: true,
priority: 0,
}
}
async fn verify(&self, _input: &str) -> GarrisonResult<bool> {
Ok(self.verify_result)
}
}
struct MockCredentialBuilder {
password_verify_result: bool,
totp_verify_result: bool,
}
impl CredentialBuilder for MockCredentialBuilder {
fn build(&self, model: CredentialModel) -> GarrisonResult<Box<dyn Credential>> {
let verify_result = match model.credential_type.as_str() {
"password" => self.password_verify_result,
"totp" => self.totp_verify_result,
_ => false,
};
Ok(Box::new(MockCredential { verify_result }))
}
}
#[derive(Default)]
struct MockCredentialRepository {
store: std::sync::Mutex<HashMap<String, CredentialModel>>,
}
#[async_trait]
impl CredentialRepository for MockCredentialRepository {
async fn create(&self, credential: CredentialModel) -> GarrisonResult<()> {
let mut store = self.store.lock().unwrap();
if store.contains_key(&credential.id) {
return Err(GarrisonError::InvalidParam(format!(
"credential already exists: {}",
credential.id
)));
}
store.insert(credential.id.clone(), credential);
Ok(())
}
async fn find_by_user(
&self,
caller_login_id: &str,
user_id: &str,
) -> GarrisonResult<Vec<CredentialModel>> {
if caller_login_id != user_id {
return Err(GarrisonError::NotPermission(format!(
"caller {} cannot query credentials of {}",
caller_login_id, user_id
)));
}
let store = self.store.lock().unwrap();
let mut creds: Vec<CredentialModel> = store
.values()
.filter(|c| c.user_id == user_id)
.cloned()
.collect();
creds.sort_by_key(|c| c.priority);
Ok(creds)
}
async fn find_by_user_and_type(
&self,
user_id: &str,
cred_type: &str,
) -> GarrisonResult<Vec<CredentialModel>> {
let all = self.find_by_user(user_id, user_id).await?;
Ok(all
.into_iter()
.filter(|c| c.credential_type == cred_type)
.collect())
}
async fn update(
&self,
caller_login_id: &str,
credential: CredentialModel,
) -> GarrisonResult<()> {
let mut store = self.store.lock().unwrap();
let existing = match store.get(&credential.id) {
Some(m) => m.clone(),
None => {
return Err(GarrisonError::InvalidParam(format!(
"credential not found: {}",
credential.id
)));
},
};
if existing.user_id != caller_login_id {
return Err(GarrisonError::NotPermission(format!(
"caller {} cannot update credential {} owned by {}",
caller_login_id, credential.id, existing.user_id
)));
}
if credential.user_id != existing.user_id {
return Err(GarrisonError::NotPermission(format!(
"cannot transfer credential {} from user {} to {}",
credential.id, existing.user_id, credential.user_id
)));
}
store.insert(credential.id.clone(), credential);
Ok(())
}
async fn delete(&self, caller_login_id: &str, credential_id: &str) -> GarrisonResult<()> {
let mut store = self.store.lock().unwrap();
let existing = match store.get(credential_id) {
Some(m) => m.clone(),
None => {
return Err(GarrisonError::InvalidParam(format!(
"credential not found: {}",
credential_id
)));
},
};
if existing.user_id != caller_login_id {
return Err(GarrisonError::NotPermission(format!(
"caller {} cannot delete credential {} owned by {}",
caller_login_id, credential_id, existing.user_id
)));
}
store.remove(credential_id);
Ok(())
}
}
struct MockInterface;
#[async_trait]
impl GarrisonInterface for MockInterface {
async fn get_permission_list(&self, _login_id: &str) -> GarrisonResult<Vec<String>> {
Ok(Vec::new())
}
async fn get_role_list(&self, _login_id: &str) -> GarrisonResult<Vec<String>> {
Ok(Vec::new())
}
}
fn make_logic() -> Arc<GarrisonLogicDefault> {
let dao: Arc<dyn GarrisonDao> = Arc::new(MockDao::new());
let config = Arc::new(GarrisonConfig::default_config());
let interface: Arc<dyn GarrisonInterface> = Arc::new(MockInterface);
let timeout = u64::try_from(config.timeout).unwrap_or(3600);
let session = Arc::new(GarrisonSession::new(dao.clone(), timeout, timeout, 0));
let firewall: Arc<dyn crate::strategy::GarrisonPermissionStrategy> =
Arc::new(GarrisonPermissionStrategyDefault::new(interface));
Arc::new(GarrisonLogicDefault::new(
session,
config,
firewall,
Arc::new(crate::account::disable::DefaultDisableRepository::new(
dao.clone(),
)),
))
}
fn make_executor(
credential_repo: Arc<dyn CredentialRepository>,
lockout: Option<Arc<UserLockoutStrategy>>,
) -> AuthExecutor {
AuthExecutor::new(
make_logic(),
credential_repo,
None,
lockout,
Arc::new(FlowRegistry::from_inventory()),
)
}
fn make_executor_with_registry(
credential_repo: Arc<dyn CredentialRepository>,
registry: Arc<FlowRegistry>,
) -> AuthExecutor {
AuthExecutor::new(make_logic(), credential_repo, None, None, registry)
}
fn make_context(user_id: &str, input: &str) -> AuthContext {
AuthContext {
input: input.to_string(),
user_id: Some(user_id.to_string()),
tenant_id: None,
ip: "127.0.0.1".to_string(),
completed_steps: Vec::new(),
extras: HashMap::new(),
}
}
fn make_credential_model(id: &str, user: &str, cred_type: &str) -> CredentialModel {
CredentialModel {
id: id.to_string(),
user_id: user.to_string(),
credential_type: cred_type.to_string(),
secret_data: "hash".to_string(),
label: None,
created_at: 0,
enabled: true,
priority: 0,
}
}
async fn make_repo_with_password(user: &str) -> Arc<MockCredentialRepository> {
let repo = MockCredentialRepository::default();
repo.create(make_credential_model("c1", user, "password"))
.await
.unwrap();
Arc::new(repo)
}
async fn make_repo_with_password_and_totp(user: &str) -> Arc<MockCredentialRepository> {
let repo = MockCredentialRepository::default();
repo.create(make_credential_model("c1", user, "password"))
.await
.unwrap();
repo.create(make_credential_model("c2", user, "totp"))
.await
.unwrap();
Arc::new(repo)
}
#[tokio::test]
async fn password_login_success() {
let repo = make_repo_with_password("alice").await;
let executor = make_executor(repo, None);
let builder = MockCredentialBuilder {
password_verify_result: true,
totp_verify_result: false,
};
let flow = FlowBuilder::new("test").login("password").build();
let mut ctx = make_context("alice", "correct-password");
let result = executor
.execute_with_builder(&flow, &mut ctx, &builder)
.await
.unwrap();
match result {
AuthResult::Success { login_id, token } => {
assert_eq!(login_id, "alice");
assert!(!token.is_empty(), "token 不应为空");
},
other => panic!("应为 Success,实际: {:?}", other),
}
}
#[tokio::test]
async fn password_login_failure() {
let _locale_guard = crate::i18n::set_locale(crate::i18n::GarrisonLocale::Zh);
let repo = make_repo_with_password("alice").await;
let executor = make_executor(repo, None);
let builder = MockCredentialBuilder {
password_verify_result: false,
totp_verify_result: false,
};
let flow = FlowBuilder::new("test").login("password").build();
let mut ctx = make_context("alice", "wrong-password");
let result = executor
.execute_with_builder(&flow, &mut ctx, &builder)
.await
.unwrap();
match result {
AuthResult::Failed { reason, step } => {
assert!(
reason.contains("凭证校验失败"),
"reason 应含凭证校验失败: {}",
reason
);
assert_eq!(step, 0);
},
other => panic!("应为 Failed,实际: {:?}", other),
}
}
#[cfg(not(feature = "security-extra"))]
#[tokio::test]
async fn mfa_check_safe_success() {
let repo: Arc<dyn CredentialRepository> = Arc::new(MockCredentialRepository::default());
let executor = make_executor(repo, None);
let flow = FlowBuilder::new("test").mfa(None).build();
let mut ctx = make_context("alice", "");
let result = executor.execute(&flow, &mut ctx).await.unwrap();
match result {
AuthResult::Success { login_id, .. } => {
assert_eq!(login_id, "alice");
},
other => panic!("应为 Success,实际: {:?}", other),
}
}
#[tokio::test]
async fn mfa_totp_success() {
let repo = make_repo_with_password_and_totp("alice").await;
let executor = make_executor(repo, None);
let builder = MockCredentialBuilder {
password_verify_result: false,
totp_verify_result: true,
};
let flow = FlowBuilder::new("test").mfa(Some("totp")).build();
let mut ctx = make_context("alice", "123456");
let result = executor
.execute_with_builder(&flow, &mut ctx, &builder)
.await
.unwrap();
match result {
AuthResult::Success { .. } => {},
other => panic!("应为 Success,实际: {:?}", other),
}
}
#[tokio::test]
async fn mfa_totp_failure() {
let repo = make_repo_with_password_and_totp("alice").await;
let executor = make_executor(repo, None);
let builder = MockCredentialBuilder {
password_verify_result: false,
totp_verify_result: false,
};
let flow = FlowBuilder::new("test").mfa(Some("totp")).build();
let mut ctx = make_context("alice", "wrong-code");
let result = executor
.execute_with_builder(&flow, &mut ctx, &builder)
.await
.unwrap();
match result {
AuthResult::Failed { reason, step } => {
assert!(reason.contains("totp"), "reason 应含 totp: {}", reason);
assert_eq!(step, 0);
},
other => panic!("应为 Failed,实际: {:?}", other),
}
}
#[tokio::test]
async fn conditional_true() {
let _locale_guard = crate::i18n::set_locale(crate::i18n::GarrisonLocale::Zh);
let repo = make_repo_with_password_and_totp("alice").await;
let executor = make_executor(repo, None);
let builder = MockCredentialBuilder {
password_verify_result: false,
totp_verify_result: false,
};
let flow = FlowBuilder::new("test")
.conditional(
AuthCondition::HasCredential("totp".to_string()),
AuthStep::Login {
credential_type: "password".to_string(),
},
None,
)
.build();
let mut ctx = make_context("alice", "wrong-password");
let result = executor
.execute_with_builder(&flow, &mut ctx, &builder)
.await
.unwrap();
match result {
AuthResult::Failed { reason, .. } => {
assert!(
reason.contains("凭证校验失败"),
"reason 应含凭证校验失败: {}",
reason
);
},
other => panic!(
"应为 Failed(条件为真执行 Login verify=false),实际: {:?}",
other
),
}
}
#[tokio::test]
async fn conditional_false() {
let repo = make_repo_with_password("alice").await;
let executor = make_executor(repo, None);
let flow = FlowBuilder::new("test")
.conditional(
AuthCondition::HasCredential("totp".to_string()),
AuthStep::Login {
credential_type: "password".to_string(),
},
None,
)
.build();
let mut ctx = make_context("alice", "");
let result = executor.execute(&flow, &mut ctx).await.unwrap();
match result {
AuthResult::Success { login_id, .. } => {
assert_eq!(login_id, "alice");
},
other => panic!("应为 Success(条件为假跳过),实际: {:?}", other),
}
}
#[tokio::test]
async fn subflow_executes_child() {
let repo = make_repo_with_password("alice").await;
let mut registry = FlowRegistry::from_inventory();
let child = FlowBuilder::new("child-flow").login("password").build();
registry.register(child);
let executor = make_executor_with_registry(repo, Arc::new(registry));
let builder = MockCredentialBuilder {
password_verify_result: true,
totp_verify_result: false,
};
let flow = FlowBuilder::new("parent").sub_flow("child-flow").build();
let mut ctx = make_context("alice", "correct-password");
let result = executor
.execute_with_builder(&flow, &mut ctx, &builder)
.await
.unwrap();
match result {
AuthResult::Success { login_id, .. } => {
assert_eq!(login_id, "alice");
},
other => panic!("应为 Success(子流程成功),实际: {:?}", other),
}
}
#[tokio::test]
async fn empty_steps_returns_success() {
let repo: Arc<dyn CredentialRepository> = Arc::new(MockCredentialRepository::default());
let executor = make_executor(repo, None);
let flow = FlowBuilder::new("empty").build();
let mut ctx = make_context("alice", "");
let result = executor.execute(&flow, &mut ctx).await.unwrap();
match result {
AuthResult::Success { login_id, .. } => {
assert_eq!(login_id, "alice");
},
other => panic!("空步骤应返回 Success,实际: {:?}", other),
}
}
#[cfg(not(feature = "security-extra"))]
#[tokio::test]
async fn multi_step_flow_success() {
let repo = make_repo_with_password("alice").await;
let executor = make_executor(repo, None);
let builder = MockCredentialBuilder {
password_verify_result: true,
totp_verify_result: false,
};
let flow = FlowBuilder::new("multi")
.login("password")
.mfa(None)
.build();
let mut ctx = make_context("alice", "correct-password");
let result = executor
.execute_with_builder(&flow, &mut ctx, &builder)
.await
.unwrap();
match result {
AuthResult::Success { login_id, token } => {
assert_eq!(login_id, "alice");
assert!(!token.is_empty(), "Login 步骤应生成 token");
assert_eq!(ctx.completed_steps.len(), 2, "应完成 2 个步骤");
},
other => panic!("应为 Success,实际: {:?}", other),
}
}
#[tokio::test]
async fn pending_intermediate_state() {
let repo = make_repo_with_password("alice").await;
let executor = make_executor(repo, None);
let builder = MockCredentialBuilder {
password_verify_result: true,
totp_verify_result: false,
};
let flow = FlowBuilder::new("multi")
.login("password")
.mfa(None)
.build();
let mut ctx = make_context("alice", "correct-password");
ctx.extras
.insert("pause_after_step".to_string(), "0".to_string());
let result = executor
.execute_with_builder(&flow, &mut ctx, &builder)
.await
.unwrap();
match result {
AuthResult::Pending {
completed_step,
next_step,
challenge,
} => {
assert_eq!(completed_step, 0);
assert_eq!(next_step, 1);
assert!(!challenge.is_empty(), "challenge 不应为空");
},
other => panic!("应为 Pending,实际: {:?}", other),
}
}
#[tokio::test]
async fn challenge_required_for_mfa() {
let repo = make_repo_with_password_and_totp("alice").await;
let executor = make_executor(repo, None);
let builder = MockCredentialBuilder {
password_verify_result: false,
totp_verify_result: true,
};
let flow = FlowBuilder::new("test").mfa(Some("totp")).build();
let mut ctx = make_context("alice", "");
let result = executor
.execute_with_builder(&flow, &mut ctx, &builder)
.await
.unwrap();
match result {
AuthResult::ChallengeRequired {
challenge_type,
message,
} => {
assert_eq!(challenge_type, "totp");
assert!(message.contains("totp"), "message 应含 totp: {}", message);
},
other => panic!("应为 ChallengeRequired,实际: {:?}", other),
}
}
#[tokio::test]
async fn lockout_blocks_login() {
let _locale_guard = crate::i18n::set_locale(crate::i18n::GarrisonLocale::Zh);
let dao: Arc<dyn GarrisonDao> = Arc::new(MockDao::new());
let lockout = Arc::new(UserLockoutStrategy::new(
crate::account::lockout::UserLockoutConfig {
max_failure_factor: 2,
permanent_lockout: false,
max_temporary_lockouts: 99,
wait_strategy: crate::account::lockout::WaitStrategy::Linear { base_seconds: 300 },
failure_window_seconds: 300,
},
dao,
));
lockout.record_failure("alice").await.unwrap();
lockout.record_failure("alice").await.unwrap();
let repo = make_repo_with_password("alice").await;
let executor = make_executor(repo, Some(lockout));
let builder = MockCredentialBuilder {
password_verify_result: true,
totp_verify_result: false,
};
let flow = FlowBuilder::new("test").login("password").build();
let mut ctx = make_context("alice", "correct-password");
let result = executor
.execute_with_builder(&flow, &mut ctx, &builder)
.await
.unwrap();
match result {
AuthResult::Failed { reason, step } => {
assert!(reason.contains("锁定"), "reason 应含锁定: {}", reason);
assert_eq!(step, 0);
},
other => panic!("应为 Failed(用户被锁定),实际: {:?}", other),
}
}
#[tokio::test]
async fn login_without_user_id_returns_failed() {
let repo: Arc<dyn CredentialRepository> = Arc::new(MockCredentialRepository::default());
let executor = make_executor(repo, None);
let builder = MockCredentialBuilder {
password_verify_result: true,
totp_verify_result: false,
};
let flow = FlowBuilder::new("test").login("password").build();
let mut ctx = make_context("alice", "password");
ctx.user_id = None;
let result = executor
.execute_with_builder(&flow, &mut ctx, &builder)
.await
.unwrap();
match result {
AuthResult::Failed { reason, step } => {
assert!(
reason.contains("user_id"),
"reason 应含 user_id: {}",
reason
);
assert_eq!(step, 0);
},
other => panic!("应为 Failed(无 user_id),实际: {:?}", other),
}
}
#[tokio::test]
async fn login_credential_not_found_returns_failed() {
let _locale_guard = crate::i18n::set_locale(crate::i18n::GarrisonLocale::Zh);
let repo: Arc<dyn CredentialRepository> = Arc::new(MockCredentialRepository::default());
let executor = make_executor(repo, None);
let builder = MockCredentialBuilder {
password_verify_result: true,
totp_verify_result: false,
};
let flow = FlowBuilder::new("test").login("password").build();
let mut ctx = make_context("alice", "password");
let result = executor
.execute_with_builder(&flow, &mut ctx, &builder)
.await
.unwrap();
match result {
AuthResult::Failed { reason, step } => {
assert!(reason.contains("未找到"), "reason 应含未找到: {}", reason);
assert!(
reason.contains("password"),
"reason 应含凭证类型 password: {}",
reason
);
assert_eq!(step, 0);
},
other => panic!("应为 Failed(无凭证),实际: {:?}", other),
}
}
#[tokio::test]
async fn execute_without_builder_login_returns_failed() {
let repo = make_repo_with_password("alice").await;
let executor = make_executor(repo, None);
let flow = FlowBuilder::new("test").login("password").build();
let mut ctx = make_context("alice", "password");
let result = executor.execute(&flow, &mut ctx).await.unwrap();
match result {
AuthResult::Failed { reason, step } => {
assert!(
reason.contains("CredentialBuilder"),
"reason 应含 CredentialBuilder: {}",
reason
);
assert_eq!(step, 0);
},
other => panic!("应为 Failed(无 builder),实际: {:?}", other),
}
}
#[tokio::test]
async fn mfa_some_without_builder_returns_failed() {
let repo = make_repo_with_password_and_totp("alice").await;
let executor = make_executor(repo, None);
let flow = FlowBuilder::new("test").mfa(Some("totp")).build();
let mut ctx = make_context("alice", "123456");
let result = executor.execute(&flow, &mut ctx).await.unwrap();
match result {
AuthResult::Failed { reason, step } => {
assert!(
reason.contains("CredentialBuilder"),
"reason 应含 CredentialBuilder: {}",
reason
);
assert_eq!(step, 0);
},
other => panic!("应为 Failed(无 builder),实际: {:?}", other),
}
}
#[tokio::test]
async fn mfa_without_user_id_returns_failed() {
let repo = make_repo_with_password_and_totp("alice").await;
let executor = make_executor(repo, None);
let builder = MockCredentialBuilder {
password_verify_result: false,
totp_verify_result: true,
};
let flow = FlowBuilder::new("test").mfa(Some("totp")).build();
let mut ctx = make_context("alice", "123456");
ctx.user_id = None;
let result = executor
.execute_with_builder(&flow, &mut ctx, &builder)
.await
.unwrap();
match result {
AuthResult::Failed { reason, step } => {
assert!(
reason.contains("user_id"),
"reason 应含 user_id: {}",
reason
);
assert_eq!(step, 0);
},
other => panic!("应为 Failed(无 user_id),实际: {:?}", other),
}
}
#[tokio::test]
async fn mfa_credential_not_found_returns_failed() {
let _locale_guard = crate::i18n::set_locale(crate::i18n::GarrisonLocale::Zh);
let repo = make_repo_with_password("alice").await;
let executor = make_executor(repo, None);
let builder = MockCredentialBuilder {
password_verify_result: false,
totp_verify_result: true,
};
let flow = FlowBuilder::new("test").mfa(Some("totp")).build();
let mut ctx = make_context("alice", "123456");
let result = executor
.execute_with_builder(&flow, &mut ctx, &builder)
.await
.unwrap();
match result {
AuthResult::Failed { reason, step } => {
assert!(reason.contains("未找到"), "reason 应含未找到: {}", reason);
assert!(
reason.contains("totp"),
"reason 应含凭证类型 totp: {}",
reason
);
assert_eq!(step, 0);
},
other => panic!("应为 Failed(无 totp 凭证),实际: {:?}", other),
}
}
#[tokio::test]
async fn subflow_unknown_flow_returns_failed() {
let _locale_guard = crate::i18n::set_locale(crate::i18n::GarrisonLocale::Zh);
let repo: Arc<dyn CredentialRepository> = Arc::new(MockCredentialRepository::default());
let executor = make_executor_with_registry(repo, Arc::new(FlowRegistry::from_inventory()));
let flow = FlowBuilder::new("parent")
.sub_flow("nonexistent-flow")
.build();
let mut ctx = make_context("alice", "");
let result = executor.execute(&flow, &mut ctx).await.unwrap();
match result {
AuthResult::Failed { reason, step } => {
assert!(
reason.contains("未找到子流程"),
"reason 应含未找到子流程: {}",
reason
);
assert!(
reason.contains("nonexistent-flow"),
"reason 应含 flow 名称: {}",
reason
);
assert_eq!(step, 0);
},
other => panic!("应为 Failed(子流程未找到),实际: {:?}", other),
}
}
#[tokio::test]
async fn subflow_max_depth_exceeded_returns_failed() {
let _locale_guard = crate::i18n::set_locale(crate::i18n::GarrisonLocale::Zh);
let repo: Arc<dyn CredentialRepository> = Arc::new(MockCredentialRepository::default());
let mut registry = FlowRegistry::from_inventory();
let recursive_flow = FlowBuilder::new("loop").sub_flow("loop").build();
registry.register(recursive_flow);
let executor = make_executor_with_registry(repo, Arc::new(registry));
let flow = FlowBuilder::new("parent").sub_flow("loop").build();
let mut ctx = make_context("alice", "");
let result = executor.execute(&flow, &mut ctx).await.unwrap();
match result {
AuthResult::Failed { reason, .. } => {
assert!(
reason.contains("嵌套深度") || reason.contains("循环引用"),
"reason 应含嵌套深度或循环引用: {}",
reason
);
},
other => panic!("应为 Failed(深度超限),实际: {:?}", other),
}
}
#[tokio::test]
async fn required_action_step_returns_failed() {
let repo: Arc<dyn CredentialRepository> = Arc::new(MockCredentialRepository::default());
let executor = make_executor(repo, None);
let flow = AuthenticationFlow {
name: "test".to_string(),
steps: vec![AuthStep::RequiredAction {
action: "verify_email".to_string(),
}],
allow_skip: false,
};
let mut ctx = make_context("alice", "");
let result = executor.execute(&flow, &mut ctx).await.unwrap();
match result {
AuthResult::Failed { reason, step } => {
assert!(
reason.contains("authflow-required-action-not-implemented"),
"reason 应含 authflow-required-action-not-implemented: {}",
reason
);
assert_eq!(step, 0);
},
other => panic!("应为 Failed(RequiredAction 未实现),实际: {:?}", other),
}
}
#[tokio::test]
async fn allow_skip_continues_after_failed_step() {
let repo: Arc<dyn CredentialRepository> = Arc::new(MockCredentialRepository::default());
let executor = make_executor(repo, None);
let flow = AuthenticationFlow {
name: "test".to_string(),
steps: vec![
AuthStep::RequiredAction {
action: "verify_email".to_string(),
},
AuthStep::Conditional {
condition: AuthCondition::IpWhitelisted,
if_step: Box::new(AuthStep::RequiredAction {
action: "x".to_string(),
}),
else_step: None,
},
],
allow_skip: true,
};
let mut ctx = make_context("alice", "");
let result = executor.execute(&flow, &mut ctx).await.unwrap();
match result {
AuthResult::Success { login_id, .. } => {
assert_eq!(login_id, "alice");
assert_eq!(
ctx.completed_steps.len(),
1,
"应只完成 1 个步骤(跳过失败的 RequiredAction)"
);
assert_eq!(ctx.completed_steps[0], 1, "应记录第 2 步(索引 1)");
},
other => panic!("应为 Success(allow_skip 跳过失败),实际: {:?}", other),
}
}
#[tokio::test]
async fn pause_after_last_step_returns_success() {
let repo = make_repo_with_password("alice").await;
let executor = make_executor(repo, None);
let builder = MockCredentialBuilder {
password_verify_result: true,
totp_verify_result: false,
};
let flow = FlowBuilder::new("single").login("password").build();
let mut ctx = make_context("alice", "correct-password");
ctx.extras
.insert("pause_after_step".to_string(), "0".to_string());
let result = executor
.execute_with_builder(&flow, &mut ctx, &builder)
.await
.unwrap();
match result {
AuthResult::Success { login_id, token } => {
assert_eq!(login_id, "alice");
assert!(!token.is_empty(), "token 不应为空");
},
other => panic!("应为 Success(最后一步不暂停),实际: {:?}", other),
}
}
#[tokio::test]
async fn conditional_with_else_step_executed_when_false() {
let _locale_guard = crate::i18n::set_locale(crate::i18n::GarrisonLocale::Zh);
let repo = make_repo_with_password("alice").await;
let executor = make_executor(repo, None);
let builder = MockCredentialBuilder {
password_verify_result: false, totp_verify_result: false,
};
let flow = FlowBuilder::new("test")
.conditional(
AuthCondition::HasCredential("totp".to_string()),
AuthStep::Login {
credential_type: "password".to_string(),
},
Some(AuthStep::Login {
credential_type: "password".to_string(),
}),
)
.build();
let mut ctx = make_context("alice", "wrong-password");
let result = executor
.execute_with_builder(&flow, &mut ctx, &builder)
.await
.unwrap();
match result {
AuthResult::Failed { reason, .. } => {
assert!(
reason.contains("凭证校验失败"),
"reason 应含凭证校验失败(else_step 执行并失败): {}",
reason
);
},
other => panic!(
"应为 Failed(else_step 执行后 verify=false),实际: {:?}",
other
),
}
}
#[tokio::test]
async fn subflow_child_failed_propagates_failed() {
let repo: Arc<dyn CredentialRepository> = Arc::new(MockCredentialRepository::default());
let mut registry = FlowRegistry::from_inventory();
let child = AuthenticationFlow {
name: "failing-child".to_string(),
steps: vec![AuthStep::RequiredAction {
action: "verify_email".to_string(),
}],
allow_skip: false,
};
registry.register(child);
let executor = make_executor_with_registry(repo, Arc::new(registry));
let flow = FlowBuilder::new("parent").sub_flow("failing-child").build();
let mut ctx = make_context("alice", "");
let result = executor.execute(&flow, &mut ctx).await.unwrap();
match result {
AuthResult::Failed { reason, step } => {
assert!(
reason.contains("子流程")
|| reason.contains("failing-child")
|| reason.contains("authflow-required-action-not-implemented"),
"reason 应含子流程: {}",
reason
);
assert!(
reason.contains("failing-child"),
"reason 应含子流程名称: {}",
reason
);
assert!(
reason.contains("authflow-required-action-not-implemented")
|| reason.contains("RequiredAction"),
"reason 应含原始失败原因: {}",
reason
);
assert_eq!(step, 0);
},
other => panic!("应为 Failed(子流程失败传播),实际: {:?}", other),
}
}
#[tokio::test]
async fn accessors_return_some_when_provided() {
use crate::account::policy::{ErrorMode, PasswordPolicyEngine};
let repo: Arc<dyn CredentialRepository> = Arc::new(MockCredentialRepository::default());
let policy = Arc::new(PasswordPolicyEngine::new(vec![], ErrorMode::FirstError));
let dao: Arc<dyn GarrisonDao> = Arc::new(MockDao::new());
let lockout = Arc::new(UserLockoutStrategy::new(
crate::account::lockout::UserLockoutConfig {
max_failure_factor: 5,
permanent_lockout: false,
max_temporary_lockouts: 99,
wait_strategy: crate::account::lockout::WaitStrategy::Linear { base_seconds: 60 },
failure_window_seconds: 300,
},
dao,
));
let executor_with = AuthExecutor::new(
make_logic(),
repo,
Some(policy),
Some(lockout),
Arc::new(FlowRegistry::from_inventory()),
);
assert!(
executor_with.policy_engine().is_some(),
"注入 policy_engine 后访问器应返回 Some"
);
assert!(
executor_with.lockout().is_some(),
"注入 lockout 后访问器应返回 Some"
);
let repo_none: Arc<dyn CredentialRepository> =
Arc::new(MockCredentialRepository::default());
let executor_none = make_executor(repo_none, None);
assert!(
executor_none.policy_engine().is_none(),
"未注入 policy_engine 时访问器应返回 None"
);
assert!(
executor_none.lockout().is_none(),
"未注入 lockout 时访问器应返回 None"
);
}
#[tokio::test]
async fn conditional_is_locked_true_executes_if_step() {
let dao: Arc<dyn GarrisonDao> = Arc::new(MockDao::new());
let lockout = Arc::new(UserLockoutStrategy::new(
crate::account::lockout::UserLockoutConfig {
max_failure_factor: 2,
permanent_lockout: false,
max_temporary_lockouts: 99,
wait_strategy: crate::account::lockout::WaitStrategy::Linear { base_seconds: 300 },
failure_window_seconds: 300,
},
dao,
));
lockout.record_failure("alice").await.unwrap();
lockout.record_failure("alice").await.unwrap();
let repo: Arc<dyn CredentialRepository> = Arc::new(MockCredentialRepository::default());
let executor = make_executor(repo, Some(lockout));
let flow = FlowBuilder::new("test")
.conditional(
AuthCondition::IsLocked,
AuthStep::RequiredAction {
action: "verify_email".to_string(),
},
None,
)
.build();
let mut ctx = make_context("alice", "");
let result = executor.execute(&flow, &mut ctx).await.unwrap();
match result {
AuthResult::Failed { reason, step } => {
assert!(
reason.contains("authflow-required-action-not-implemented")
|| reason.contains("RequiredAction"),
"reason 应含 RequiredAction 失败 key(IsLocked=true → if_step 执行): {}",
reason
);
assert_eq!(step, 0);
},
other => panic!(
"应为 Failed(IsLocked=true,if_step 执行失败),实际: {:?}",
other
),
}
}
#[tokio::test]
async fn conditional_custom_condition_unregistered_returns_error() {
let repo: Arc<dyn CredentialRepository> = Arc::new(MockCredentialRepository::default());
let executor = make_executor(repo, None);
let flow = FlowBuilder::new("test")
.conditional(
AuthCondition::Custom("never_registered".to_string()),
AuthStep::RequiredAction {
action: "x".to_string(),
},
None,
)
.build();
let mut ctx = make_context("alice", "");
let result = executor.execute(&flow, &mut ctx).await;
match result {
Err(GarrisonError::InvalidParam(msg)) => {
assert!(
msg.contains("never_registered"),
"错误应包含条件名,实际: {msg}"
);
},
other => panic!(
"未注册 Custom 条件应为 InvalidParam 显性错误,实际: {:?}",
other
),
}
}
#[tokio::test]
async fn conditional_custom_condition_registered_true_executes_if_step() {
struct AlwaysTrue;
#[async_trait]
impl CustomConditionEvaluator for AlwaysTrue {
async fn evaluate(&self, _ctx: &AuthContext) -> GarrisonResult<bool> {
Ok(true)
}
}
let repo: Arc<dyn CredentialRepository> = Arc::new(MockCredentialRepository::default());
let executor =
make_executor(repo, None).with_custom_evaluator("my_check", Arc::new(AlwaysTrue));
let flow = FlowBuilder::new("test")
.conditional(
AuthCondition::Custom("my_check".to_string()),
AuthStep::RequiredAction {
action: "do_review".to_string(),
},
None,
)
.build();
let mut ctx = make_context("alice", "");
let result = executor.execute(&flow, &mut ctx).await.unwrap();
match result {
AuthResult::Failed { reason, .. } => {
assert!(
reason.contains("authflow-required-action-not-implemented")
|| reason.contains("RequiredAction"),
"注册求值器=true 应执行 if_step(RequiredAction → Failed),reason: {reason}"
);
},
other => panic!(
"注册求值器=true 应执行 if_step(Failed),实际: {:?}",
other
),
}
}
#[tokio::test]
async fn conditional_custom_condition_evaluator_error_propagates() {
struct FailingEval;
#[async_trait]
impl CustomConditionEvaluator for FailingEval {
async fn evaluate(&self, _ctx: &AuthContext) -> GarrisonResult<bool> {
Err(GarrisonError::InvalidParam(
"evaluator-intentional-failure".to_string(),
))
}
}
let repo: Arc<dyn CredentialRepository> = Arc::new(MockCredentialRepository::default());
let executor =
make_executor(repo, None).with_custom_evaluator("failing", Arc::new(FailingEval));
let flow = FlowBuilder::new("test")
.conditional(
AuthCondition::Custom("failing".to_string()),
AuthStep::RequiredAction {
action: "x".to_string(),
},
None,
)
.build();
let mut ctx = make_context("alice", "");
let result = executor.execute(&flow, &mut ctx).await;
match result {
Err(GarrisonError::InvalidParam(msg)) => {
assert!(msg.contains("evaluator-intentional-failure"), "got: {msg}");
},
other => panic!("求值器错误应传播,实际: {:?}", other),
}
}
#[tokio::test]
async fn conditional_ip_whitelisted_hit_executes_if_step() {
let repo: Arc<dyn CredentialRepository> = Arc::new(MockCredentialRepository::default());
let whitelist =
IpWhitelist::parse(&["10.0.0.0/8".to_string(), "192.168.1.0/24".to_string()]).unwrap();
let executor = make_executor(repo, None).with_ip_whitelist(whitelist);
let flow = FlowBuilder::new("test")
.conditional(
AuthCondition::IpWhitelisted,
AuthStep::RequiredAction {
action: "trusted_path".to_string(),
},
None,
)
.build();
let mut ctx = make_context("alice", "");
ctx.ip = "10.20.30.40".to_string();
let result = executor.execute(&flow, &mut ctx).await.unwrap();
match result {
AuthResult::Failed { reason, .. } => {
assert!(
reason.contains("authflow-required-action-not-implemented")
|| reason.contains("RequiredAction"),
"白名单命中应执行 if_step(RequiredAction → Failed),reason: {reason}"
);
},
other => panic!("白名单命中应执行 if_step(Failed),实际: {:?}", other),
}
}
#[tokio::test]
async fn conditional_ip_whitelisted_miss_skips() {
let repo: Arc<dyn CredentialRepository> = Arc::new(MockCredentialRepository::default());
let whitelist = IpWhitelist::parse(&["10.0.0.0/8".to_string()]).unwrap();
let executor = make_executor(repo, None).with_ip_whitelist(whitelist);
let flow = FlowBuilder::new("test")
.conditional(
AuthCondition::IpWhitelisted,
AuthStep::RequiredAction {
action: "trusted_path".to_string(),
},
None,
)
.build();
let mut ctx = make_context("alice", "");
ctx.ip = "203.0.113.9".to_string();
let result = executor.execute(&flow, &mut ctx).await.unwrap();
match result {
AuthResult::Success { login_id, .. } => assert_eq!(login_id, "alice"),
other => panic!("未命中应跳过(Success),实际: {:?}", other),
}
}
#[tokio::test]
async fn conditional_ip_whitelisted_not_configured_is_false() {
let repo: Arc<dyn CredentialRepository> = Arc::new(MockCredentialRepository::default());
let executor = make_executor(repo, None);
let flow = FlowBuilder::new("test")
.conditional(
AuthCondition::IpWhitelisted,
AuthStep::RequiredAction {
action: "trusted_path".to_string(),
},
None,
)
.build();
let mut ctx = make_context("alice", "");
ctx.ip = "10.0.0.1".to_string();
let result = executor.execute(&flow, &mut ctx).await.unwrap();
match result {
AuthResult::Success { login_id, .. } => assert_eq!(login_id, "alice"),
other => panic!("无白名单配置应为 false→Success,实际: {:?}", other),
}
}
#[test]
fn ip_whitelist_ipv6_single_ip_and_invalid() {
let wl = IpWhitelist::parse(&[
"2001:db8::/32".to_string(),
"172.16.5.7".to_string(), ])
.unwrap();
assert!(wl.contains("2001:db8::1"));
assert!(!wl.contains("2001:db9::1"));
assert!(wl.contains("172.16.5.7"));
assert!(!wl.contains("172.16.5.8"));
assert!(!wl.contains("not-an-ip"));
let err = IpWhitelist::parse(&["10.0.0.0/99".to_string()]).unwrap_err();
match err {
GarrisonError::InvalidParam(msg) => assert!(msg.contains("10.0.0.0/99")),
other => panic!("非法 CIDR 应为 InvalidParam,实际: {other:?}"),
}
}
#[tokio::test]
async fn pause_after_step_challenge_for_login_step() {
let _locale_guard = crate::i18n::set_locale(crate::i18n::GarrisonLocale::Zh);
let repo = make_repo_with_password_and_totp("alice").await;
let executor = make_executor(repo, None);
let builder = MockCredentialBuilder {
password_verify_result: true,
totp_verify_result: false,
};
let flow = FlowBuilder::new("two-login")
.login("password")
.login("totp")
.build();
let mut ctx = make_context("alice", "correct-password");
ctx.extras
.insert("pause_after_step".to_string(), "0".to_string());
let result = executor
.execute_with_builder(&flow, &mut ctx, &builder)
.await
.unwrap();
match result {
AuthResult::Pending {
completed_step,
next_step,
challenge,
} => {
assert_eq!(completed_step, 0);
assert_eq!(next_step, 1);
assert!(
challenge.contains("请输入"),
"challenge 应含请输入: {}",
challenge
);
assert!(
challenge.contains("totp"),
"challenge 应含 totp: {}",
challenge
);
},
other => panic!("应为 Pending,实际: {:?}", other),
}
}
#[tokio::test]
async fn login_with_lockout_success_resets_failure_count() {
let dao: Arc<dyn GarrisonDao> = Arc::new(MockDao::new());
let lockout = Arc::new(UserLockoutStrategy::new(
crate::account::lockout::UserLockoutConfig {
max_failure_factor: 2,
permanent_lockout: false,
max_temporary_lockouts: 99,
wait_strategy: crate::account::lockout::WaitStrategy::Linear { base_seconds: 300 },
failure_window_seconds: 300,
},
dao,
));
let repo = make_repo_with_password("alice").await;
let executor = make_executor(repo, Some(lockout));
let flow = FlowBuilder::new("test").login("password").build();
let builder_fail = MockCredentialBuilder {
password_verify_result: false,
totp_verify_result: false,
};
let builder_ok = MockCredentialBuilder {
password_verify_result: true,
totp_verify_result: false,
};
let mut ctx1 = make_context("alice", "wrong");
let _ = executor
.execute_with_builder(&flow, &mut ctx1, &builder_fail)
.await
.unwrap();
let mut ctx2 = make_context("alice", "correct");
let _ = executor
.execute_with_builder(&flow, &mut ctx2, &builder_ok)
.await
.unwrap();
let mut ctx3 = make_context("alice", "wrong");
let _ = executor
.execute_with_builder(&flow, &mut ctx3, &builder_fail)
.await
.unwrap();
let mut ctx4 = make_context("alice", "correct");
let result = executor
.execute_with_builder(&flow, &mut ctx4, &builder_ok)
.await
.unwrap();
match result {
AuthResult::Success { login_id, .. } => {
assert_eq!(
login_id, "alice",
"record_success 应在第 2 次尝试重置失败计数,使第 4 次尝试不被锁定"
);
},
AuthResult::Failed { reason, .. } => {
panic!(
"应为 Success(record_success 重置了计数),但 Failed: {}",
reason
);
},
other => panic!("应为 Success,实际: {:?}", other),
}
}
#[tokio::test]
async fn login_with_lockout_failure_triggers_lockout() {
let _locale_guard = crate::i18n::set_locale(crate::i18n::GarrisonLocale::Zh);
let dao: Arc<dyn GarrisonDao> = Arc::new(MockDao::new());
let lockout = Arc::new(UserLockoutStrategy::new(
crate::account::lockout::UserLockoutConfig {
max_failure_factor: 1,
permanent_lockout: false,
max_temporary_lockouts: 99,
wait_strategy: crate::account::lockout::WaitStrategy::Linear { base_seconds: 300 },
failure_window_seconds: 300,
},
dao,
));
let repo = make_repo_with_password("alice").await;
let executor = make_executor(repo, Some(lockout));
let flow = FlowBuilder::new("test").login("password").build();
let builder_fail = MockCredentialBuilder {
password_verify_result: false,
totp_verify_result: false,
};
let builder_ok = MockCredentialBuilder {
password_verify_result: true,
totp_verify_result: false,
};
let mut ctx1 = make_context("alice", "wrong");
let r1 = executor
.execute_with_builder(&flow, &mut ctx1, &builder_fail)
.await
.unwrap();
assert!(
matches!(r1, AuthResult::Failed { ref reason, .. } if reason.contains("凭证校验失败")),
"第 1 次尝试应为凭证校验失败: {:?}",
r1
);
let mut ctx2 = make_context("alice", "correct");
let r2 = executor
.execute_with_builder(&flow, &mut ctx2, &builder_ok)
.await
.unwrap();
match r2 {
AuthResult::Failed { reason, step } => {
assert!(
reason.contains("锁定"),
"reason 应含锁定(record_failure 已触发锁定): {}",
reason
);
assert_eq!(step, 0);
},
other => panic!(
"应为 Failed(record_failure 触发锁定,第 2 次 login 被拦截),实际: {:?}",
other
),
}
}
#[tokio::test]
async fn subflow_pending_propagates_as_failed() {
let _locale_guard = crate::i18n::set_locale(crate::i18n::GarrisonLocale::Zh);
let repo = make_repo_with_password("alice").await;
let mut registry = FlowRegistry::from_inventory();
let child = FlowBuilder::new("child-with-pause")
.login("password")
.mfa(None)
.build();
registry.register(child);
let executor = make_executor_with_registry(repo, Arc::new(registry));
let builder = MockCredentialBuilder {
password_verify_result: true,
totp_verify_result: false,
};
let flow = FlowBuilder::new("parent")
.sub_flow("child-with-pause")
.build();
let mut ctx = make_context("alice", "correct-password");
ctx.extras
.insert("pause_after_step".to_string(), "0".to_string());
let result = executor
.execute_with_builder(&flow, &mut ctx, &builder)
.await
.unwrap();
match result {
AuthResult::Failed { reason, step } => {
assert!(
reason.contains("子流程")
|| reason.contains("failing-child")
|| reason.contains("authflow-required-action-not-implemented"),
"reason 应含子流程: {}",
reason
);
assert!(
reason.contains("child-with-pause"),
"reason 应含子流程名称: {}",
reason
);
assert!(
reason.contains("Pending"),
"reason 应含 Pending: {}",
reason
);
assert_eq!(step, 0);
},
other => panic!(
"应为 Failed(子流程 Pending 传播为 Failed),实际: {:?}",
other
),
}
}
#[tokio::test]
async fn subflow_challenge_required_propagates() {
let repo = make_repo_with_password_and_totp("alice").await;
let mut registry = FlowRegistry::from_inventory();
let child = FlowBuilder::new("child-challenge")
.mfa(Some("totp"))
.build();
registry.register(child);
let executor = make_executor_with_registry(repo, Arc::new(registry));
let builder = MockCredentialBuilder {
password_verify_result: false,
totp_verify_result: true,
};
let flow = FlowBuilder::new("parent")
.sub_flow("child-challenge")
.build();
let mut ctx = make_context("alice", "");
let result = executor
.execute_with_builder(&flow, &mut ctx, &builder)
.await
.unwrap();
match result {
AuthResult::ChallengeRequired {
challenge_type,
message,
} => {
assert_eq!(challenge_type, "totp");
assert!(message.contains("totp"), "message 应含 totp: {}", message);
},
other => panic!(
"应为 ChallengeRequired(子流程 ChallengeRequired 传播),实际: {:?}",
other
),
}
}
#[tokio::test]
async fn conditional_has_credential_without_user_id_returns_false() {
let repo: Arc<dyn CredentialRepository> = Arc::new(MockCredentialRepository::default());
let executor = make_executor(repo, None);
let flow = FlowBuilder::new("test")
.conditional(
AuthCondition::HasCredential("password".to_string()),
AuthStep::RequiredAction {
action: "x".to_string(),
},
None,
)
.build();
let mut ctx = make_context("alice", "");
ctx.user_id = None;
let result = executor.execute(&flow, &mut ctx).await.unwrap();
match result {
AuthResult::Success { login_id, .. } => {
assert_eq!(login_id, "", "user_id=None 时 login_id 应为空串");
},
other => panic!(
"应为 Success(HasCredential user_id=None → false → 跳过),实际: {:?}",
other
),
}
}
#[tokio::test]
async fn conditional_is_locked_without_lockout_returns_false() {
let repo: Arc<dyn CredentialRepository> = Arc::new(MockCredentialRepository::default());
let executor = make_executor(repo, None); let flow = FlowBuilder::new("test")
.conditional(
AuthCondition::IsLocked,
AuthStep::RequiredAction {
action: "x".to_string(),
},
None,
)
.build();
let mut ctx = make_context("alice", "");
let result = executor.execute(&flow, &mut ctx).await.unwrap();
match result {
AuthResult::Success { .. } => {},
other => panic!(
"应为 Success(IsLocked lockout=None → false → 跳过),实际: {:?}",
other
),
}
}
#[tokio::test]
async fn conditional_is_locked_without_user_id_returns_false() {
let dao: Arc<dyn GarrisonDao> = Arc::new(MockDao::new());
let lockout = Arc::new(UserLockoutStrategy::new(
crate::account::lockout::UserLockoutConfig {
max_failure_factor: 2,
permanent_lockout: false,
max_temporary_lockouts: 99,
wait_strategy: crate::account::lockout::WaitStrategy::Linear { base_seconds: 300 },
failure_window_seconds: 300,
},
dao,
));
let repo: Arc<dyn CredentialRepository> = Arc::new(MockCredentialRepository::default());
let executor = make_executor(repo, Some(lockout));
let flow = FlowBuilder::new("test")
.conditional(
AuthCondition::IsLocked,
AuthStep::RequiredAction {
action: "x".to_string(),
},
None,
)
.build();
let mut ctx = make_context("alice", "");
ctx.user_id = None;
let result = executor.execute(&flow, &mut ctx).await.unwrap();
match result {
AuthResult::Success { .. } => {},
other => panic!(
"应为 Success(IsLocked user_id=None → false → 跳过),实际: {:?}",
other
),
}
}
#[tokio::test]
async fn pause_after_step_challenge_for_mfa_some_step() {
let _locale_guard = crate::i18n::set_locale(crate::i18n::GarrisonLocale::Zh);
let repo = make_repo_with_password("alice").await;
let executor = make_executor(repo, None);
let builder = MockCredentialBuilder {
password_verify_result: true,
totp_verify_result: false,
};
let flow = FlowBuilder::new("two-step")
.login("password")
.mfa(Some("totp"))
.build();
let mut ctx = make_context("alice", "correct-password");
ctx.extras
.insert("pause_after_step".to_string(), "0".to_string());
let result = executor
.execute_with_builder(&flow, &mut ctx, &builder)
.await
.unwrap();
match result {
AuthResult::Pending {
challenge,
next_step,
..
} => {
assert_eq!(next_step, 1);
assert!(
challenge.contains("totp"),
"challenge 应含 totp: {}",
challenge
);
assert!(
challenge.contains("验证码"),
"challenge 应含验证码: {}",
challenge
);
},
other => panic!("应为 Pending,实际: {:?}", other),
}
}
#[tokio::test]
async fn pause_after_step_challenge_for_mfa_none_step() {
let repo = make_repo_with_password("alice").await;
let executor = make_executor(repo, None);
let builder = MockCredentialBuilder {
password_verify_result: true,
totp_verify_result: false,
};
let flow = FlowBuilder::new("two-step")
.login("password")
.mfa(None)
.build();
let mut ctx = make_context("alice", "correct-password");
ctx.extras
.insert("pause_after_step".to_string(), "0".to_string());
let result = executor
.execute_with_builder(&flow, &mut ctx, &builder)
.await
.unwrap();
match result {
AuthResult::Pending { challenge, .. } => {
assert!(
challenge.contains("MFA"),
"challenge 应含 MFA: {}",
challenge
);
},
other => panic!("应为 Pending,实际: {:?}", other),
}
}
#[tokio::test]
async fn pause_after_step_challenge_for_required_action_step() {
let _locale_guard = crate::i18n::set_locale(crate::i18n::GarrisonLocale::Zh);
let repo = make_repo_with_password("alice").await;
let executor = make_executor(repo, None);
let builder = MockCredentialBuilder {
password_verify_result: true,
totp_verify_result: false,
};
let flow = AuthenticationFlow {
name: "two-step".to_string(),
steps: vec![
AuthStep::Login {
credential_type: "password".to_string(),
},
AuthStep::RequiredAction {
action: "verify_email".to_string(),
},
],
allow_skip: false,
};
let mut ctx = make_context("alice", "correct-password");
ctx.extras
.insert("pause_after_step".to_string(), "0".to_string());
let result = executor
.execute_with_builder(&flow, &mut ctx, &builder)
.await
.unwrap();
match result {
AuthResult::Pending { challenge, .. } => {
assert!(
challenge.contains("verify_email"),
"challenge 应含 action 名: {}",
challenge
);
assert!(
challenge.contains("必需动作"),
"challenge 应含必需动作: {}",
challenge
);
},
other => panic!("应为 Pending,实际: {:?}", other),
}
}
#[tokio::test]
async fn pause_after_step_challenge_for_conditional_step() {
let _locale_guard = crate::i18n::set_locale(crate::i18n::GarrisonLocale::Zh);
let repo = make_repo_with_password("alice").await;
let executor = make_executor(repo, None);
let builder = MockCredentialBuilder {
password_verify_result: true,
totp_verify_result: false,
};
let flow = AuthenticationFlow {
name: "two-step".to_string(),
steps: vec![
AuthStep::Login {
credential_type: "password".to_string(),
},
AuthStep::Conditional {
condition: AuthCondition::IpWhitelisted,
if_step: Box::new(AuthStep::RequiredAction {
action: "x".to_string(),
}),
else_step: None,
},
],
allow_skip: false,
};
let mut ctx = make_context("alice", "correct-password");
ctx.extras
.insert("pause_after_step".to_string(), "0".to_string());
let result = executor
.execute_with_builder(&flow, &mut ctx, &builder)
.await
.unwrap();
match result {
AuthResult::Pending { challenge, .. } => {
assert!(
challenge.contains("条件分支"),
"challenge 应含条件分支: {}",
challenge
);
},
other => panic!("应为 Pending,实际: {:?}", other),
}
}
#[tokio::test]
async fn pause_after_step_challenge_for_subflow_step() {
let _locale_guard = crate::i18n::set_locale(crate::i18n::GarrisonLocale::Zh);
let repo = make_repo_with_password("alice").await;
let mut registry = FlowRegistry::from_inventory();
let child = FlowBuilder::new("child-flow").login("password").build();
registry.register(child);
let executor = make_executor_with_registry(repo, Arc::new(registry));
let builder = MockCredentialBuilder {
password_verify_result: true,
totp_verify_result: false,
};
let flow = AuthenticationFlow {
name: "two-step".to_string(),
steps: vec![
AuthStep::Login {
credential_type: "password".to_string(),
},
AuthStep::SubFlow {
flow_name: "child-flow".to_string(),
},
],
allow_skip: false,
};
let mut ctx = make_context("alice", "correct-password");
ctx.extras
.insert("pause_after_step".to_string(), "0".to_string());
let result = executor
.execute_with_builder(&flow, &mut ctx, &builder)
.await
.unwrap();
match result {
AuthResult::Pending { challenge, .. } => {
assert!(
challenge.contains("child-flow"),
"challenge 应含子流程名: {}",
challenge
);
assert!(
challenge.contains("子流程"),
"challenge 应含子流程: {}",
challenge
);
},
other => panic!("应为 Pending,实际: {:?}", other),
}
}
#[tokio::test]
async fn pause_after_step_invalid_value_does_not_pause() {
let repo: Arc<dyn CredentialRepository> = Arc::new(MockCredentialRepository::default());
let executor = make_executor(repo, None);
let flow = AuthenticationFlow {
name: "two-conditional".to_string(),
steps: vec![
AuthStep::Conditional {
condition: AuthCondition::IpWhitelisted,
if_step: Box::new(AuthStep::RequiredAction {
action: "x".to_string(),
}),
else_step: None,
},
AuthStep::Conditional {
condition: AuthCondition::IpWhitelisted,
if_step: Box::new(AuthStep::RequiredAction {
action: "y".to_string(),
}),
else_step: None,
},
],
allow_skip: false,
};
let mut ctx = make_context("alice", "");
ctx.extras
.insert("pause_after_step".to_string(), "abc".to_string());
let result = executor.execute(&flow, &mut ctx).await.unwrap();
match result {
AuthResult::Success { .. } => {
assert_eq!(
ctx.completed_steps.len(),
2,
"应完成 2 个步骤(无效 pause 值不触发暂停)"
);
},
other => panic!("应为 Success(无效 pause 值不触发暂停),实际: {:?}", other),
}
}
#[tokio::test]
async fn pause_after_step_out_of_range_index_does_not_pause() {
let repo: Arc<dyn CredentialRepository> = Arc::new(MockCredentialRepository::default());
let executor = make_executor(repo, None);
let flow = AuthenticationFlow {
name: "two-conditional".to_string(),
steps: vec![
AuthStep::Conditional {
condition: AuthCondition::IpWhitelisted,
if_step: Box::new(AuthStep::RequiredAction {
action: "x".to_string(),
}),
else_step: None,
},
AuthStep::Conditional {
condition: AuthCondition::IpWhitelisted,
if_step: Box::new(AuthStep::RequiredAction {
action: "y".to_string(),
}),
else_step: None,
},
],
allow_skip: false,
};
let mut ctx = make_context("alice", "");
ctx.extras
.insert("pause_after_step".to_string(), "99".to_string());
let result = executor.execute(&flow, &mut ctx).await.unwrap();
match result {
AuthResult::Success { .. } => {
assert_eq!(
ctx.completed_steps.len(),
2,
"应完成 2 个步骤(pause 索引越界不触发暂停)"
);
},
other => panic!(
"应为 Success(pause 索引越界不触发暂停),实际: {:?}",
other
),
}
}
#[tokio::test]
async fn empty_flow_without_user_id_returns_success_with_empty_login_id() {
let repo: Arc<dyn CredentialRepository> = Arc::new(MockCredentialRepository::default());
let executor = make_executor(repo, None);
let flow = FlowBuilder::new("empty").build();
let mut ctx = make_context("alice", "");
ctx.user_id = None;
let result = executor.execute(&flow, &mut ctx).await.unwrap();
match result {
AuthResult::Success { login_id, token } => {
assert_eq!(login_id, "", "user_id=None 时 login_id 应为空串");
assert_eq!(token, "", "空流程 token 应为空");
},
other => panic!("应为 Success,实际: {:?}", other),
}
}
#[tokio::test]
async fn sso_step_without_resolver_returns_failed() {
let repo: Arc<dyn CredentialRepository> = Arc::new(MockCredentialRepository::default());
let executor = make_executor(repo, None);
let flow = FlowBuilder::new("sso-flow").sso("keycloak").build();
let mut ctx = make_context("alice", "ticket_abc");
let result = executor.execute(&flow, &mut ctx).await.unwrap();
match result {
AuthResult::Failed { reason, step } => {
assert!(
reason.contains("SsoServerResolver"),
"reason 应含 SsoServerResolver: {}",
reason
);
assert_eq!(step, 0);
},
other => panic!("应为 Failed(无 SSO resolver),实际: {:?}", other),
}
}
#[cfg(feature = "metrics-prometheus")]
#[tokio::test]
async fn execute_with_metrics_records_durations() {
let registry = prometheus::Registry::new();
let metrics = crate::account::metrics::AccountMetrics::register_to(®istry).unwrap();
let repo = make_repo_with_password("alice").await;
let executor = make_executor(repo, None);
let builder = MockCredentialBuilder {
password_verify_result: true,
totp_verify_result: false,
};
let flow = FlowBuilder::new("metrics-test").login("password").build();
let mut ctx = make_context("alice", "correct-password");
let result = executor
.execute_with_metrics(&flow, &mut ctx, &builder, &metrics)
.await
.unwrap();
match result {
AuthResult::Success { login_id, .. } => {
assert_eq!(login_id, "alice");
},
other => panic!("应为 Success,实际: {:?}", other),
}
let gathered = prometheus::TextEncoder::new()
.encode_to_string(®istry.gather())
.unwrap();
assert!(
gathered.contains("garrison_authflow_execute_duration_seconds"),
"应记录 authflow_execute_duration: {}",
gathered
);
assert!(
gathered.contains("garrison_credential_verify_duration_seconds"),
"应记录 credential_verify_duration: {}",
gathered
);
}
#[cfg(feature = "metrics-prometheus")]
#[tokio::test]
async fn execute_with_metrics_records_mfa_verify_duration() {
let registry = prometheus::Registry::new();
let metrics = crate::account::metrics::AccountMetrics::register_to(®istry).unwrap();
let repo = make_repo_with_password_and_totp("alice").await;
let executor = make_executor(repo, None);
let builder = MockCredentialBuilder {
password_verify_result: false,
totp_verify_result: true,
};
let flow = FlowBuilder::new("mfa-metrics").mfa(Some("totp")).build();
let mut ctx = make_context("alice", "123456");
let result = executor
.execute_with_metrics(&flow, &mut ctx, &builder, &metrics)
.await
.unwrap();
match result {
AuthResult::Success { .. } => {},
other => panic!("应为 Success,实际: {:?}", other),
}
let gathered = prometheus::TextEncoder::new()
.encode_to_string(®istry.gather())
.unwrap();
assert!(
gathered.contains("garrison_credential_verify_duration_seconds"),
"应记录 credential_verify_duration: {}",
gathered
);
assert!(gathered.contains("totp"), "应记录 totp 标签: {}", gathered);
}
#[cfg(feature = "metrics-prometheus")]
#[tokio::test]
async fn execute_with_metrics_empty_flow_records_duration() {
let registry = prometheus::Registry::new();
let metrics = crate::account::metrics::AccountMetrics::register_to(®istry).unwrap();
let repo: Arc<dyn CredentialRepository> = Arc::new(MockCredentialRepository::default());
let executor = make_executor(repo, None);
let builder = MockCredentialBuilder {
password_verify_result: false,
totp_verify_result: false,
};
let flow = FlowBuilder::new("empty-metrics").build();
let mut ctx = make_context("alice", "");
let result = executor
.execute_with_metrics(&flow, &mut ctx, &builder, &metrics)
.await
.unwrap();
match result {
AuthResult::Success { login_id, .. } => {
assert_eq!(login_id, "alice");
},
other => panic!("应为 Success,实际: {:?}", other),
}
let gathered = prometheus::TextEncoder::new()
.encode_to_string(®istry.gather())
.unwrap();
assert!(
gathered.contains("garrison_authflow_execute_duration_seconds"),
"空流程也应记录 authflow_execute_duration: {}",
gathered
);
}
#[cfg(all(feature = "social-wechat", feature = "protocol-sso-server"))]
mod t017 {
use super::*;
use crate::error::GarrisonError;
use crate::protocol::social::{provider_names, SocialLoginProvider, SocialUserInfo};
use crate::protocol::sso::SsoServer;
use std::collections::HashMap;
use std::sync::atomic::{AtomicUsize, Ordering};
struct MockSocialLoginProvider {
provider_user_id: String,
exchange_count: AtomicUsize,
fail_exchange: bool,
}
impl MockSocialLoginProvider {
fn new(provider_user_id: &str) -> Self {
Self {
provider_user_id: provider_user_id.to_string(),
exchange_count: AtomicUsize::new(0),
fail_exchange: false,
}
}
fn exchange_count(&self) -> usize {
self.exchange_count.load(Ordering::SeqCst)
}
}
#[async_trait]
impl SocialLoginProvider for MockSocialLoginProvider {
async fn get_authorization_url(
&self,
_state: &str,
_redirect_uri: &str,
) -> GarrisonResult<String> {
Ok("https://example.com/auth".to_string())
}
async fn exchange_token(
&self,
_code: &str,
_state: &str,
) -> GarrisonResult<SocialUserInfo> {
self.exchange_count.fetch_add(1, Ordering::SeqCst);
if self.fail_exchange {
return Err(GarrisonError::Internal(
"mock exchange_token 失败".to_string(),
));
}
Ok(SocialUserInfo {
provider: provider_names::WECHAT.to_string(),
provider_user_id: self.provider_user_id.clone(),
nickname: None,
avatar: None,
union_id: None,
raw: serde_json::json!({}),
})
}
async fn get_user_info(&self, _access_token: &str) -> GarrisonResult<SocialUserInfo> {
Ok(SocialUserInfo {
provider: provider_names::WECHAT.to_string(),
provider_user_id: self.provider_user_id.clone(),
nickname: None,
avatar: None,
union_id: None,
raw: serde_json::json!({}),
})
}
}
struct MockSocialProviderResolver {
providers: HashMap<String, Arc<dyn SocialLoginProvider>>,
}
impl MockSocialProviderResolver {
fn new() -> Self {
Self {
providers: HashMap::new(),
}
}
fn register(&mut self, name: &str, provider: Arc<dyn SocialLoginProvider>) {
self.providers.insert(name.to_string(), provider);
}
}
#[async_trait]
impl SocialProviderResolver for MockSocialProviderResolver {
async fn resolve_login_id(
&self,
provider: &str,
code: &str,
state: &str,
) -> GarrisonResult<String> {
let p = self.providers.get(provider).ok_or_else(|| {
GarrisonError::InvalidParam(format!("unknown social provider: {}", provider))
})?;
let user_info = p.exchange_token(code, state).await?;
Ok(user_info.provider_user_id)
}
}
struct MockSsoServer {
login_id: String,
validate_count: AtomicUsize,
fail_validate: bool,
}
impl MockSsoServer {
fn new(login_id: &str) -> Self {
Self {
login_id: login_id.to_string(),
validate_count: AtomicUsize::new(0),
fail_validate: false,
}
}
fn new_failing() -> Self {
Self {
login_id: String::new(),
validate_count: AtomicUsize::new(0),
fail_validate: true,
}
}
fn validate_count(&self) -> usize {
self.validate_count.load(Ordering::SeqCst)
}
}
#[async_trait]
impl SsoServer for MockSsoServer {
async fn issue_ticket(
&self,
_login_id: &str,
_client_id: i64,
) -> GarrisonResult<String> {
Ok("mock-ticket".to_string())
}
async fn validate_ticket(
&self,
_ticket: &str,
_client_id: i64,
) -> GarrisonResult<String> {
self.validate_count.fetch_add(1, Ordering::SeqCst);
if self.fail_validate {
return Err(GarrisonError::InvalidToken(
"mock validate_ticket 失败".to_string(),
));
}
Ok(self.login_id.clone())
}
async fn destroy_ticket(&self, _ticket: &str) -> GarrisonResult<()> {
Ok(())
}
async fn push_message(&self, _login_id: &str, _message: &str) -> GarrisonResult<()> {
Ok(())
}
}
struct MockSsoServerResolver {
servers: HashMap<String, Arc<dyn SsoServer>>,
}
impl MockSsoServerResolver {
fn new() -> Self {
Self {
servers: HashMap::new(),
}
}
fn register(&mut self, id: &str, server: Arc<dyn SsoServer>) {
self.servers.insert(id.to_string(), server);
}
}
#[async_trait]
impl SsoServerResolver for MockSsoServerResolver {
async fn validate_and_get_login_id(
&self,
server_id: &str,
ticket: &str,
client_id: i64,
) -> GarrisonResult<String> {
let s = self.servers.get(server_id).ok_or_else(|| {
GarrisonError::InvalidParam(format!("unknown sso server: {}", server_id))
})?;
s.validate_ticket(ticket, client_id).await
}
}
fn make_t017_executor() -> AuthExecutor {
let repo: Arc<dyn CredentialRepository> = Arc::new(MockCredentialRepository::default());
make_executor(repo, None)
}
fn dummy_builder() -> MockCredentialBuilder {
MockCredentialBuilder {
password_verify_result: false,
totp_verify_result: false,
}
}
#[tokio::test]
async fn t017_social_login_success() {
let wechat = Arc::new(MockSocialLoginProvider::new("wx_user_123"));
let wechat_ref = wechat.clone() as Arc<dyn SocialLoginProvider>;
let mut social_resolver = MockSocialProviderResolver::new();
social_resolver.register("wechat", wechat_ref);
let executor = make_t017_executor();
let builder = dummy_builder();
let sso_resolver = MockSsoServerResolver::new();
let flow = FlowBuilder::new("wechat-flow").social("wechat").build();
let mut ctx = make_context("", "auth_code_456");
ctx.extras
.insert("state".to_string(), "state_789".to_string());
let result = executor
.execute_with_full(&flow, &mut ctx, &builder, &social_resolver, &sso_resolver)
.await
.unwrap();
match result {
AuthResult::Success { login_id, token } => {
assert_eq!(login_id, "wx_user_123");
assert!(!token.is_empty(), "token 不应为空");
assert_eq!(ctx.user_id.as_deref(), Some("wx_user_123"));
},
other => panic!("应为 Success,实际: {:?}", other),
}
assert_eq!(wechat.exchange_count(), 1, "exchange_token 应被调用 1 次");
}
#[tokio::test]
async fn t017_social_login_unknown_provider_returns_failed() {
let social_resolver = MockSocialProviderResolver::new(); let executor = make_t017_executor();
let builder = dummy_builder();
let sso_resolver = MockSsoServerResolver::new();
let flow = FlowBuilder::new("github-flow").social("github").build();
let mut ctx = make_context("", "auth_code");
ctx.extras.insert("state".to_string(), "state".to_string());
let result = executor
.execute_with_full(&flow, &mut ctx, &builder, &social_resolver, &sso_resolver)
.await
.unwrap();
match result {
AuthResult::Failed { reason, step } => {
assert!(
reason.contains("unknown social provider"),
"reason 应含 unknown social provider: {}",
reason
);
assert_eq!(step, 0);
},
other => panic!("应为 Failed,实际: {:?}", other),
}
}
#[tokio::test]
async fn t017_social_login_empty_input_challenge_required() {
let social_resolver = MockSocialProviderResolver::new();
let executor = make_t017_executor();
let builder = dummy_builder();
let sso_resolver = MockSsoServerResolver::new();
let flow = FlowBuilder::new("wechat-flow").social("wechat").build();
let mut ctx = make_context("", "");
let result = executor
.execute_with_full(&flow, &mut ctx, &builder, &social_resolver, &sso_resolver)
.await
.unwrap();
match result {
AuthResult::ChallengeRequired {
challenge_type,
message,
} => {
assert_eq!(challenge_type, "social:wechat");
assert!(
message.contains("wechat"),
"message 应含 wechat: {}",
message
);
},
other => panic!("应为 ChallengeRequired,实际: {:?}", other),
}
}
#[tokio::test]
async fn t017_social_login_multiple_providers_switch() {
let wechat = Arc::new(MockSocialLoginProvider::new("wx_openid"));
let alipay = Arc::new(MockSocialLoginProvider::new("alipay_uid"));
let wechat_ref = wechat.clone() as Arc<dyn SocialLoginProvider>;
let alipay_ref = alipay.clone() as Arc<dyn SocialLoginProvider>;
let mut social_resolver = MockSocialProviderResolver::new();
social_resolver.register("wechat", wechat_ref);
social_resolver.register("alipay", alipay_ref);
let executor = make_t017_executor();
let builder = dummy_builder();
let sso_resolver = MockSsoServerResolver::new();
let flow_w = FlowBuilder::new("w").social("wechat").build();
let mut ctx_w = make_context("", "code_w");
ctx_w.extras.insert("state".to_string(), "s".to_string());
let result_w = executor
.execute_with_full(
&flow_w,
&mut ctx_w,
&builder,
&social_resolver,
&sso_resolver,
)
.await
.unwrap();
match result_w {
AuthResult::Success { login_id, .. } => assert_eq!(login_id, "wx_openid"),
other => panic!("wechat 应返回 Success,实际: {:?}", other),
}
let flow_a = FlowBuilder::new("a").social("alipay").build();
let mut ctx_a = make_context("", "code_a");
ctx_a.extras.insert("state".to_string(), "s".to_string());
let result_a = executor
.execute_with_full(
&flow_a,
&mut ctx_a,
&builder,
&social_resolver,
&sso_resolver,
)
.await
.unwrap();
match result_a {
AuthResult::Success { login_id, .. } => assert_eq!(login_id, "alipay_uid"),
other => panic!("alipay 应返回 Success,实际: {:?}", other),
}
assert_eq!(wechat.exchange_count(), 1);
assert_eq!(alipay.exchange_count(), 1);
}
#[tokio::test]
async fn t017_sso_login_success() {
let server = Arc::new(MockSsoServer::new("1001"));
let server_ref = server.clone() as Arc<dyn SsoServer>;
let mut sso_resolver = MockSsoServerResolver::new();
sso_resolver.register("keycloak", server_ref);
let social_resolver = MockSocialProviderResolver::new();
let executor = make_t017_executor();
let builder = dummy_builder();
let flow = FlowBuilder::new("sso-flow").sso("keycloak").build();
let mut ctx = make_context("", "ticket_abc");
ctx.extras
.insert("client_id".to_string(), "2001".to_string());
let result = executor
.execute_with_full(&flow, &mut ctx, &builder, &social_resolver, &sso_resolver)
.await
.unwrap();
match result {
AuthResult::Success { login_id, token } => {
assert_eq!(login_id, "1001");
assert!(!token.is_empty(), "token 不应为空");
assert_eq!(ctx.user_id.as_deref(), Some("1001"));
},
other => panic!("应为 Success,实际: {:?}", other),
}
assert_eq!(server.validate_count(), 1, "validate_ticket 应被调用 1 次");
}
#[tokio::test]
async fn t017_sso_login_invalid_ticket_returns_failed() {
let _locale_guard = crate::i18n::set_locale(crate::i18n::GarrisonLocale::Zh);
let server = Arc::new(MockSsoServer::new_failing());
let server_ref = server.clone() as Arc<dyn SsoServer>;
let mut sso_resolver = MockSsoServerResolver::new();
sso_resolver.register("keycloak", server_ref);
let social_resolver = MockSocialProviderResolver::new();
let executor = make_t017_executor();
let builder = dummy_builder();
let flow = FlowBuilder::new("sso-flow").sso("keycloak").build();
let mut ctx = make_context("", "invalid-ticket");
ctx.extras
.insert("client_id".to_string(), "2001".to_string());
let result = executor
.execute_with_full(&flow, &mut ctx, &builder, &social_resolver, &sso_resolver)
.await
.unwrap();
match result {
AuthResult::Failed { reason, step } => {
assert!(
reason.contains("SSO 票据校验失败"),
"reason 应含 SSO 票据校验失败: {}",
reason
);
assert_eq!(step, 0);
},
other => panic!("应为 Failed,实际: {:?}", other),
}
assert_eq!(
server.validate_count(),
1,
"validate_ticket 应被调用 1 次(即使失败)"
);
}
#[tokio::test]
async fn t017_sso_login_empty_input_challenge_required() {
let _locale_guard = crate::i18n::set_locale(crate::i18n::GarrisonLocale::Zh);
let sso_resolver = MockSsoServerResolver::new();
let social_resolver = MockSocialProviderResolver::new();
let executor = make_t017_executor();
let builder = dummy_builder();
let flow = FlowBuilder::new("sso-flow").sso("keycloak").build();
let mut ctx = make_context("", "");
let result = executor
.execute_with_full(&flow, &mut ctx, &builder, &social_resolver, &sso_resolver)
.await
.unwrap();
match result {
AuthResult::ChallengeRequired {
challenge_type,
message,
} => {
assert_eq!(challenge_type, "sso:keycloak");
assert!(
message.contains("SSO 登录"),
"message 应含 SSO 登录: {}",
message
);
},
other => panic!("应为 ChallengeRequired,实际: {:?}", other),
}
}
#[tokio::test]
async fn t017_social_sso_combined_flow_success() {
let wechat = Arc::new(MockSocialLoginProvider::new("wx_user"));
let wechat_ref = wechat.clone() as Arc<dyn SocialLoginProvider>;
let server = Arc::new(MockSsoServer::new("sso_user"));
let server_ref = server.clone() as Arc<dyn SsoServer>;
let mut social_resolver = MockSocialProviderResolver::new();
social_resolver.register("wechat", wechat_ref);
let mut sso_resolver = MockSsoServerResolver::new();
sso_resolver.register("keycloak", server_ref);
let executor = make_t017_executor();
let builder = dummy_builder();
let flow = FlowBuilder::new("combined")
.social("wechat")
.sso("keycloak")
.build();
let mut ctx = make_context("", "auth_code");
ctx.extras.insert("state".to_string(), "s".to_string());
ctx.extras
.insert("client_id".to_string(), "2001".to_string());
let result = executor
.execute_with_full(&flow, &mut ctx, &builder, &social_resolver, &sso_resolver)
.await
.unwrap();
match result {
AuthResult::Success { login_id, token } => {
assert_eq!(login_id, "sso_user");
assert!(!token.is_empty(), "token 不应为空");
assert_eq!(ctx.user_id.as_deref(), Some("sso_user"));
assert_eq!(ctx.completed_steps.len(), 2, "应完成 2 个步骤");
},
other => panic!("应为 Success,实际: {:?}", other),
}
assert_eq!(wechat.exchange_count(), 1);
assert_eq!(server.validate_count(), 1);
}
#[tokio::test]
async fn t017_social_step_without_resolver_returns_failed() {
let repo: Arc<dyn CredentialRepository> = Arc::new(MockCredentialRepository::default());
let executor = make_executor(repo, None);
let builder = dummy_builder();
let flow = FlowBuilder::new("wechat-flow").social("wechat").build();
let mut ctx = make_context("", "auth_code");
ctx.extras.insert("state".to_string(), "s".to_string());
let result = executor
.execute_with_builder(&flow, &mut ctx, &builder)
.await
.unwrap();
match result {
AuthResult::Failed { reason, step } => {
assert!(
reason.contains("SocialProviderResolver"),
"reason 应含 SocialProviderResolver: {}",
reason
);
assert_eq!(step, 0);
},
other => panic!("应为 Failed(无 resolver),实际: {:?}", other),
}
}
#[tokio::test]
async fn t017_pause_after_step_challenge_for_social_step() {
let _locale_guard = crate::i18n::set_locale(crate::i18n::GarrisonLocale::Zh);
let repo = make_repo_with_password("alice").await;
let executor = make_executor(repo, None);
let builder = MockCredentialBuilder {
password_verify_result: true,
totp_verify_result: false,
};
let flow = FlowBuilder::new("two-step")
.login("password")
.social("wechat")
.build();
let mut ctx = make_context("alice", "correct-password");
ctx.extras
.insert("pause_after_step".to_string(), "0".to_string());
let result = executor
.execute_with_builder(&flow, &mut ctx, &builder)
.await
.unwrap();
match result {
AuthResult::Pending {
challenge,
next_step,
..
} => {
assert_eq!(next_step, 1);
assert!(
challenge.contains("wechat"),
"challenge 应含 wechat: {}",
challenge
);
assert!(
challenge.contains("社交登录"),
"challenge 应含社交登录: {}",
challenge
);
},
other => panic!("应为 Pending,实际: {:?}", other),
}
}
#[tokio::test]
async fn t017_pause_after_step_challenge_for_sso_step() {
let repo = make_repo_with_password("alice").await;
let executor = make_executor(repo, None);
let builder = MockCredentialBuilder {
password_verify_result: true,
totp_verify_result: false,
};
let flow = FlowBuilder::new("two-step")
.login("password")
.sso("keycloak")
.build();
let mut ctx = make_context("alice", "correct-password");
ctx.extras
.insert("pause_after_step".to_string(), "0".to_string());
let result = executor
.execute_with_builder(&flow, &mut ctx, &builder)
.await
.unwrap();
match result {
AuthResult::Pending {
challenge,
next_step,
..
} => {
assert_eq!(next_step, 1);
assert!(
challenge.contains("keycloak"),
"challenge 应含 keycloak: {}",
challenge
);
assert!(
challenge.contains("SSO"),
"challenge 应含 SSO: {}",
challenge
);
},
other => panic!("应为 Pending,实际: {:?}", other),
}
}
#[tokio::test]
async fn t017_sso_login_missing_client_id_uses_zero() {
let server = Arc::new(MockSsoServer::new("1001"));
let server_ref = server.clone() as Arc<dyn SsoServer>;
let mut sso_resolver = MockSsoServerResolver::new();
sso_resolver.register("keycloak", server_ref);
let social_resolver = MockSocialProviderResolver::new();
let executor = make_t017_executor();
let builder = dummy_builder();
let flow = FlowBuilder::new("sso-flow").sso("keycloak").build();
let mut ctx = make_context("", "ticket_abc");
let result = executor
.execute_with_full(&flow, &mut ctx, &builder, &social_resolver, &sso_resolver)
.await
.unwrap();
match result {
AuthResult::Success { login_id, .. } => {
assert_eq!(login_id, "1001");
},
other => panic!("应为 Success(缺失 client_id 默认 0),实际: {:?}", other),
}
assert_eq!(server.validate_count(), 1, "validate_ticket 应被调用 1 次");
}
#[tokio::test]
async fn t017_sso_login_invalid_client_id_uses_zero() {
let server = Arc::new(MockSsoServer::new("1001"));
let server_ref = server.clone() as Arc<dyn SsoServer>;
let mut sso_resolver = MockSsoServerResolver::new();
sso_resolver.register("keycloak", server_ref);
let social_resolver = MockSocialProviderResolver::new();
let executor = make_t017_executor();
let builder = dummy_builder();
let flow = FlowBuilder::new("sso-flow").sso("keycloak").build();
let mut ctx = make_context("", "ticket_abc");
ctx.extras
.insert("client_id".to_string(), "not-a-number".to_string());
let result = executor
.execute_with_full(&flow, &mut ctx, &builder, &social_resolver, &sso_resolver)
.await
.unwrap();
match result {
AuthResult::Success { login_id, .. } => {
assert_eq!(login_id, "1001");
},
other => panic!("应为 Success(无效 client_id 回退为 0),实际: {:?}", other),
}
assert_eq!(server.validate_count(), 1, "validate_ticket 应被调用 1 次");
}
#[tokio::test]
async fn t017_social_login_without_state_succeeds() {
let wechat = Arc::new(MockSocialLoginProvider::new("wx_user"));
let wechat_ref = wechat.clone() as Arc<dyn SocialLoginProvider>;
let mut social_resolver = MockSocialProviderResolver::new();
social_resolver.register("wechat", wechat_ref);
let executor = make_t017_executor();
let builder = dummy_builder();
let sso_resolver = MockSsoServerResolver::new();
let flow = FlowBuilder::new("wechat-flow").social("wechat").build();
let mut ctx = make_context("", "auth_code");
let result = executor
.execute_with_full(&flow, &mut ctx, &builder, &social_resolver, &sso_resolver)
.await
.unwrap();
match result {
AuthResult::Success { login_id, .. } => {
assert_eq!(login_id, "wx_user");
},
other => panic!("应为 Success(无 state 仍可执行),实际: {:?}", other),
}
assert_eq!(wechat.exchange_count(), 1, "exchange_token 应被调用 1 次");
}
#[tokio::test]
async fn t017_sso_step_without_resolver_returns_failed() {
let repo: Arc<dyn CredentialRepository> = Arc::new(MockCredentialRepository::default());
let executor = make_executor(repo, None);
let builder = dummy_builder();
let flow = FlowBuilder::new("sso-flow").sso("keycloak").build();
let mut ctx = make_context("", "ticket_abc");
ctx.extras
.insert("client_id".to_string(), "2001".to_string());
let result = executor
.execute_with_builder(&flow, &mut ctx, &builder)
.await
.unwrap();
match result {
AuthResult::Failed { reason, step } => {
assert!(
reason.contains("SsoServerResolver"),
"reason 应含 SsoServerResolver: {}",
reason
);
assert_eq!(step, 0);
},
other => panic!("应为 Failed(无 SSO resolver),实际: {:?}", other),
}
}
}
#[tokio::test]
async fn mock_methods_coverage_credential_repo_interface_credential() {
let repo = MockCredentialRepository::default();
repo.create(make_credential_model("c1", "u1", "password"))
.await
.unwrap();
let mut model = make_credential_model("c1", "u1", "password");
model.secret_data = "new_hash".to_string();
repo.update("u1", model).await.unwrap();
repo.delete("u1", "c1").await.unwrap();
let iface = MockInterface;
let _ = iface.get_permission_list("u1").await.unwrap();
let _ = iface.get_role_list("u1").await.unwrap();
let _ = iface
.get_permission_list_with_type("u1", "user")
.await
.unwrap();
let _ = iface.get_role_list_with_type("u1", "admin").await.unwrap();
let cred = MockCredential {
verify_result: true,
};
let _ = cred.credential_type();
let _ = cred.to_model();
}
#[tokio::test]
async fn execute_social_without_resolver_returns_failed() {
let repo: Arc<dyn CredentialRepository> = Arc::new(MockCredentialRepository::default());
let executor = make_executor(repo, None);
let mut ctx = make_context("", "oauth_code");
let result = executor
.execute_social("wechat", &mut ctx, None)
.await
.unwrap();
match result {
StepOutcome::Failed(_) => {},
other => panic!("应为 Failed,实际: {:?}", other),
}
}
#[tokio::test]
async fn execute_sso_without_resolver_returns_failed() {
let repo: Arc<dyn CredentialRepository> = Arc::new(MockCredentialRepository::default());
let executor = make_executor(repo, None);
let mut ctx = make_context("", "ticket_abc");
let result = executor
.execute_sso("keycloak", &mut ctx, None)
.await
.unwrap();
match result {
StepOutcome::Failed(_) => {},
other => panic!("应为 Failed,实际: {:?}", other),
}
}
}