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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
use std::{
    collections::HashMap,
    fmt::{self, Debug, Formatter},
    path::PathBuf,
    sync::Arc,
};

use http::Uri;
use parking_lot::RwLock;

use crate::listener::acme::{
    builder::AutoCertBuilder, endpoint::Http01Endpoint, keypair::KeyPair, ChallengeType,
};

/// ACME configuration
pub struct AutoCert {
    pub(crate) directory_url: Uri,
    pub(crate) domains: Vec<String>,
    pub(crate) contacts: Vec<String>,
    pub(crate) key_pair: Arc<KeyPair>,
    pub(crate) challenge_type: ChallengeType,
    pub(crate) keys_for_http01: Option<Arc<RwLock<HashMap<String, String>>>>,
    pub(crate) cache_path: Option<PathBuf>,
    pub(crate) cache_cert: Option<Vec<u8>>,
    pub(crate) cache_key: Option<Vec<u8>>,
}

impl AutoCert {
    /// Create an ACME configuration builder.
    pub fn builder() -> AutoCertBuilder {
        AutoCertBuilder::new()
    }

    /// Create an endpoint for HTTP-01 challenge
    ///
    /// Reference: <https://letsencrypt.org/docs/challenge-types/#http-01-challenge>
    ///
    /// # Panics
    ///
    /// Panic if current challenge type is not [`ChallengeType::Http01`].
    pub fn http_01_endpoint(&self) -> Http01Endpoint {
        if let Some(keys_for_http01) = &self.keys_for_http01 {
            Http01Endpoint {
                keys: keys_for_http01.clone(),
            }
        } else {
            panic!("current challenge type is not `HTTP-01`");
        }
    }
}

impl Debug for AutoCert {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        f.debug_struct("AutoCert")
            .field("directory_url", &self.directory_url)
            .field("domains", &self.domains)
            .field("cache_path", &self.cache_path)
            .finish()
    }
}