Skip to content
This repository has been archived by the owner on Jan 14, 2025. It is now read-only.

Commit

Permalink
I'm a man of few words and even fewer opinions.
Browse files Browse the repository at this point in the history
  • Loading branch information
raisfeld-ori committed Mar 18, 2024
1 parent 29d4c9a commit a4d0d81
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 52 deletions.
102 changes: 57 additions & 45 deletions src-tauri/src/data/auth.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
use crate::dir;
use crate::fs::encryption::{aes_encrypt, aes_decrypt, aes_try_decrypt};
use crate::data::json;
use crate::fs::commands::{FS, Home};
use std::error::Error;
use std::fs::{create_dir, read_dir, read_to_string, File, OpenOptions};
use crate::fs::commands::FS;
use std::fs::{create_dir, metadata, read_dir, read, set_permissions, File};
use std::io::{Read, Write};
#[cfg(target_os = "windows")]
use std::os::windows::ffi::OsStrExt;
#[cfg(target_os = "linux")]
use std::os::unix::ffi::OsStrExt;
use crate::user_dir;
use crate::get_user_dir;
use crate::data::json::init_user_data;
use base64::{decode, encode};
use serde::Serialize;
use std::ffi::OsStr;
use crate::get_user_dir;

use serde_json::{Map, Value};
use super::json::set_data;
pub fn init_dir() -> Result<(), std::io::Error>{if dir().exists() {Ok(())}else{create_dir(dir())}}

#[tauri::command]
Expand Down Expand Up @@ -56,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(){
"auth" => {
"authenti" => {
let mut auth_data = File::open(entry_path).unwrap();
let mut buffer: Vec<u8> = Vec::new();
let result = auth_data.read_to_end(&mut buffer);
Expand All @@ -69,17 +68,9 @@ pub fn authenticate_user(name: &str, password: &str) -> bool{
return false;
}

#[tauri::command]
pub fn user_exists(name: &str, password: &str) -> bool {
let location = user_dir(name, password);
if location.exists(){true}
else{false}
}

#[tauri::command]
pub fn load_user(name: &str, password: &str) -> Result<(), String>{
let location = get_user_dir(name, password);
println!("{:?}", location);
if !location.exists() {
return Err(String::from("user directory was not initialized"));
}
Expand All @@ -89,43 +80,49 @@ pub fn load_user(name: &str, password: &str) -> Result<(), String>{
let entry = decode(entry.file_name().to_bytes()).unwrap();
let entry = aes_decrypt(name, password, &entry);
let entry = String::from_utf8(entry).unwrap();
let mut system_data = Map::new();
let mut user_data = Map::new();
match entry.as_str(){
"user data" => {
let file_content = read_to_string(&path).unwrap();
let content = decode(file_content).unwrap();
let original_content = aes_decrypt(name, password, &content);

"user_json" => {
let file_content = read(&path).unwrap();
let original = aes_decrypt(name, password, &file_content);
let original_user_data: Map<String, Value> = serde_json::from_slice(&original).unwrap();
user_data = original_user_data;
},
"system data" => {
let file_content = read_to_string(&path).unwrap();
let content = decode(file_content).unwrap();
let original_content = aes_decrypt(name, password, &content);
"sys_json" => {
let file_content = read(&path).unwrap();
let original = aes_decrypt(name, password, &file_content);
let original_system_data: Map<String, Value> = serde_json::from_slice(&original).unwrap();
system_data = original_system_data;
},
_ => {}

}
set_data(user_data, system_data);
}
return Ok(());
}

let encrypted_content = read_to_string(&path).expect("failed to read file content");
let decrypted_content = aes_decrypt(name, password, &decode(encrypted_content).expect("failed to decode encrypted file content"));
fn save_data(username: &str, password: &str, data_name: String, data: Vec<u8>) -> Result<(), String>{
let location = encode(aes_encrypt(username, password, &data_name.into_bytes())).replace('/', "_");
let location = get_user_dir(username, password).join(location);
if location.exists(){
let file = File::open(location.as_path());
if file.is_err() {return Err(String::from("failed to create the file"));}
let file = file.unwrap().write_all(&data);
if file.is_err() {return Err(String::from("failed to write into the file"))}
}
else{
let file = File::create(location.as_path());
if file.is_err() {return Err(String::from("failed to create the file"));}
let file = file.unwrap().write_all(&data);
if file.is_err() {return Err(String::from("failed to write into the file"))}
}
return Ok(());
}

#[tauri::command]
pub fn save_user(username: &str, password: &str) -> Result<(), String> {
fn save_data(username: &str, password: &str, data_name: String, data: Vec<u8>) -> Result<(), String>{
let location = encode(aes_encrypt(username, password, &data_name.into_bytes())).replace('/', "_");
let location = get_user_dir(username, password).join(location);
if location.exists(){
let err = File::open(location.as_path()).unwrap().write_all(&data);
if err.is_err(){return Err(String::from("failed to write into the file"));}
}
else{
let err = File::create(location.as_path()).unwrap().write_all(&data);
if err.is_err() {return Err(String::from("failed to write into the file"));}
}
return Ok(());
}
let (user_json, sys_json) = json::data_bytes();
let user_json = aes_encrypt(username, password, &user_json);
let err = save_data(username, password, String::from("user_json"), user_json);
Expand All @@ -141,15 +138,30 @@ pub fn save_user(username: &str, password: &str) -> Result<(), String> {
return Ok(());
}

#[tauri::command]
pub fn create_user(name: &str, password: &str) -> Result<(), String> {
let location = get_user_dir(name, password);
if location.exists(){return Err(String::from("user already exists"));}
else {
let err = create_dir(location.as_path());
if err.is_err() {return Err(err.unwrap_err().to_string());}
}
let mut permissions = metadata(&location).unwrap().permissions();
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());
}

#[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 err = save_user(name, password);
println!("test: {}", err.unwrap_err());
//if err.is_err() {panic!("save user failed");}
authenticate_user(name, password);
let err = load_user(name, password);
if user_exists(name, password) {}
else {assert!(create_user(name, password).is_ok());}
assert!(save_user(name, password).is_ok());
assert!(authenticate_user(name, password));
assert!(load_user(name, password).is_ok());
}
21 changes: 17 additions & 4 deletions src-tauri/src/data/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,17 @@ pub fn init_user_data() {
})}
}

fn user<'a>() -> &'a Map<String, Value>{unsafe {USER_DATA.get("user").expect("USER_DATA WAS NOT INITIALIZED")}.as_object().expect("user was not an object")}
fn system<'a>() -> &'a Map<String, Value>{unsafe {USER_DATA.get("system").expect("USER_DATA WAS NOT INITIALIZED")}.as_object().expect("user was not an object")}
fn user_mut<'a>() -> &'a mut Map<String, Value> {unsafe {USER_DATA.get_mut("user").expect("USER_DATA WAS NOT INITIALIZED")}.as_object_mut().expect("user was not an object")}
fn system_mut<'a>() -> &'a mut Map<String, Value>{unsafe {USER_DATA.get_mut("system").expect("USER_DATA WAS NOT INITIALIZED")}.as_object_mut().expect("user was not an object")}
pub fn set_data(user: Map<String, Value>, system: Map<String, Value>){
unsafe {
USER_DATA.insert(String::from("user"), Value::Object(user));
USER_DATA.insert(String::from("system"), Value::Object(system));
}
}

pub fn user<'a>() -> &'a Map<String, Value>{unsafe {USER_DATA.get("user").expect("USER_DATA WAS NOT INITIALIZED")}.as_object().expect("user was not an object")}
pub fn system<'a>() -> &'a Map<String, Value>{unsafe {USER_DATA.get("system").expect("USER_DATA WAS NOT INITIALIZED")}.as_object().expect("user was not an object")}
pub fn user_mut<'a>() -> &'a mut Map<String, Value> {unsafe {USER_DATA.get_mut("user").expect("USER_DATA WAS NOT INITIALIZED")}.as_object_mut().expect("user was not an object")}
pub fn system_mut<'a>() -> &'a mut Map<String, Value>{unsafe {USER_DATA.get_mut("system").expect("USER_DATA WAS NOT INITIALIZED")}.as_object_mut().expect("user was not an object")}


#[tauri::command]
Expand Down Expand Up @@ -50,6 +57,12 @@ pub fn user_make(key: String, data: Value) {
result.insert(key, data);
}

#[tauri::command]
pub fn system_make(key: String, data: Value) {
let result = user_mut();
result.insert(key, data);
}

#[derive(Serialize)]
struct SerializeUserData{
user: Map<String, Value>,
Expand Down
6 changes: 3 additions & 3 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::data::json::{init_user_data, user_get};
use dirs::data_dir;
use base64::{decode, encode};
use crate::fs::encryption::aes_encrypt;
use crate::data::auth::{init_dir, save_user, authenticate_user, load_user, user_exists};
use crate::data::auth::{init_dir, save_user, authenticate_user, load_user, user_exists, create_user};
use crate::data::auth::Encodable;
use crate::fs::commands::{pwd, ls, FS, cd};

Expand Down Expand Up @@ -38,7 +38,7 @@ pub fn open_file(name: &str, password: &str, target: String) -> Option<PathBuf>{
fn test_general(){
let name = "test";
let password = "test";
user_dir(name, password);
get_user_dir(name, password);
open_file(name, password, String::from("something"));
}
#[allow(unused)]
Expand Down Expand Up @@ -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, load_user, ls, pwd, cd
first_init, list_commands, console, user_get, authenticate_user, save_user, user_exists, create_user, load_user, ls, pwd, cd
]).run(tauri::generate_context!()).expect("failed to run the code");
}

0 comments on commit a4d0d81

Please sign in to comment.