// Libraries use std::{io, process::exit}; use crate::manager; // Functions fn get_credentials() -> (String, String) { // Variables let mut username: String = String::new(); let mut password: String = String::new(); // Asking for the username println!("Username: "); io::stdin().read_line(&mut username).expect("Failed to read line"); username = String::from(username.to_lowercase().trim()); // Asking for the password println!("Password: "); io::stdin().read_line(&mut password).expect("Failed to read line"); 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); } } 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!("\n - 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!("\nWould 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!("\nWould 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); } pub fn int_create() -> (String, String, String) { // Variables let mut raw_pass_name: String = String::new(); let mut raw_pass_user: String = String::new(); let mut raw_pass_type: String = String::new(); let mut raw_pass_phrase: String = String::new(); // Asking the user some questions println!(" - Create a Password - "); println!("To create a password, we need to ask you a few simple questions."); println!("First, what would you like to call your Password?"); println!("Select Name: "); io::stdin().read_line(&mut raw_pass_name).expect("Failed to read line"); println!("\nNext, What is the username for this password?"); println!("Select User: "); io::stdin().read_line(&mut raw_pass_user).expect("Failed to read line"); println!("\nFinally, Would you like to use your own passphrase or a generated one?"); println!("Select passphrase [generate, custom]: "); io::stdin().read_line(&mut raw_pass_type).expect("Failed to read line"); // Formatting inputs let pass_name: String = String::from(raw_pass_name.trim()); let pass_user: String = String::from(raw_pass_user.trim()); let pass_type: String = String::from(raw_pass_type.to_lowercase().trim()); let pass_phrase: String; // Are we generating or using a prior one if pass_type == "generate" { let (pass_len, pass_num, pass_spe) = int_gen(); pass_phrase = manager::Manager::password_generate(pass_len, pass_num, pass_spe); } else if pass_type == "custom" { println!("Enter your custom password"); println!("Select Passphrase: "); io::stdin().read_line(&mut raw_pass_phrase).expect("Failed to read line"); pass_phrase = String::from(raw_pass_phrase.trim()); } else { println!("ERROR: Incorrect passphrase type!"); exit(0); } // Returning values return (pass_name, pass_user, pass_phrase); } pub fn int_view(manager: &mut manager::Manager) { // Variables let mut pass_name: String = String::new(); // Asking the user some questions println!(" - Password View - "); println!("What is your passwords name?"); println!("Select Name: "); io::stdin().read_line(&mut pass_name).expect("Failed to read line"); // Query password let (success, pass_obj) = manager.password_view(String::from(pass_name.trim())); // Was it successful? if !success { println!("Failed to get password of name {}!", pass_name); return; } // Show us that beautiful password! println!("\n - Password: {} - ", pass_obj.name); println!("Username: {}", pass_obj.user); println!("Passphrase: {}", pass_obj.phrase); println!(""); }