HomeDisk/utils/src/database/user.rs

57 lines
1.4 KiB
Rust

use hex::encode;
use uuid::Uuid;
use crate::crypto::CryptographicHash;
#[derive(Debug, sqlx::FromRow)]
pub struct User {
pub id: String,
pub username: String,
pub password: String,
}
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!");
/// ```
pub fn new(username: &str, password: &str) -> Self {
// create user UUID
let sha1_name = CryptographicHash::hash("SHA-1", username.as_bytes()).unwrap();
let id = Uuid::new_v5(&Uuid::NAMESPACE_X500, &sha1_name).to_string();
let username = username.to_lowercase();
let password = encode(CryptographicHash::hash("SHA-512", password.as_bytes()).unwrap());
Self {
id,
username,
password,
}
}
}
#[cfg(test)]
mod tests {
use super::User;
#[test]
fn check_username_is_in_lowercase() {
let user = User::new("MEdzIk", "SuperSecretPassword123!");
assert_eq!(user.username, "medzik")
}
#[test]
fn check_password_is_hashed() {
let password = "Password";
let user = User::new("test", password);
assert!(user.password != password)
}
}