use std::collections::HashMap; use std::sync::{Arc, Mutex}; use actix_web::web; use ovunto_security::{crypto::key::PublicKey, CipherMessage, Error, Result, Salt, TOTPGenerator}; #[derive(Debug, Clone)] pub struct User { pub username: String, pub public_key: PublicKey, pub layers: Vec, pub salt: Salt, pub totp_generator: TOTPGenerator, } #[derive(Debug)] pub struct AppState { pub users: Arc>>, } impl AppState { pub fn new() -> Self { Self { users: Arc::new(Mutex::new(HashMap::new())), } } pub fn web() -> web::Data { web::Data::new(Self::new()) } pub fn add_user( &self, username: String, public_key: PublicKey, salt: Salt, totp_generator: TOTPGenerator, ) -> Result { let mut users = self.users.lock()?; if users.contains_key(&username) { return Err(Error::from_string("Duplicate user")); } let user = User { username: username.clone(), public_key, layers: Vec::new(), salt, totp_generator, }; users.insert(username, user.clone()); Ok(user) } }