Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
fix(auth): proper status codes on GitHub relink, expire stale link caps (#4482)
Jacob Beckerman30aeb9245323
diff --git a/rust/cloud-storage/.sqlx/query-ad0e23af0a67a769bf40b3ab388c21c001e01d1691861d8b9df950b03bdb2f4b.json b/rust/cloud-storage/.sqlx/query-d05f5cfa9fc6c226d8b63517805851fddb3cf3a5129edc7e2caa141878b33a28.jsonsimilarity index 63%rename from rust/cloud-storage/.sqlx/query-ad0e23af0a67a769bf40b3ab388c21c001e01d1691861d8b9df950b03bdb2f4b.jsonrename to rust/cloud-storage/.sqlx/query-d05f5cfa9fc6c226d8b63517805851fddb3cf3a5129edc7e2caa141878b33a28.jsonindex 77bae849be..2a0ee382b6 100644--- a/rust/cloud-storage/.sqlx/query-ad0e23af0a67a769bf40b3ab388c21c001e01d1691861d8b9df950b03bdb2f4b.json+++ b/rust/cloud-storage/.sqlx/query-d05f5cfa9fc6c226d8b63517805851fddb3cf3a5129edc7e2caa141878b33a28.json@@ -1,6 +1,6 @@{"db_name": "PostgreSQL",- "query": "\n SELECT\n COUNT(id) as count\n FROM\n in_progress_user_link\n WHERE\n macro_user_id = $1\n ",+ "query": "\n SELECT\n COUNT(id) as count\n FROM\n in_progress_user_link\n WHERE\n macro_user_id = $1\n AND created_at > $2\n ","describe": {"columns": [{@@ -11,12 +11,13 @@],"parameters": {"Left": [- "Uuid"+ "Uuid",+ "Timestamp"]},"nullable": [null]},- "hash": "ad0e23af0a67a769bf40b3ab388c21c001e01d1691861d8b9df950b03bdb2f4b"+ "hash": "d05f5cfa9fc6c226d8b63517805851fddb3cf3a5129edc7e2caa141878b33a28"}diff --git a/rust/cloud-storage/authentication_service/src/api/oauth2/github.rs b/rust/cloud-storage/authentication_service/src/api/oauth2/github.rsindex d9fb3bf7d7..f45931d3c6 100644--- a/rust/cloud-storage/authentication_service/src/api/oauth2/github.rs+++ b/rust/cloud-storage/authentication_service/src/api/oauth2/github.rs@@ -1,6 +1,7 @@use anyhow::Context;use axum::{Json,+ http::StatusCode,response::{Html, IntoResponse, Redirect, Response},};use github::domain::{models::GithubError, ports::GithubLinkService};@@ -10,6 +11,7 @@ use tower_cookies::Cookies;use crate::api::{context::ApiContext,+ link::github::REAUTHENTICATION_REQUIRED_MESSAGE,oauth2::{OAuthState, format_redirect_uri,login::{self},@@ -29,10 +31,35 @@ pub enum GithubLinkError {impl IntoResponse for GithubLinkError {fn into_response(self) -> Response {- Json(ErrorResponse {- message: self.to_string().into(),- })- .into_response()+ let (status_code, message): (StatusCode, &str) = match &self {+ GithubLinkError::GithubServiceError(GithubError::AccountAlreadyLinked) => (+ StatusCode::CONFLICT,+ "github account is already linked with another Macro account",+ ),+ GithubLinkError::GithubServiceError(GithubError::NoLinkFound) => {+ (StatusCode::NOT_FOUND, "no github link found")+ }+ GithubLinkError::GithubServiceError(GithubError::ReauthenticationRequired) => (+ StatusCode::PRECONDITION_REQUIRED,+ REAUTHENTICATION_REQUIRED_MESSAGE,+ ),+ GithubLinkError::InternalError(error) => {+ tracing::error!(error=?error, "internal error occurred while linking github account");+ (StatusCode::INTERNAL_SERVER_ERROR, "internal error occurred")+ }+ GithubLinkError::GithubServiceError(error) => {+ tracing::error!(error=?error, "github service error occurred while linking github account");+ (StatusCode::INTERNAL_SERVER_ERROR, "internal error occurred")+ }+ };++ (+ status_code,+ Json(ErrorResponse {+ message: message.into(),+ }),+ )+ .into_response()}}diff --git a/rust/cloud-storage/macro_db_client/src/in_progress_user_link.rs b/rust/cloud-storage/macro_db_client/src/in_progress_user_link.rsindex 753db99b0c..b8044614ce 100644--- a/rust/cloud-storage/macro_db_client/src/in_progress_user_link.rs+++ b/rust/cloud-storage/macro_db_client/src/in_progress_user_link.rs@@ -3,11 +3,17 @@ use uuid::Uuid;#[cfg(test)]mod test;+/// The age after which an in-progress user link is considered expired: it no longer counts+/// toward a user's in-progress link cap, and is eligible for cleanup by+/// [`delete_day_old_in_progress_user_links`].+const IN_PROGRESS_USER_LINK_MAX_AGE: chrono::Duration = chrono::Duration::hours(24);+pub async fn count_existing_in_progress_user_links_for_user(db: &sqlx::Pool<sqlx::Postgres>,macro_user_id: &str,) -> anyhow::Result<i64> {let macro_user_id = macro_uuid::string_to_uuid(macro_user_id)?;+ let cutoff = (chrono::Utc::now() - IN_PROGRESS_USER_LINK_MAX_AGE).naive_utc();let count = sqlx::query!(r#"SELECT@@ -16,8 +22,10 @@ pub async fn count_existing_in_progress_user_links_for_user(in_progress_user_linkWHEREmacro_user_id = $1+ AND created_at > $2"#,- ¯o_user_id+ ¯o_user_id,+ cutoff).map(|row| row.count).fetch_one(db)@@ -70,9 +78,7 @@ wherepub async fn delete_day_old_in_progress_user_links(db: &sqlx::Pool<sqlx::Postgres>,) -> anyhow::Result<()> {- let now = chrono::Utc::now();- let yesterday = now - chrono::Duration::hours(24);- let yesterday = yesterday.naive_utc();+ let yesterday = (chrono::Utc::now() - IN_PROGRESS_USER_LINK_MAX_AGE).naive_utc();sqlx::query!(r#"diff --git a/rust/cloud-storage/macro_db_client/src/in_progress_user_link/test.rs b/rust/cloud-storage/macro_db_client/src/in_progress_user_link/test.rsindex 84e9bde93f..b63a16afce 100644--- a/rust/cloud-storage/macro_db_client/src/in_progress_user_link/test.rs+++ b/rust/cloud-storage/macro_db_client/src/in_progress_user_link/test.rs@@ -34,6 +34,39 @@ async fn set_linked_email_then_get(pool: Pool<Postgres>) -> anyhow::Result<()> {Ok(())}+#[sqlx::test]+async fn count_excludes_expired_links(pool: Pool<Postgres>) -> anyhow::Result<()> {+ let macro_user_id = macro_uuid::generate_uuid_v7();+ insert_macro_user(&pool, macro_user_id).await?;++ // A link created just over 24 hours ago should no longer count toward the cap.+ let expired_link_id = create_in_progress_user_link(&pool, ¯o_user_id.to_string()).await?;+ let stale_created_at = chrono::Utc::now().naive_utc() - chrono::Duration::hours(25);+ sqlx::query!(+ r#"+ UPDATE in_progress_user_link+ SET created_at = $1+ WHERE id = $2+ "#,+ stale_created_at,+ expired_link_id+ )+ .execute(&pool)+ .await?;++ // A freshly created link should still count.+ create_in_progress_user_link(&pool, ¯o_user_id.to_string()).await?;++ let count =+ count_existing_in_progress_user_links_for_user(&pool, ¯o_user_id.to_string()).await?;+ assert_eq!(+ count, 1,+ "in-progress links older than 24 hours should not count toward the cap"+ );++ Ok(())+}+#[sqlx::test]async fn delete_clears_row(pool: Pool<Postgres>) -> anyhow::Result<()> {let macro_user_id = macro_uuid::generate_uuid_v7();