use crate::{
DriverCallback, DriverCategory, DriverContext, DriverError, DriverResult,
operating_system_security::common::{get_password_strength, is_password_weak},
types::{Driver, DriverParameter},
};
use serde_json::{Value, json};
use std::collections::HashMap;
use tracing::{debug, info};
#[derive(Debug)]
pub struct WeakPasswordCheckDriver;
#[async_trait::async_trait]
impl Driver for WeakPasswordCheckDriver {
fn name(&self) -> &str {
"security_weak_password_check"
}
fn description(&self) -> &str {
"Check if a password is weak or meets security requirements"
}
fn usage_hint(&self) -> &str {
"Use this skill to test password strength. Checks against common weak passwords, length, complexity, and patterns."
}
fn parameters(&self) -> Vec<DriverParameter> {
return vec![
DriverParameter {
name: "password".to_string(),
param_type: "string".to_string(),
description: "Password to check".to_string(),
required: true,
default: None,
example: Some(Value::String("MySecureP@ssw0rd123".to_string())),
enum_values: None,
},
DriverParameter {
name: "username".to_string(),
param_type: "string".to_string(),
description: "Associated username (optional, for context)".to_string(),
required: false,
default: None,
example: Some(Value::String("admin".to_string())),
enum_values: None,
},
];
}
fn example_call(&self) -> DriverResult<Value> {
return Ok(json!({
"action": "security_weak_password_check",
"parameters": {
"password": "MySecureP@ssw0rd123",
"username": "admin"
}
}));
}
fn example_output(&self) -> String {
return "Password strength: Strong\nPassword meets all security requirements".to_string();
}
fn category(&self) -> DriverCategory {
return DriverCategory::OperatingSystemSecurity;
}
async fn execute(
&self,
parameters: &HashMap<String, Value>,
_callback: Option<&dyn DriverCallback>,
_context: Option<&DriverContext>,
) -> DriverResult<String> {
debug!("Executing security_weak_password_check driver");
let password = parameters.get("password").and_then(|v| v.as_str()).ok_or_else(|| DriverError::missing_parameter("password"))?;
let _username = parameters.get("username").and_then(|v| v.as_str()).unwrap_or("");
info!("Checking password strength");
let (is_weak, reason) = is_password_weak(password);
let strength = get_password_strength(password);
let mut result = format!("Password strength: {:?}\n", strength);
result.push_str(&format!("Password: {}\n", if is_weak { "WEAK" } else { "SECURE" }));
result.push_str(&format!("Reason: {}\n", reason));
if is_weak {
info!("Password is weak: {}", reason);
result.push_str("\nRecommendations:\n");
if password.len() < 8 {
result.push_str("- Use at least 8 characters\n");
}
if !password.chars().any(|c| c.is_uppercase()) {
result.push_str("- Include uppercase letters\n");
}
if !password.chars().any(|c| c.is_lowercase()) {
result.push_str("- Include lowercase letters\n");
}
if !password.chars().any(|c| c.is_ascii_digit()) {
result.push_str("- Include numbers\n");
}
if !password.chars().any(|c| !c.is_alphanumeric()) {
result.push_str("- Include special characters (!@#$%^&*)\n");
}
} else {
info!("Password is secure");
}
return Ok(result);
}
}