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

Commit

Permalink
Browse files Browse the repository at this point in the history
raisfeld-ori committed Mar 17, 2024
2 parents 6dc0dcc + a4b10df commit 6e501d1
Showing 3 changed files with 75 additions and 28 deletions.
76 changes: 52 additions & 24 deletions src-tauri/src/data/auth.rs
Original file line number Diff line number Diff line change
@@ -3,7 +3,10 @@ use crate::fs::encryption::{aes_encrypt, aes_decrypt, aes_try_decrypt};
use crate::data::json::data_bytes;
use std::fs::{create_dir, read_dir, read_to_string, File, OpenOptions};
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 std::path::{Path, PathBuf};
use crate::init_user_data;
use base64::{decode, encode};
@@ -12,8 +15,8 @@ use tauri::{Manager, Pixel, Runtime};
use std::ffi::OsStr;

pub fn init_dir() -> Result<(), std::io::Error>{if dir().exists() {Ok(())}else{create_dir(dir())}}
#[tauri::command]
pub fn update<R: Runtime>(app: tauri::AppHandle<R>) {app.trigger_global("rust_event", None)}
trait Encodable{fn to_bytes(&self) -> Vec<u8>;}
#[cfg(target_os = "windows")]
pub trait Encodable{fn to_bytes(&self) -> Vec<u8>;}
impl Encodable for OsStr{
fn to_bytes(&self) -> Vec<u8> {
@@ -28,6 +31,13 @@ impl Encodable for OsStr{
bytes
}
}
#[cfg(target_os = "linux")]
impl Encodable for OsStr {
fn to_bytes(&self) -> Vec<u8> {
self.as_encoded_bytes().to_vec()
}
}


#[tauri::command]
pub fn authenticate_user(name: &str, password: &str) -> bool{
@@ -62,6 +72,7 @@ pub fn authenticate_user(name: &str, password: &str) -> bool{

#[tauri::command]
pub fn load_user(name: &str, password: &str){
let location = encode(aes_encrypt(name, password, name.as_bytes()));
let location: &[u8] = &aes_encrypt(name, password, name.as_bytes());
let location = dir().join(format!("{:?}", location));

@@ -72,18 +83,33 @@ pub fn load_user(name: &str, password: &str){
for entry in read_dir(location).expect("failed to read directory"){
let entry = entry.expect("failed to read file");
let path = entry.path();
let entry = decode(entry.file_name().to_bytes()).unwrap();
println!("ni: {:?}", path);
let entry = aes_decrypt(name, password, &entry);
let entry = String::from_utf8(entry).unwrap();
println!("file name: {}", entry);
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);

},
"system data" => {
let file_content = read_to_string(&path).unwrap();
let content = decode(file_content).unwrap();
let original_content = aes_decrypt(name, password, &content);
},
_ => {}

}

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"));
}

}

#[tauri::command]
pub fn user_exists(name: &str, password: &str) -> bool {
let location: &[u8] = &aes_encrypt(name, password, name.as_bytes());
let location = dir().join(format!("{:?}", location));
if location.exists(){true}
else{false}
}

#[tauri::command]
pub fn save_user(name: &str, password: &str) {
let location = encode(aes_encrypt(name, password, name.as_bytes())).replace('/', "_");
@@ -95,40 +121,41 @@ pub fn save_user(name: &str, password: &str) {
let encrypted_user_data = aes_encrypt(name, password, data_0);
let encrypted_user_name = format!("{:?}", aes_encrypt(name, password, b"user data"));
let encrypted_system_data = aes_encrypt(name, password, data_1);
let encrypted_user_data_base64 = base64::encode(&encrypted_user_data);
let encrypted_system_data_base64 = base64::encode(&encrypted_system_data);
let encrypted_system_name = format!("{:?}", aes_encrypt(name, password, b"system data"));
println!("{:?}", location);
if !location.exists(){create_dir(&location).expect("could not create a directory");}
if location.join(&encrypted_user_name).exists(){
let encrypted_user_name_base64 = encode(&encrypted_user_name);
let encrypted_system_name_base64 = encode(&encrypted_system_name);
println!("us: {}",encrypted_user_name);
if location.join(&encrypted_user_name_base64).exists(){
OpenOptions::new()
.write(true)
.append(true)
.open(location.join(encrypted_user_name))
.open(location.join(encrypted_user_name_base64))
.expect("failed to open an existing file")
.write_all(&encrypted_user_data_base64.as_bytes())
.write_all(&encrypted_user_data)
.expect("failed to write data");
}
else{
File::create(location.join(encrypted_user_name))
File::create(location.join(encrypted_user_name_base64))
.expect("failed to create a file")
.write_all(&encrypted_user_data_base64.as_bytes())
.write_all(&encrypted_user_data)
.expect("failed to write data into file");

}
if location.join(&encrypted_system_name).exists(){
if location.join(&encrypted_system_name_base64).exists(){
OpenOptions::new()
.write(true)
.append(true)
.open(location.join(encrypted_system_name))
.open(location.join(encrypted_system_name_base64))
.expect("failed to open an existing file")
.write_all(&encrypted_system_data_base64.as_bytes())
.write_all(&encrypted_system_data)
.expect("failed to write data");
}
else{
File::create(location.join(encrypted_system_name))
File::create(location.join(encrypted_system_name_base64))
.expect("failed to create a file")
.write_all(&encrypted_system_data_base64.as_bytes())
.write_all(&encrypted_system_data)
.expect("failed to write data into file");

}
@@ -139,8 +166,9 @@ pub fn save_user(name: &str, password: &str) {
fn test_authentication(){
init_user_data();
init_dir().expect("failed to create the main directory");
let name = "non existed user";
let password = "non existed user";
let name = "non oe user";
let password = "non oe user";
save_user(name, password);
authenticate_user(name, password);
load_user(name, password);
}
23 changes: 21 additions & 2 deletions src-tauri/src/fs/commands.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use std::{fs::{self, read}, io::Write, path::PathBuf};
use std::error::Error;
use std::{fs::{self, read}, io::{Read, Write}, path::PathBuf};
use crate::dir;
use serde::{Deserialize, Serialize};
use serde_json::to_vec;
use base64::{self, decode, encode};
use std::error::Error;
use tauri::api::file;

use crate::fs::encryption::{aes_decrypt, xor_encrypt};

@@ -24,6 +25,24 @@ pub fn save_fs(name: String, password: String) -> Result<(), Box<dyn Error>>{
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 {
4 changes: 2 additions & 2 deletions src-tauri/src/fs/encryption.rs
Original file line number Diff line number Diff line change
@@ -23,7 +23,7 @@ pub fn aes_encrypt(username: &str, password: &str, data: &[u8]) -> Vec<u8> {
Ok(BufferResult::BufferOverflow) => {
final_result.extend(write_buffer.take_read_buffer().take_remaining());
}
Err(e) => panic!("{:?}", e),
Err(e) => println!("{:?}", e),
}
}
final_result.extend(write_buffer.take_read_buffer().take_remaining());
@@ -54,7 +54,7 @@ pub fn aes_decrypt(username: &str, password: &str, data: &[u8]) -> Vec<u8> {
Ok(BufferResult::BufferOverflow) => {
final_result.extend(write_buffer.take_read_buffer().take_remaining());
}
Err(e) => panic!("{:?}", e),
Err(e) => println!("{:?}", e),
}
}

0 comments on commit 6e501d1

Please sign in to comment.