2025-03-21 01:38:07 -04:00
|
|
|
use rainbeam_shared::{
|
|
|
|
hash::{hash_salted, salt},
|
|
|
|
snow::AlmostSnowflake,
|
|
|
|
unix_epoch_timestamp,
|
|
|
|
};
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum Error {
|
|
|
|
DatabaseConnection(String),
|
|
|
|
UserNotFound,
|
|
|
|
RegistrationDisabled,
|
|
|
|
DatabaseError,
|
|
|
|
IncorrectPassword,
|
2025-03-21 22:18:36 -04:00
|
|
|
AlreadyAuthenticated,
|
|
|
|
Unknown,
|
2025-03-21 01:38:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ToString for Error {
|
|
|
|
fn to_string(&self) -> String {
|
|
|
|
match self {
|
|
|
|
Error::DatabaseConnection(msg) => msg.to_owned(),
|
|
|
|
Error::UserNotFound => "Unable to find user with given parameters".to_string(),
|
|
|
|
Error::RegistrationDisabled => "Registration is disabled".to_string(),
|
|
|
|
Error::IncorrectPassword => "The given password is invalid".to_string(),
|
2025-03-21 22:18:36 -04:00
|
|
|
Error::AlreadyAuthenticated => "Already authenticated".to_string(),
|
2025-03-21 01:38:07 -04:00
|
|
|
_ => format!("An unknown error as occurred ({:?})", self),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub type Result<T> = std::result::Result<T, Error>;
|
|
|
|
|
|
|
|
/// `(ip, token)`
|
|
|
|
pub type Token = (String, String);
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct User {
|
|
|
|
pub id: usize,
|
|
|
|
pub created: usize,
|
|
|
|
pub username: String,
|
|
|
|
pub password: String,
|
|
|
|
pub salt: String,
|
|
|
|
pub settings: UserSettings,
|
|
|
|
pub tokens: Vec<Token>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
|
|
|
pub struct UserSettings;
|
|
|
|
|
|
|
|
impl Default for UserSettings {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl User {
|
|
|
|
/// Create a new [`User`].
|
|
|
|
pub fn new(username: String, password: String) -> Self {
|
|
|
|
let salt = salt();
|
|
|
|
let password = hash_salted(password, salt.clone());
|
|
|
|
|
|
|
|
Self {
|
|
|
|
id: AlmostSnowflake::new(1234567890)
|
|
|
|
.to_string()
|
|
|
|
.parse::<usize>()
|
|
|
|
.unwrap(),
|
|
|
|
created: unix_epoch_timestamp() as usize,
|
|
|
|
username,
|
|
|
|
password,
|
|
|
|
salt,
|
|
|
|
settings: UserSettings::default(),
|
|
|
|
tokens: vec![(String::new(), AlmostSnowflake::new(1234567890).to_string())],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|