From 21841b4910aaf2086a9ab829f4d44a0956b3b170 Mon Sep 17 00:00:00 2001 From: Sebastian Martinez Date: Fri, 23 Aug 2024 17:35:32 +0200 Subject: [PATCH] Add authenticate command Checks if there is a profile to be loaded, if the ssh agent is running and if there is a Radicle identity stored in the ssh-agent. --- src-tauri/src/auth.rs | 27 +++++++++++++++++++++++++++ src-tauri/src/lib.rs | 1 + 2 files changed, 28 insertions(+) create mode 100644 src-tauri/src/auth.rs diff --git a/src-tauri/src/auth.rs b/src-tauri/src/auth.rs new file mode 100644 index 0000000..648f7e4 --- /dev/null +++ b/src-tauri/src/auth.rs @@ -0,0 +1,27 @@ +use anyhow::anyhow; +use radicle::crypto::ssh; + +use crate::{error::Error, AppState}; + +#[tauri::command] +pub fn authenticate(ctx: tauri::State) -> Result<(), Error> { + let profile = &ctx.profile; + + if !profile.keystore.is_encrypted()? { + return Ok(()); + } + match ssh::agent::Agent::connect() { + Ok(mut agent) => { + if agent.request_identities()?.contains(&profile.public_key) { + return Ok(()); + } else { + Err(Error::WithHint { + err: anyhow!("Not able to find your keys in the ssh agent"), + hint: "Make sure to run rad auth in your terminal to add your keys to the ssh-agent.", + })? + } + } + Err(e) if e.is_not_running() => Err(Error::WithHint { err: anyhow!("SSH Agent is not running"), hint: "For now we require the user to have an ssh agent running, since we don't have passphrase inputs yet." })?, + Err(e) => Err(e)?, + } +} diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index d166206..36ef08c 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -31,6 +31,7 @@ pub fn run() { }) .plugin(tauri_plugin_shell::init()) .plugin(tauri_plugin_window_state::Builder::default().build()) + .invoke_handler(tauri::generate_handler![authenticate]) .run(tauri::generate_context!()) .expect("error while running tauri application"); }