[Add] App storage

This commit is contained in:
Alex Emmet 2026-04-14 21:37:51 +02:00
commit 3b218411d0
4 changed files with 279 additions and 44 deletions

92
Cargo.lock generated
View file

@ -73,7 +73,7 @@ dependencies = [
"mime",
"percent-encoding",
"pin-project-lite",
"rand 0.9.2",
"rand 0.9.4",
"sha1",
"smallvec",
"tokio",
@ -552,9 +552,9 @@ dependencies = [
[[package]]
name = "cc"
version = "1.2.59"
version = "1.2.60"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b7a4d3ec6524d28a329fc53654bbadc9bdd7b0431f5d65f1a56ffb28a1ee5283"
checksum = "43c5703da9466b66a946814e1adf53ea2c90f10063b86290cc9eb67ce3478a20"
dependencies = [
"find-msvc-tools",
"jobserver",
@ -1344,6 +1344,12 @@ dependencies = [
"foldhash 0.2.0",
]
[[package]]
name = "hashbrown"
version = "0.17.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4f467dd6dccf739c208452f8014c75c18bb8301b050ad1cfb27153803edb0f51"
[[package]]
name = "hashlink"
version = "0.11.0"
@ -1493,15 +1499,14 @@ dependencies = [
[[package]]
name = "hyper-rustls"
version = "0.27.7"
version = "0.27.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e3c93eb611681b207e1fe55d5a71ecf91572ec8a6705cdb6857f7d8d5242cf58"
checksum = "c2b52f86d1d4bc0d6b4e6826d960b1b333217e07d36b882dca570a5e1c48895b"
dependencies = [
"http 1.4.0",
"hyper",
"hyper-util",
"rustls",
"rustls-pki-types",
"tokio",
"tokio-rustls",
"tower-service",
@ -1679,12 +1684,12 @@ checksum = "e8a5a9a0ff0086c7a148acb942baaabeadf9504d10400b5a05645853729b9cd2"
[[package]]
name = "indexmap"
version = "2.13.1"
version = "2.14.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "45a8a2b9cb3e0b0c1803dbb0758ffac5de2f425b23c28f518faabd9d805342ff"
checksum = "d466e9454f08e4a911e14806c24e16fba1b4c121d1ea474396f396069cf949d9"
dependencies = [
"equivalent",
"hashbrown 0.16.1",
"hashbrown 0.17.0",
"serde",
"serde_core",
]
@ -2028,9 +2033,9 @@ dependencies = [
[[package]]
name = "js-sys"
version = "0.3.94"
version = "0.3.95"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2e04e2ef80ce82e13552136fabeef8a5ed1f985a96805761cbb9a2c34e7664d9"
checksum = "2964e92d1d9dc3364cae4d718d93f227e3abb088e747d92e0395bfdedf1c12ca"
dependencies = [
"cfg-if",
"futures-util",
@ -2087,9 +2092,9 @@ checksum = "2c4a545a15244c7d945065b5d392b2d2d7f21526fba56ce51467b06ed445e8f7"
[[package]]
name = "libc"
version = "0.2.184"
version = "0.2.185"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "48f5d2a454e16a5ea0f4ced81bd44e4cfc7bd3a507b61887c99fd3538b28e4af"
checksum = "52ff2c0fe9bc6cb6b14a0592c2ff4fa9ceb83eea9db979b0487cd054946a2b8f"
[[package]]
name = "libsqlite3-sys"
@ -2449,9 +2454,9 @@ dependencies = [
[[package]]
name = "openssl"
version = "0.10.76"
version = "0.10.77"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "951c002c75e16ea2c65b8c7e4d3d51d5530d8dfa7d060b4776828c88cfb18ecf"
checksum = "bfe4646e360ec77dff7dde40ed3d6c5fee52d156ef4a62f53973d38294dad87f"
dependencies = [
"bitflags 2.11.0",
"cfg-if",
@ -2481,9 +2486,9 @@ checksum = "7c87def4c32ab89d880effc9e097653c8da5d6ef28e6b539d313baaacfbafcbe"
[[package]]
name = "openssl-sys"
version = "0.9.112"
version = "0.9.113"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "57d55af3b3e226502be1526dfdba67ab0e9c96fc293004e79576b2b9edb0dbdb"
checksum = "ad2f2c0eba47118757e4c6d2bff2838f3e0523380021356e7875e858372ce644"
dependencies = [
"cc",
"libc",
@ -2678,9 +2683,9 @@ checksum = "a89322df9ebe1c1578d689c92318e070967d1042b512afbe49518723f4e6d5cd"
[[package]]
name = "pkg-config"
version = "0.3.32"
version = "0.3.33"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7edddbd0b52d732b21ad9a5fab5c704c14cd949e5e9a1ec5929a24fded1b904c"
checksum = "19f132c84eca552bf34cab8ec81f1c1dcc229b811638f9d283dceabe58c5569e"
[[package]]
name = "pnet"
@ -2870,7 +2875,7 @@ dependencies = [
"bytes",
"getrandom 0.3.4",
"lru-slab",
"rand 0.9.2",
"rand 0.9.4",
"ring",
"rustc-hash",
"rustls",
@ -2930,9 +2935,9 @@ dependencies = [
[[package]]
name = "rand"
version = "0.9.2"
version = "0.9.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6db2770f06117d490610c7488547d543617b21bfa07796d7a12f6f1bd53850d1"
checksum = "44c5af06bb1b7d3216d91932aed5265164bf384dc89cd6ba05cf59a35f5f76ea"
dependencies = [
"rand_chacha 0.9.0",
"rand_core 0.9.5",
@ -3240,9 +3245,9 @@ dependencies = [
[[package]]
name = "rustls"
version = "0.23.37"
version = "0.23.38"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "758025cb5fccfd3bc2fd74708fd4682be41d99e5dff73c377c0646c6012c73a4"
checksum = "69f9466fb2c14ea04357e91413efb882e2a6d4a406e625449bc0a5d360d53a21"
dependencies = [
"aws-lc-rs",
"log",
@ -3314,9 +3319,9 @@ checksum = "f87165f0995f63a9fbeea62b64d10b4d9d8e78ec6d7d51fb2125fda7bb36788f"
[[package]]
name = "rustls-webpki"
version = "0.103.10"
version = "0.103.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "df33b2b81ac578cabaf06b89b0631153a3f416b0a886e8a7a1707fb51abbd1ef"
checksum = "20a6af516fea4b20eccceaf166e8aa666ac996208e8a644ce3ef5aa783bc7cd4"
dependencies = [
"aws-lc-rs",
"ring",
@ -3892,9 +3897,9 @@ checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20"
[[package]]
name = "tokio"
version = "1.51.0"
version = "1.51.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2bd1c4c0fc4a7ab90fc15ef6daaa3ec3b893f004f915f2392557ed23237820cd"
checksum = "f66bf9585cda4b724d3e78ab34b73fb2bbaba9011b9bfdf69dc836382ea13b8c"
dependencies = [
"bytes",
"libc",
@ -4051,11 +4056,12 @@ checksum = "e421abadd41a4225275504ea4d6566923418b7f05506fbc9c0fe86ba7396114b"
[[package]]
name = "ttp-core"
version = "0.1.0"
source = "git+https://git.methanium.net/Tensamin/TTP.git#2322bdced8183e970405d5017cd87ba805309a4c"
source = "git+https://git.methanium.net/Tensamin/TTP.git#ac3c0e1cf9e1957b554f0b19f0b0d7cf9cca46c1"
dependencies = [
"base64",
"byteorder",
"rand 0.8.5",
"serde_json",
"strum 0.28.0",
"strum_macros 0.28.0",
]
@ -4063,7 +4069,7 @@ dependencies = [
[[package]]
name = "ttp-native"
version = "0.1.0"
source = "git+https://git.methanium.net/Tensamin/TTP.git#2322bdced8183e970405d5017cd87ba805309a4c"
source = "git+https://git.methanium.net/Tensamin/TTP.git#ac3c0e1cf9e1957b554f0b19f0b0d7cf9cca46c1"
dependencies = [
"quinn",
"rustls",
@ -4086,7 +4092,7 @@ dependencies = [
"httparse",
"log",
"native-tls",
"rand 0.9.2",
"rand 0.9.4",
"sha1",
"thiserror 2.0.18",
]
@ -4295,9 +4301,9 @@ dependencies = [
[[package]]
name = "wasm-bindgen"
version = "0.2.117"
version = "0.2.118"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0551fc1bb415591e3372d0bc4780db7e587d84e2a7e79da121051c5c4b89d0b0"
checksum = "0bf938a0bacb0469e83c1e148908bd7d5a6010354cf4fb73279b7447422e3a89"
dependencies = [
"cfg-if",
"once_cell",
@ -4308,9 +4314,9 @@ dependencies = [
[[package]]
name = "wasm-bindgen-futures"
version = "0.4.67"
version = "0.4.68"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "03623de6905b7206edd0a75f69f747f134b7f0a2323392d664448bf2d3c5d87e"
checksum = "f371d383f2fb139252e0bfac3b81b265689bf45b6874af544ffa4c975ac1ebf8"
dependencies = [
"js-sys",
"wasm-bindgen",
@ -4318,9 +4324,9 @@ dependencies = [
[[package]]
name = "wasm-bindgen-macro"
version = "0.2.117"
version = "0.2.118"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7fbdf9a35adf44786aecd5ff89b4563a90325f9da0923236f6104e603c7e86be"
checksum = "eeff24f84126c0ec2db7a449f0c2ec963c6a49efe0698c4242929da037ca28ed"
dependencies = [
"quote",
"wasm-bindgen-macro-support",
@ -4328,9 +4334,9 @@ dependencies = [
[[package]]
name = "wasm-bindgen-macro-support"
version = "0.2.117"
version = "0.2.118"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dca9693ef2bab6d4e6707234500350d8dad079eb508dca05530c85dc3a529ff2"
checksum = "9d08065faf983b2b80a79fd87d8254c409281cf7de75fc4b773019824196c904"
dependencies = [
"bumpalo",
"proc-macro2",
@ -4341,9 +4347,9 @@ dependencies = [
[[package]]
name = "wasm-bindgen-shared"
version = "0.2.117"
version = "0.2.118"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "39129a682a6d2d841b6c429d0c51e5cb0ed1a03829d8b3d1e69a011e62cb3d3b"
checksum = "5fd04d9e306f1907bd13c6361b5c6bfc7b3b3c095ed3f8a9246390f8dbdee129"
dependencies = [
"unicode-ident",
]
@ -4392,9 +4398,9 @@ dependencies = [
[[package]]
name = "web-sys"
version = "0.3.94"
version = "0.3.95"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cd70027e39b12f0849461e08ffc50b9cd7688d942c1c8e3c7b22273236b4dd0a"
checksum = "4f2dfbb17949fa2088e5d39408c48368947b86f7834484e87b73de55bc14d97d"
dependencies = [
"js-sys",
"wasm-bindgen",

View file

@ -42,6 +42,14 @@ pub async fn load_from_tu(username: &str) -> Result<(), ()> {
pub fn add_user(user: UserProfile) {
USERS.lock().unwrap().push(user);
}
pub fn update_user(user: UserProfile) {
let mut users = USERS.lock().unwrap();
if let Some(pos) = users.iter().position(|u| u.user_id == user.user_id) {
users[pos] = user;
}
*UNIQUE.lock().unwrap() = true;
}
pub fn get_user_by_username(username: &str) -> Option<UserProfile> {
USERS
.lock()
@ -111,3 +119,15 @@ pub async fn load_users() -> io::Result<()> {
pub fn set_unique(val: bool) {
*UNIQUE.lock().unwrap() = val;
}
pub fn save_app_data(user_id: i64, app_identifier: &str, data: &str) {
let path = format!("users/{}/apps", user_id);
let name = format!("{}.json", app_identifier);
save_file(&path, &name, data);
}
pub fn load_app_data(user_id: i64, app_identifier: &str) -> String {
let path = format!("users/{}/apps", user_id);
let name = format!("{}.json", app_identifier);
load_file(&path, &name)
}

View file

@ -16,6 +16,7 @@ pub struct UserProfile {
pub reset_token: String,
pub created_at: i64,
pub display_name: Option<String>,
pub trusted_apps: std::collections::HashMap<String, String>,
}
impl UserProfile {
@ -38,17 +39,24 @@ impl UserProfile {
.unwrap()
.as_millis() as i64,
reset_token,
trusted_apps: std::collections::HashMap::new(),
}
}
pub fn to_json(&self) -> JsonValue {
let mut trusted_apps_obj = json::JsonValue::new_object();
for (k, v) in &self.trusted_apps {
trusted_apps_obj[k] = v.clone().into();
}
let mut obj = object! {
"uuid" => self.user_id,
"username" => self.username.clone(),
"public_key" => self.public_key.clone(),
"private_key_hash" => self.private_key_hash.clone(),
"created_at" => self.created_at,
"reset_token" => self.reset_token.clone()
"reset_token" => self.reset_token.clone(),
"trusted_apps" => trusted_apps_obj,
};
if let Some(d) = &self.display_name {
obj["display_name"] = d.clone().into();
@ -82,6 +90,15 @@ impl UserProfile {
let created_at = j["created_at"].as_i64()?;
let display_name = j["display_name"].as_str().map(|s| s.to_string());
let mut trusted_apps = std::collections::HashMap::new();
if j["trusted_apps"].is_object() {
for (key, value) in j["trusted_apps"].entries() {
if let Some(s) = value.as_str() {
trusted_apps.insert(key.to_string(), s.to_string());
}
}
}
let up = UserProfile {
user_id,
username,
@ -90,6 +107,7 @@ impl UserProfile {
private_key_hash,
created_at,
reset_token,
trusted_apps,
};
// TODO: Migrate to Omikron / Wss

View file

@ -91,6 +91,8 @@ pub struct OmikronConnection {
pub connection_id: Uuid,
shutdown_tx: Arc<Mutex<Option<watch::Sender<bool>>>>,
reconnect_on_close: Arc<RwLock<bool>>,
pub app_challenges: Arc<RwLock<HashMap<u64, String>>>,
pub app_sessions: Arc<RwLock<HashMap<u64, (i64, String)>>>,
}
impl OmikronConnection {
@ -113,6 +115,8 @@ impl OmikronConnection {
connection_id: Uuid::new_v4(),
shutdown_tx: Arc::new(Mutex::new(Some(shutdown_tx))),
reconnect_on_close: Arc::new(RwLock::new(true)),
app_challenges: Arc::new(RwLock::new(HashMap::new())),
app_sessions: Arc::new(RwLock::new(HashMap::new())),
}
}
@ -424,6 +428,193 @@ impl OmikronConnection {
return;
}
if cv.is_type(CommunicationType::app_identification) {
let sender_id = cv.get_sender();
let app_identifier = cv
.get_data(DataTypes::app_identifier)
.as_str()
.unwrap_or("")
.to_string();
let app_public_key = cv
.get_data(DataTypes::app_public_key)
.as_str()
.unwrap_or("")
.to_string();
let user_id = cv.get_data(DataTypes::user_id).as_number().unwrap_or(0) as i64;
let mut trusted = false;
if let Some(user) = iota_storage::users::user_manager::get_user(user_id) {
if let Some(pub_k) = user.trusted_apps.get(&app_identifier) {
if pub_k == &app_public_key {
trusted = true;
}
}
}
if trusted {
use iota_util::crypto_util::{DataFormat, SecurePayload};
let challenge = Uuid::new_v4().to_string();
self.app_challenges
.write()
.await
.insert(sender_id, challenge.clone());
self.app_sessions
.write()
.await
.insert(sender_id, (user_id, app_identifier.clone()));
if let Some(pub_key) = iota_util::crypto_helper::load_public_key(&app_public_key) {
let conf = CONFIG.read().await;
let priv_k_str = conf.get_private_key().unwrap_or_default();
let pub_k_str = conf.get_public_key().unwrap_or_default();
drop(conf);
if let Some(priv_key) = iota_util::crypto_helper::load_secret_key(&priv_k_str) {
let encrypted_challenge =
SecurePayload::new(challenge.as_bytes(), DataFormat::Raw, priv_key)
.unwrap()
.encrypt_x448(pub_key)
.unwrap()
.export(DataFormat::Base64);
let res = CommunicationValue::new(CommunicationType::app_challange)
.with_id(cv.get_id())
.with_receiver(sender_id)
.add_data(DataTypes::public_key, DataValue::Str(pub_k_str))
.add_data(DataTypes::challenge, DataValue::Str(encrypted_challenge));
self.send_message(&res).await;
return;
}
}
}
let res = CommunicationValue::new(CommunicationType::error)
.with_id(cv.get_id())
.with_receiver(sender_id);
self.send_message(&res).await;
return;
}
if cv.is_type(CommunicationType::app_challange_response) {
let sender_id = cv.get_sender();
let mut challenges = self.app_challenges.write().await;
if let Some(expected) = challenges.remove(&sender_id) {
if let DataValue::Str(response) = cv.get_data(DataTypes::challenge) {
if expected == *response {
let res =
CommunicationValue::new(CommunicationType::app_identification_response)
.with_id(cv.get_id())
.with_receiver(sender_id);
self.send_message(&res).await;
return;
}
}
}
let res = CommunicationValue::new(CommunicationType::error)
.with_id(cv.get_id())
.with_receiver(sender_id);
self.send_message(&res).await;
return;
}
if cv.is_type(CommunicationType::save_app_data) {
let sender_id = cv.get_sender();
let app_data = cv
.get_data(DataTypes::app_data)
.as_str()
.unwrap_or("")
.to_string();
let sessions = self.app_sessions.read().await;
if let Some((user_id, app_identifier)) = sessions.get(&sender_id) {
iota_storage::users::user_manager::save_app_data(
*user_id,
app_identifier,
&app_data,
);
}
let res = CommunicationValue::new(CommunicationType::save_app_data)
.with_id(cv.get_id())
.with_receiver(sender_id);
self.send_message(&res).await;
return;
}
if cv.is_type(CommunicationType::load_app_data) {
let sender_id = cv.get_sender();
let mut app_data = String::new();
let sessions = self.app_sessions.read().await;
if let Some((user_id, app_identifier)) = sessions.get(&sender_id) {
app_data =
iota_storage::users::user_manager::load_app_data(*user_id, app_identifier);
}
let res = CommunicationValue::new(CommunicationType::load_app_data)
.with_id(cv.get_id())
.with_receiver(sender_id)
.add_data(DataTypes::app_data, DataValue::Str(app_data));
self.send_message(&res).await;
return;
}
if cv.is_type(CommunicationType::create_app) {
let sender_id = cv.get_sender() as i64;
let app_identifier = cv
.get_data(DataTypes::app_identifier)
.as_str()
.unwrap_or("")
.to_string();
let app_public_key = cv
.get_data(DataTypes::app_public_key)
.as_str()
.unwrap_or("")
.to_string();
if !app_identifier.is_empty() && !app_public_key.is_empty() {
if let Some(mut user) = iota_storage::users::user_manager::get_user(sender_id) {
if !user.trusted_apps.contains_key(&app_identifier) {
user.trusted_apps.insert(app_identifier, app_public_key);
iota_storage::users::user_manager::update_user(user);
}
}
}
let res = CommunicationValue::new(CommunicationType::create_app)
.with_id(cv.get_id())
.with_receiver(sender_id as u64);
self.send_message(&res).await;
return;
}
if cv.is_type(CommunicationType::delete_app) {
let sender_id = cv.get_sender() as i64;
let app_identifier = cv
.get_data(DataTypes::app_identifier)
.as_str()
.unwrap_or("")
.to_string();
if !app_identifier.is_empty() {
if let Some(mut user) = iota_storage::users::user_manager::get_user(sender_id) {
if user.trusted_apps.contains_key(&app_identifier) {
user.trusted_apps.remove(&app_identifier);
iota_storage::users::user_manager::update_user(user);
}
}
}
let res = CommunicationValue::new(CommunicationType::delete_app)
.with_id(cv.get_id())
.with_receiver(sender_id as u64);
self.send_message(&res).await;
return;
}
if cv.is_type(CommunicationType::client_connected) {
let user_id = cv.get_data(DataTypes::user_id).as_number().unwrap_or(0) as i64;
let session_id = cv.get_data(DataTypes::session_id).as_number().unwrap_or(0) as i64;