diff --git a/src-tauri/src/data/auth.rs b/src-tauri/src/data/auth.rs index 67641ac..b4ce77e 100644 --- a/src-tauri/src/data/auth.rs +++ b/src-tauri/src/data/auth.rs @@ -10,7 +10,7 @@ use std::os::windows::ffi::OsStrExt; use std::os::unix::ffi::OsStrExt; use crate::get_user_dir; use crate::data::json::init_user_data; -use base64::{decode, encode}; +use base64::{decode, encode, URL_SAFE_NO_PAD}; use std::ffi::OsStr; use serde_json::{Map, Value}; use super::json::set_data; @@ -55,7 +55,7 @@ pub fn authenticate_user(name: &str, password: &str) -> bool{ let entry = String::from_utf8(entry); if entry.is_err() {continue;} match entry.unwrap().as_str(){ - "authenti" => { + "auth" => { let mut auth_data = File::open(entry_path).unwrap(); let mut buffer: Vec = Vec::new(); let result = auth_data.read_to_end(&mut buffer); @@ -104,7 +104,7 @@ pub fn load_user(name: &str, password: &str) -> Result<(), String>{ } fn save_data(username: &str, password: &str, data_name: String, data: Vec) -> Result<(), String>{ - let location = encode(aes_encrypt(username, password, &data_name.into_bytes())).replace('/', "_"); + let location = encode(aes_encrypt(username, password, &data_name.into_bytes())); let location = get_user_dir(username, password).join(location); if location.exists(){ let file = File::open(location.as_path()); @@ -150,15 +150,15 @@ pub fn create_user(name: &str, password: &str) -> Result<(), String> { permissions.set_readonly(false); let err = set_permissions(location, permissions); if err.is_err() {return Err(err.unwrap_err().to_string());} - return save_data(name, password, String::from("authenti"), b"arg arg mbc mbc".to_vec()); + return save_data(name, password, String::from("auth"), b"arg arg mbc mbc".to_vec()); } #[test] fn test_authentication(){ init_user_data(); init_dir().expect("failed to create the main directory"); - let name = "non oe user"; - let password = "non oe user"; + let name = "a"; + let password = "a"; if user_exists(name, password) {} else {assert!(create_user(name, password).is_ok());} assert!(save_user(name, password).is_ok()); diff --git a/src-tauri/src/fs/commands.rs b/src-tauri/src/fs/commands.rs index 3b61148..63ec23d 100644 --- a/src-tauri/src/fs/commands.rs +++ b/src-tauri/src/fs/commands.rs @@ -10,39 +10,6 @@ use crate::fs::encryption::{aes_decrypt, xor_encrypt}; pub static mut FS: Home = Home::new(); -pub fn save_fs(name: String, password: String) -> Result<(), Box>{ - let dir = dir(); - let fs_bytes = unsafe{to_vec(&FS)?}; - let dir = dir.join(encode(xor_encrypt(b"encrypt".to_vec(), &fs_bytes))); - if dir.exists(){ - let mut writer = fs::File::open(dir.as_path()).unwrap(); - writer.write_all(&fs_bytes)?; - } - else { - let mut writer = fs::File::create(dir.as_path()).unwrap(); - writer.write_all(&fs_bytes)?; - } - println!("{:?}", dir.as_path()); - Ok(()) -} -pub fn load_fs(name: String, password: String) -> Result<(), String>{ - let dir = dir(); - let fs_bytes = unsafe{to_vec(&FS)}; - if fs_bytes.is_err(){return Err(format!("{}", fs_bytes.unwrap_err()));} - let fs_bytes = fs_bytes.unwrap(); - let file_path = dir.join(encode(xor_encrypt(b"encrypt".to_vec(), &fs_bytes))); - let mut file = fs::File::open(&file_path).map_err(|e| format!("failed to open file: {}", e))?; - let mut content = Vec::new(); - file.read_to_end(&mut content).map_err(|e|format!("failed to read file: {}", e))?; - - let decoded = decode(&content).map_err(|e| format!("failed to decode: {}", e))?; - let decrypted = xor_encrypt(decoded, &password.as_bytes()); - - let home: Home = serde_json::from_slice(&decrypted).map_err(|e| format!("failed to deserialize data: {}", e))?; - unsafe{FS = home;} - println!("Loaded file system state from: {:?}", dir.as_path()); - Ok(()) -} #[tauri::command] pub fn pwd() -> String{return unsafe { @@ -120,6 +87,5 @@ impl File{ #[test] fn test_fs(){ - let err = save_fs(String::from("test"), String::from("test")); - println!("{:?}", err); + } \ No newline at end of file diff --git a/src-tauri/src/fs/encryption.rs b/src-tauri/src/fs/encryption.rs index 934fc24..2dad1d8 100644 --- a/src-tauri/src/fs/encryption.rs +++ b/src-tauri/src/fs/encryption.rs @@ -140,7 +140,7 @@ fn test_xor_encryption(){ fn test_aes_encryption(){ let username = "name"; let password = "passwordpasswora"; - let data = b"aaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; + let data = b"auth"; let encrypted: &[u8] = &aes_encrypt(username, password, data); let decrypted = aes_decrypt(username, password, encrypted); assert_eq!(data.to_vec(), decrypted); diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index b98ef7a..7f7b6a6 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -78,6 +78,6 @@ pwd - shows your current path } fn main() { tauri::Builder::default().invoke_handler(tauri::generate_handler![ - first_init, list_commands, console, user_get, authenticate_user, save_user, user_exists, create_user, load_user, ls, pwd, cd + first_init, list_commands, console, user_get, authenticate_user, save_user, user_exists, load_user, ls, pwd, cd ]).run(tauri::generate_context!()).expect("failed to run the code"); } diff --git a/src/pages/login/signup.tsx b/src/pages/login/signup.tsx index 342458d..d43dc7f 100644 --- a/src/pages/login/signup.tsx +++ b/src/pages/login/signup.tsx @@ -11,7 +11,8 @@ function Login() { async function authenticate() { let user_exists = await invoke("user_exists", {name, password}); if (!user_exists) { - await invoke("save_user", {name, password}); + await invoke("create_user", {name, password}) + await invoke("save_user", {username: name, password}); navigate("/loading", {state: {name, password}}); } }