From c136b491e4a9a553aee9854a2e06c31bc5d93ca8 Mon Sep 17 00:00:00 2001 From: OBJNULL Date: Fri, 14 Jun 2024 14:32:02 -0400 Subject: [PATCH] Added register prompt --- src/auth.rs | 15 ++++++++++++++- src/interface.rs | 28 +++++++++++++++++++++++++--- src/main.rs | 22 ++++++++++++++++------ src/resource.rs | 14 ++++++++++++-- 4 files changed, 67 insertions(+), 12 deletions(-) diff --git a/src/auth.rs b/src/auth.rs index 78cd46b..b6c5a4e 100644 --- a/src/auth.rs +++ b/src/auth.rs @@ -9,9 +9,12 @@ pub struct Auth { // Implementations impl Auth { + pub fn exists() -> bool { + return resource::ResourceFile::exists("res/auth.dat"); + } pub fn authenticate(username: String, password: String) -> Self { // Read from the Auth file - let file_auth: resource::ResourceFile = resource::ResourceFile::init("res/auth.dat"); + let file_auth: resource::ResourceFile = resource::ResourceFile::read("res/auth.dat"); // Decrypting the content of the file with the password let auth_plaintext: String = secure::Secure::decrypt(file_auth.content, password); @@ -22,4 +25,14 @@ impl Auth { // Returning the result return Auth {verified: auth_status, username: username}; } + pub fn create(username: String, password: String) -> Self { + // Encrypting our username based on our password + let cipher_username: String = secure::Secure::encrypt(username.clone(), password); + + // Saving that to a file + let _: resource::ResourceFile = resource::ResourceFile::write("res/auth.dat", cipher_username); + + // Returning the result + return Auth {verified: true, username: username}; + } } \ No newline at end of file diff --git a/src/interface.rs b/src/interface.rs index 53cd31e..b5e25ab 100644 --- a/src/interface.rs +++ b/src/interface.rs @@ -1,14 +1,13 @@ // Libraries -use std::io; +use std::{io, process::exit}; // Functions -pub fn int_auth() -> (String, String) { +fn get_credentials() -> (String, String) { // Variables let mut username: String = String::new(); let mut password: String = String::new(); // Asking for the username - println!(" - LOGIN - "); println!("Username: "); io::stdin().read_line(&mut username).expect("Failed to read line"); username = String::from(username.to_lowercase().trim()); @@ -19,4 +18,27 @@ pub fn int_auth() -> (String, String) { password = String::from(password.trim()); return (username, password); +} +pub fn int_auth() -> (String, String) { + println!(" - LOGIN - "); + return get_credentials(); +} +pub fn int_reg() -> (String, String) { + // Register form + println!(" - REGISTER - "); + let (username, password) = get_credentials(); + + // Confirming password + let mut cpassword: String = String::new(); + println!("Confirm Password: "); + io::stdin().read_line(&mut cpassword).expect("Failed to read line"); + cpassword = String::from(cpassword.trim()); + + // Valid password? + if password != cpassword { + println!("Passwords do not match!"); + exit(0); + } else { + return (username, password); + } } \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index ef07c1c..92c7aa6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,3 @@ -use interface::int_auth; - // Libraries mod interface; mod resource; @@ -10,11 +8,23 @@ mod auth; // Entry-Point fn main() { - // Asking user for login credentials - let (username, password) = int_auth(); + // Variables + let auth_profile: auth::Auth; - // Verify the user - let auth_profile: auth::Auth = auth::Auth::authenticate(username, password); + // Checking if we need to register or log in + if auth::Auth::exists() { + // Asking user for login credentials + let (username, password) = interface::int_auth(); + + // Verify the user + auth_profile = auth::Auth::authenticate(username, password); + } else { + // Asking user for register credentials + let (username, password) = interface::int_reg(); + + // Creating the user + auth_profile = auth::Auth::create(username, password); + } // DEBUG: Testing if the user is authenticated println!("Authenticated Status: {}", auth_profile.verified); diff --git a/src/resource.rs b/src/resource.rs index a6872e4..abf529f 100644 --- a/src/resource.rs +++ b/src/resource.rs @@ -1,5 +1,5 @@ // Libraries -use std::fs; +use std::{fs, path::Path}; // Structures pub struct ResourceFile { @@ -9,11 +9,21 @@ pub struct ResourceFile { // Implementations impl ResourceFile { - pub fn init(file_path: &str) -> Self { + pub fn exists(file_path: &str) -> bool{ + return Path::new(file_path).exists(); + } + pub fn read(file_path: &str) -> Self { // Reading from a file: let content: String = fs::read_to_string(String::from(file_path)).expect("Failed to read file."); // Returning the value: return ResourceFile {path: String::from(file_path), content}; } + pub fn write(file_path: &str, content: String) -> Self { + // Writing to file + fs::write(file_path, &content).expect("Failed writing to file"); + + // Returning the value: + return ResourceFile {path: String::from(file_path), content: content}; + } } \ No newline at end of file