2023-10-08 20:11:59 +00:00
|
|
|
use color_eyre::Result;
|
2023-10-08 19:53:42 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
2023-10-08 20:11:59 +00:00
|
|
|
use sqlx::SqlitePool;
|
2023-10-08 19:53:42 +00:00
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Deserialize, Serialize)]
|
|
|
|
|
pub struct User {
|
2023-10-08 20:11:59 +00:00
|
|
|
pub id: i64,
|
2023-10-08 19:53:42 +00:00
|
|
|
pub name: String,
|
|
|
|
|
/// I hate this. It's stored in plaintext. Why?
|
2023-10-08 20:11:59 +00:00
|
|
|
password: String,
|
2023-10-08 19:53:42 +00:00
|
|
|
pub is_admin: bool,
|
|
|
|
|
}
|
2023-10-08 20:11:59 +00:00
|
|
|
|
|
|
|
|
impl User {
|
|
|
|
|
pub fn verify(&self, hashed_password: &str, salt: &str) -> bool {
|
|
|
|
|
let ours = md5::compute(format!("{}{}", self.password, salt));
|
|
|
|
|
let ours = format!("{ours:x}");
|
|
|
|
|
|
|
|
|
|
ours == hashed_password
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub async fn get_user(pool: &SqlitePool, name: &str) -> Result<Option<User>> {
|
|
|
|
|
Ok(
|
|
|
|
|
sqlx::query_as!(User, "SELECT * FROM users WHERE name = ?", name)
|
|
|
|
|
.fetch_optional(pool)
|
|
|
|
|
.await?,
|
|
|
|
|
)
|
|
|
|
|
}
|