use super::{Arc, LinkHost, LysConfig, LysPassAdapter, MachineConfig, PassRefusal};
use crate::link::certificate::{service_key, standing::Standing};
use std::time::Duration;
pub(super) struct PeerTrust {
config: crate::config::peers::PeersConfig,
service_key: [u8; 32],
standing: Arc<Standing>,
}
impl PeerTrust {
pub(super) fn install(self, host: LinkHost) -> Result<LinkHost, PassRefusal> {
host.with_peer_security(self.config, self.service_key)
.map(|host| host.with_standing(self.standing))
.map_err(|_| PassRefusal("peer_configuration_refused"))
}
}
pub(super) fn prepare(
lys: &LysConfig,
machine: &MachineConfig,
adapter: &Arc<LysPassAdapter>,
handle: Option<&tokio::runtime::Handle>,
) -> Result<Option<PeerTrust>, PassRefusal> {
let Some(peers) = machine.carrier_choice.peers.as_ref() else {
return Ok(None);
};
peers
.validate()
.map_err(|_| PassRefusal("peer_configuration_refused"))?;
let base = lys
.api_url
.as_deref()
.ok_or(PassRefusal("certificate_service_key_refused"))?;
let handle = handle.ok_or(PassRefusal("lys_ask_runtime_absent"))?;
let deadline = Duration::from_millis(machine.carrier_choice.crossing_deadline_ms);
let key = service_key::fetch(base, peers.max_key_response_bytes, deadline, handle)
.map_err(|_| PassRefusal("certificate_service_key_refused"))?;
let standing = Standing::new(
base,
Arc::clone(adapter),
handle.clone(),
peers.clone(),
deadline,
)
.map_err(|_| PassRefusal("certificate_status_unavailable"))?;
Ok(Some(PeerTrust {
config: peers.clone(),
service_key: key,
standing,
}))
}