Skip to main content

mail_auth/dkim2/
builder.rs

1/*
2 * SPDX-FileCopyrightText: 2020 Stalwart Labs LLC <hello@stalw.art>
3 *
4 * SPDX-License-Identifier: Apache-2.0 OR MIT
5 */
6
7use super::{Dkim2Signer, Done, Flag, KeyEntry, NeedDomain, NeedSelector};
8use crate::common::crypto::DkimKey;
9
10impl Dkim2Signer<NeedDomain> {
11    pub fn from_key(key: impl Into<DkimKey>) -> Dkim2Signer<NeedDomain> {
12        Dkim2Signer {
13            _state: Default::default(),
14            keys: vec![KeyEntry {
15                key: key.into(),
16                selector: String::new(),
17            }],
18            domain: String::new(),
19            flags: Vec::new(),
20            nonce: None,
21        }
22    }
23
24    /// Sets the domain to use for signing.
25    pub fn domain(self, domain: impl Into<String>) -> Dkim2Signer<NeedSelector> {
26        Dkim2Signer {
27            _state: Default::default(),
28            keys: self.keys,
29            domain: domain.into(),
30            flags: self.flags,
31            nonce: self.nonce,
32        }
33    }
34}
35
36impl Dkim2Signer<NeedSelector> {
37    /// Sets the selector to use for signing.
38    pub fn selector(mut self, selector: impl Into<String>) -> Dkim2Signer<Done> {
39        if let Some(entry) = self.keys.first_mut() {
40            entry.selector = selector.into();
41        }
42        Dkim2Signer {
43            _state: Default::default(),
44            keys: self.keys,
45            domain: self.domain,
46            flags: self.flags,
47            nonce: self.nonce,
48        }
49    }
50}
51
52impl Dkim2Signer<Done> {
53    /// Adds an additional signing key and selector.
54    pub fn additional_key(mut self, key: impl Into<DkimKey>, selector: impl Into<String>) -> Self {
55        self.keys.push(KeyEntry {
56            key: key.into(),
57            selector: selector.into(),
58        });
59        self
60    }
61
62    /// Sets the flags (f= tag) to add to the signature.
63    pub fn flags(mut self, flags: impl IntoIterator<Item = Flag>) -> Self {
64        for flag in flags {
65            if !self.flags.contains(&flag) {
66                self.flags.push(flag);
67            }
68        }
69        self
70    }
71
72    /// Sets the nonce (n= tag) to add to the signature.
73    pub fn nonce(mut self, nonce: impl Into<String>) -> Self {
74        self.nonce = Some(nonce.into());
75        self
76    }
77}