tap_node/validation/
agent_validator.rs1use super::{MessageValidator, ValidationResult};
4use crate::storage::Storage;
5use async_trait::async_trait;
6use std::sync::Arc;
7use tap_msg::didcomm::PlainMessage;
8use tap_msg::message::TapMessage;
9
10pub struct AgentAuthorizationValidator {
15 storage: Arc<Storage>,
16}
17
18impl AgentAuthorizationValidator {
19 pub fn new(storage: Arc<Storage>) -> Self {
21 Self { storage }
22 }
23
24 fn is_transaction_response(message: &PlainMessage) -> bool {
26 matches!(
28 message.type_.as_str(),
29 "https://tap.rsvp/schema/1.0#Authorize"
30 | "https://tap.rsvp/schema/1.0#Cancel"
31 | "https://tap.rsvp/schema/1.0#Reject"
32 | "https://tap.rsvp/schema/1.0#Settle"
33 | "https://tap.rsvp/schema/1.0#Revert"
34 | "https://tap.rsvp/schema/1.0#AddAgents"
35 | "https://tap.rsvp/schema/1.0#RemoveAgent"
36 | "https://tap.rsvp/schema/1.0#ReplaceAgent"
37 | "https://tap.rsvp/schema/1.0#UpdatePolicies"
38 )
39 }
40
41 async fn get_transaction_id(&self, message: &PlainMessage) -> Option<String> {
43 if let Some(thread_id) = &message.thid {
45 if let Ok(Some(transaction)) =
47 self.storage.get_transaction_by_thread_id(thread_id).await
48 {
49 return Some(transaction.reference_id);
50 }
51 }
52
53 if let Ok(tap_message) = TapMessage::from_plain_message(message) {
55 match tap_message {
56 TapMessage::Authorize(auth) => Some(auth.transaction_id),
57 TapMessage::Cancel(cancel) => Some(cancel.transaction_id),
58 TapMessage::Reject(reject) => Some(reject.transaction_id),
59 TapMessage::Settle(settle) => Some(settle.transaction_id),
60 TapMessage::Revert(revert) => Some(revert.transaction_id),
61 _ => None,
62 }
63 } else {
64 None
65 }
66 }
67}
68
69#[async_trait]
70impl MessageValidator for AgentAuthorizationValidator {
71 async fn validate(&self, message: &PlainMessage) -> ValidationResult {
72 if !Self::is_transaction_response(message) {
74 return ValidationResult::Accept;
75 }
76
77 let transaction_id = match self.get_transaction_id(message).await {
79 Some(id) => id,
80 None => {
81 return ValidationResult::Accept;
84 }
85 };
86
87 match self
89 .storage
90 .is_agent_authorized_for_transaction(&transaction_id, &message.from)
91 .await
92 {
93 Ok(true) => ValidationResult::Accept,
94 Ok(false) => ValidationResult::Reject(format!(
95 "Agent {} is not authorized to respond to transaction {}",
96 message.from, transaction_id
97 )),
98 Err(e) => {
99 ValidationResult::Reject(format!("Unable to verify agent authorization: {}", e))
100 }
101 }
102 }
103}
104
105#[cfg(test)]
106mod tests {
107 use super::*;
108 use tap_msg::message::Authorize;
109 use tempfile::tempdir;
110
111 #[tokio::test]
112 async fn test_non_transaction_response_accepted() {
113 let dir = tempdir().unwrap();
114 let storage = Arc::new(
115 Storage::new(Some(dir.path().join("test.db")))
116 .await
117 .unwrap(),
118 );
119 let validator = AgentAuthorizationValidator::new(storage);
120
121 let message = PlainMessage::new(
123 "test_msg_1".to_string(),
124 "https://tap.rsvp/schema/1.0#Connect".to_string(),
125 serde_json::json!({}),
126 "did:example:sender".to_string(),
127 )
128 .with_recipient("did:example:receiver");
129
130 match validator.validate(&message).await {
131 ValidationResult::Accept => {} ValidationResult::Reject(reason) => panic!("Expected accept, got reject: {}", reason),
133 }
134 }
135
136 #[tokio::test]
137 async fn test_authorize_for_new_transaction_rejected() {
138 let dir = tempdir().unwrap();
139 let storage = Arc::new(
140 Storage::new(Some(dir.path().join("test.db")))
141 .await
142 .unwrap(),
143 );
144 let validator = AgentAuthorizationValidator::new(storage);
145
146 let authorize = Authorize {
149 transaction_id: "new_transaction_123".to_string(),
150 settlement_address: None,
151 expiry: None,
152 };
153
154 let message = PlainMessage::new(
155 "test_msg_2".to_string(),
156 "https://tap.rsvp/schema/1.0#Authorize".to_string(),
157 serde_json::to_value(&authorize).unwrap(),
158 "did:example:sender".to_string(),
159 )
160 .with_recipient("did:example:receiver");
161
162 match validator.validate(&message).await {
163 ValidationResult::Accept => panic!("Expected reject, got accept"),
164 ValidationResult::Reject(reason) => {
165 assert!(reason.contains("not authorized"));
166 }
167 }
168 }
169}