177 lines
4.2 KiB
Rust
Raw Normal View History

2022-11-28 16:16:33 -08:00
use serde::{Serialize, Deserialize};
2024-06-25 15:19:29 -04:00
use sqlx::types::Uuid;
2024-06-28 20:35:18 -04:00
use tauri::{AppHandle, State};
2022-11-28 16:16:33 -08:00
use crate::config::AppConfig;
2024-06-25 15:19:29 -04:00
use crate::credentials::{
AppSession,
2024-07-01 06:38:46 -04:00
CredentialRecord,
SshKey,
2024-06-25 15:19:29 -04:00
};
use crate::errors::*;
2022-12-19 16:20:46 -08:00
use crate::clientinfo::Client;
use crate::state::AppState;
2023-08-02 19:57:37 -07:00
use crate::terminal;
2022-11-28 16:16:33 -08:00
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct AwsRequestNotification {
2022-11-29 16:13:09 -08:00
pub id: u64,
pub client: Client,
pub base: bool,
2022-11-29 16:13:09 -08:00
}
2024-06-19 05:10:55 -04:00
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct SshRequestNotification {
pub id: u64,
pub client: Client,
pub key_name: String,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
2024-06-25 15:19:29 -04:00
#[serde(tag = "type")]
2024-06-19 05:10:55 -04:00
pub enum RequestNotification {
Aws(AwsRequestNotification),
Ssh(SshRequestNotification),
}
impl RequestNotification {
pub fn new_aws(id: u64, client: Client, base: bool) -> Self {
Self::Aws(AwsRequestNotification {id, client, base})
}
pub fn new_ssh(id: u64, client: Client, key_name: String) -> Self {
Self::Ssh(SshRequestNotification {id, client, key_name})
}
}
#[derive(Debug, Serialize, Deserialize)]
2022-11-28 16:16:33 -08:00
pub struct RequestResponse {
pub id: u64,
pub approval: Approval,
pub base: bool,
2022-11-28 16:16:33 -08:00
}
#[derive(Debug, Serialize, Deserialize)]
2022-11-28 16:16:33 -08:00
pub enum Approval {
Approved,
Denied,
}
2022-11-28 16:16:33 -08:00
#[tauri::command]
2023-05-02 11:33:18 -07:00
pub async fn respond(response: RequestResponse, app_state: State<'_, AppState>) -> Result<(), SendResponseError> {
app_state.send_response(response).await
2022-11-28 16:16:33 -08:00
}
#[tauri::command]
2022-12-29 16:40:48 -08:00
pub async fn unlock(passphrase: String, app_state: State<'_, AppState>) -> Result<(), UnlockError> {
app_state.unlock(&passphrase).await
2022-12-03 21:47:09 -08:00
}
2024-06-25 15:19:29 -04:00
#[tauri::command]
pub async fn lock(app_state: State<'_, AppState>) -> Result<(), LockError> {
app_state.lock().await
}
2024-06-28 11:19:52 -04:00
#[tauri::command]
pub async fn reset_session(app_state: State<'_, AppState>) -> Result<(), SaveCredentialsError> {
app_state.reset_session().await
}
2024-06-25 15:19:29 -04:00
#[tauri::command]
pub async fn set_passphrase(passphrase: &str, app_state: State<'_, AppState>) -> Result<(), SaveCredentialsError> {
app_state.set_passphrase(passphrase).await
}
2022-12-03 21:47:09 -08:00
#[tauri::command]
2023-05-02 15:24:35 -07:00
pub async fn get_session_status(app_state: State<'_, AppState>) -> Result<String, ()> {
2024-06-19 05:10:55 -04:00
let session = app_state.app_session.read().await;
2023-05-02 15:24:35 -07:00
let status = match *session {
2024-06-19 05:10:55 -04:00
AppSession::Locked{..} => "locked".into(),
AppSession::Unlocked{..} => "unlocked".into(),
AppSession::Empty => "empty".into(),
2023-05-02 15:24:35 -07:00
};
Ok(status)
2022-11-28 16:16:33 -08:00
}
2022-12-13 21:50:34 -08:00
#[tauri::command]
pub async fn signal_activity(app_state: State<'_, AppState>) -> Result<(), ()> {
app_state.signal_activity().await;
Ok(())
}
2022-12-13 21:50:34 -08:00
#[tauri::command]
2024-06-25 15:19:29 -04:00
pub async fn save_credential(
record: CredentialRecord,
2022-12-13 21:50:34 -08:00
app_state: State<'_, AppState>
2024-06-19 05:10:55 -04:00
) -> Result<(), SaveCredentialsError> {
app_state.save_credential(record).await
2024-06-25 15:19:29 -04:00
}
#[tauri::command]
pub async fn delete_credential(id: &str, app_state: State<'_, AppState>) -> Result<(), SaveCredentialsError> {
let id = Uuid::try_parse(id)
.map_err(|_| LoadCredentialsError::NoCredentials)?;
app_state.delete_credential(&id).await
}
#[tauri::command]
pub async fn list_credentials(app_state: State<'_, AppState>) -> Result<Vec<CredentialRecord>, GetCredentialsError> {
2024-06-25 15:19:29 -04:00
app_state.list_credentials().await
2022-12-13 21:50:34 -08:00
}
2024-07-01 06:38:46 -04:00
#[tauri::command]
pub async fn sshkey_from_file(path: String, passphrase: &str) -> Result<SshKey, LoadSshKeyError> {
SshKey::from_file(path, passphrase)
}
#[tauri::command]
2023-05-02 15:24:35 -07:00
pub async fn get_config(app_state: State<'_, AppState>) -> Result<AppConfig, ()> {
let config = app_state.config.read().await;
Ok(config.clone())
}
2023-04-25 22:10:14 -07:00
#[tauri::command]
2023-04-26 15:49:08 -07:00
pub async fn save_config(config: AppConfig, app_state: State<'_, AppState>) -> Result<(), String> {
app_state.update_config(config)
.await
2023-04-28 14:33:04 -07:00
.map_err(|e| format!("Error saving config: {e}"))?;
Ok(())
2023-04-25 22:10:14 -07:00
}
2023-08-02 19:57:37 -07:00
#[tauri::command]
pub async fn launch_terminal(base: bool) -> Result<(), LaunchTerminalError> {
let res = terminal::launch(base).await;
res
2023-08-02 19:57:37 -07:00
}
#[tauri::command]
pub async fn get_setup_errors(app_state: State<'_, AppState>) -> Result<Vec<String>, ()> {
Ok(app_state.setup_errors.clone())
}
2024-06-28 20:35:18 -04:00
#[tauri::command]
pub fn exit(app_handle: AppHandle) {
app_handle.exit(0)
}