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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
use crate::channel_bindings::{ChannelBindingCallback, NoChannelBindings};
use crate::config::SASLConfig;
use core::fmt::{Debug, Formatter};
use crate::error::SASLError;
use crate::mechname::Mechname;
use crate::registry::Mechanism;
use crate::session::{Session, Side};
use crate::validate::{NoValidation, Validation};
use crate::alloc::{sync::Arc, vec::Vec};
use crate::typed::Tagged;
#[derive(Debug)]
pub struct SASLClient<CB = NoChannelBindings> {
inner: Sasl<NoValidation, CB>,
}
pub struct SASLServer<V: Validation, CB = NoChannelBindings> {
inner: Sasl<V, CB>,
}
pub(crate) struct Sasl<V: Validation = NoValidation, CB = NoChannelBindings> {
pub(crate) config: Arc<SASLConfig>,
pub(crate) cb: CB,
pub(crate) validation: Tagged<'static, V>,
}
impl<V: Validation + Debug, CB: Debug> Debug for Sasl<V, CB> {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
f.debug_struct("Sasl")
.field("config", &self.config)
.field("cb", &self.cb)
.finish_non_exhaustive()
}
}
#[cfg(any(feature = "provider", test))]
mod provider {
use super::{
Arc, ChannelBindingCallback, Mechanism, Mechname, NoChannelBindings, NoValidation,
SASLClient, SASLConfig, SASLError, SASLServer, Sasl, Session, Side, Tagged, Validation,
Vec,
};
impl SASLClient {
#[must_use]
pub fn new(config: Arc<SASLConfig>) -> Self {
Self {
inner: Sasl::client(config),
}
}
}
impl<CB: ChannelBindingCallback> SASLClient<CB> {
pub fn with_cb(config: Arc<SASLConfig>, cb: CB) -> Self {
Self {
inner: Sasl::with_cb(config, cb),
}
}
pub fn start_suggested(
self,
offered: &[&Mechname],
) -> Result<Session<NoValidation, CB>, SASLError> {
self.inner.client_start_suggested(offered)
}
}
impl<V: Validation> SASLServer<V> {
#[must_use]
pub fn new(config: Arc<SASLConfig>) -> Self {
Self {
inner: Sasl::server(config),
}
}
}
impl<V: Validation, CB: ChannelBindingCallback> SASLServer<V, CB> {
pub fn with_cb(config: Arc<SASLConfig>, cb: CB) -> Self {
Self {
inner: Sasl::with_cb(config, cb),
}
}
pub fn get_available(&self) -> impl IntoIterator<Item = &Mechanism> {
let mut vec: Vec<&Mechanism> = self.inner.get_available().collect();
vec.as_mut_slice()
.sort_unstable_by(|a, b| self.inner.config.sort(a, b));
vec
}
pub fn start_suggested(self, selected: &Mechname) -> Result<Session<V, CB>, SASLError> {
self.inner.server_start_suggested(selected)
}
}
impl Sasl {
fn client(config: Arc<SASLConfig>) -> Self {
Self {
config,
cb: NoChannelBindings,
validation: Tagged(None),
}
}
}
impl<V: Validation> Sasl<V> {
fn server(config: Arc<SASLConfig>) -> Self {
Self {
config,
cb: NoChannelBindings,
validation: Tagged(None),
}
}
}
impl<CB: ChannelBindingCallback, V: Validation> Sasl<V, CB> {
fn with_cb(config: Arc<SASLConfig>, cb: CB) -> Self {
Self {
config,
cb,
validation: Tagged(None),
}
}
fn client_start_suggested(
self,
offered: &[&Mechname],
) -> Result<Session<V, CB>, SASLError> {
let (mechanism, mechanism_desc) = self.config.select_mechanism(offered)?;
let mechanism_desc = *mechanism_desc;
Ok(Session::new(self, Side::Client, mechanism, mechanism_desc))
}
fn server_start_suggested(self, selected: &Mechname) -> Result<Session<V, CB>, SASLError> {
let config = self.config.clone();
let mech = self
.get_available()
.find(|mech| mech.mechanism == selected)
.ok_or(SASLError::NoSharedMechanism)?;
let auth = mech
.server(config.as_ref())
.ok_or(SASLError::NoSharedMechanism)??;
Ok(Session::new(self, Side::Server, auth, *mech))
}
pub fn get_available<'a>(&self) -> impl Iterator<Item = &'a Mechanism> {
self.config.mech_list().filter(|mech| mech.server.is_some())
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_sasl_autoimpls() {
static_assertions::assert_impl_all!(Sasl: Send, Sync);
}
}