Skip to main content

foctet_http/
config.rs

1use foctet_core::BodyEnvelopeLimits;
2
3/// Shared HTTP behavior configuration for high-level opener/sealer helpers.
4#[derive(Clone, Debug, Eq, PartialEq)]
5pub struct HttpConfig {
6    strip_content_type_on_open: bool,
7    set_scope_header_on_seal: bool,
8}
9
10impl Default for HttpConfig {
11    fn default() -> Self {
12        Self {
13            strip_content_type_on_open: true,
14            set_scope_header_on_seal: true,
15        }
16    }
17}
18
19impl HttpConfig {
20    /// Creates a config with default HTTP behavior.
21    pub fn new() -> Self {
22        Self::default()
23    }
24
25    /// Returns whether `Content-Type` is removed after opening.
26    pub fn strip_content_type_on_open(&self) -> bool {
27        self.strip_content_type_on_open
28    }
29
30    /// Returns whether the advisory Foctet scope header is added when sealing.
31    pub fn set_scope_header_on_seal(&self) -> bool {
32        self.set_scope_header_on_seal
33    }
34
35    /// Controls whether `Content-Type` is removed after opening.
36    pub fn with_strip_content_type_on_open(mut self, value: bool) -> Self {
37        self.strip_content_type_on_open = value;
38        self
39    }
40
41    /// Controls whether sealed HTTP messages receive the advisory Foctet scope header.
42    pub fn with_scope_header_on_seal(mut self, value: bool) -> Self {
43        self.set_scope_header_on_seal = value;
44        self
45    }
46}
47
48/// High-level options used to construct an [`crate::HttpSealer`].
49#[derive(Clone, Debug, Eq, PartialEq)]
50pub struct HttpSealOptions {
51    recipient_public_key: [u8; 32],
52    recipient_key_id: Vec<u8>,
53    limits: Option<BodyEnvelopeLimits>,
54}
55
56impl HttpSealOptions {
57    /// Creates sealing options for a recipient public key and key identifier.
58    pub fn new(recipient_public_key: [u8; 32], recipient_key_id: impl AsRef<[u8]>) -> Self {
59        Self {
60            recipient_public_key,
61            recipient_key_id: recipient_key_id.as_ref().to_vec(),
62            limits: None,
63        }
64    }
65
66    /// Applies explicit body envelope limits.
67    pub fn with_limits(mut self, limits: BodyEnvelopeLimits) -> Self {
68        self.limits = Some(limits);
69        self
70    }
71
72    /// Returns the recipient public key.
73    pub fn recipient_public_key(&self) -> [u8; 32] {
74        self.recipient_public_key
75    }
76
77    /// Returns the recipient key identifier.
78    pub fn recipient_key_id(&self) -> &[u8] {
79        &self.recipient_key_id
80    }
81
82    /// Returns explicit sealing limits, if configured.
83    pub fn limits(&self) -> Option<&BodyEnvelopeLimits> {
84        self.limits.as_ref()
85    }
86}
87
88/// High-level options used to construct an [`crate::HttpOpener`].
89#[derive(Clone, Debug, Eq, PartialEq)]
90pub struct HttpOpenOptions {
91    recipient_secret_key: [u8; 32],
92    limits: Option<BodyEnvelopeLimits>,
93}
94
95impl HttpOpenOptions {
96    /// Creates opening options for a recipient secret key.
97    pub fn new(recipient_secret_key: [u8; 32]) -> Self {
98        Self {
99            recipient_secret_key,
100            limits: None,
101        }
102    }
103
104    /// Applies explicit body envelope limits.
105    pub fn with_limits(mut self, limits: BodyEnvelopeLimits) -> Self {
106        self.limits = Some(limits);
107        self
108    }
109
110    /// Returns the recipient secret key.
111    pub fn recipient_secret_key(&self) -> [u8; 32] {
112        self.recipient_secret_key
113    }
114
115    /// Returns explicit opening limits, if configured.
116    pub fn limits(&self) -> Option<&BodyEnvelopeLimits> {
117        self.limits.as_ref()
118    }
119}