use super::{AuthCondition, AuthStep, AuthenticationFlow};
#[derive(Debug)]
pub struct FlowBuilder {
name: String,
steps: Vec<AuthStep>,
allow_skip: bool,
}
impl FlowBuilder {
pub fn new(name: &str) -> Self {
Self {
name: name.to_string(),
steps: Vec::new(),
allow_skip: false,
}
}
pub fn login(mut self, credential_type: &str) -> Self {
self.steps.push(AuthStep::Login {
credential_type: credential_type.to_string(),
});
self
}
pub fn mfa(mut self, credential_type: Option<&str>) -> Self {
self.steps.push(AuthStep::Mfa {
credential_type: credential_type.map(|s| s.to_string()),
});
self
}
pub fn social(mut self, provider: &str) -> Self {
self.steps.push(AuthStep::SocialProvider {
provider: provider.to_string(),
});
self
}
pub fn sso(mut self, server_id: &str) -> Self {
self.steps.push(AuthStep::SsoServer {
server_id: server_id.to_string(),
});
self
}
pub fn conditional(
mut self,
condition: AuthCondition,
if_step: AuthStep,
else_step: Option<AuthStep>,
) -> Self {
self.steps.push(AuthStep::Conditional {
condition,
if_step: Box::new(if_step),
else_step: else_step.map(Box::new),
});
self
}
pub fn sub_flow(mut self, flow_name: &str) -> Self {
self.steps.push(AuthStep::SubFlow {
flow_name: flow_name.to_string(),
});
self
}
pub fn allow_skip(mut self) -> Self {
self.allow_skip = true;
self
}
pub fn build(self) -> AuthenticationFlow {
AuthenticationFlow {
name: self.name,
steps: self.steps,
allow_skip: self.allow_skip,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn empty_flow() {
let flow = FlowBuilder::new("empty").build();
assert_eq!(flow.name, "empty");
assert!(flow.steps.is_empty());
assert!(!flow.allow_skip);
}
#[test]
fn single_step_flow() {
let flow = FlowBuilder::new("single").login("password").build();
assert_eq!(flow.steps.len(), 1);
assert!(matches!(
&flow.steps[0],
AuthStep::Login { credential_type } if credential_type == "password"
));
}
#[test]
fn multi_step_flow() {
let flow = FlowBuilder::new("multi")
.login("password")
.mfa(Some("totp"))
.social("wechat")
.sso("keycloak")
.build();
assert_eq!(flow.steps.len(), 4);
assert!(matches!(flow.steps[0], AuthStep::Login { .. }));
assert!(matches!(
&flow.steps[1],
AuthStep::Mfa { credential_type } if credential_type.as_deref() == Some("totp")
));
assert!(matches!(
&flow.steps[2],
AuthStep::SocialProvider { provider } if provider == "wechat"
));
assert!(matches!(
&flow.steps[3],
AuthStep::SsoServer { server_id } if server_id == "keycloak"
));
}
#[test]
fn conditional_flow() {
let flow = FlowBuilder::new("cond")
.login("password")
.conditional(
AuthCondition::HasCredential("totp".to_string()),
AuthStep::Mfa {
credential_type: Some("totp".to_string()),
},
None,
)
.build();
assert_eq!(flow.steps.len(), 2);
match &flow.steps[1] {
AuthStep::Conditional {
condition,
if_step,
else_step,
} => {
assert!(matches!(
condition,
AuthCondition::HasCredential(ref s) if s == "totp"
));
assert!(matches!(if_step.as_ref(), AuthStep::Mfa { .. }));
assert!(else_step.is_none());
},
_ => panic!("第二个步骤应为 Conditional"),
}
}
#[test]
fn sub_flow_step() {
let flow = FlowBuilder::new("parent")
.login("password")
.sub_flow("child-flow")
.build();
assert_eq!(flow.steps.len(), 2);
assert!(matches!(
&flow.steps[1],
AuthStep::SubFlow { flow_name } if flow_name == "child-flow"
));
}
#[test]
fn allow_skip_flag() {
let flow = FlowBuilder::new("skippable")
.login("password")
.allow_skip()
.build();
assert!(flow.allow_skip);
let flow_default = FlowBuilder::new("not-skippable").login("password").build();
assert!(!flow_default.allow_skip);
}
#[test]
fn mfa_none_variant() {
let flow = FlowBuilder::new("mfa-none").mfa(None).build();
assert_eq!(flow.steps.len(), 1);
assert!(matches!(
&flow.steps[0],
AuthStep::Mfa { credential_type } if credential_type.is_none()
));
}
}