2017-02-24 18:29:10 +01:00
|
|
|
//! Provides the SASL "PLAIN" mechanism.
|
|
|
|
|
|
2017-02-25 06:49:13 +01:00
|
|
|
use sasl::{SaslMechanism, SaslCredentials, SaslSecret};
|
2017-02-24 18:29:10 +01:00
|
|
|
|
|
|
|
|
pub struct Plain {
|
2017-02-25 03:43:11 +01:00
|
|
|
username: String,
|
2017-02-24 18:29:10 +01:00
|
|
|
password: String,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Plain {
|
2017-02-25 03:43:11 +01:00
|
|
|
pub fn new<N: Into<String>, P: Into<String>>(username: N, password: P) -> Plain {
|
2017-02-24 18:29:10 +01:00
|
|
|
Plain {
|
2017-02-25 03:43:11 +01:00
|
|
|
username: username.into(),
|
2017-02-24 18:29:10 +01:00
|
|
|
password: password.into(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl SaslMechanism for Plain {
|
2017-02-25 03:43:11 +01:00
|
|
|
fn name(&self) -> &str { "PLAIN" }
|
2017-02-24 18:29:10 +01:00
|
|
|
|
2017-02-25 06:49:13 +01:00
|
|
|
fn from_credentials(credentials: SaslCredentials) -> Result<Plain, String> {
|
|
|
|
|
if let SaslSecret::Password(password) = credentials.secret {
|
|
|
|
|
Ok(Plain::new(credentials.username, password))
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
Err("PLAIN requires a password".to_owned())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-24 23:42:08 +01:00
|
|
|
fn initial(&mut self) -> Result<Vec<u8>, String> {
|
2017-02-24 18:29:10 +01:00
|
|
|
let mut auth = Vec::new();
|
|
|
|
|
auth.push(0);
|
2017-02-25 03:43:11 +01:00
|
|
|
auth.extend(self.username.bytes());
|
2017-02-24 18:29:10 +01:00
|
|
|
auth.push(0);
|
|
|
|
|
auth.extend(self.password.bytes());
|
2017-02-24 23:42:08 +01:00
|
|
|
Ok(auth)
|
2017-02-24 18:29:10 +01:00
|
|
|
}
|
|
|
|
|
}
|