Added password creation and viewing

This commit is contained in:
Maddox Werts 2024-06-15 15:13:46 -04:00
parent a902cdd0ae
commit 04afce87b2
3 changed files with 122 additions and 5 deletions

View file

@ -1,5 +1,6 @@
// Libraries // Libraries
use std::{io, process::exit}; use std::{io, process::exit};
use crate::manager;
// Functions // Functions
fn get_credentials() -> (String, String) { fn get_credentials() -> (String, String) {
@ -67,15 +68,15 @@ pub fn int_gen() -> (u32, bool, bool) {
let mut raw_pass_spe: String = String::new(); let mut raw_pass_spe: String = String::new();
// Asking the user the big questions // Asking the user the big questions
println!(" - Password Generator - "); println!("\n - Password Generator - ");
println!("Before we give you a password, just a few questions!"); println!("Before we give you a password, just a few questions!");
println!("How long should your password be?"); println!("How long should your password be?");
println!("Select size[number]: "); println!("Select size[number]: ");
io::stdin().read_line(&mut raw_pass_len).expect("Failed to read line."); 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!("\nWould you like numbers to be included in your password?");
println!("Select option[y/n]: "); println!("Select option[y/n]: ");
io::stdin().read_line(&mut raw_pass_num).expect("Failed to read line."); 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!("\nWould you like special characters to be included in your password?");
println!("Select option[y/n]: "); println!("Select option[y/n]: ");
io::stdin().read_line(&mut raw_pass_spe).expect("Failed to read line."); io::stdin().read_line(&mut raw_pass_spe).expect("Failed to read line.");
@ -98,4 +99,72 @@ pub fn int_gen() -> (u32, bool, bool) {
// Returning all of the values // Returning all of the values
return (pass_len, pass_num, pass_spe); 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!("");
} }

View file

@ -23,6 +23,9 @@ fn stage_authorize(auth_profile: &mut auth::Auth) {
} }
} }
fn stage_manager(auth_profile: &mut auth::Auth) { fn stage_manager(auth_profile: &mut auth::Auth) {
// Loading the manager
let mut manager: manager::Manager = manager::Manager::init();
// Loop // Loop
loop { loop {
// What page shall we go to? // What page shall we go to?
@ -39,6 +42,14 @@ fn stage_manager(auth_profile: &mut auth::Auth) {
let (pass_len, pass_num, pass_spe) = interface::int_gen(); let (pass_len, pass_num, pass_spe) = interface::int_gen();
let gen_pass: String = manager::Manager::password_generate(pass_len, pass_num, pass_spe); let gen_pass: String = manager::Manager::password_generate(pass_len, pass_num, pass_spe);
println!("Your password is: {}", gen_pass); println!("Your password is: {}", gen_pass);
} else if page == "2" {
let (pass_name, pass_user, pass_phrase) = interface::int_create();
manager.password_create(pass_name.clone(), pass_user, pass_phrase);
println!("\nSuccessfully added {}!", pass_name);
} else if page == "1" {
interface::int_view(&mut manager);
} else {
println!("ERROR: Unrecognized Page!");
} }
} }
} }

View file

@ -2,12 +2,23 @@
use rand::Rng; use rand::Rng;
// Structs // Structs
pub struct Password {
pub name: String,
pub user: String,
pub phrase: String,
}
pub struct Manager{ pub struct Manager{
passwords: Vec<Password>
} }
// Implementations // Implementations
impl Manager { impl Manager {
// Constructors
pub fn init() -> Self {
return Manager { passwords: vec![] };
}
// Functions
pub fn password_generate(length: u32, numbers: bool, special: bool) -> String { pub fn password_generate(length: u32, numbers: bool, special: bool) -> String {
// Variables // Variables
let mut result: String = String::new(); let mut result: String = String::new();
@ -25,7 +36,7 @@ impl Manager {
// Creating the password! // Creating the password!
for _ in 0..length { for _ in 0..length {
let rand_index: usize = rand::thread_rng().gen_range(0..=possible_characters.len()); let rand_index: usize = rand::thread_rng().gen_range(0..=possible_characters.len()-1);
let rand_character: char = possible_characters.chars().nth(rand_index).unwrap(); let rand_character: char = possible_characters.chars().nth(rand_index).unwrap();
result += &rand_character.to_string(); result += &rand_character.to_string();
} }
@ -33,4 +44,30 @@ impl Manager {
// Giving back the result // Giving back the result
return result; return result;
} }
pub fn password_create(&mut self, name: String, user: String, pass: String) {
// Create the password object
let new_password: Password = Password{name: name, user: user, phrase: pass};
// Adding it to our list
self.passwords.push(new_password);
}
pub fn password_view(&mut self, name: String) -> (bool, Password) {
// Variables
let mut success: bool = false;
let mut selected_password: Password = Password { name: String::new(), user: String::new(), phrase: String::new() };
// Getting the right password
for c_pass in &self.passwords {
success = name.to_lowercase() == c_pass.name.to_lowercase();
if success {
selected_password.name = c_pass.name.clone();
selected_password.user = c_pass.user.clone();
selected_password.phrase = c_pass.phrase.clone();
break;
}
}
// Returning the password
return (success, selected_password);
}
} }