Added register prompt

This commit is contained in:
Maddox Werts 2024-06-14 14:32:02 -04:00
parent 69cfd0c91f
commit c136b491e4
4 changed files with 67 additions and 12 deletions

View file

@ -9,9 +9,12 @@ pub struct Auth {
// Implementations // Implementations
impl Auth { impl Auth {
pub fn exists() -> bool {
return resource::ResourceFile::exists("res/auth.dat");
}
pub fn authenticate(username: String, password: String) -> Self { pub fn authenticate(username: String, password: String) -> Self {
// Read from the Auth file // 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 // Decrypting the content of the file with the password
let auth_plaintext: String = secure::Secure::decrypt(file_auth.content, password); let auth_plaintext: String = secure::Secure::decrypt(file_auth.content, password);
@ -22,4 +25,14 @@ impl Auth {
// Returning the result // Returning the result
return Auth {verified: auth_status, username: username}; 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};
}
} }

View file

@ -1,14 +1,13 @@
// Libraries // Libraries
use std::io; use std::{io, process::exit};
// Functions // Functions
pub fn int_auth() -> (String, String) { fn get_credentials() -> (String, String) {
// Variables // Variables
let mut username: String = String::new(); let mut username: String = String::new();
let mut password: String = String::new(); let mut password: String = String::new();
// Asking for the username // Asking for the username
println!(" - LOGIN - ");
println!("Username: "); println!("Username: ");
io::stdin().read_line(&mut username).expect("Failed to read line"); io::stdin().read_line(&mut username).expect("Failed to read line");
username = String::from(username.to_lowercase().trim()); username = String::from(username.to_lowercase().trim());
@ -20,3 +19,26 @@ pub fn int_auth() -> (String, String) {
return (username, password); 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);
}
}

View file

@ -1,5 +1,3 @@
use interface::int_auth;
// Libraries // Libraries
mod interface; mod interface;
mod resource; mod resource;
@ -10,11 +8,23 @@ mod auth;
// Entry-Point // Entry-Point
fn main() { fn main() {
// Asking user for login credentials // Variables
let (username, password) = int_auth(); let auth_profile: auth::Auth;
// Verify the user // Checking if we need to register or log in
let auth_profile: auth::Auth = auth::Auth::authenticate(username, password); 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 // DEBUG: Testing if the user is authenticated
println!("Authenticated Status: {}", auth_profile.verified); println!("Authenticated Status: {}", auth_profile.verified);

View file

@ -1,5 +1,5 @@
// Libraries // Libraries
use std::fs; use std::{fs, path::Path};
// Structures // Structures
pub struct ResourceFile { pub struct ResourceFile {
@ -9,11 +9,21 @@ pub struct ResourceFile {
// Implementations // Implementations
impl ResourceFile { 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: // Reading from a file:
let content: String = fs::read_to_string(String::from(file_path)).expect("Failed to read file."); let content: String = fs::read_to_string(String::from(file_path)).expect("Failed to read file.");
// Returning the value: // Returning the value:
return ResourceFile {path: String::from(file_path), content}; 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};
}
} }