authly_client/
identity.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
use crate::Error;

/// Client identitity.
///
/// All authly clients identifies themselves using mutual TLS.
pub struct Identity {
    pub(crate) cert_pem: Vec<u8>,
    pub(crate) key_pem: Vec<u8>,
}

impl Identity {
    /// Load identity from PEM file containing a certificate and private key
    pub fn from_multi_pem(pem: impl AsRef<[u8]>) -> Result<Self, Error> {
        let mut identity_pems = pem::parse_many(pem)
            .map_err(|_| Error::Identity("invalid pem format"))?
            .into_iter();
        let cert = identity_pems
            .next()
            .ok_or_else(|| Error::Identity("pem: missing certificate"))?;
        let key = identity_pems
            .next()
            .ok_or_else(|| Error::Identity("pem: missing private key"))?;

        Ok(Self {
            cert_pem: pem::encode(&cert).into_bytes(),
            key_pem: pem::encode(&key).into_bytes(),
        })
    }
}