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
133
134
135
136
137
138
139
140
141
142
143
144
#![deny(missing_docs)]
extern crate openssl;
extern crate base64;
mod error;
pub use error::Error;
#[derive(Clone, Debug)]
pub struct Credentials {
pub username: Option<String>,
pub secret: Secret,
pub channel_binding: ChannelBinding,
}
impl Default for Credentials {
fn default() -> Credentials {
Credentials {
username: None,
secret: Secret::None,
channel_binding: ChannelBinding::None,
}
}
}
impl Credentials {
pub fn with_username<N: Into<String>>(mut self, username: N) -> Credentials {
self.username = Some(username.into());
self
}
pub fn with_password<P: Into<String>>(mut self, password: P) -> Credentials {
self.secret = Secret::Password(password.into());
self
}
pub fn with_channel_binding(mut self, channel_binding: ChannelBinding) -> Credentials {
self.channel_binding = channel_binding;
self
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum ChannelBinding {
None,
Unsupported,
TlsUnique(Vec<u8>),
}
impl ChannelBinding {
pub fn header(&self) -> &[u8] {
match *self {
ChannelBinding::None => b"n,,",
ChannelBinding::Unsupported => b"y,,",
ChannelBinding::TlsUnique(_) => b"p=tls-unique,,",
}
}
pub fn data(&self) -> &[u8] {
match *self {
ChannelBinding::None => &[],
ChannelBinding::Unsupported => &[],
ChannelBinding::TlsUnique(ref data) => data,
}
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Secret {
None,
Password(String),
}
pub trait Mechanism {
fn name(&self) -> &str;
fn from_credentials(credentials: Credentials) -> Result<Self, String> where Self: Sized;
fn initial(&mut self) -> Result<Vec<u8>, String> {
Ok(Vec::new())
}
fn response(&mut self, _challenge: &[u8]) -> Result<Vec<u8>, String> {
Ok(Vec::new())
}
fn success(&mut self, _data: &[u8]) -> Result<(), String> {
Ok(())
}
}
pub mod mechanisms;