| 82 | |
| 83 | impl Client { |
| 84 | pub fn new(c: ClientConfig) -> Result<Self> { |
| 85 | let mut headers = HeaderMap::new(); |
| 86 | headers.insert(CONTENT_TYPE, "application/json".parse().unwrap()); |
| 87 | if let Some(auth) = &c.authorization { |
| 88 | headers.insert(AUTHORIZATION, auth.clone().parse()?); |
| 89 | } |
| 90 | |
| 91 | let mut client = reqwest::Client::builder() |
| 92 | .timeout(std::time::Duration::from_secs(5)) |
| 93 | .use_rustls_tls(); // this is important as else the client-certificate does not work! |
| 94 | |
| 95 | if !c.tls_cert.is_empty() && !c.tls_key.is_empty() { |
| 96 | trace!(tls_cert = %c.tls_cert, tls_key = %c.tls_key, "Reading client certificate"); |
| 97 | |
| 98 | let mut b: Vec<u8> = Vec::new(); |
| 99 | File::open(&c.tls_key) |
| 100 | .context("Open tls_key")? |
| 101 | .read_to_end(&mut b) |
| 102 | .context("Read tls_key")?; |
| 103 | File::open(&c.tls_cert) |
| 104 | .context("Open tls_cert")? |
| 105 | .read_to_end(&mut b) |
| 106 | .context("Read tls_cert")?; |
| 107 | |
| 108 | trace!("Parsing client certificate"); |
| 109 | let id = Identity::from_pem(&b).context("Parse tls_cert and tls_key")?; |
| 110 | |
| 111 | trace!("Adding client certificate as identity"); |
| 112 | client = client.identity(id); |
| 113 | } else { |
| 114 | trace!("No client certificate configured"); |
| 115 | } |
| 116 | |
| 117 | if !c.ca_cert.is_empty() { |
| 118 | trace!(ca_cert = %c.ca_cert, "Reading CA certificate"); |
| 119 | let mut b: Vec<u8> = Vec::new(); |
| 120 | File::open(&c.ca_cert) |
| 121 | .context("Open ca_cert")? |
| 122 | .read_to_end(&mut b) |
| 123 | .context("Read ca_cert")?; |
| 124 | |
| 125 | trace!("Parsing CA certificate"); |
| 126 | let cert = Certificate::from_pem(&b).context("Parse ca_cert")?; |
| 127 | |
| 128 | trace!("Adding CA certificate to root certificate bundle"); |
| 129 | client = client.add_root_certificate(cert); |
| 130 | } else { |
| 131 | trace!("No CA certificate configured"); |
| 132 | } |
| 133 | |
| 134 | Ok(Client { |
| 135 | config: c, |
| 136 | client: client.build()?, |
| 137 | headers, |
| 138 | }) |
| 139 | } |
| 140 | |
| 141 | pub fn get_sender_id(&self) -> Vec<u8> { |