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

Commit

Permalink
Merge branch 'raisfeld-ori:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
arielldev authored Mar 19, 2024
2 parents b964934 + 6302913 commit d7490e2
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 44 deletions.
12 changes: 6 additions & 6 deletions src-tauri/src/data/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<u8> = Vec::new();
let result = auth_data.read_to_end(&mut buffer);
Expand Down Expand Up @@ -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<u8>) -> 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());
Expand Down Expand Up @@ -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());
Expand Down
36 changes: 1 addition & 35 deletions src-tauri/src/fs/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<dyn Error>>{
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 {
Expand Down Expand Up @@ -120,6 +87,5 @@ impl File{

#[test]
fn test_fs(){
let err = save_fs(String::from("test"), String::from("test"));
println!("{:?}", err);

}
2 changes: 1 addition & 1 deletion src-tauri/src/fs/encryption.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src-tauri/src/main.rs
Original file line number Diff line number Diff line change
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, 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");
}
3 changes: 2 additions & 1 deletion src/pages/login/signup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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}});
}
}
Expand Down

0 comments on commit d7490e2

Please sign in to comment.