1
use bonsaidb_core::{
2
    define_basic_mapped_view, define_basic_unique_mapped_view,
3
    document::{CollectionDocument, Emit, KeyId},
4
    schema::{Collection, NamedCollection, Schema},
5
};
6
use fabruic::{CertificateChain, PrivateKey};
7
use serde::{de::Visitor, Deserialize, Serialize};
8

            
9
4360
#[derive(Debug, Schema)]
10
#[schema(name = "hosted", authority = "khonsulabs", collections = [TlsCertificate], core = bonsaidb_core)]
11
#[cfg_attr(feature = "acme", schema(collections = [crate::server::acme::AcmeAccount]))]
12
pub struct Hosted;
13

            
14
19831
#[derive(Clone, Debug, Serialize, Deserialize, Collection)]
15
#[collection(name = "tls-certificates", authority = "khonsulabs", views = [TlsCertificatesByDomain, TlsCertificateByAllDomains])]
16
#[collection(encryption_key = Some(KeyId::Master), encryption_optional, core = bonsaidb_core)]
17
pub struct TlsCertificate {
18
    pub domains: Vec<String>,
19
    pub private_key: SerializablePrivateKey,
20
    pub certificate_chain: CertificateChain,
21
}
22

            
23
impl NamedCollection for TlsCertificate {
24
    type ByNameView = TlsCertificateByAllDomains;
25
}
26

            
27
define_basic_unique_mapped_view!(
28
    TlsCertificateByAllDomains,
29
    TlsCertificate,
30
    1,
31
    "by-all-domains",
32
    String,
33
806
    |document: CollectionDocument<TlsCertificate>| {
34
806
        document
35
806
            .header
36
806
            .emit_key(document.contents.domains.join(";"))
37
806
    }
38
);
39

            
40
define_basic_mapped_view!(
41
    TlsCertificatesByDomain,
42
    TlsCertificate,
43
    1,
44
    "by-domain",
45
    String,
46
    CertificateChain,
47
824
    |document: CollectionDocument<TlsCertificate>| {
48
824
        document
49
824
            .contents
50
824
            .domains
51
824
            .into_iter()
52
824
            .map(|domain| {
53
824
                document
54
824
                    .header
55
824
                    .emit_key_and_value(domain, document.contents.certificate_chain.clone())
56
824
            })
57
824
            .collect()
58
824
    }
59
);
60

            
61
#[derive(Clone, Debug)]
62
pub struct SerializablePrivateKey(pub PrivateKey);
63

            
64
impl Serialize for SerializablePrivateKey {
65
74
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
66
74
    where
67
74
        S: serde::Serializer,
68
74
    {
69
74
        fabruic::dangerous::PrivateKey::serialize(&self.0, serializer)
70
74
    }
71
}
72

            
73
impl<'de> Deserialize<'de> for SerializablePrivateKey {
74
2833
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
75
2833
    where
76
2833
        D: serde::Deserializer<'de>,
77
2833
    {
78
2833
        PrivateKey::deserialize(deserializer).map(Self)
79
2833
    }
80
}
81

            
82
struct SerializablePrivateKeyVisitor;
83

            
84
impl<'de> Visitor<'de> for SerializablePrivateKeyVisitor {
85
    type Value = SerializablePrivateKey;
86

            
87
    fn expecting(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
88
        formatter.write_str("byte array")
89
    }
90

            
91
    fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E>
92
    where
93
        E: serde::de::Error,
94
    {
95
        Ok(SerializablePrivateKey(PrivateKey::unchecked_from_der(
96
            v.to_vec(),
97
        )))
98
    }
99
}