use super::builder::FlowBuilder;
use super::registry::FlowRegistration;
use super::{AuthCondition, AuthStep};
fn default_password_flow() -> super::AuthenticationFlow {
FlowBuilder::new("default-password-flow")
.login("password")
.conditional(
AuthCondition::HasCredential("totp".to_string()),
AuthStep::Mfa {
credential_type: Some("totp".to_string()),
},
None,
)
.build()
}
fn default_mfa_flow() -> super::AuthenticationFlow {
FlowBuilder::new("default-mfa-flow")
.login("password")
.mfa(Some("totp"))
.build()
}
fn default_social_flow() -> super::AuthenticationFlow {
FlowBuilder::new("default-social-flow")
.social("wechat")
.conditional(
AuthCondition::HasCredential("totp".to_string()),
AuthStep::Mfa {
credential_type: Some("totp".to_string()),
},
None,
)
.build()
}
fn default_sso_flow() -> super::AuthenticationFlow {
FlowBuilder::new("default-sso-flow")
.sso("default")
.conditional(
AuthCondition::HasCredential("totp".to_string()),
AuthStep::Mfa {
credential_type: Some("totp".to_string()),
},
None,
)
.build()
}
inventory::submit! {
FlowRegistration {
name: "default-password-flow",
flow: default_password_flow,
}
}
inventory::submit! {
FlowRegistration {
name: "default-mfa-flow",
flow: default_mfa_flow,
}
}
inventory::submit! {
FlowRegistration {
name: "default-social-flow",
flow: default_social_flow,
}
}
inventory::submit! {
FlowRegistration {
name: "default-sso-flow",
flow: default_sso_flow,
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::account::authflow::FlowRegistry;
#[test]
fn default_password_flow_registered() {
let registry = FlowRegistry::from_inventory();
let flow = registry
.get("default-password-flow")
.expect("default-password-flow 应已注册");
assert_eq!(flow.name, "default-password-flow");
assert_eq!(flow.steps.len(), 2);
assert!(matches!(flow.steps[0], AuthStep::Login { .. }));
assert!(matches!(flow.steps[1], AuthStep::Conditional { .. }));
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 { credential_type: Some(ref ct) } if ct == "totp"
));
assert!(else_step.is_none());
},
_ => panic!("第二个步骤应为 Conditional"),
}
}
#[test]
fn default_mfa_flow_registered() {
let registry = FlowRegistry::from_inventory();
let flow = registry
.get("default-mfa-flow")
.expect("default-mfa-flow 应已注册");
assert_eq!(flow.name, "default-mfa-flow");
assert_eq!(flow.steps.len(), 2);
assert!(matches!(
&flow.steps[0],
AuthStep::Login { credential_type } if credential_type == "password"
));
assert!(matches!(
&flow.steps[1],
AuthStep::Mfa { credential_type: Some(ref ct) } if ct == "totp"
));
}
#[test]
fn default_social_flow_registered() {
let registry = FlowRegistry::from_inventory();
let flow = registry
.get("default-social-flow")
.expect("default-social-flow 应已注册");
assert_eq!(flow.name, "default-social-flow");
assert_eq!(flow.steps.len(), 2);
assert!(matches!(
&flow.steps[0],
AuthStep::SocialProvider { provider } if provider == "wechat"
));
assert!(matches!(flow.steps[1], AuthStep::Conditional { .. }));
}
#[test]
fn default_sso_flow_registered() {
let registry = FlowRegistry::from_inventory();
let flow = registry
.get("default-sso-flow")
.expect("default-sso-flow 应已注册");
assert_eq!(flow.name, "default-sso-flow");
assert_eq!(flow.steps.len(), 2);
assert!(matches!(
&flow.steps[0],
AuthStep::SsoServer { server_id } if server_id == "default"
));
assert!(matches!(flow.steps[1], AuthStep::Conditional { .. }));
}
}