81 lines
1.9 KiB
Rust
Raw Normal View History

2022-11-28 16:16:33 -08:00
use serde::{Serialize, Deserialize};
use tauri::State;
use crate::config::AppConfig;
2023-05-06 11:48:08 -07:00
use crate::credentials::{Session,BaseCredentials};
use crate::errors::*;
2022-12-19 16:20:46 -08:00
use crate::clientinfo::Client;
2023-05-06 11:48:08 -07:00
use crate::state::AppState;
2022-11-28 16:16:33 -08:00
#[derive(Clone, Debug, Serialize, Deserialize)]
2022-11-29 16:13:09 -08:00
pub struct Request {
pub id: u64,
2022-12-20 16:11:49 -08:00
pub clients: Vec<Option<Client>>,
2023-05-06 12:00:31 -07:00
pub base: bool,
2022-11-29 16:13:09 -08:00
}
#[derive(Debug, Serialize, Deserialize)]
2022-11-28 16:16:33 -08:00
pub struct RequestResponse {
pub id: u64,
pub approval: Approval,
}
#[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
}
#[tauri::command]
2023-05-02 15:24:35 -07:00
pub async fn get_session_status(app_state: State<'_, AppState>) -> Result<String, ()> {
let session = app_state.session.read().await;
let status = match *session {
2022-12-03 21:47:09 -08:00
Session::Locked(_) => "locked".into(),
Session::Unlocked{..} => "unlocked".into(),
2022-12-03 21:47:09 -08:00
Session::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 save_credentials(
credentials: BaseCredentials,
2022-12-13 21:50:34 -08:00
passphrase: String,
app_state: State<'_, AppState>
2022-12-29 16:40:48 -08:00
) -> Result<(), UnlockError> {
2023-05-06 11:48:08 -07:00
app_state.new_creds(credentials, &passphrase).await
2022-12-13 21:50:34 -08:00
}
#[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
}