mail_auth/dkim/
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::{Canonicalization, DkimSigner, Done, NeedDomain, NeedHeaders, NeedSelector, Signature};
8use crate::common::crypto::{HashAlgorithm, SigningKey};
9
10impl<T: SigningKey> DkimSigner<T> {
11    pub fn from_key(key: T) -> DkimSigner<T, NeedDomain> {
12        DkimSigner {
13            _state: Default::default(),
14            template: Signature {
15                v: 1,
16                a: key.algorithm(),
17                ..Default::default()
18            },
19            key,
20        }
21    }
22}
23
24impl<T: SigningKey> DkimSigner<T, NeedDomain> {
25    /// Sets the domain to use for signing.
26    pub fn domain(mut self, domain: impl Into<String>) -> DkimSigner<T, NeedSelector> {
27        self.template.d = domain.into();
28        DkimSigner {
29            _state: Default::default(),
30            key: self.key,
31            template: self.template,
32        }
33    }
34}
35
36impl<T: SigningKey> DkimSigner<T, NeedSelector> {
37    /// Sets the selector to use for signing.
38    pub fn selector(mut self, selector: impl Into<String>) -> DkimSigner<T, NeedHeaders> {
39        self.template.s = selector.into();
40        DkimSigner {
41            _state: Default::default(),
42            key: self.key,
43            template: self.template,
44        }
45    }
46}
47
48impl<T: SigningKey> DkimSigner<T, NeedHeaders> {
49    /// Sets the headers to sign.
50    pub fn headers(
51        mut self,
52        headers: impl IntoIterator<Item = impl Into<String>>,
53    ) -> DkimSigner<T, Done> {
54        self.template.h = headers.into_iter().map(|h| h.into()).collect();
55        DkimSigner {
56            _state: Default::default(),
57            key: self.key,
58            template: self.template,
59        }
60    }
61}
62
63impl<T: SigningKey> DkimSigner<T, Done> {
64    /// Sets the third party signature.
65    pub fn atps(mut self, atps: impl Into<String>) -> Self {
66        self.template.atps = Some(atps.into());
67        self
68    }
69
70    /// Sets the third-party signature hashing algorithm.
71    pub fn atpsh(mut self, atpsh: HashAlgorithm) -> Self {
72        self.template.atpsh = atpsh.into();
73        self
74    }
75
76    /// Sets the selector to use for signing.
77    pub fn agent_user_identifier(mut self, auid: impl Into<String>) -> Self {
78        self.template.i = auid.into();
79        self
80    }
81
82    /// Sets the number of seconds from now to use for the signature expiration.
83    pub fn expiration(mut self, expiration: u64) -> Self {
84        self.template.x = expiration;
85        self
86    }
87
88    /// Include the body length in the signature.
89    pub fn body_length(mut self, body_length: bool) -> Self {
90        self.template.l = u64::from(body_length);
91        self
92    }
93
94    /// Request reports.
95    pub fn reporting(mut self, reporting: bool) -> Self {
96        self.template.r = reporting;
97        self
98    }
99
100    /// Sets header canonicalization algorithm.
101    pub fn header_canonicalization(mut self, ch: Canonicalization) -> Self {
102        self.template.ch = ch;
103        self
104    }
105
106    /// Sets header canonicalization algorithm.
107    pub fn body_canonicalization(mut self, cb: Canonicalization) -> Self {
108        self.template.cb = cb;
109        self
110    }
111}