#![doc = include_str!("../README.md")]
mod third;
mod util;
use std::{fs::File, path::Path};
use chrono::{DateTime, Utc};
use rpgpie::certificate::{Certificate, Checked};
use wot_network::Network;
use crate::{
third::{ThirdPartyLookup, handle_dks_third_party, handle_userid_third_party},
util::{to_certificate, to_identity, user_id_valid},
};
pub fn from_file(path: &Path, reference_time: DateTime<Utc>) -> Result<Network, rpgpie::Error> {
let mut file = File::open(path)?;
let certs = Certificate::load(&mut file)?;
log::debug!(
"Loaded {} certificates from source file {:?}",
certs.len(),
path
);
Ok(from_certs(&certs, reference_time))
}
pub fn from_certs(certs: &[Certificate], reference_time: DateTime<Utc>) -> Network {
let ccerts: Vec<Checked> = certs.iter().map(Into::into).collect();
let tplookup = ThirdPartyLookup::new(&ccerts);
let mut net = Network::new();
for checked in &ccerts {
add_cert_to_net(&mut net, &tplookup, checked, &reference_time);
}
net
}
fn add_cert_to_net(
net: &mut Network,
tplookup: &ThirdPartyLookup,
checked: &Checked,
reference_time: &DateTime<Utc>,
) {
if !checked.primary_valid_at(reference_time).unwrap_or(false) {
return;
}
for user in checked.user_ids() {
if user_id_valid(user, reference_time) {
if let Some(id) = to_identity(&user.id) {
let cert = to_certificate(checked);
net.add_binding(cert.clone(), cert, id);
};
}
}
for (user, sigs) in checked.user_id_third_party_certifications() {
handle_userid_third_party(net, tplookup, checked, &user, &sigs, reference_time);
}
handle_dks_third_party(
net,
tplookup,
checked,
&checked.direct_third_party_certifications(),
reference_time,
);
}