diff --git a/src/auth/auth_connector.rs b/src/auth/auth_connector.rs index 67c3784..574fdfa 100644 --- a/src/auth/auth_connector.rs +++ b/src/auth/auth_connector.rs @@ -142,7 +142,7 @@ pub async fn migrate_user(user_profile: &mut UserProfile) -> bool { let client = client(); let mut payload = JsonValue::new_object(); - payload["iota_id"] = JsonValue::String(CONFIG.lock().unwrap().get_iota_id().to_string()); + payload["iota_id"] = JsonValue::String(CONFIG.lock().await.get_iota_id().to_string()); payload["reset_token"] = user_profile.reset_token.clone().into(); payload["new_token"] = user_profile.randomize_reset_token().into(); diff --git a/src/data/communication.rs b/src/data/communication.rs index c9d2312..26b5767 100644 --- a/src/data/communication.rs +++ b/src/data/communication.rs @@ -77,6 +77,8 @@ pub enum DataTypes { challenge, community_title, communities, + + user, } impl DataTypes { @@ -153,6 +155,8 @@ impl DataTypes { "challenge" => DataTypes::challenge, "communitytitle" => DataTypes::community_title, "communities" => DataTypes::communities, + + "user" => DataTypes::user, _ => DataTypes::error_type, // fallback if unknown } } @@ -215,6 +219,8 @@ pub enum CommunicationType { end_call, function, update, + + create_user, } impl CommunicationType { pub fn parse(p0: String) -> CommunicationType { @@ -269,6 +275,8 @@ impl CommunicationType { "endcall" => CommunicationType::end_call, "function" => CommunicationType::function, "update" => CommunicationType::update, + + "createuser" => CommunicationType::create_user, _ => CommunicationType::error, } } diff --git a/src/main.rs b/src/main.rs index 999534b..bf87f03 100644 --- a/src/main.rs +++ b/src/main.rs @@ -58,10 +58,10 @@ async fn main() { } // BASIC CONFIGURATION - CONFIG.lock().unwrap().load(); - if !CONFIG.lock().unwrap().config.has_key("iota_id") { - CONFIG.lock().unwrap().change("iota_id", Uuid::new_v4()); - CONFIG.lock().unwrap().update(); + CONFIG.lock().await.load(); + if !CONFIG.lock().await.config.has_key("iota_id") { + CONFIG.lock().await.change("iota_id", Uuid::new_v4()); + CONFIG.lock().await.update(); } // USER MANAGEMENT @@ -84,7 +84,7 @@ async fn main() { "IOTA ID: {}-####-####-####-############", CONFIG .lock() - .unwrap() + .await .get_iota_id() .to_string() .split("-") @@ -107,7 +107,7 @@ async fn main() { sb1 = sb1 + ","; } log_message(format!("Community IDS: {}", sb1)); - let port = CONFIG.lock().unwrap().get_port(); + let port = CONFIG.lock().await.get_port(); if start(port).await { log_message(format("community_active", &[&port.to_string()])); } else { @@ -133,7 +133,7 @@ async fn main() { .add_data(DataTypes::user_ids, String(sb.to_string())) .add_data( DataTypes::iota_id, - String(CONFIG.lock().unwrap().get_iota_id().to_string()), + String(CONFIG.lock().await.get_iota_id().to_string()), ) .to_json() .to_string() diff --git a/src/server/api.rs b/src/server/api.rs index 6640ea4..12d4516 100644 --- a/src/server/api.rs +++ b/src/server/api.rs @@ -1,9 +1,11 @@ +use crate::data::communication::{CommunicationType, CommunicationValue, DataTypes}; use crate::gui::log_panel::log_message; use axum::http::HeaderValue; use http_body_util::Full; use hyper::body::Bytes; use hyper::{HeaderMap, Response as HttpResponse, StatusCode}; use json::JsonValue; +use uuid::Uuid; use crate::util::config_util::CONFIG; use crate::{APP_STATE, communities::community_manager, users::user_manager}; @@ -31,8 +33,27 @@ pub async fn handle( "users" => (StatusCode::OK, "application/json", { if path_parts.len() >= 4 { match path_parts[3] { - "add" => "{}".to_string(), - "remove" => "{}".to_string(), + "add" => { + let username = headers.get("username"); + if let (Some(user), Some(_private_key)) = user_manager::create_user( + &username.unwrap().to_str().unwrap().to_string(), + ) + .await + { + let cv = CommunicationValue::new(CommunicationType::create_user) + .add_data(DataTypes::user, user.to_json()); + cv.to_json().to_string() + } else { + "{}".to_string() + } + } + "remove" => { + let uuid = + Uuid::parse_str(headers.get("uuid").unwrap().to_str().unwrap()) + .unwrap(); + user_manager::remove_user(uuid); + "{}".to_string() + } "get" => { let users = user_manager::get_users(); let mut json = JsonValue::new_array(); @@ -61,7 +82,7 @@ pub async fn handle( json.to_string() }), "settings" => (StatusCode::OK, "application/json", { - CONFIG.lock().unwrap().config.to_string() + CONFIG.lock().await.config.to_string() }), _ => { diff --git a/src/users/user_manager.rs b/src/users/user_manager.rs index 5703335..fc3207f 100644 --- a/src/users/user_manager.rs +++ b/src/users/user_manager.rs @@ -44,7 +44,7 @@ pub async fn create_user(username: &str) -> (Option, Option reset_token, ); - auth_connector::complete_register(&up, &CONFIG.lock().unwrap().get_iota_id().to_string()).await; + auth_connector::complete_register(&up, &CONFIG.lock().await.get_iota_id().to_string()).await; save_file( "", &format!("{}.tu", username), diff --git a/src/util/config_util.rs b/src/util/config_util.rs index 7a99460..594981d 100644 --- a/src/util/config_util.rs +++ b/src/util/config_util.rs @@ -1,7 +1,7 @@ use crate::util::file_util::{load_file, save_file}; use json::JsonValue; use once_cell::sync::Lazy; -use std::sync::Mutex; +use tokio::sync::Mutex; use uuid::Uuid; pub static CONFIG: Lazy> = Lazy::new(|| Mutex::new(ConfigUtil::new()));