vw_small

Hardened fork of Vaultwarden (https://github.com/dani-garcia/vaultwarden) with fewer features.
git clone https://git.philomathiclife.com/repos/vw_small
Log | Files | Refs | README

commit e985221b50d42a9c43d3f958a1de4ff9f69eb351
parent faec050a6d241d820a2cf11dff7815ddf613cb25
Author: janost <janost@users.noreply.github.com>
Date:   Sun, 14 Oct 2018 19:32:43 +0200

User::save() should return QueryResult instead of bool

Diffstat:
Msrc/api/core/accounts.rs | 51++++++++++++++++++++++++++++++---------------------
Msrc/api/core/mod.rs | 6++++--
Msrc/api/core/organizations.rs | 6+++---
Msrc/api/core/two_factor.rs | 11+++++++----
Msrc/db/models/user.rs | 10+++-------
5 files changed, 47 insertions(+), 37 deletions(-)

diff --git a/src/api/core/accounts.rs b/src/api/core/accounts.rs @@ -84,9 +84,10 @@ fn register(data: JsonUpcase<RegisterData>, conn: DbConn) -> EmptyResult { user.public_key = Some(keys.PublicKey); } - user.save(&conn); - - Ok(()) + match user.save(&conn) { + Ok(()) => Ok(()), + Err(_) => err!("Failed to save user") + } } #[get("/accounts/profile")] @@ -119,9 +120,10 @@ fn post_profile(data: JsonUpcase<ProfileData>, headers: Headers, conn: DbConn) - Some(ref h) if h.is_empty() => None, _ => data.MasterPasswordHint, }; - user.save(&conn); - - Ok(Json(user.to_json(&conn))) + match user.save(&conn) { + Ok(()) => Ok(Json(user.to_json(&conn))), + Err(_) => err!("Failed to save user profile") + } } #[get("/users/<uuid>/public-key")] @@ -147,11 +149,14 @@ fn post_keys(data: JsonUpcase<KeysData>, headers: Headers, conn: DbConn) -> Json user.private_key = Some(data.EncryptedPrivateKey); user.public_key = Some(data.PublicKey); - user.save(&conn); - - Ok(Json(user.to_json(&conn))) + match user.save(&conn) { + Ok(()) => Ok(Json(user.to_json(&conn))), + Err(_) => err!("Failed to save the user's keys") + } } + + #[derive(Deserialize)] #[allow(non_snake_case)] struct ChangePassData { @@ -171,9 +176,10 @@ fn post_password(data: JsonUpcase<ChangePassData>, headers: Headers, conn: DbCon user.set_password(&data.NewMasterPasswordHash); user.key = data.Key; - user.save(&conn); - - Ok(()) + match user.save(&conn) { + Ok(()) => Ok(()), + Err(_) => err!("Failed to save password") + } } #[derive(Deserialize)] @@ -200,9 +206,10 @@ fn post_kdf(data: JsonUpcase<ChangeKdfData>, headers: Headers, conn: DbConn) -> user.client_kdf_type = data.Kdf; user.set_password(&data.NewMasterPasswordHash); user.key = data.Key; - user.save(&conn); - - Ok(()) + match user.save(&conn) { + Ok(()) => Ok(()), + Err(_) => err!("Failed to save password settings") + } } #[post("/accounts/security-stamp", data = "<data>")] @@ -215,9 +222,10 @@ fn post_sstamp(data: JsonUpcase<PasswordData>, headers: Headers, conn: DbConn) - } user.reset_security_stamp(); - user.save(&conn); - - Ok(()) + match user.save(&conn) { + Ok(()) => Ok(()), + Err(_) => err!("Failed to reset security stamp") + } } #[derive(Deserialize)] @@ -272,9 +280,10 @@ fn post_email(data: JsonUpcase<ChangeEmailData>, headers: Headers, conn: DbConn) user.set_password(&data.NewMasterPasswordHash); user.key = data.Key; - user.save(&conn); - - Ok(()) + match user.save(&conn) { + Ok(()) => Ok(()), + Err(_) => err!("Failed to save email address") + } } #[post("/accounts/delete", data = "<data>")] diff --git a/src/api/core/mod.rs b/src/api/core/mod.rs @@ -227,7 +227,9 @@ fn post_eq_domains(data: JsonUpcase<EquivDomainData>, headers: Headers, conn: Db user.excluded_globals = to_string(&excluded_globals).unwrap_or("[]".to_string()); user.equivalent_domains = to_string(&equivalent_domains).unwrap_or("[]".to_string()); - user.save(&conn); + match user.save(&conn) { + Ok(()) => Ok(()), + Err(_) => err!("Failed to save user") + } - Ok(()) } diff --git a/src/api/core/organizations.rs b/src/api/core/organizations.rs @@ -391,11 +391,11 @@ fn send_invite(org_id: String, data: JsonUpcase<InviteData>, headers: AdminHeade match invitation.save(&conn) { Ok(()) => { let mut user = User::new(email.clone()); - if user.save(&conn) { + if user.save(&conn).is_err() { + err!("Failed to create placeholder for invited user") + } else { user_org_status = UserOrgStatus::Invited as i32; user - } else { - err!("Failed to create placeholder for invited user") } } Err(_) => err!(format!("Failed to invite: {}", email)) diff --git a/src/api/core/two_factor.rs b/src/api/core/two_factor.rs @@ -75,9 +75,10 @@ fn recover(data: JsonUpcase<RecoverTwoFactor>, conn: DbConn) -> JsonResult { // Remove the recovery code, not needed without twofactors user.totp_recover = None; - user.save(&conn); - - Ok(Json(json!({}))) + match user.save(&conn) { + Ok(()) => Ok(Json(json!({}))), + Err(_) => err!("Failed to remove the user's two factor recovery code") + } } #[derive(Deserialize)] @@ -217,7 +218,9 @@ fn _generate_recover_code(user: &mut User, conn: &DbConn) { if user.totp_recover.is_none() { let totp_recover = BASE32.encode(&crypto::get_random(vec![0u8; 20])); user.totp_recover = Some(totp_recover); - user.save(conn); + if user.save(conn).is_err() { + println!("Error: Failed to save the user's two factor recovery code") + } } } diff --git a/src/db/models/user.rs b/src/db/models/user.rs @@ -148,15 +148,11 @@ impl User { } - pub fn save(&mut self, conn: &DbConn) -> bool { + pub fn save(&mut self, conn: &DbConn) -> QueryResult<()> { self.updated_at = Utc::now().naive_utc(); - match diesel::replace_into(users::table) // Insert or update - .values(&*self) - .execute(&**conn) { - Ok(1) => true, // One row inserted - _ => false, - } + diesel::replace_into(users::table) // Insert or update + .values(&*self).execute(&**conn).and(Ok(())) } pub fn delete(self, conn: &DbConn) -> QueryResult<()> {