use affinidi_messaging_didcomm_service::{DIDCommServiceError, HandlerContext, TspHandler};
use tracing::{info, warn};
use crate::messaging::auth::auth_from_did;
use crate::server::AppState;
pub async fn dispatch_one(app_state: &AppState, payload: &[u8], sender_vid: &str) {
match auth_from_did(sender_vid, &app_state.acl_ks).await {
Ok(auth) => {
let outcome =
crate::trust_tasks::dispatch_trust_task_core(app_state, &auth, payload).await;
info!(
sender = %sender_vid,
status = %outcome.status,
"TSP trust-task dispatched"
);
}
Err(e) => {
warn!(
sender = %sender_vid,
error = %e,
"TSP message from unauthorized sender — dropped"
);
}
}
}
pub struct VtaTspHandler {
app_state: AppState,
}
impl VtaTspHandler {
pub fn new(app_state: AppState) -> Self {
Self { app_state }
}
}
#[async_trait::async_trait]
impl TspHandler for VtaTspHandler {
async fn handle(
&self,
_ctx: HandlerContext,
payload: Vec<u8>,
sender_vid: String,
) -> Result<(), DIDCommServiceError> {
dispatch_one(&self.app_state, &payload, &sender_vid).await;
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::acl::{AclEntry, Role, store_acl_entry};
use crate::test_support::build_signing_test_app_state;
#[tokio::test]
async fn dispatch_one_unknown_sender_drops_without_panic() {
let (app_state, _dir) = build_signing_test_app_state().await;
dispatch_one(&app_state, b"{}", "did:key:zUnauthorizedTspSender").await;
}
#[tokio::test]
async fn dispatch_one_authorized_sender_reaches_spine() {
let (app_state, _dir) = build_signing_test_app_state().await;
let did = "did:key:zAuthorizedTspSender";
store_acl_entry(&app_state.acl_ks, &AclEntry::new(did, Role::Admin, "test"))
.await
.unwrap();
dispatch_one(&app_state, b"{}", did).await;
}
}