mirror of
https://github.com/Drop-OSS/drop-app.git
synced 2025-11-14 08:41:21 +10:00
auth initiate, database and more
This commit is contained in:
@ -1,14 +1,63 @@
|
||||
use std::{
|
||||
borrow::{Borrow, BorrowMut},
|
||||
sync::Mutex,
|
||||
};
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
use tauri::Error;
|
||||
use url::Url;
|
||||
|
||||
use crate::{data::DatabaseInterface, AppAuthenticationStatus, User};
|
||||
use crate::{data::DatabaseInterface, AppState, AppStatus, User, DB};
|
||||
|
||||
#[derive(Serialize)]
|
||||
struct InitiateRequestBody {
|
||||
name: String,
|
||||
platform: String,
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn auth_initiate<'a>() -> Result<(), String> {
|
||||
let base_url = {
|
||||
let db_lock = DB.borrow_data().unwrap();
|
||||
Url::parse(&db_lock.base_url.clone()).unwrap()
|
||||
};
|
||||
|
||||
let current_os_info = os_info::get();
|
||||
|
||||
let endpoint = base_url.join("/api/v1/client/initiate").unwrap();
|
||||
let body = InitiateRequestBody {
|
||||
name: format!("Drop Desktop Client"),
|
||||
platform: current_os_info.os_type().to_string(),
|
||||
};
|
||||
|
||||
let client = reqwest::Client::new();
|
||||
let response = client
|
||||
.post(endpoint.to_string())
|
||||
.json(&body)
|
||||
.send()
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let redir_url = response.text().await.unwrap();
|
||||
let complete_redir_url = base_url.join(&redir_url).unwrap();
|
||||
|
||||
webbrowser::open(&complete_redir_url.to_string()).unwrap();
|
||||
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
pub fn setup() -> Result<(AppStatus, Option<User>), Error> {
|
||||
let data = DB.borrow_data().unwrap();
|
||||
|
||||
pub fn setup(db: DatabaseInterface) -> Result<(AppAuthenticationStatus, Option<User>), Error> {
|
||||
let data = db.get_data(true).unwrap();
|
||||
// If we have certs, exit for now
|
||||
if data.certs.is_some() {
|
||||
// TODO: check if it's still valid, and fetch user information
|
||||
return Ok((AppAuthenticationStatus::SignedInNeedsReauth, None));
|
||||
return Ok((AppStatus::SignedInNeedsReauth, None));
|
||||
}
|
||||
|
||||
return Ok((AppAuthenticationStatus::SignedOut, None));
|
||||
drop(data);
|
||||
|
||||
auth_initiate();
|
||||
|
||||
return Ok((AppStatus::SignedOut, None));
|
||||
}
|
||||
|
||||
@ -1,32 +1,40 @@
|
||||
use std::fs::create_dir_all;
|
||||
use std::fs;
|
||||
|
||||
use directories::BaseDirs;
|
||||
use rustbreak::{deser::Bincode, FileDatabase, PathDatabase};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use rustbreak::{deser::Bincode, PathDatabase};
|
||||
use serde::Deserialize;
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone)]
|
||||
use crate::DB;
|
||||
|
||||
#[derive(serde::Serialize, Clone, Deserialize)]
|
||||
pub struct DatabaseCerts {
|
||||
pub private: String,
|
||||
pub public: String,
|
||||
pub cert: String,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Clone, Deserialize)]
|
||||
#[derive(serde::Serialize, Clone, Deserialize)]
|
||||
pub struct Database {
|
||||
pub certs: Option<DatabaseCerts>,
|
||||
pub base_url: String,
|
||||
}
|
||||
|
||||
pub type DatabaseInterface =
|
||||
rustbreak::Database<Database, rustbreak::backend::PathBackend, Bincode>;
|
||||
pub type DatabaseInterface = rustbreak::Database<Database, rustbreak::backend::PathBackend, Bincode>;
|
||||
|
||||
pub fn setup() -> DatabaseInterface {
|
||||
let db_path = BaseDirs::new().unwrap().data_dir().join("drop");
|
||||
let default = Database {
|
||||
certs: None
|
||||
certs: None,
|
||||
base_url: "".to_string(),
|
||||
};
|
||||
let db = match fs::exists(db_path.clone()).unwrap() {
|
||||
true => PathDatabase::load_from_path(db_path).expect("Database loading failed"),
|
||||
false => PathDatabase::create_at_path(db_path, default).unwrap(),
|
||||
};
|
||||
let db = PathDatabase::<Database, Bincode>::create_at_path(db_path, default).unwrap();
|
||||
|
||||
db.save().unwrap();
|
||||
|
||||
return db;
|
||||
}
|
||||
|
||||
pub fn is_set_up() -> bool {
|
||||
return !DB.borrow_data().unwrap().base_url.is_empty();
|
||||
}
|
||||
|
||||
@ -1,13 +1,21 @@
|
||||
mod auth;
|
||||
mod data;
|
||||
mod remote;
|
||||
mod unpacker;
|
||||
|
||||
use std::{
|
||||
borrow::Borrow,
|
||||
sync::{LazyLock, Mutex},
|
||||
};
|
||||
|
||||
use auth::auth_initiate;
|
||||
use data::DatabaseInterface;
|
||||
use remote::use_remote;
|
||||
use serde::Serialize;
|
||||
use tauri::Runtime;
|
||||
|
||||
#[derive(Clone, Copy, Serialize)]
|
||||
pub enum AppAuthenticationStatus {
|
||||
pub enum AppStatus {
|
||||
NotConfigured,
|
||||
SignedOut,
|
||||
SignedIn,
|
||||
SignedInNeedsReauth,
|
||||
@ -17,28 +25,47 @@ pub struct User {}
|
||||
|
||||
#[derive(Clone, Copy, Serialize)]
|
||||
pub struct AppState {
|
||||
auth: AppAuthenticationStatus,
|
||||
status: AppStatus,
|
||||
user: Option<User>,
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
fn fetch_state(state: tauri::State<AppState>) -> Result<AppState, String> {
|
||||
Ok(*state.inner())
|
||||
fn fetch_state<'a>(state: tauri::State<'_, Mutex<AppState>>) -> Result<AppState, String> {
|
||||
let guard = state.lock().unwrap();
|
||||
let cloned_state = guard.clone();
|
||||
drop(guard);
|
||||
Ok(cloned_state)
|
||||
}
|
||||
|
||||
fn setup<'a>() -> AppState {
|
||||
let is_set_up = data::is_set_up();
|
||||
if !is_set_up {
|
||||
return AppState {
|
||||
status: AppStatus::NotConfigured,
|
||||
user: None,
|
||||
};
|
||||
}
|
||||
|
||||
let auth_result = auth::setup().unwrap();
|
||||
return AppState {
|
||||
status: auth_result.0,
|
||||
user: auth_result.1,
|
||||
};
|
||||
}
|
||||
|
||||
pub static DB: LazyLock<DatabaseInterface> = LazyLock::new(|| data::setup());
|
||||
|
||||
#[cfg_attr(mobile, tauri::mobile_entry_point)]
|
||||
pub fn run() {
|
||||
let db: DatabaseInterface = data::setup();
|
||||
let auth_result = auth::setup(db).unwrap();
|
||||
|
||||
let state = AppState {
|
||||
auth: auth_result.0,
|
||||
user: auth_result.1,
|
||||
};
|
||||
let state = setup();
|
||||
|
||||
tauri::Builder::default()
|
||||
.manage(state)
|
||||
.invoke_handler(tauri::generate_handler![fetch_state])
|
||||
.manage(Mutex::new(state))
|
||||
.invoke_handler(tauri::generate_handler![
|
||||
fetch_state,
|
||||
auth_initiate,
|
||||
use_remote
|
||||
])
|
||||
.plugin(tauri_plugin_shell::init())
|
||||
.run(tauri::generate_context!())
|
||||
.expect("error while running tauri application");
|
||||
|
||||
@ -4,3 +4,4 @@
|
||||
fn main() {
|
||||
drop_app_lib::run()
|
||||
}
|
||||
|
||||
|
||||
59
src-tauri/src/remote.rs
Normal file
59
src-tauri/src/remote.rs
Normal file
@ -0,0 +1,59 @@
|
||||
use std::{
|
||||
borrow::{Borrow, BorrowMut},
|
||||
sync::Mutex,
|
||||
};
|
||||
|
||||
use serde::Deserialize;
|
||||
use url::Url;
|
||||
|
||||
use crate::{AppState, AppStatus, DB};
|
||||
|
||||
macro_rules! unwrap_or_return {
|
||||
( $e:expr ) => {
|
||||
match $e {
|
||||
Ok(x) => x,
|
||||
Err(e) => {
|
||||
return Err(format!(
|
||||
"Invalid URL or Drop is inaccessible ({})",
|
||||
e.to_string()
|
||||
))
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct DropHealthcheck {
|
||||
appName: String,
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub async fn use_remote<'a>(
|
||||
url: String,
|
||||
state: tauri::State<'_, Mutex<AppState>>,
|
||||
) -> Result<(), String> {
|
||||
println!("connecting to url {}", url);
|
||||
let base_url = unwrap_or_return!(Url::parse(&url));
|
||||
|
||||
// Test Drop url
|
||||
let test_endpoint = base_url.join("/api/v1").unwrap();
|
||||
let response = unwrap_or_return!(reqwest::get(test_endpoint.to_string()).await);
|
||||
|
||||
let result = response.json::<DropHealthcheck>().await.unwrap();
|
||||
|
||||
if result.appName != "Drop" {
|
||||
return Err("Not a valid Drop endpoint".to_string());
|
||||
}
|
||||
|
||||
let mut app_state = state.lock().unwrap();
|
||||
app_state.status = AppStatus::SignedOut;
|
||||
drop(app_state);
|
||||
|
||||
let mut db_state = DB.borrow_data_mut().unwrap();
|
||||
db_state.base_url = base_url.to_string();
|
||||
drop(db_state);
|
||||
|
||||
DB.save().unwrap();
|
||||
|
||||
return Ok(());
|
||||
}
|
||||
Reference in New Issue
Block a user