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
use anyhow::Context;
use axum::{
Json,
extract::{Query, State},
http::StatusCode,
response::{IntoResponse, Response},
};
use calendar_events::domain::models::google_calendar_scope_parameter;
use macro_authorization::{AccountHolderOrInternal, MacroAuthorizationExtractor};
use macro_middleware::tracking::ClientIp;
use model::response::ErrorResponse;
use roles_and_permissions::domain::model::PermissionId;
use serde_utils::urlencode::UrlEncoded;
use tracing::Level;
use url::Url;
use crate::{
account_link_state::{AccountLinkState, LinkProvider, sign_account_link_state},
api::{
context::{ApiContext, AuthorizationService},
link::github::REAUTHENTICATION_REQUIRED_MESSAGE,
permissions_extractor::DbPermissionsExtractor,
},
};
#[cfg(test)]
mod test;
const GOOGLE_AUTHORIZATION_URL: &str = "https://accounts.google.com/o/oauth2/v2/auth";
const GMAIL_IDENTITY_PROVIDER_NAME: &str = "google_gmail";
/// The scope that makes a Google account a mailbox Macro can read and send
/// from. A consent that leaves it out cannot provision an inbox, so the
/// callback refuses it (`api::oauth2::google`).
pub(crate) const GMAIL_MAILBOX_SCOPE: &str = "https://www.googleapis.com/auth/gmail.modify";
const GMAIL_SCOPES: [&str; 7] = [
"openid",
"profile",
"email",
GMAIL_MAILBOX_SCOPE,
"https://www.googleapis.com/auth/contacts.readonly",
"https://www.googleapis.com/auth/contacts.other.readonly",
"https://www.googleapis.com/auth/gmail.settings.basic",
];
/// The identity scopes every consent needs: the callback resolves the account
/// that consented from the `sub` and `email` claims on Google's id_token, and
/// Google only mints one when `openid` is requested.
const IDENTITY_SCOPES: &str = "openid email";
/// Which capabilities a consent request covers. Calendar surfaces ask for
/// [`ConsentScopes::Calendar`] when the mailbox is already connected, so the
/// consent screen lists the calendar permissions alone; Google's incremental
/// authorization carries the mailbox grant forward untouched.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, serde::Deserialize)]
#[serde(rename_all = "snake_case")]
pub(crate) enum ConsentScopes {
/// Connecting or reconnecting a mailbox.
#[default]
Gmail,
/// Connecting a first mailbox from a calendar surface.
GmailAndCalendar,
/// Adding calendar access to a mailbox that is already connected.
Calendar,
}
#[derive(serde::Deserialize, serde::Serialize, Debug, utoipa::ToSchema)]
pub struct InitGmailLinkResponse {
/// The OAuth authorization URL to redirect the user to
pub authorization_url: String,
/// The link ID for tracking the OAuth flow
pub link_id: uuid::Uuid,
}
/// Error type for init Gmail operations
#[derive(thiserror::Error, Debug)]
pub enum InitGmailLinkError {
/// Too many in-progress links
#[error("too many in progress links")]
TooManyInProgressLinks,
/// The user lacks the subscription required to link an additional inbox
#[error("a professional subscription is required to link an additional inbox")]
PaymentRequired,
/// Internal error
#[error("internal error occurred")]
InternalError(#[from] anyhow::Error),
/// The identity provider was not found
#[error("identity provider not found")]
IdentityProviderNotFound,
}
impl IntoResponse for InitGmailLinkError {
fn into_response(self) -> Response {
let message = self.to_string();
let status_code: StatusCode = match &self {
InitGmailLinkError::TooManyInProgressLinks => StatusCode::TOO_MANY_REQUESTS,
InitGmailLinkError::PaymentRequired => StatusCode::PAYMENT_REQUIRED,
InitGmailLinkError::InternalError(_) | InitGmailLinkError::IdentityProviderNotFound => {
tracing::error!(error = ?self, "gmail link initiation failed");
StatusCode::INTERNAL_SERVER_ERROR
}
};
(
status_code,
Json(ErrorResponse {
message: message.into(),
}),
)
.into_response()
}
}
#[derive(Debug, serde::Deserialize)]
pub(crate) struct InitGmailLinkQueryParams {
/// Once the frontend is update to NOT 2x urlencode this then this should be changed to
/// `Option<Url>`
original_url: Option<UrlEncoded<Url>>,
#[serde(default)]
scopes: ConsentScopes,
reconnect_link_id: Option<uuid::Uuid>,
}
/// Initiates a Gmail link for a user
#[utoipa::path(
post,
operation_id = "init_gmail_link",
path = "/link/gmail",
params(
("original_url" = String, Query, description = "**OPTIONAL**. The original url to redirect to."),
("reconnect_link_id" = Option<uuid::Uuid>, Query, description = "Existing accessible Gmail inbox being reconnected; does not consume an additional inbox slot."),
("scopes" = Option<String>, Query, description = "**OPTIONAL**. Which capabilities to request consent for: `gmail` (default), `gmail_and_calendar`, or `calendar`. The calendar variants are only honored when the deployment allows calendar scope requests.")
),
responses(
(status = 200, body=InitGmailLinkResponse),
(status = 400, body=ErrorResponse),
(status = 402, body=ErrorResponse),
(status = 429, body=ErrorResponse),
(status = 401, body=ErrorResponse),
(status = 500, body=ErrorResponse),
)
)]
#[tracing::instrument(skip(ctx, ip_context, db_permissions), fields(client_ip=%ip_context, user_id=%db_permissions.authorization.authorization.user.user_context.user_id, fusion_user_id=%db_permissions.authorization.authorization.user.user_context.fusion_user_id), err(level = Level::INFO))]
pub async fn init_gmail_link_handler(
State(ctx): State<ApiContext>,
query: Query<InitGmailLinkQueryParams>,
ip_context: ClientIp,
db_permissions: DbPermissionsExtractor,
) -> Result<Json<InitGmailLinkResponse>, InitGmailLinkError> {
let Query(InitGmailLinkQueryParams {
original_url,
scopes,
reconnect_link_id,
}) = query;
let authorization = &db_permissions.authorization;
let fusion_user_id = &authorization.authorization.user.user_context.fusion_user_id;
let initiator =
macro_uuid::string_to_uuid(fusion_user_id).context("fusion user id must be a uuid")?;
if !ctx
.inbox_connections
.permits_connection(
authorization.authorization.user.macro_user_id.clone(),
db_permissions
.permissions
.contains(&PermissionId::ReadProfessionalFeatures.to_string()),
email::domain::models::UserProvider::Gmail,
reconnect_link_id,
)
.await
.map_err(anyhow::Error::from)?
{
return Err(InitGmailLinkError::PaymentRequired);
}
let count =
macro_db_client::in_progress_user_link::count_existing_in_progress_user_links_for_user(
&ctx.db,
fusion_user_id,
)
.await?;
if count >= 10 {
return Err(InitGmailLinkError::TooManyInProgressLinks);
}
let authorization_scopes = gmail_authorization_scopes(ctx.calendar_scope_enabled, scopes);
let requested_google_scopes: Vec<String> = authorization_scopes
.split_ascii_whitespace()
.map(ToOwned::to_owned)
.collect();
let link_id = macro_db_client::in_progress_user_link::create_in_progress_google_link(
&ctx.db,
fusion_user_id,
&requested_google_scopes,
)
.await?;
let gmail_idp_id = ctx
.auth_client
.get_identity_provider_id_by_name(GMAIL_IDENTITY_PROVIDER_NAME)
.await
.map_err(|_| InitGmailLinkError::IdentityProviderNotFound)?;
// The state is signed and bound to this user, this link, and the Google
// callback: the callback refuses anything else, so the authorization URL
// cannot be rewritten to point another user's consent at this link.
let state = AccountLinkState::new(
LinkProvider::Google,
gmail_idp_id,
link_id,
initiator,
original_url.map(|x| x.0.to_string()),
);
let redirect_uri = crate::api::oauth2::format_redirect_uri(LinkProvider::Google.as_str());
let state_str = sign_account_link_state(&state, &ctx.account_link_state_key)
.context("failed to sign OAuth state")?;
let authorization_url = google_authorization_url(
ctx.auth_client.google_client_id(),
&redirect_uri,
&authorization_scopes,
&state_str,
)?;
Ok(Json(InitGmailLinkResponse {
authorization_url: authorization_url.to_string(),
link_id,
}))
}
/// The calendar scopes must never ride along on plain Gmail connects: they are
/// requested only when the caller explicitly asks, and never when the
/// deployment-level `CALENDAR_SCOPE_ENABLED` kill switch is off. A caller whose
/// calendar request is vetoed by the kill switch falls back to the mailbox
/// consent, which is what it would have gotten before calendar existed.
fn gmail_authorization_scopes(calendar_scope_enabled: bool, scopes: ConsentScopes) -> String {
let calendar = google_calendar_scope_parameter();
let mailbox = GMAIL_SCOPES.join(" ");
match scopes {
ConsentScopes::Calendar if calendar_scope_enabled => {
format!("{IDENTITY_SCOPES} {calendar}")
}
ConsentScopes::GmailAndCalendar if calendar_scope_enabled => {
format!("{mailbox} {calendar}")
}
_ => mailbox,
}
}
fn google_authorization_url(
client_id: &str,
redirect_uri: &str,
scopes: &str,
state: &str,
) -> anyhow::Result<Url> {
let mut authorization_url =
Url::parse(GOOGLE_AUTHORIZATION_URL).context("invalid Google authorization URL")?;
authorization_url
.query_pairs_mut()
.append_pair("client_id", client_id)
.append_pair("redirect_uri", redirect_uri)
.append_pair("response_type", "code")
.append_pair("scope", scopes)
.append_pair("state", state)
.append_pair("access_type", "offline")
.append_pair("include_granted_scopes", "true")
.append_pair("prompt", "consent");
Ok(authorization_url)
}
#[derive(serde::Deserialize, serde::Serialize, Debug, utoipa::ToSchema)]
pub struct GmailLinkStatusResponse {
/// Whether the user must reauthenticate their Gmail link.
pub reauthentication_required: bool,
}
#[derive(thiserror::Error, Debug)]
pub enum GmailLinkStatusError {
#[error("reauthentication required")]
ReauthenticationRequired,
#[error("internal")]
Internal(#[from] anyhow::Error),
}
impl IntoResponse for GmailLinkStatusError {
fn into_response(self) -> Response {
match &self {
GmailLinkStatusError::ReauthenticationRequired => (
StatusCode::PRECONDITION_REQUIRED,
Json(ErrorResponse {
message: REAUTHENTICATION_REQUIRED_MESSAGE.into(),
}),
),
GmailLinkStatusError::Internal(_) => (
StatusCode::INTERNAL_SERVER_ERROR,
Json(ErrorResponse {
message: "internal error occurred".into(),
}),
),
}
.into_response()
}
}
/// Checks whether the authenticated user's gmail link is valid.
#[utoipa::path(
get,
operation_id = "check_gmail_link_status",
path = "/link/gmail/status",
responses(
(status = 200, body=GmailLinkStatusResponse),
(status = 401, body=ErrorResponse),
(status = 404, body=ErrorResponse),
(status = 428, body=ErrorResponse),
(status = 500, body=ErrorResponse),
)
)]
#[tracing::instrument(skip(ctx, ip_context, authorization), fields(client_ip=%ip_context, user_id=%authorization.authorization.user.macro_user_id), err)]
pub async fn check_gmail_link_status_handler(
State(ctx): State<ApiContext>,
ip_context: ClientIp,
authorization: MacroAuthorizationExtractor<AuthorizationService, AccountHolderOrInternal>,
) -> Result<Json<GmailLinkStatusResponse>, GmailLinkStatusError> {
// Check if the user has an email link in db
if macro_db_client::email::check_user_email_link(
&ctx.db,
&authorization.authorization.user.macro_user_id,
)
.await
.map_err(GmailLinkStatusError::Internal)?
{
let links = ctx
.auth_client
.get_links(
&authorization.authorization.user.user_context.fusion_user_id,
None,
)
.await
.map_err(|e| GmailLinkStatusError::Internal(e.into()))?;
let result = links
.iter()
.filter_map(|l| {
if l.identity_provider_name.eq("google_gmail") {
Some(true)
} else {
None
}
})
.collect::<Vec<bool>>();
// If no, return 428
if result.is_empty() {
return Err(GmailLinkStatusError::ReauthenticationRequired);
}
}
Ok(Json(GmailLinkStatusResponse {
reauthentication_required: false,
}))
}