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.

2.7 KB
Raw Blame
//! Shared capability minting for authenticated entity-message adapters.

#[cfg(test)]
mod test;

use crate::domain::{
    models::{AccessError, BotAccessScope, EntityAccessReceipt, EntityType, RequiredPermission},
    ports::EntityAccessService,
};
use macro_authorization::MacroAuthorization;

/// Mint a parent capability while preserving bot identity and its verified scope.
/// Identity-less internal services and harness credentials cannot impersonate a message author.
pub async fn principal_entity_access_receipt<T: RequiredPermission>(
    service: &impl EntityAccessService,
    principal: &MacroAuthorization,
    entity_id: &str,
    entity_type: EntityType,
) -> Result<EntityAccessReceipt<T>, AccessError> {
    match principal {
        MacroAuthorization::Bot(authentication) => {
            let scope = super::bot::map_bot_access_scope(authentication)
                .map_err(|_| AccessError::Unauthorized)?;
            service
                .generate_bot_entity_access_receipt::<T>(
                    authentication.bot_id,
                    scope,
                    entity_id,
                    entity_type,
                )
                .await
        }
        MacroAuthorization::User(user) | MacroAuthorization::Internal(Some(user)) => {
            service
                .generate_entity_access_receipt::<T>(
                    &user.macro_user_id,
                    user.user_context.organization_id.map(i64::from),
                    entity_id,
                    entity_type,
                )
                .await
        }
        MacroAuthorization::Internal(None) | MacroAuthorization::Harness(_) => {
            Err(AccessError::Unauthorized)
        }
    }
}

/// Mint a new message's sender capability, retaining signed agent attribution.
/// Use the caller capability above for reads, edits, deletes, and reactions.
pub async fn message_sender_entity_access_receipt<T: RequiredPermission>(
    service: &impl EntityAccessService,
    principal: &MacroAuthorization,
    entity_id: &str,
    entity_type: EntityType,
) -> Result<EntityAccessReceipt<T>, AccessError> {
    if let MacroAuthorization::User(user) = principal
        && let Some(agent_id) = user.agent_id
    {
        return service
            .generate_bot_entity_access_receipt::<T>(
                agent_id,
                BotAccessScope::User {
                    user_id: user.macro_user_id.clone(),
                    user_org_id: user.user_context.organization_id.map(i64::from),
                },
                entity_id,
                entity_type,
            )
            .await;
    }
    principal_entity_access_receipt(service, principal, entity_id, entity_type).await
}