Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

fix(auth): delete account race can permanently brick an account sso works macrodb user gone 401 everywhere (#4514)

Will Hutchinson4eaf594025a1
diff --git a/rust/cloud-storage/.sqlx/query-51873bdb08381231f01403f4e58bb5f5d73338d8cb9ec96c67fd8d6ea4746970.json b/rust/cloud-storage/.sqlx/query-60ae20b86b2837cc1008b8dde5e0c7d64ff472e76d5bb30995d389fd2901b489.json
similarity index 66%
rename from rust/cloud-storage/.sqlx/query-51873bdb08381231f01403f4e58bb5f5d73338d8cb9ec96c67fd8d6ea4746970.json
rename to rust/cloud-storage/.sqlx/query-60ae20b86b2837cc1008b8dde5e0c7d64ff472e76d5bb30995d389fd2901b489.json
index 7d015cf85e..5f22ac5ad9 100644
--- a/rust/cloud-storage/.sqlx/query-51873bdb08381231f01403f4e58bb5f5d73338d8cb9ec96c67fd8d6ea4746970.json
+++ b/rust/cloud-storage/.sqlx/query-60ae20b86b2837cc1008b8dde5e0c7d64ff472e76d5bb30995d389fd2901b489.json
@@ -1,6 +1,6 @@
{
"db_name": "PostgreSQL",
- "query": "DELETE FROM \"User\" WHERE id = $1 RETURNING id, email, \"organizationId\" as organization_id",
+ "query": "DELETE FROM \"User\" WHERE id = $1 AND macro_user_id = $2 RETURNING id, email, \"organizationId\" as organization_id",
"describe": {
"columns": [
{
@@ -21,7 +21,8 @@
],
"parameters": {
"Left": [
- "Text"
+ "Text",
+ "Uuid"
]
},
"nullable": [
@@ -30,5 +31,5 @@
true
]
},
- "hash": "51873bdb08381231f01403f4e58bb5f5d73338d8cb9ec96c67fd8d6ea4746970"
+ "hash": "60ae20b86b2837cc1008b8dde5e0c7d64ff472e76d5bb30995d389fd2901b489"
}
diff --git a/rust/cloud-storage/authentication_service/src/api/webhooks/user/create_user_webhook.rs b/rust/cloud-storage/authentication_service/src/api/webhooks/user/create_user_webhook.rs
index 6a2ae2f0b1..9626c58e2c 100644
--- a/rust/cloud-storage/authentication_service/src/api/webhooks/user/create_user_webhook.rs
+++ b/rust/cloud-storage/authentication_service/src/api/webhooks/user/create_user_webhook.rs
@@ -114,54 +114,15 @@ async fn create_user_webhook(ctx: &ApiContext, req: FusionAuthUserWebhook) -> an
}
// check if user exists
- if let Ok((user_id, stripe_customer_id)) =
+ if let Ok((user_id, _stripe_customer_id)) =
macro_db_client::user::get::get_user_id_and_stripe_customer_id_by_email(&ctx.db, &email)
.await
{
+ // The macro_user already exists for that email
+ // We do not allow a user to login through their secondary linked account for SSO so we shouldn't allow for passwordless either
tracing::info!(user_id=?user_id, "user already exists");
- // There are 2 reasons why the user profile already exists:
- // 1. The user was not correctly updated to have a macro_user_id.
- // 2. The user has performed an email link to another macro account.
- // To check this, we can check if `macro_user_email_verification` exists for the email
- // and if it does and is true, we do not need to create any new rows in macrodb.
- // If not, we need to create a new macro_user and update their user profile.
-
- let verification_status =
- macro_db_client::macro_user_email_verification::get_macro_user_email_verification(
- &ctx.db, &email,
- )
- .await
- .context("failed to get macro user email verification")?;
-
- if let Some(verification_status) = verification_status {
- if !verification_status {
- anyhow::bail!("user already exists and is not verified. cannot create user");
- }
-
- // We do nothing here, this allows the FusionAuth user to be created which will allow
- // login through this email.
-
- tracing::info!("user already exists and is verified. doing nothing");
- } else {
- // Need to backfill the user due to use case 1
- tracing::warn!("user was not backfilled correctly. backfilling user");
-
- let stripe_customer_id = stripe_customer_id.context("expected stripe_customer_id")?;
-
- backfill_user(
- ctx,
- &fusionauth_user_id,
- &user_id,
- &username,
- &email,
- &stripe_customer_id,
- )
- .await
- .context("failed to backfill user")?;
- }
-
- return Ok(());
+ anyhow::bail!("cannot login through secondary account link");
}
let start_time = std::time::Instant::now();
@@ -233,48 +194,6 @@ async fn create_user_webhook(ctx: &ApiContext, req: FusionAuthUserWebhook) -> an
Ok(())
}
-/// Used to handle updating an existing macro user to have a macro_user record.
-async fn backfill_user(
- ctx: &ApiContext,
- fusionauth_user_id: &str,
- user_id: &str,
- username: &str,
- email: &str,
- stripe_customer_id: &str,
-) -> anyhow::Result<()> {
- let mut transaction = ctx.db.begin().await?;
-
- // ensure macro user is created
- macro_db_client::macro_user::create_macro_user(
- &mut transaction,
- fusionauth_user_id,
- username,
- stripe_customer_id,
- email,
- )
- .await?;
-
- // link the user to the correct fusionauth user
- macro_db_client::user::update::upsert_macro_user_id(
- &mut transaction,
- user_id,
- fusionauth_user_id,
- )
- .await?;
-
- // move over their user profile
- macro_db_client::user::update::migrate_macro_user_info(
- &mut transaction,
- fusionauth_user_id,
- user_id,
- )
- .await?;
-
- transaction.commit().await?;
-
- Ok(())
-}
-
/// Initializes the experiments for a provided user
#[allow(dead_code)]
async fn initialize_user_experiments(
diff --git a/rust/cloud-storage/authentication_service/src/api/webhooks/user/delete_user_webhook.rs b/rust/cloud-storage/authentication_service/src/api/webhooks/user/delete_user_webhook.rs
index f689eefecc..236f14a209 100644
--- a/rust/cloud-storage/authentication_service/src/api/webhooks/user/delete_user_webhook.rs
+++ b/rust/cloud-storage/authentication_service/src/api/webhooks/user/delete_user_webhook.rs
@@ -259,7 +259,7 @@ async fn delete_user(
tracing::trace!(user_id, "delete_document_storage_service_items complete");
tracing::trace!(user_id, "delete_user_macro_db");
- if let Err(e) = macro_db_client::user::delete_user::delete_user(&db, &user_id).await {
+ if let Err(e) = macro_db_client::user::delete_user::delete_user(&db, &user_id, &macro_user_id).await {
tracing::error!(error=?e, user_id, "delete_user_macro_db unable to delete user");
}
tracing::trace!(user_id, "delete_user_macro_db complete");
diff --git a/rust/cloud-storage/macro_db_client/src/user/delete_user.rs b/rust/cloud-storage/macro_db_client/src/user/delete_user.rs
index f588787cc1..3e2def9b6b 100644
--- a/rust/cloud-storage/macro_db_client/src/user/delete_user.rs
+++ b/rust/cloud-storage/macro_db_client/src/user/delete_user.rs
@@ -6,14 +6,16 @@ pub struct DeletedUserInfo {
pub organization_id: Option<i32>,
}
/// Deletes a user from the database
-#[tracing::instrument(skip(db))]
+#[tracing::instrument(skip(db), err)]
pub async fn delete_user(
db: &sqlx::Pool<sqlx::Postgres>,
user_id: &str,
+ macro_user_id: &uuid::Uuid,
) -> Result<DeletedUserInfo, sqlx::Error> {
let info = sqlx::query!(
- r#"DELETE FROM "User" WHERE id = $1 RETURNING id, email, "organizationId" as organization_id"#,
- user_id
+ r#"DELETE FROM "User" WHERE id = $1 AND macro_user_id = $2 RETURNING id, email, "organizationId" as organization_id"#,
+ user_id,
+ macro_user_id,
)
.map(|r| DeletedUserInfo {
email: r.email,