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

50 lines
1.4 KiB
Rust
Raw Normal View History

2017-02-27 16:08:09 +01:00
//! Provides the SASL "PLAIN" mechanism.
use client::Mechanism;
use 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, String> {
if let Secret::Password(Password::Plain(password)) = credentials.secret {
if let Identity::Username(username) = credentials.identity {
Ok(Plain::new(username, password))
} else {
Err("PLAIN requires a username".to_owned())
}
2017-02-27 16:08:09 +01:00
} else {
Err("PLAIN requires a plaintext password".to_owned())
2017-02-27 16:08:09 +01:00
}
}
fn initial(&mut self) -> Result<Vec<u8>, String> {
let mut auth = Vec::new();
auth.push(0);
auth.extend(self.username.bytes());
auth.push(0);
auth.extend(self.password.bytes());
Ok(auth)
}
}