2022-04-17 22:07:37 +00:00
|
|
|
use hex::encode;
|
|
|
|
use uuid::Uuid;
|
|
|
|
|
2022-04-19 11:05:47 +00:00
|
|
|
use crate::crypto::CryptographicHash;
|
2022-04-17 22:07:37 +00:00
|
|
|
|
|
|
|
#[derive(Debug)]
|
2022-04-16 22:09:23 +00:00
|
|
|
pub struct User {
|
|
|
|
pub id: String,
|
|
|
|
pub username: String,
|
|
|
|
pub password: String,
|
|
|
|
}
|
2022-04-17 22:07:37 +00:00
|
|
|
|
|
|
|
impl User {
|
|
|
|
/// Create a new User type (note **this doesn't create a new user in the database!**)
|
|
|
|
///
|
|
|
|
/// This function creates a unique UUID for the user and creates a password hash using SHA-512
|
|
|
|
/// ```
|
|
|
|
/// use homedisk_utils::database::User;
|
|
|
|
///
|
|
|
|
/// let user = User::new("medzik", "SuperSecretPassword123!").unwrap();
|
|
|
|
/// ```
|
2022-04-19 11:05:47 +00:00
|
|
|
pub fn new(username: &str, password: &str) -> Self {
|
2022-04-17 22:07:37 +00:00
|
|
|
// create user UUID
|
2022-04-19 11:05:47 +00:00
|
|
|
let sha1_name = CryptographicHash::hash("SHA-1", username.as_bytes()).unwrap();
|
2022-04-17 22:07:37 +00:00
|
|
|
let id = Uuid::new_v5(&Uuid::NAMESPACE_X500, &sha1_name).to_string();
|
|
|
|
|
2022-04-19 11:05:47 +00:00
|
|
|
let password = encode(CryptographicHash::hash("SHA-512", password.as_bytes()).unwrap());
|
2022-04-17 22:07:37 +00:00
|
|
|
|
2022-04-19 11:05:47 +00:00
|
|
|
Self {
|
2022-04-17 22:07:37 +00:00
|
|
|
id,
|
|
|
|
username: username.to_string(),
|
|
|
|
password,
|
2022-04-19 11:05:47 +00:00
|
|
|
}
|
2022-04-17 22:07:37 +00:00
|
|
|
}
|
|
|
|
}
|