use adk_core::Result;
use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use crate::domain::{EvidenceReference, ProtocolDescriptor};
use super::mandates::{CartMandate, PaymentMandate};
#[derive(Debug, Clone)]
pub struct CartMandateEnvelope {
pub mandate: CartMandate,
pub raw_bytes: Vec<u8>,
}
#[derive(Debug, Clone)]
pub struct PaymentMandateEnvelope {
pub mandate: PaymentMandate,
pub raw_bytes: Vec<u8>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct VerifiedCartMandate {
pub mandate: CartMandate,
pub evidence_ref: EvidenceReference,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct VerifiedPaymentMandate {
pub mandate: PaymentMandate,
pub evidence_ref: EvidenceReference,
}
#[async_trait]
pub trait MandateVerifier: Send + Sync {
async fn verify_cart_mandate(
&self,
envelope: &CartMandateEnvelope,
) -> Result<VerifiedCartMandate>;
async fn verify_payment_mandate(
&self,
envelope: &PaymentMandateEnvelope,
) -> Result<VerifiedPaymentMandate>;
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct UserAuthorizationEnvelope {
pub kind: String,
pub raw_bytes: Vec<u8>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub source: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct VerifiedUserAuthorization {
pub subject: String,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub scopes: Vec<String>,
pub evidence_ref: EvidenceReference,
}
#[async_trait]
pub trait UserAuthorizationVerifier: Send + Sync {
async fn verify_user_authorization(
&self,
envelope: &UserAuthorizationEnvelope,
) -> Result<VerifiedUserAuthorization>;
}
#[derive(Debug, Clone, Default)]
pub struct NoOpMandateVerifier;
impl NoOpMandateVerifier {
#[must_use]
pub fn new() -> Self {
Self
}
fn make_evidence_ref(mandate_id: &str, artifact_kind: &str) -> EvidenceReference {
EvidenceReference {
evidence_id: format!("noop-{mandate_id}"),
protocol: ProtocolDescriptor::ap2("v0.1-alpha"),
artifact_kind: artifact_kind.to_string(),
digest: None,
}
}
}
#[async_trait]
impl MandateVerifier for NoOpMandateVerifier {
async fn verify_cart_mandate(
&self,
envelope: &CartMandateEnvelope,
) -> Result<VerifiedCartMandate> {
Ok(VerifiedCartMandate {
mandate: envelope.mandate.clone(),
evidence_ref: Self::make_evidence_ref(&envelope.mandate.id, "cart_mandate"),
})
}
async fn verify_payment_mandate(
&self,
envelope: &PaymentMandateEnvelope,
) -> Result<VerifiedPaymentMandate> {
Ok(VerifiedPaymentMandate {
mandate: envelope.mandate.clone(),
evidence_ref: Self::make_evidence_ref(&envelope.mandate.id, "payment_mandate"),
})
}
}