39 lines
No EOL
1.3 KiB
Rust
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};
|
|
}
|
|
} |