Add keyring template function

This commit is contained in:
Gregory Schier
2025-09-29 08:56:24 -07:00
parent b3d6d87bee
commit 0b0b05d29c
12 changed files with 73 additions and 134 deletions

View File

@@ -6,6 +6,8 @@ use crate::template_callback::PluginTemplateCallback;
use base64::Engine;
use base64::prelude::BASE64_STANDARD;
use std::collections::HashMap;
use keyring::Error::NoEntry;
use log::debug;
use tauri::{AppHandle, Runtime};
use yaak_crypto::manager::EncryptionManagerExt;
use yaak_templates::error::Error::RenderError;
@@ -32,6 +34,34 @@ pub(crate) fn template_function_secure() -> TemplateFunction {
}
}
pub(crate) fn template_function_keyring() -> TemplateFunction {
TemplateFunction {
name: "keyring".to_string(),
description: Some("Get a password from the OS keychain/keyring".to_string()),
aliases: None,
args: vec![
TemplateFunctionArg::FormInput(FormInput::Text(FormInputText {
base: FormInputBase {
name: "service".to_string(),
label: Some("Service".to_string()),
description: Some("App or URL for the password".to_string()),
..Default::default()
},
..Default::default()
})),
TemplateFunctionArg::FormInput(FormInput::Text(FormInputText {
base: FormInputBase {
name: "account".to_string(),
label: Some("Account".to_string()),
description: Some("Username or email address".to_string()),
..Default::default()
},
..Default::default()
})),
],
}
}
pub fn template_function_secure_run<R: Runtime>(
app_handle: &AppHandle<R>,
args: HashMap<String, serde_json::Value>,
@@ -163,3 +193,15 @@ pub fn encrypt_secure_template_function<R: Runtime>(
)?
.to_string())
}
pub fn template_function_keychain_run(args: HashMap<String, serde_json::Value>) -> Result<String> {
let service = args.get("service").and_then(|v| v.as_str()).unwrap_or_default().to_owned();
let user = args.get("account").and_then(|v| v.as_str()).unwrap_or_default().to_owned();
debug!("Getting password for service {} and user {}", service, user);
let entry = keyring::Entry::new(&service, &user).map_err(|e| RenderError(e.to_string()))?;
match entry.get_password() {
Ok(p) => Ok(p),
Err(NoEntry) => Err(RenderError(format!("No password found for '{}' and '{}'", service, user))),
Err(e) => Err(RenderError(e.to_string())),
}
}