2024-12-15 20:21:03 +00:00
|
|
|
/*
|
2021-03-26 22:30:38 +00:00
|
|
|
use uuid::Uuid;
|
|
|
|
use diesel::prelude::*;
|
2021-03-27 05:45:59 +00:00
|
|
|
use diesel::result::Error as DieselError;
|
2021-03-26 22:30:38 +00:00
|
|
|
use diesel_derive_enum::DbEnum;
|
2022-04-24 10:42:53 +00:00
|
|
|
use rocket::request::{FromRequest, Request, Outcome};
|
2022-04-22 17:16:57 +00:00
|
|
|
use rocket::outcome::try_outcome;
|
2022-03-04 12:08:03 +00:00
|
|
|
use serde::{Deserialize};
|
2022-04-23 23:06:43 +00:00
|
|
|
use argon2::password_hash::{PasswordHash, PasswordHasher, PasswordVerifier, SaltString};
|
|
|
|
use argon2::password_hash::errors::Error as PasswordHashError;
|
2022-04-24 10:42:53 +00:00
|
|
|
use rand::rngs::OsRng;
|
2022-04-23 23:06:43 +00:00
|
|
|
use argon2::{Algorithm, Argon2, Version, Params};
|
2022-04-24 10:42:53 +00:00
|
|
|
|
2021-03-26 22:30:38 +00:00
|
|
|
|
|
|
|
use crate::schema::*;
|
2021-03-27 05:45:59 +00:00
|
|
|
use crate::DbConn;
|
2022-04-24 10:42:53 +00:00
|
|
|
|
2022-03-04 12:08:03 +00:00
|
|
|
use crate::models::errors::{UserError, ErrorResponse, make_500};
|
|
|
|
use crate::models::zone::Zone;
|
2022-04-27 20:07:32 +00:00
|
|
|
use crate::models::session::Session;
|
2021-03-26 22:30:38 +00:00
|
|
|
|
2021-03-27 17:23:19 +00:00
|
|
|
|
2021-04-03 06:16:54 +00:00
|
|
|
#[derive(Debug, DbEnum, Deserialize, Clone)]
|
2021-04-03 06:19:58 +00:00
|
|
|
#[serde(rename_all = "lowercase")]
|
2021-03-26 22:30:38 +00:00
|
|
|
pub enum Role {
|
2021-04-03 06:19:58 +00:00
|
|
|
#[db_rename = "admin"]
|
2021-03-26 22:30:38 +00:00
|
|
|
Admin,
|
2021-04-03 06:16:54 +00:00
|
|
|
#[db_rename = "zoneadmin"]
|
2021-03-26 22:30:38 +00:00
|
|
|
ZoneAdmin,
|
|
|
|
}
|
|
|
|
|
2021-03-27 05:45:59 +00:00
|
|
|
// TODO: Store Uuid instead of string??
|
|
|
|
#[derive(Debug, Queryable, Identifiable, Insertable)]
|
2021-03-26 22:30:38 +00:00
|
|
|
#[table_name = "user"]
|
|
|
|
pub struct User {
|
|
|
|
pub id: String,
|
|
|
|
}
|
|
|
|
|
2021-03-27 05:45:59 +00:00
|
|
|
#[derive(Debug, Queryable, Identifiable, Insertable)]
|
2021-03-26 22:30:38 +00:00
|
|
|
#[table_name = "localuser"]
|
|
|
|
#[primary_key(user_id)]
|
|
|
|
pub struct LocalUser {
|
|
|
|
pub user_id: String,
|
2023-02-22 12:52:12 +00:00
|
|
|
pub email: String,
|
2021-03-26 22:30:38 +00:00
|
|
|
pub password: String,
|
2021-04-05 22:56:15 +00:00
|
|
|
pub role: Role,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Queryable, Identifiable, Insertable)]
|
|
|
|
#[table_name = "user_zone"]
|
|
|
|
#[primary_key(user_id, zone_id)]
|
|
|
|
pub struct UserZone {
|
|
|
|
pub user_id: String,
|
|
|
|
pub zone_id: String,
|
|
|
|
}
|
|
|
|
|
2021-03-27 05:45:59 +00:00
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
pub struct CreateUserRequest {
|
|
|
|
pub password: String,
|
|
|
|
pub email: String,
|
|
|
|
pub role: Option<Role>
|
|
|
|
}
|
|
|
|
|
2023-02-22 15:28:12 +00:00
|
|
|
#[derive(Debug, Clone)]
|
2021-03-26 22:30:38 +00:00
|
|
|
pub struct UserInfo {
|
2021-03-27 05:45:59 +00:00
|
|
|
pub id: String,
|
2021-04-03 06:16:54 +00:00
|
|
|
pub role: Role,
|
2021-03-27 05:45:59 +00:00
|
|
|
pub username: String,
|
2021-03-26 22:30:38 +00:00
|
|
|
}
|
|
|
|
|
2021-04-05 22:56:15 +00:00
|
|
|
impl UserInfo {
|
|
|
|
pub fn is_admin(&self) -> bool {
|
|
|
|
matches!(self.role, Role::Admin)
|
|
|
|
}
|
|
|
|
|
2021-04-06 02:39:56 +00:00
|
|
|
pub fn check_admin(&self) -> Result<(), UserError> {
|
|
|
|
if self.is_admin() {
|
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
Err(UserError::PermissionDenied)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-05 22:56:15 +00:00
|
|
|
pub fn get_zone(&self, conn: &diesel::SqliteConnection, zone_name: &str) -> Result<Zone, UserError> {
|
|
|
|
use crate::schema::user_zone::dsl::*;
|
|
|
|
use crate::schema::zone::dsl::*;
|
|
|
|
|
|
|
|
let (res_zone, _): (Zone, UserZone) = zone.inner_join(user_zone)
|
|
|
|
.filter(name.eq(zone_name))
|
|
|
|
.filter(user_id.eq(&self.id))
|
|
|
|
.get_result(conn)
|
|
|
|
.map_err(|e| match e {
|
|
|
|
DieselError::NotFound => UserError::ZoneNotFound,
|
|
|
|
other => UserError::DbError(other)
|
|
|
|
})?;
|
|
|
|
|
|
|
|
Ok(res_zone)
|
|
|
|
}
|
2021-04-06 02:39:56 +00:00
|
|
|
|
2021-05-02 13:56:42 +00:00
|
|
|
pub fn get_zones(&self, conn: &diesel::SqliteConnection) -> Result<Vec<Zone>, UserError> {
|
2021-04-06 02:39:56 +00:00
|
|
|
use crate::schema::user_zone::dsl::*;
|
|
|
|
use crate::schema::zone::dsl::*;
|
|
|
|
|
2021-05-02 13:56:42 +00:00
|
|
|
let res: Vec<(Zone, UserZone)> = zone.inner_join(user_zone)
|
|
|
|
.filter(user_id.eq(&self.id))
|
|
|
|
.get_results(conn)
|
|
|
|
.map_err(UserError::DbError)?;
|
2021-04-06 02:39:56 +00:00
|
|
|
|
2021-05-02 13:56:42 +00:00
|
|
|
Ok(res.into_iter().map(|(z, _)| z).collect())
|
2021-04-06 02:39:56 +00:00
|
|
|
}
|
2021-04-05 22:56:15 +00:00
|
|
|
}
|
|
|
|
|
2021-04-02 17:33:59 +00:00
|
|
|
#[rocket::async_trait]
|
|
|
|
impl<'r> FromRequest<'r> for UserInfo {
|
2021-04-02 21:12:29 +00:00
|
|
|
type Error = ErrorResponse;
|
2021-03-27 19:36:57 +00:00
|
|
|
|
2021-04-02 17:33:59 +00:00
|
|
|
async fn from_request(request: &'r Request<'_>) -> Outcome<Self, Self::Error> {
|
2022-04-27 20:38:37 +00:00
|
|
|
let session = try_outcome!(request.guard::<Session>().await);
|
2021-04-05 01:05:39 +00:00
|
|
|
let conn = try_outcome!(request.guard::<DbConn>().await.map_failure(make_500));
|
2021-03-26 22:30:38 +00:00
|
|
|
|
2021-05-02 13:56:42 +00:00
|
|
|
conn.run(move |c| {
|
2022-04-24 10:42:53 +00:00
|
|
|
match LocalUser::get_user_by_uuid(c, &session.user_id) {
|
2021-04-02 21:12:29 +00:00
|
|
|
Err(e) => ErrorResponse::from(e).into(),
|
2021-04-02 17:33:59 +00:00
|
|
|
Ok(d) => Outcome::Success(d),
|
|
|
|
}
|
|
|
|
}).await
|
2021-03-26 22:30:38 +00:00
|
|
|
}
|
|
|
|
}
|
2021-03-27 05:45:59 +00:00
|
|
|
|
|
|
|
impl LocalUser {
|
2022-04-23 23:06:43 +00:00
|
|
|
pub fn hash_password(password: &str) -> String {
|
|
|
|
let salt = SaltString::generate(&mut OsRng);
|
|
|
|
|
|
|
|
// https://cheatsheetseries.owasp.org/cheatsheets/Password_Storage_Cheat_Sheet.html
|
|
|
|
let argon2 = Argon2::new(
|
|
|
|
Algorithm::Argon2id,
|
|
|
|
Version::V0x13, // v19
|
|
|
|
Params::new(15000, 2, 1, None).expect("password param error"),
|
|
|
|
);
|
|
|
|
argon2.hash_password(password.as_bytes(), &salt).expect("password hash failed").to_string()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn verify_password(password: &str, password_hash: &str) -> Result<bool, PasswordHashError> {
|
|
|
|
let parsed_hash = PasswordHash::new(&password_hash)?;
|
|
|
|
|
|
|
|
Ok(Argon2::default().verify_password(password.as_bytes(), &parsed_hash).is_ok())
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-04-02 17:33:59 +00:00
|
|
|
pub fn create_user(conn: &diesel::SqliteConnection, user_request: CreateUserRequest) -> Result<UserInfo, UserError> {
|
2021-03-27 05:45:59 +00:00
|
|
|
use crate::schema::localuser::dsl::*;
|
|
|
|
use crate::schema::user::dsl::*;
|
|
|
|
|
|
|
|
let new_user_id = Uuid::new_v4().to_simple().to_string();
|
|
|
|
|
|
|
|
let new_user = User {
|
|
|
|
id: new_user_id.clone(),
|
|
|
|
};
|
|
|
|
|
|
|
|
let new_localuser = LocalUser {
|
2021-04-05 01:05:39 +00:00
|
|
|
user_id: new_user_id,
|
2023-02-22 12:52:12 +00:00
|
|
|
email: user_request.email.clone(),
|
2022-04-23 23:06:43 +00:00
|
|
|
password: LocalUser::hash_password(&user_request.password),
|
2022-04-23 10:14:19 +00:00
|
|
|
role: if let Some(user_role) = user_request.role { user_role } else { Role::ZoneAdmin },
|
2021-03-27 05:45:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let res = UserInfo {
|
|
|
|
id: new_user.id.clone(),
|
2021-04-05 22:56:15 +00:00
|
|
|
role: new_localuser.role.clone(),
|
2023-02-22 12:52:12 +00:00
|
|
|
username: new_localuser.email.clone(),
|
2021-03-27 05:45:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
conn.immediate_transaction(|| -> diesel::QueryResult<()> {
|
|
|
|
diesel::insert_into(user)
|
|
|
|
.values(new_user)
|
2021-04-02 17:33:59 +00:00
|
|
|
.execute(conn)?;
|
2021-03-27 05:45:59 +00:00
|
|
|
|
|
|
|
diesel::insert_into(localuser)
|
|
|
|
.values(new_localuser)
|
2021-04-02 17:33:59 +00:00
|
|
|
.execute(conn)?;
|
2021-03-27 05:45:59 +00:00
|
|
|
|
|
|
|
Ok(())
|
2021-04-06 02:39:56 +00:00
|
|
|
}).map_err(|e| match e {
|
|
|
|
DieselError::DatabaseError(diesel::result::DatabaseErrorKind::UniqueViolation, _) => UserError::UserConflict,
|
|
|
|
other => UserError::DbError(other)
|
2021-03-27 05:45:59 +00:00
|
|
|
})?;
|
|
|
|
|
|
|
|
Ok(res)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_user_by_creds(
|
2021-04-02 17:33:59 +00:00
|
|
|
conn: &diesel::SqliteConnection,
|
2023-02-22 12:52:12 +00:00
|
|
|
request_email: &str,
|
2021-03-27 05:45:59 +00:00
|
|
|
request_password: &str
|
|
|
|
) -> Result<UserInfo, UserError> {
|
|
|
|
|
|
|
|
use crate::schema::localuser::dsl::*;
|
|
|
|
use crate::schema::user::dsl::*;
|
|
|
|
|
|
|
|
let (client_user, client_localuser): (User, LocalUser) = user.inner_join(localuser)
|
2023-02-22 12:52:12 +00:00
|
|
|
.filter(email.eq(request_email))
|
2021-04-06 02:39:56 +00:00
|
|
|
.get_result(conn)
|
|
|
|
.map_err(|e| match e {
|
|
|
|
DieselError::NotFound => UserError::BadCreds,
|
|
|
|
other => UserError::DbError(other)
|
|
|
|
})?;
|
2021-03-27 05:45:59 +00:00
|
|
|
|
2022-04-23 23:06:43 +00:00
|
|
|
if !LocalUser::verify_password(&request_password, &client_localuser.password)? {
|
2021-04-06 02:39:56 +00:00
|
|
|
return Err(UserError::BadCreds);
|
2021-03-27 05:45:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(UserInfo {
|
|
|
|
id: client_user.id,
|
2021-04-05 22:56:15 +00:00
|
|
|
role: client_localuser.role,
|
2023-02-22 12:52:12 +00:00
|
|
|
username: client_localuser.email,
|
2021-03-27 05:45:59 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-05-02 13:56:42 +00:00
|
|
|
pub fn get_user_by_uuid(conn: &diesel::SqliteConnection, request_user_id: &str) -> Result<UserInfo, UserError> {
|
2021-03-27 19:36:57 +00:00
|
|
|
use crate::schema::localuser::dsl::*;
|
|
|
|
use crate::schema::user::dsl::*;
|
|
|
|
|
|
|
|
let (client_user, client_localuser): (User, LocalUser) = user.inner_join(localuser)
|
|
|
|
.filter(id.eq(request_user_id))
|
2021-04-06 02:39:56 +00:00
|
|
|
.get_result(conn)
|
|
|
|
.map_err(|e| match e {
|
|
|
|
DieselError::NotFound => UserError::NotFound,
|
|
|
|
other => UserError::DbError(other)
|
|
|
|
})?;
|
2021-03-27 19:36:57 +00:00
|
|
|
|
|
|
|
Ok(UserInfo {
|
|
|
|
id: client_user.id,
|
2021-04-05 22:56:15 +00:00
|
|
|
role: client_localuser.role,
|
2023-02-22 12:52:12 +00:00
|
|
|
username: client_localuser.email,
|
2021-03-27 19:36:57 +00:00
|
|
|
})
|
2021-03-27 05:45:59 +00:00
|
|
|
}
|
2022-04-22 22:57:32 +00:00
|
|
|
}
|
2024-12-15 20:21:03 +00:00
|
|
|
*/
|