mod.rs (1687B)
1 mod admin; 2 pub mod core; 3 mod icons; 4 mod identity; 5 mod web; 6 pub use crate::api::{ 7 admin::catchers as admin_catchers, admin::routes as admin_routes, 8 core::catchers as core_catchers, core::events_routes as core_events_routes, 9 core::routes as core_routes, icons::routes as icons_routes, 10 identity::routes as identity_routes, web::catchers as web_catchers, web::routes as web_routes, 11 }; 12 use crate::db::models::User; 13 use crate::error::Error; 14 use rocket::serde::json::Json; 15 use serde_json::Value; 16 // Type aliases for API methods results 17 type ApiResult<T> = Result<T, Error>; 18 type JsonResult = ApiResult<Json<Value>>; 19 pub type EmptyResult = ApiResult<()>; 20 21 // Common structs representing JSON data received 22 #[derive(Deserialize)] 23 #[serde(rename_all = "camelCase")] 24 struct PasswordOrOtpData { 25 master_password_hash: Option<String>, 26 otp: Option<String>, 27 } 28 29 impl PasswordOrOtpData { 30 /// Tokens used via this struct can be used multiple times during the process 31 /// First for the validation to continue, after that to enable or validate the following actions 32 /// This is different per caller, so it can be adjusted to delete the token or not 33 fn validate(&self, user: &User) -> EmptyResult { 34 match (self.master_password_hash.as_deref(), self.otp.as_deref()) { 35 (Some(pw_hash), None) => { 36 if !user.check_valid_password(pw_hash) { 37 err!("Invalid password"); 38 } 39 } 40 (None, Some(_)) => { 41 err!("Login via device is not allowed. You must use the master password.") 42 } 43 _ => err!("No validation provided"), 44 } 45 Ok(()) 46 } 47 }