180 lines
No EOL
6.9 KiB
Rust
180 lines
No EOL
6.9 KiB
Rust
// Libraries
|
|
use std::{io, process::exit};
|
|
use crate::manager;
|
|
|
|
// Constants
|
|
pub const COLOR_RESET: &str = "\x1B[0m";
|
|
pub const COLOR_RED: &str = "\x1B[31m";
|
|
pub const COLOR_GREEN: &str = "\x1B[32m";
|
|
pub const COLOR_YELLOW: &str = "\x1B[33m";
|
|
pub const COLOR_MAGENTA: &str = "\x1B[35m";
|
|
pub const COLOR_CYAN: &str = "\x1B[36m";
|
|
|
|
// 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 {} - ", COLOR_YELLOW, COLOR_RESET);
|
|
return get_credentials();
|
|
}
|
|
pub fn int_reg() -> (String, String) {
|
|
// Register form
|
|
println!(" - {} REGISTER {} - ", COLOR_YELLOW, COLOR_RESET);
|
|
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 {} - ", COLOR_YELLOW, COLOR_RESET);
|
|
println!("Welcome, {}{}{}!", COLOR_CYAN, username, COLOR_RESET);
|
|
println!("Please select one of the following options: ");
|
|
println!("\n{}1{}) View a Password", COLOR_GREEN, COLOR_RESET);
|
|
println!("{}2{}) Create a Password", COLOR_GREEN, COLOR_RESET);
|
|
println!("{}3{}) Generate a Password", COLOR_GREEN, COLOR_RESET);
|
|
println!("{}4{}) Quit", COLOR_GREEN, COLOR_RESET);
|
|
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 {} - ", COLOR_YELLOW, COLOR_RESET);
|
|
println!("Before we give you a password, just a few questions!");
|
|
println!("How long should your password be?");
|
|
println!("Select size[{}number{}]: ", COLOR_CYAN, COLOR_RESET);
|
|
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[{0}y{1}/{0}n{1}]: ", COLOR_CYAN, COLOR_RESET);
|
|
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[{0}y{1}/{0}n{1}]: ", COLOR_CYAN, COLOR_RESET);
|
|
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
|
|
};
|
|
|
|
println!("");
|
|
|
|
// 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 {} - ", COLOR_YELLOW, COLOR_RESET);
|
|
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{}: ", COLOR_CYAN, COLOR_RESET);
|
|
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{}: ", COLOR_CYAN, COLOR_RESET);
|
|
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 [{0}generate{1}/{0}custom{1}]: ", COLOR_CYAN, COLOR_RESET);
|
|
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!("\nEnter your custom password");
|
|
println!("Select {}Passphrase{}: ", COLOR_CYAN, COLOR_RESET);
|
|
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 {} - ", COLOR_YELLOW, COLOR_RESET);
|
|
println!("What is your passwords name?");
|
|
println!("Select {}Name{}: ", COLOR_CYAN, COLOR_RESET);
|
|
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 {}!{}", COLOR_RED, pass_name, COLOR_RESET);
|
|
return;
|
|
}
|
|
|
|
// Show us that beautiful password!
|
|
println!("\n - {0}Password{3}: {1}{2}{3} - ", COLOR_YELLOW, COLOR_CYAN, pass_obj.name, COLOR_RESET);
|
|
println!("Username:\t{}{}{}", COLOR_MAGENTA, pass_obj.user, COLOR_RESET);
|
|
println!("Passphrase:\t{}{}{}", COLOR_GREEN, pass_obj.phrase, COLOR_RESET);
|
|
println!("");
|
|
} |