summaryrefslogtreecommitdiff
path: root/src/api
diff options
context:
space:
mode:
authorStefan Melmuk <[email protected]>2024-04-27 22:16:05 +0200
committerGitHub <[email protected]>2024-04-27 22:16:05 +0200
commite9aa5a545eda43e2e868f76115a3cc9c13d4cc30 (patch)
treea25ea4205cce51c545356ea8eccf5863b206d0cd /src/api
parent9dcc738f856d818b261507376290c544bf712d47 (diff)
downloadvaultwarden-e9aa5a545eda43e2e868f76115a3cc9c13d4cc30.tar.gz
vaultwarden-e9aa5a545eda43e2e868f76115a3cc9c13d4cc30.zip
fix emergency access invites (#4337)
* fix emergency access invites with no mail when mail is disabled instead of accepting emergency access for all invited users automatically, we only accept if the user already exists on registration of a new account any open emergency access invitations will be accepted, if mail is disabled also prevent invited emergency access contacts to register if emergency access is disabled (this is only relevant for when mail is enabled, if mail is disabled they should have an Invitation entry) * delete emergency access invitations if an invited user is deleted in the /admin panel their emergency access invitation will remain in the database which causes the to_json_grantee_details fn to panic * improve missing emergency access grantees instead of returning an empty emergency access contact the entry should not be added to the list. also the error handling can be improved a bit.
Diffstat (limited to 'src/api')
-rw-r--r--src/api/core/accounts.rs12
-rw-r--r--src/api/core/emergency_access.rs69
2 files changed, 30 insertions, 51 deletions
diff --git a/src/api/core/accounts.rs b/src/api/core/accounts.rs
index b2afe905..5b947b85 100644
--- a/src/api/core/accounts.rs
+++ b/src/api/core/accounts.rs
@@ -166,7 +166,8 @@ pub async fn _register(data: JsonUpcase<RegisterData>, mut conn: DbConn) -> Json
}
user
} else if CONFIG.is_signup_allowed(&email)
- || EmergencyAccess::find_invited_by_grantee_email(&email, &mut conn).await.is_some()
+ || (CONFIG.emergency_access_allowed()
+ && EmergencyAccess::find_invited_by_grantee_email(&email, &mut conn).await.is_some())
{
user
} else {
@@ -217,7 +218,6 @@ pub async fn _register(data: JsonUpcase<RegisterData>, mut conn: DbConn) -> Json
if let Err(e) = mail::send_welcome_must_verify(&user.email, &user.uuid).await {
error!("Error sending welcome email: {:#?}", e);
}
-
user.last_verifying_at = Some(user.created_at);
} else if let Err(e) = mail::send_welcome(&user.email).await {
error!("Error sending welcome email: {:#?}", e);
@@ -229,6 +229,14 @@ pub async fn _register(data: JsonUpcase<RegisterData>, mut conn: DbConn) -> Json
}
user.save(&mut conn).await?;
+
+ // accept any open emergency access invitations
+ if !CONFIG.mail_enabled() && CONFIG.emergency_access_allowed() {
+ for mut emergency_invite in EmergencyAccess::find_all_invited_by_grantee_email(&user.email, &mut conn).await {
+ let _ = emergency_invite.accept_invite(&user.uuid, &user.email, &mut conn).await;
+ }
+ }
+
Ok(Json(json!({
"Object": "register",
"CaptchaBypassToken": "",
diff --git a/src/api/core/emergency_access.rs b/src/api/core/emergency_access.rs
index 5f64cde0..5d522c61 100644
--- a/src/api/core/emergency_access.rs
+++ b/src/api/core/emergency_access.rs
@@ -61,7 +61,9 @@ async fn get_contacts(headers: Headers, mut conn: DbConn) -> Json<Value> {
let emergency_access_list = EmergencyAccess::find_all_by_grantor_uuid(&headers.user.uuid, &mut conn).await;
let mut emergency_access_list_json = Vec::with_capacity(emergency_access_list.len());
for ea in emergency_access_list {
- emergency_access_list_json.push(ea.to_json_grantee_details(&mut conn).await);
+ if let Some(grantee) = ea.to_json_grantee_details(&mut conn).await {
+ emergency_access_list_json.push(grantee)
+ }
}
Json(json!({
@@ -95,7 +97,9 @@ async fn get_emergency_access(emer_id: &str, mut conn: DbConn) -> JsonResult {
check_emergency_access_enabled()?;
match EmergencyAccess::find_by_uuid(emer_id, &mut conn).await {
- Some(emergency_access) => Ok(Json(emergency_access.to_json_grantee_details(&mut conn).await)),
+ Some(emergency_access) => Ok(Json(
+ emergency_access.to_json_grantee_details(&mut conn).await.expect("Grantee user should exist but does not!"),
+ )),
None => err!("Emergency access not valid."),
}
}
@@ -209,7 +213,7 @@ async fn send_invite(data: JsonUpcase<EmergencyAccessInviteData>, headers: Heade
err!("You can not set yourself as an emergency contact.")
}
- let grantee_user = match User::find_by_mail(&email, &mut conn).await {
+ let (grantee_user, new_user) = match User::find_by_mail(&email, &mut conn).await {
None => {
if !CONFIG.invitations_allowed() {
err!(format!("Grantee user does not exist: {}", &email))
@@ -226,9 +230,10 @@ async fn send_invite(data: JsonUpcase<EmergencyAccessInviteData>, headers: Heade
let mut user = User::new(email.clone());
user.save(&mut conn).await?;
- user
+ (user, true)
}
- Some(user) => user,
+ Some(user) if user.password_hash.is_empty() => (user, true),
+ Some(user) => (user, false),
};
if EmergencyAccess::find_by_grantor_uuid_and_grantee_uuid_or_email(
@@ -256,15 +261,9 @@ async fn send_invite(data: JsonUpcase<EmergencyAccessInviteData>, headers: Heade
&grantor_user.email,
)
.await?;
- } else {
- // Automatically mark user as accepted if no email invites
- match User::find_by_mail(&email, &mut conn).await {
- Some(user) => match accept_invite_process(&user.uuid, &mut new_emergency_access, &email, &mut conn).await {
- Ok(v) => v,
- Err(e) => err!(e.to_string()),
- },
- None => err!("Grantee user not found."),
- }
+ } else if !new_user {
+ // if mail is not enabled immediately accept the invitation for existing users
+ new_emergency_access.accept_invite(&grantee_user.uuid, &email, &mut conn).await?;
}
Ok(())
@@ -308,17 +307,12 @@ async fn resend_invite(emer_id: &str, headers: Headers, mut conn: DbConn) -> Emp
&grantor_user.email,
)
.await?;
- } else {
- if Invitation::find_by_mail(&email, &mut conn).await.is_none() {
- let invitation = Invitation::new(&email);
- invitation.save(&mut conn).await?;
- }
-
- // Automatically mark user as accepted if no email invites
- match accept_invite_process(&grantee_user.uuid, &mut emergency_access, &email, &mut conn).await {
- Ok(v) => v,
- Err(e) => err!(e.to_string()),
- }
+ } else if !grantee_user.password_hash.is_empty() {
+ // accept the invitation for existing user
+ emergency_access.accept_invite(&grantee_user.uuid, &email, &mut conn).await?;
+ } else if CONFIG.invitations_allowed() && Invitation::find_by_mail(&email, &mut conn).await.is_none() {
+ let invitation = Invitation::new(&email);
+ invitation.save(&mut conn).await?;
}
Ok(())
@@ -367,10 +361,7 @@ async fn accept_invite(emer_id: &str, data: JsonUpcase<AcceptData>, headers: Hea
&& grantor_user.name == claims.grantor_name
&& grantor_user.email == claims.grantor_email
{
- match accept_invite_process(&grantee_user.uuid, &mut emergency_access, &grantee_user.email, &mut conn).await {
- Ok(v) => v,
- Err(e) => err!(e.to_string()),
- }
+ emergency_access.accept_invite(&grantee_user.uuid, &grantee_user.email, &mut conn).await?;
if CONFIG.mail_enabled() {
mail::send_emergency_access_invite_accepted(&grantor_user.email, &grantee_user.email).await?;
@@ -382,26 +373,6 @@ async fn accept_invite(emer_id: &str, data: JsonUpcase<AcceptData>, headers: Hea
}
}
-async fn accept_invite_process(
- grantee_uuid: &str,
- emergency_access: &mut EmergencyAccess,
- grantee_email: &str,
- conn: &mut DbConn,
-) -> EmptyResult {
- if emergency_access.email.is_none() || emergency_access.email.as_ref().unwrap() != grantee_email {
- err!("User email does not match invite.");
- }
-
- if emergency_access.status == EmergencyAccessStatus::Accepted as i32 {
- err!("Emergency contact already accepted.");
- }
-
- emergency_access.status = EmergencyAccessStatus::Accepted as i32;
- emergency_access.grantee_uuid = Some(String::from(grantee_uuid));
- emergency_access.email = None;
- emergency_access.save(conn).await
-}
-
#[derive(Deserialize)]
#[allow(non_snake_case)]
struct ConfirmData {