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.

5.5 KB
Raw Blame
//! Entity access extractor for entities identified in a JSON request body.

#[cfg(test)]
mod test;

use std::{marker::PhantomData, sync::Arc};

use axum::{
    Json, RequestExt,
    extract::{FromRef, FromRequest, Request},
};
use macro_authorization::{
    AnyPrincipal, MacroAuthorization, MacroAuthorizationService, MacroAuthorizationState,
    OptionalMacroAuthorizationExtractor,
};
use serde::{Deserialize, de::DeserializeOwned};
use tracing::Level;

use super::{ExtractorError, RequiredPermission, bot::generate_bot_entity_access_receipt};
use crate::domain::{
    models::{Entity, EntityAccessAuth, EntityAccessReceipt, EntityPermission, EntityType},
    ports::EntityAccessService,
};

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct EntityBodyFields {
    entity_type: EntityType,
    entity_id: String,
}

/// Validates that an authenticated principal has access to an entity identified in the request body.
///
/// Type parameter `T` specifies the required access level, `Svc` is the entity access service,
/// `V` is the typed request body, and `Auth` is the authorization service. This extractor consumes
/// the request body and must therefore be the final body-consuming extractor in a handler.
#[derive(Debug)]
pub struct EntityBodyAccessLevelExtractor<T: RequiredPermission, Svc, V, Auth> {
    /// The receipt proving access to the entity from the request body.
    pub entity_access_receipt: EntityAccessReceipt<T>,
    /// The deserialized request body.
    pub inner: V,
    _marker: PhantomData<(T, Svc, Auth)>,
}

impl<T, S, Svc, V, Auth> FromRequest<S> for EntityBodyAccessLevelExtractor<T, Svc, V, Auth>
where
    T: RequiredPermission,
    Arc<Svc>: FromRef<S>,
    Svc: EntityAccessService,
    MacroAuthorizationState<Auth>: FromRef<S>,
    Auth: MacroAuthorizationService,
    V: DeserializeOwned + Send,
    S: Send + Sync + 'static,
{
    type Rejection = ExtractorError;

    #[tracing::instrument(err(level = Level::INFO), skip(req, state))]
    async fn from_request(mut req: Request, state: &S) -> Result<Self, Self::Rejection> {
        let authorization = req
            .extract_parts_with_state::<OptionalMacroAuthorizationExtractor<Auth, AnyPrincipal>, _>(
                state,
            )
            .await
            .map_err(ExtractorError::from)?
            .authorization
            .ok_or(ExtractorError::Unauthorized)?;

        if matches!(authorization, MacroAuthorization::Internal(None)) {
            return Err(ExtractorError::Unauthorized);
        }

        let Json(json): Json<serde_json::Value> = req
            .extract()
            .await
            .map_err(|_| ExtractorError::BadRequest("Invalid JSON body"))?;
        let EntityBodyFields {
            entity_type,
            entity_id,
        } = serde_json::from_value(json.clone())
            .map_err(|_| ExtractorError::BadRequest("Invalid entity body"))?;

        let service = Arc::<Svc>::from_ref(state);
        if let MacroAuthorization::Bot(authentication) = &authorization {
            let inner = deserialize_body(json)?;
            let entity_access_receipt = generate_bot_entity_access_receipt::<T>(
                service.as_ref(),
                authentication,
                &entity_id,
                entity_type,
            )
            .await?;

            return Ok(Self {
                entity_access_receipt,
                inner,
                _marker: PhantomData,
            });
        }

        let (user, user_org_id) = match authorization {
            MacroAuthorization::User(user) | MacroAuthorization::Internal(Some(user)) => {
                let org_id = user.user_context.organization_id.map(i64::from);
                (user.macro_user_id, org_id)
            }
            MacroAuthorization::Harness(_) => return Err(ExtractorError::Unauthorized),
            MacroAuthorization::Bot(_) | MacroAuthorization::Internal(None) => {
                unreachable!("bot and identity-less internal access returned above")
            }
        };
        // Channels require `get_entity_permission` with user_org_id for proper
        // access checks on public/organization/team channels. Other entity
        // types use the simpler `get_access_level`.
        let entity_permission = if entity_type == EntityType::Channel {
            service
                .get_entity_permission(Some(&user), &entity_id, entity_type, user_org_id)
                .await
                .map_err(ExtractorError::from)?
        } else {
            let access_level = service
                .get_access_level(Some(&user), &entity_id, entity_type)
                .await
                .map_err(ExtractorError::from)?
                .ok_or(ExtractorError::Unauthorized)?;
            EntityPermission::AccessLevel { access_level }
        };
        if !entity_permission.satisfies::<T>() {
            return Err(ExtractorError::Unauthorized);
        }

        Ok(Self {
            entity_access_receipt: EntityAccessReceipt {
                auth: EntityAccessAuth::Authenticated(user),
                entity: Entity {
                    entity_id,
                    entity_type,
                },
                entity_permission,
                _marker: PhantomData,
            },
            inner: deserialize_body(json)?,
            _marker: PhantomData,
        })
    }
}

fn deserialize_body<V: DeserializeOwned>(json: serde_json::Value) -> Result<V, ExtractorError> {
    serde_json::from_value(json).map_err(|_| ExtractorError::BadRequest("Invalid request body"))
}