use super::common::{argon2_verify, bcrypt_verify};
use crate::DriverCallback;
use crate::DriverContext;
use crate::types::{Driver, DriverParameter};
use crate::{DriverError, DriverResult};
use serde_json::{Value, json};
use std::collections::HashMap;
use tracing::{debug, info, warn};
#[derive(Debug)]
pub struct PasswordVerifyDriver;
#[async_trait::async_trait]
impl Driver for PasswordVerifyDriver {
fn name(&self) -> &str {
"password_verify"
}
fn description(&self) -> &str {
"Verify a password against a stored hash"
}
fn usage_hint(&self) -> &str {
"Use this skill to verify passwords during authentication. Supports bcrypt and Argon2id hashes."
}
fn parameters(&self) -> Vec<DriverParameter> {
return vec![
DriverParameter {
name: "password".to_string(),
param_type: "string".to_string(),
description: "Password to verify".to_string(),
required: true,
default: None,
example: Some(Value::String("MySecureP@ssw0rd".to_string())),
enum_values: None,
},
DriverParameter {
name: "hash".to_string(),
param_type: "string".to_string(),
description: "Stored password hash".to_string(),
required: true,
default: None,
example: Some(Value::String("$2b$12$abc123def456...".to_string())),
enum_values: None,
},
];
}
fn example_call(&self) -> DriverResult<Value> {
return Ok(json!({
"action": "password_verify",
"parameters": {
"password": "MySecureP@ssw0rd",
"hash": "$2b$12$abc123def456..."
}
}));
}
fn example_output(&self) -> String {
return "Password matches".to_string();
}
fn category(&self) -> crate::DriverCategory {
return crate::DriverCategory::Cryptography;
}
async fn execute(
&self,
parameters: &HashMap<String, Value>,
callback: Option<&dyn DriverCallback>,
context: Option<&DriverContext>,
) -> DriverResult<String> {
debug!("Executing password_verify driver");
let password = parameters.get("password").and_then(|v| v.as_str()).ok_or_else(|| {
debug!("Missing 'password' parameter");
DriverError::missing_parameter("password")
})?;
let hash = parameters.get("hash").and_then(|v| v.as_str()).ok_or_else(|| {
debug!("Missing 'hash' parameter");
DriverError::missing_parameter("hash")
})?;
debug!("Verifying password against hash");
let is_valid = if hash.starts_with("$2") {
debug!("Detected bcrypt hash format");
bcrypt_verify(password, hash).map_err(|e| DriverError::execution(format!("bcrypt verification failed: {}", e)))?
} else if hash.starts_with("$argon2id") {
debug!("Detected Argon2id hash format");
argon2_verify(password, hash).map_err(|e| DriverError::execution(format!("Argon2id verification failed: {}", e)))?
} else {
warn!("Unsupported hash format: {}", hash);
return Err(DriverError::execution(format!("Unsupported hash format: {}", hash)));
};
if is_valid {
info!("Password matches");
return Ok("Password matches".to_string());
} else {
info!("Password does not match");
return Ok("Password does not match".to_string());
}
}
fn validate(&self, parameters: &HashMap<String, Value>) -> DriverResult<()> {
debug!("Validating password_verify parameters");
if parameters.get("password").is_none() {
return Err(DriverError::missing_parameter("password"));
}
if parameters.get("hash").is_none() {
return Err(DriverError::missing_parameter("hash"));
}
debug!("Parameter validation passed");
return Ok(());
}
}