From a902cdd0aefb17a172a8ffe5d2e71894879e34f2 Mon Sep 17 00:00:00 2001 From: OBJNULL Date: Sat, 15 Jun 2024 14:23:52 -0400 Subject: [PATCH] Added Home Screen & Password Generator --- Cargo.toml | 1 + src/interface.rs | 57 ++++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 52 ++++++++++++++++++++++++++++++++++--------- src/manager.rs | 36 ++++++++++++++++++++++++++++++ 4 files changed, 136 insertions(+), 10 deletions(-) create mode 100644 src/manager.rs diff --git a/Cargo.toml b/Cargo.toml index f37c4c0..8366cee 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,4 +5,5 @@ edition = "2021" [dependencies] base64 = "0.22.1" +rand = "0.8.5" ring = "0.17.8" diff --git a/src/interface.rs b/src/interface.rs index b5e25ab..a13fa63 100644 --- a/src/interface.rs +++ b/src/interface.rs @@ -41,4 +41,61 @@ pub fn int_reg() -> (String, String) { } else { return (username, password); } +} + +pub fn int_welcome(username: &String) -> String { + // Variables + let mut user_input: String = String::new(); + + // Welcome message + println!("\n - RUSTYPASS - "); + println!("Welcome, {}!", username); + println!("Please select one of the following options: "); + println!("\n1) View a Password"); + println!("2) Create a Password"); + println!("3) Generate a Password"); + println!("4) Quit"); + io::stdin().read_line(&mut user_input).expect("Failed to read line"); + + // Returning what the user said + return String::from(user_input.trim()); +} +pub fn int_gen() -> (u32, bool, bool) { + // Variables + let mut raw_pass_len: String = String::new(); + let mut raw_pass_num: String = String::new(); + let mut raw_pass_spe: String = String::new(); + + // Asking the user the big questions + println!(" - Password Generator - "); + println!("Before we give you a password, just a few questions!"); + println!("How long should your password be?"); + println!("Select size[number]: "); + io::stdin().read_line(&mut raw_pass_len).expect("Failed to read line."); + println!("Would you like numbers to be included in your password?"); + println!("Select option[y/n]: "); + io::stdin().read_line(&mut raw_pass_num).expect("Failed to read line."); + println!("Would you like special characters to be included in your password?"); + println!("Select option[y/n]: "); + io::stdin().read_line(&mut raw_pass_spe).expect("Failed to read line."); + + // Converting the stuff + let pass_len: u32 = raw_pass_len.trim().parse().unwrap(); + let str_pass_num: String = String::from(raw_pass_num.to_lowercase().trim()); + let str_pass_spe: String = String::from(raw_pass_spe.to_lowercase().trim()); + + // Y/N to bool: + let pass_num: bool = if str_pass_num == "y" { + true + } else{ + false + }; + let pass_spe: bool = if str_pass_spe == "y" { + true + } else { + false + }; + + // Returning all of the values + return (pass_len, pass_num, pass_spe); } \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 92c7aa6..6c57bc5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,31 +1,63 @@ // Libraries mod interface; mod resource; +mod manager; mod secure; mod auth; // Functions - -// Entry-Point -fn main() { - // Variables - let auth_profile: auth::Auth; - +fn stage_authorize(auth_profile: &mut auth::Auth) { // 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); + *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); + *auth_profile = auth::Auth::create(username, password); + } +} +fn stage_manager(auth_profile: &mut auth::Auth) { + // Loop + loop { + // What page shall we go to? + let page: String = interface::int_welcome(&auth_profile.username); + + // New line + println!("\n"); + + // Deciding what page we are on + if page == "4" { + println!("Goodbye, {}!", auth_profile.username); + break; + } else if page == "3" { + let (pass_len, pass_num, pass_spe) = interface::int_gen(); + let gen_pass: String = manager::Manager::password_generate(pass_len, pass_num, pass_spe); + println!("Your password is: {}", gen_pass); + } + } +} + +// Entry-Point +fn main() { + // Authorizing User + let mut auth_profile: auth::Auth = auth::Auth{verified: false, username: String::from("ERROR")}; + while !auth_profile.verified { + stage_authorize(&mut auth_profile); + + // Before going to the accounts page, check if the auth failed + if !auth_profile.verified { + println!("\nPlease try again!\n"); + } else {break;} } - // DEBUG: Testing if the user is authenticated - println!("Authenticated Status: {}", auth_profile.verified); + println!(""); + + // Passwords interface + stage_manager(&mut auth_profile); } \ No newline at end of file diff --git a/src/manager.rs b/src/manager.rs new file mode 100644 index 0000000..787c55b --- /dev/null +++ b/src/manager.rs @@ -0,0 +1,36 @@ +// Libraries +use rand::Rng; + +// Structs +pub struct Manager{ + +} + +// Implementations +impl Manager { + pub fn password_generate(length: u32, numbers: bool, special: bool) -> String { + // Variables + let mut result: String = String::new(); + let mut possible_characters: String = String::new(); + + // Making lists of possibilities + let ascii_letters: &str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; + let ascii_numbers: &str = "1234567890"; + let ascii_special: &str = "!\"#$%&'()*+,-./:;<=>?@[]^_`{|}~"; + + // Putting everything together! + possible_characters += ascii_letters; + if numbers {possible_characters += ascii_numbers;} + if special {possible_characters += ascii_special;} + + // Creating the password! + for _ in 0..length { + let rand_index: usize = rand::thread_rng().gen_range(0..=possible_characters.len()); + let rand_character: char = possible_characters.chars().nth(rand_index).unwrap(); + result += &rand_character.to_string(); + } + + // Giving back the result + return result; + } +} \ No newline at end of file