use crate::error::Error;
use reqwest::{Certificate, Identity};
use std::path::PathBuf;
#[derive(Debug)]
pub struct TlsConfig {
ca_path: Option<PathBuf>,
cert_path: Option<PathBuf>,
certs_verification: bool,
}
#[derive(Clone)]
pub(crate) struct ClientTlsConfig {
pub(crate) ca: Option<Certificate>,
pub(crate) identity: Option<Identity>,
pub(crate) certs_verification: bool,
}
impl Default for TlsConfig {
#[inline]
fn default() -> Self {
Self {
ca_path: None,
cert_path: None,
certs_verification: true,
}
}
}
impl TlsConfig {
pub fn with_cert(mut self, cert: impl Into<PathBuf>) -> Self {
self.cert_path = Some(cert.into());
self
}
pub fn with_ca(mut self, path: impl Into<PathBuf>) -> Self {
self.ca_path = Some(path.into());
self
}
pub fn with_certs_verification(mut self, certs_verification: bool) -> Self {
self.certs_verification = certs_verification;
self
}
pub(crate) fn build(self) -> Result<ClientTlsConfig, Error> {
let ca = if let Some(ca_path) = self.ca_path {
let ca = std::fs::read(ca_path)
.map_err(Error::from)
.and_then(|b| Certificate::from_pem(&b).map_err(Into::into))?;
Some(ca)
} else {
None
};
let identity = if let Some(cert_path) = self.cert_path {
let identity = std::fs::read(cert_path)
.map_err(Error::from)
.and_then(|b| Identity::from_pem(&b).map_err(Into::into))?;
Some(identity)
} else {
None
};
Ok(ClientTlsConfig {
ca,
identity,
certs_verification: self.certs_verification,
})
}
}