mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-03-24 02:11:10 +01:00
Add keyring template function
This commit is contained in:
@@ -32,7 +32,7 @@ pub enum Error {
|
||||
|
||||
#[error("JSON error: {0}")]
|
||||
JsonErr(#[from] serde_json::Error),
|
||||
|
||||
|
||||
#[error("API Error: {0}")]
|
||||
ApiErr(String),
|
||||
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::Value;
|
||||
use std::collections::HashMap;
|
||||
use tauri::{Runtime, WebviewWindow};
|
||||
use ts_rs::TS;
|
||||
@@ -163,7 +162,7 @@ pub enum InternalEventPayload {
|
||||
|
||||
impl InternalEventPayload {
|
||||
pub fn type_name(&self) -> String {
|
||||
if let Ok(Value::Object(map)) = serde_json::to_value(self) {
|
||||
if let Ok(serde_json::Value::Object(map)) = serde_json::to_value(self) {
|
||||
map.get("type").map(|s| s.as_str().unwrap_or("unknown").to_string())
|
||||
} else {
|
||||
None
|
||||
|
||||
@@ -14,7 +14,7 @@ use crate::events::{
|
||||
ImportResponse, InternalEvent, InternalEventPayload, JsonPrimitive, PluginWindowContext,
|
||||
RenderPurpose,
|
||||
};
|
||||
use crate::native_template_functions::template_function_secure;
|
||||
use crate::native_template_functions::{template_function_keyring, template_function_secure};
|
||||
use crate::nodejs::start_nodejs_plugin_runtime;
|
||||
use crate::plugin_handle::PluginHandle;
|
||||
use crate::server_ws::PluginRuntimeServerWebsocket;
|
||||
@@ -514,7 +514,7 @@ impl PluginManager {
|
||||
// Add Rust-based functions
|
||||
result.push(GetTemplateFunctionsResponse {
|
||||
plugin_ref_id: "__NATIVE__".to_string(), // Meh
|
||||
functions: vec![template_function_secure()],
|
||||
functions: vec![template_function_secure(), template_function_keyring()],
|
||||
});
|
||||
|
||||
Ok(result)
|
||||
|
||||
@@ -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())),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
use crate::events::{PluginWindowContext, RenderPurpose};
|
||||
use crate::manager::PluginManager;
|
||||
use crate::native_template_functions::{
|
||||
template_function_secure_run, template_function_secure_transform_arg,
|
||||
template_function_keychain_run, template_function_secure_run,
|
||||
template_function_secure_transform_arg,
|
||||
};
|
||||
use std::collections::HashMap;
|
||||
use tauri::{AppHandle, Manager, Runtime};
|
||||
use yaak_templates::error::Result;
|
||||
use yaak_templates::TemplateCallback;
|
||||
use yaak_templates::error::Result;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct PluginTemplateCallback<R: Runtime> {
|
||||
@@ -37,6 +38,8 @@ impl<R: Runtime> TemplateCallback for PluginTemplateCallback<R> {
|
||||
|
||||
if fn_name == "secure" {
|
||||
return template_function_secure_run(&self.app_handle, args, &self.window_context);
|
||||
} else if fn_name == "keyring" {
|
||||
return template_function_keychain_run(args);
|
||||
}
|
||||
|
||||
let plugin_manager = &*self.app_handle.state::<PluginManager>();
|
||||
@@ -51,12 +54,7 @@ impl<R: Runtime> TemplateCallback for PluginTemplateCallback<R> {
|
||||
Ok(resp)
|
||||
}
|
||||
|
||||
fn transform_arg(
|
||||
&self,
|
||||
fn_name: &str,
|
||||
arg_name: &str,
|
||||
arg_value: &str,
|
||||
) -> Result<String> {
|
||||
fn transform_arg(&self, fn_name: &str, arg_name: &str, arg_value: &str) -> Result<String> {
|
||||
if fn_name == "secure" {
|
||||
return template_function_secure_transform_arg(
|
||||
&self.app_handle,
|
||||
|
||||
Reference in New Issue
Block a user