xmpp-rs/sasl/src/client/mechanisms/plain.rs

50 lines
1.5 KiB
Rust
Raw Normal View History

2017-02-27 16:08:09 +01:00
//! Provides the SASL "PLAIN" mechanism.
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
/// A struct for the SASL PLAIN mechanism.
2017-02-27 16:08:09 +01:00
pub struct Plain {
username: String,
password: String,
}
impl Plain {
/// Constructs a new struct for authenticating using the SASL PLAIN mechanism.
///
/// It is recommended that instead you use a `Credentials` struct and turn it into the
/// 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(),
}
}
}
impl Mechanism for Plain {
2017-02-27 16:08:09 +01:00
fn name(&self) -> &str {
"PLAIN"
}
fn from_credentials(credentials: Credentials) -> Result<Plain, MechanismError> {
if let Secret::Password(Password::Plain(password)) = credentials.secret {
if let Identity::Username(username) = credentials.identity {
Ok(Plain::new(username, password))
} else {
Err(MechanismError::PlainRequiresUsername)
}
2017-02-27 16:08:09 +01:00
} else {
Err(MechanismError::PlainRequiresPlaintextPassword)
2017-02-27 16:08:09 +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());
auth
2017-02-27 16:08:09 +01:00
}
}