2017-02-27 16:08:09 +01:00
|
|
|
//! Provides the SASL "PLAIN" mechanism.
|
|
|
|
|
|
2020-05-15 13:48:27 +02:00
|
|
|
use crate::client::{Mechanism, MechanismError};
|
2019-01-17 22:54:32 +01:00
|
|
|
use crate::common::{Credentials, Identity, Password, Secret};
|
2017-02-27 16:08:09 +01:00
|
|
|
|
2017-02-28 13:05:17 +01:00
|
|
|
/// A struct for the SASL PLAIN mechanism.
|
2017-02-27 16:08:09 +01:00
|
|
|
pub struct Plain {
|
|
|
|
|
username: String,
|
|
|
|
|
password: String,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Plain {
|
2017-02-28 13:05:17 +01:00
|
|
|
/// Constructs a new struct for authenticating using the SASL PLAIN mechanism.
|
|
|
|
|
///
|
2017-03-07 17:02:57 +01:00
|
|
|
/// It is recommended that instead you use a `Credentials` struct and turn it into the
|
2017-02-28 13:05:17 +01:00
|
|
|
/// requested mechanism using `from_credentials`.
|
2017-02-27 16:08:09 +01:00
|
|
|
pub fn new<N: Into<String>, P: Into<String>>(username: N, password: P) -> Plain {
|
|
|
|
|
Plain {
|
|
|
|
|
username: username.into(),
|
|
|
|
|
password: password.into(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-07 17:02:57 +01:00
|
|
|
impl Mechanism for Plain {
|
2017-02-27 16:08:09 +01:00
|
|
|
fn name(&self) -> &str {
|
|
|
|
|
"PLAIN"
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-15 13:48:27 +02:00
|
|
|
fn from_credentials(credentials: Credentials) -> Result<Plain, MechanismError> {
|
2017-03-16 20:04:22 +01:00
|
|
|
if let Secret::Password(Password::Plain(password)) = credentials.secret {
|
|
|
|
|
if let Identity::Username(username) = credentials.identity {
|
2017-03-07 15:02:38 +01:00
|
|
|
Ok(Plain::new(username, password))
|
|
|
|
|
} else {
|
2020-05-15 13:48:27 +02:00
|
|
|
Err(MechanismError::PlainRequiresUsername)
|
2017-03-07 15:02:38 +01:00
|
|
|
}
|
2017-02-27 16:08:09 +01:00
|
|
|
} else {
|
2020-05-15 13:48:27 +02:00
|
|
|
Err(MechanismError::PlainRequiresPlaintextPassword)
|
2017-02-27 16:08:09 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-25 23:31:21 +01:00
|
|
|
fn initial(&mut self) -> Vec<u8> {
|
2017-02-27 16:08:09 +01:00
|
|
|
let mut auth = Vec::new();
|
|
|
|
|
auth.push(0);
|
|
|
|
|
auth.extend(self.username.bytes());
|
|
|
|
|
auth.push(0);
|
|
|
|
|
auth.extend(self.password.bytes());
|
2020-02-25 23:31:21 +01:00
|
|
|
auth
|
2017-02-27 16:08:09 +01:00
|
|
|
}
|
|
|
|
|
}
|