This repository has been archived on 2026-04-25. You can view files and clone it, but cannot push or open issues or pull requests.
RustyPass/src/auth.rs
2024-06-15 16:35:24 -04:00

39 lines
No EOL
1.3 KiB
Rust

// Libraries
use crate::{resource, secure};
// Structures
pub struct Auth {
pub verified: bool,
pub username: String,
pub key: String
}
// Implementations
impl Auth {
pub fn exists() -> bool {
return resource::ResourceFile::exists("res/auth.dat");
}
pub fn authenticate(username: String, password: String) -> Self {
// Read from the Auth file
let file_auth: resource::ResourceFile = resource::ResourceFile::read("res/auth.dat");
// Decrypting the content of the file with the password
let auth_plaintext: String = secure::Secure::decrypt(file_auth.content, password.clone());
// Testing if the user is authenticated:
let auth_status: bool = auth_plaintext == username;
// Returning the result
return Auth {verified: auth_status, username: username, key: password};
}
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.clone());
// 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, key: password};
}
}