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(search): use MacroAuthorizationExtractor (#5042)

Will Hutchinson02558f940c8c
diff --git a/Cargo.lock b/Cargo.lock
index 9a3f2af2a1..022948e596 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -12517,6 +12517,7 @@ dependencies = [
"indexmap 2.14.0",
"item_filters",
"macro_auth",
+ "macro_authorization",
"macro_aws_config",
"macro_db_client",
"macro_env",
diff --git a/crates/search_service/Cargo.toml b/crates/search_service/Cargo.toml
index 5b639f992d..aec5696f2c 100644
--- a/crates/search_service/Cargo.toml
+++ b/crates/search_service/Cargo.toml
@@ -31,6 +31,7 @@ http-body-util = { workspace = true }
indexmap = { workspace = true }
item_filters = { path = "../item_filters" }
macro_auth = { path = "../macro_auth" }
+macro_authorization = { path = "../macro_authorization" }
macro_db_client = { path = "../macro_db_client" }
models_properties = { path = "../models_properties" }
models_soup = { path = "../models_soup" }
diff --git a/crates/search_service/src/api/context.rs b/crates/search_service/src/api/context.rs
index f8b1b83508..01bb965183 100644
--- a/crates/search_service/src/api/context.rs
+++ b/crates/search_service/src/api/context.rs
@@ -1,6 +1,9 @@
use axum::extract::FromRef;
use entity_access::domain::service::EntityAccessServiceImpl;
use entity_access::outbound::PgAccessRepository;
+use macro_authorization::{
+ MacroAuthJwtValidator, MacroAuthorizationServiceImpl, MacroAuthorizationState,
+};
use opensearch_client::OpensearchClient;
use readonly_pool::ReadOnlyPool;
use std::sync::Arc;
@@ -9,6 +12,9 @@ use std::sync::Arc;
/// type the parent app constructs, so its `Arc` can be passed in directly.
pub type SearchEntityAccessService = EntityAccessServiceImpl<PgAccessRepository>;
+/// Concrete authorization service backing search request authentication.
+pub type SearchAuthorizationService = MacroAuthorizationServiceImpl<MacroAuthJwtValidator>;
+
/// Minimal state required by search handlers.
/// This can be extracted from any state that implements `FromRef<SearchHandlerState>`.
#[derive(Clone, FromRef)]
@@ -19,4 +25,6 @@ pub struct SearchHandlerState {
pub opensearch_client: Arc<OpensearchClient>,
/// Resolves the caller's team membership to mint CRM capability receipts.
pub entity_access_service: Arc<SearchEntityAccessService>,
+ /// Authorization state used to authenticate search requests.
+ pub authorization_state: MacroAuthorizationState<SearchAuthorizationService>,
}
diff --git a/crates/search_service/src/api/search/channel.rs b/crates/search_service/src/api/search/channel.rs
index 39645c8798..eb142c82ee 100644
--- a/crates/search_service/src/api/search/channel.rs
+++ b/crates/search_service/src/api/search/channel.rs
@@ -4,17 +4,17 @@ use chrono::{DateTime, Utc};
use indexmap::IndexMap;
use std::collections::HashMap;
-use crate::api::context::SearchHandlerState;
+use crate::api::context::{SearchAuthorizationService, SearchHandlerState};
use crate::api::search::SearchPaginationParams;
use axum::{
- Extension, Router,
+ Router,
extract::{self, State},
response::Json,
routing::post,
};
use channels::domain::models::ChannelHistoryInfo;
+use macro_authorization::MacroAuthorizationExtractor;
use macro_user_id::user_id::MacroUserId;
-use model::user::UserContext;
use models_search::MatchType;
use models_search::channel::{
ChannelMessageSearchResponseItem, ChannelSearchRequest, ChannelSearchResponse,
@@ -272,11 +272,11 @@ pub fn router() -> Router<SearchHandlerState> {
)]
pub async fn handler(
State(ctx): State<SearchHandlerState>,
- user_context: Extension<UserContext>,
+ authorization: MacroAuthorizationExtractor<SearchAuthorizationService>,
extract::Query(query_params): extract::Query<SearchPaginationParams>,
extract::Json(req): extract::Json<ChannelSearchRequest>,
) -> Result<Json<ChannelSearchResponse>, SearchError> {
- let user_id = user_context.user_id.clone();
+ let user_id = authorization.user_context.user_id.clone();
if user_id.is_empty() {
return Err(SearchError::NoUserId);
}
diff --git a/crates/search_service/src/api/search/simple/simple_unified.rs b/crates/search_service/src/api/search/simple/simple_unified.rs
index fb3c59a8b8..669ed3842e 100644
--- a/crates/search_service/src/api/search/simple/simple_unified.rs
+++ b/crates/search_service/src/api/search/simple/simple_unified.rs
@@ -3,16 +3,16 @@ use crate::api::search::simple::filter::FilterVariantToSearchArgs;
use crate::api::search::crm_company;
use crate::api::search::simple::simple_chat;
use crate::api::{
- context::SearchHandlerState,
+ context::{SearchAuthorizationService, SearchHandlerState},
search::{SearchPaginationParams, simple::SearchError},
};
use axum::{
- Extension,
extract::{self, State},
response::Json,
};
use crm::domain::auth::CrmTeamReceipt;
use entity_access::domain::models::MemberTeamRole;
+use macro_authorization::MacroAuthorizationExtractor;
use macro_user_id::user_id::MacroUserId;
use model::{response::ErrorResponse, user::UserContext};
use models_search::unified::SearchEntityFilters;
@@ -647,10 +647,12 @@ pub(in crate::api::search) async fn perform_unified_search(
)]
pub async fn handler(
State(ctx): State<SearchHandlerState>,
- user_context: Extension<UserContext>,
+ authorization: MacroAuthorizationExtractor<SearchAuthorizationService>,
extract::Query(query_params): extract::Query<SearchPaginationParams>,
extract::Json(req): extract::Json<UnifiedSearchRequest>,
) -> Result<Json<SimpleSearchResponse>, SearchError> {
+ let user_context = &authorization.user_context;
+
tracing::info!(
user_id = user_context.user_id,
query = ?req.query,
@@ -660,7 +662,7 @@ pub async fn handler(
// CRM opt-in is wired through the unified `/search` endpoint only.
let (results, _next_cursor) =
- perform_unified_search(&ctx, &user_context, None, query_params, req).await?;
+ perform_unified_search(&ctx, user_context, None, query_params, req).await?;
let results = results.into_iter().map(|a| a.into()).collect();
diff --git a/crates/search_service/src/api/search/unified.rs b/crates/search_service/src/api/search/unified.rs
index 508f92e9a4..754bab270d 100644
--- a/crates/search_service/src/api/search/unified.rs
+++ b/crates/search_service/src/api/search/unified.rs
@@ -1,6 +1,6 @@
use super::SearchPaginationParams;
use crate::api::{
- context::SearchHandlerState,
+ context::{SearchAuthorizationService, SearchHandlerState},
search::{
crm_company::{enrich_crm_companies, resolve_crm_team_receipt},
enrich::enrich_search_response,
@@ -8,11 +8,11 @@ use crate::api::{
},
};
use axum::{
- Extension,
extract::{self, State},
response::Json,
};
-use model::{response::ErrorResponse, user::UserContext};
+use macro_authorization::MacroAuthorizationExtractor;
+use model::response::ErrorResponse;
use models_search::unified::{
UnifiedSearchRequest, UnifiedSearchResponse, UnifiedSearchResponseItem,
};
@@ -39,10 +39,12 @@ use std::cmp::Ordering;
)]
pub async fn handler(
State(ctx): State<SearchHandlerState>,
- user_context: Extension<UserContext>,
+ authorization: MacroAuthorizationExtractor<SearchAuthorizationService>,
extract::Query(query_params): extract::Query<SearchPaginationParams>,
extract::Json(req): extract::Json<UnifiedSearchRequest>,
) -> Result<Json<UnifiedSearchResponse>, SearchError> {
+ let user_context = &authorization.user_context;
+
tracing::info!(
user_id = user_context.user_id,
query = ?req.query,
@@ -54,7 +56,7 @@ pub async fn handler(
// CRM is opt-in: only when the caller asks for it does this resolve a
// team membership and mint a capability receipt. No membership → empty
// CRM slice, not a failed search. See `resolve_crm_team_receipt`.
- let crm_access = resolve_crm_team_receipt(&ctx, &user_context, req.include_crm).await?;
+ let crm_access = resolve_crm_team_receipt(&ctx, user_context, req.include_crm).await?;
let document_name_term = match req.search_on {
models_search::SearchOn::Name | models_search::SearchOn::NameContent => {
@@ -65,7 +67,7 @@ pub async fn handler(
};
let (results, next_cursor) =
- perform_unified_search(&ctx, &user_context, crm_access.as_ref(), query_params, req).await?;
+ perform_unified_search(&ctx, user_context, crm_access.as_ref(), query_params, req).await?;
// Split the results by entity type
let SplitUnifiedSearchResponseValues {
diff --git a/services/document_storage_service/src/api/context.rs b/services/document_storage_service/src/api/context.rs
index 81678a63aa..f3402e6a51 100644
--- a/services/document_storage_service/src/api/context.rs
+++ b/services/document_storage_service/src/api/context.rs
@@ -475,6 +475,7 @@ impl From<&ApiContext> for SearchHandlerState {
db: ctx.readonly_db.clone(),
opensearch_client: ctx.opensearch_client.clone(),
entity_access_service: ctx.entity_access_service.clone(),
+ authorization_state: ctx.authorization_state.clone(),
}
}
}