mithril_client/certificate_client/
api.rsuse async_trait::async_trait;
use mithril_common::logging::LoggerExtensions;
use std::sync::Arc;
use crate::aggregator_client::AggregatorClient;
use crate::certificate_client::fetch::InternalCertificateRetriever;
use crate::certificate_client::{fetch, verify};
use crate::{MithrilCertificate, MithrilCertificateListItem, MithrilResult};
pub struct CertificateClient {
pub(super) aggregator_client: Arc<dyn AggregatorClient>,
pub(super) retriever: Arc<InternalCertificateRetriever>,
pub(super) verifier: Arc<dyn CertificateVerifier>,
}
impl CertificateClient {
pub fn new(
aggregator_client: Arc<dyn AggregatorClient>,
verifier: Arc<dyn CertificateVerifier>,
logger: slog::Logger,
) -> Self {
let logger = logger.new_with_component_name::<Self>();
let retriever = Arc::new(InternalCertificateRetriever::new(
aggregator_client.clone(),
logger,
));
Self {
aggregator_client,
retriever,
verifier,
}
}
pub async fn list(&self) -> MithrilResult<Vec<MithrilCertificateListItem>> {
fetch::list(self).await
}
pub async fn get(&self, certificate_hash: &str) -> MithrilResult<Option<MithrilCertificate>> {
fetch::get(self, certificate_hash).await
}
pub async fn verify_chain(&self, certificate_hash: &str) -> MithrilResult<MithrilCertificate> {
verify::verify_chain(self, certificate_hash).await
}
}
#[cfg_attr(test, mockall::automock)]
#[cfg_attr(target_family = "wasm", async_trait(?Send))]
#[cfg_attr(not(target_family = "wasm"), async_trait)]
pub trait CertificateVerifier: Sync + Send {
async fn verify_chain(&self, certificate: &MithrilCertificate) -> MithrilResult<()>;
}
#[cfg(feature = "unstable")]
#[cfg_attr(test, mockall::automock)]
#[cfg_attr(target_family = "wasm", async_trait(?Send))]
#[cfg_attr(not(target_family = "wasm"), async_trait)]
pub trait CertificateVerifierCache: Sync + Send {
async fn store_validated_certificate(
&self,
certificate_hash: &str,
previous_certificate_hash: &str,
) -> MithrilResult<()>;
async fn get_previous_hash(&self, certificate_hash: &str) -> MithrilResult<Option<String>>;
async fn reset(&self) -> MithrilResult<()>;
}