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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
use crate::host_id::HostId;
use crate::host_info::HostInfo;
use signature;
use url::Url;
pub trait Host {
/// Informations about the host.
///
/// This returns the information that is common to all hosts,
/// and is easy to marshal to dump/restore.
fn info(&self) -> &HostInfo;
/// ID of the host.
///
/// Helper method to access directly the ID of the host.
///
/// # Examples
/// ```
/// use confitul::LocalHost;
/// use confitul::Host;
///
/// let local_host = LocalHost::new(None).unwrap();
/// let info = local_host.info();
/// assert_eq!(info.id, local_host.id());
/// ```
fn id(&self) -> HostId {
self.info().id
}
/// Name of the host.
///
/// Helper method to access directly the name of the host.
///
/// # Examples
/// ```
/// use confitul::LocalHost;
/// use confitul::Host;
/// use confitul::LocalHostOptions;
///
/// let local_host = LocalHost::new(Some(LocalHostOptions::new().with_name("my-computer"))).unwrap();
/// assert_eq!("my-computer", local_host.name());
/// ```
fn name(&self) -> &String {
&self.info().name
}
/// Description of the host.
///
/// Helper method to access directly the description of the host.
///
/// # Examples
/// ```
/// use confitul::LocalHost;
/// use confitul::Host;
/// use confitul::LocalHostOptions;
///
/// let local_host = LocalHost::new(Some(LocalHostOptions::new().with_description("a test"))).unwrap();
/// assert_eq!("a test", local_host.description());
/// ```
fn description(&self) -> &String {
&self.info().description
}
/// URLs of the host.
///
/// Helper method to access directly the URLs of the host.
///
/// # Examples
/// ```
/// use confitul::LocalHost;
/// use confitul::Host;
///
/// let local_host = LocalHost::new(None).unwrap();
/// let info = local_host.info();
/// assert_eq!(&info.urls, local_host.urls());
/// ```
fn urls(&self) -> &Vec<Url> {
&self.info().urls
}
/// Signature of the host.
///
/// Helper method to access directly the signature of the host.
///
/// # Examples
/// ```
/// use confitul::LocalHost;
/// use confitul::Host;
///
/// let local_host = LocalHost::new(None).unwrap();
/// let info = local_host.info();
/// assert_eq!(info.sig.as_ref().unwrap(), local_host.sig());
/// ```
fn sig(&self) -> &Vec<u8> {
self.info().sig.as_ref().unwrap()
}
/// Verify a message signature.
///
/// Useful to check incoming messages, check they come from the
/// right host and have been correctly signed.
///
/// # Examples
/// ```
/// use confitul::LocalHost;
/// use confitul::Host;
///
/// let local_host = LocalHost::new(None).unwrap();
/// let msg = "a message".as_bytes();
/// let sig = local_host.sign_msg(msg);
/// assert!(matches!(local_host.verify_msg(msg, &sig), Ok(())));
/// assert!(matches!(local_host.verify_msg(msg, msg), Err(_)));
/// ```
fn verify_msg(&self, msg: &[u8], sig: &[u8]) -> Result<(), signature::Error>;
/// Verify this host signature.
///
/// This can be used when de-serializing content from external
/// sources -> is that host legit or not ?
///
/// # Examples
/// ```
/// use confitul::LocalHost;
/// use confitul::Host;
///
/// let local_host = LocalHost::new(None).unwrap();
/// assert!(matches!(local_host.verify_self(), Ok(())));
/// ```
fn verify_self(&self) -> Result<(), signature::Error> {
self.verify_msg(&self.info().content_to_verify(), &self.sig())
}
}