some work towards proper SASL support
This commit is contained in:
parent
226c1ced97
commit
2722e1ebf3
3 changed files with 66 additions and 5 deletions
|
@ -5,6 +5,8 @@ use ns;
|
||||||
use plugin::{Plugin, PluginProxyBinding};
|
use plugin::{Plugin, PluginProxyBinding};
|
||||||
use event::AbstractEvent;
|
use event::AbstractEvent;
|
||||||
use connection::{Connection, C2S};
|
use connection::{Connection, C2S};
|
||||||
|
use sasl::SaslMechanism;
|
||||||
|
use sasl::mechanisms::Plain as SaslPlain;
|
||||||
|
|
||||||
use base64;
|
use base64;
|
||||||
|
|
||||||
|
@ -148,11 +150,9 @@ impl Client {
|
||||||
self.transport.write_element(&elem)?;
|
self.transport.write_element(&elem)?;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
let mut auth = Vec::new();
|
let name = self.jid.node.clone().expect("JID has no node");
|
||||||
auth.push(0);
|
let mut plain = SaslPlain::new(name, password.to_owned());
|
||||||
auth.extend(self.jid.node.as_ref().expect("JID has no node").bytes());
|
let auth = plain.initial();
|
||||||
auth.push(0);
|
|
||||||
auth.extend(password.bytes());
|
|
||||||
let elem = Element::builder("auth")
|
let elem = Element::builder("auth")
|
||||||
.text(base64::encode(&auth))
|
.text(base64::encode(&auth))
|
||||||
.ns(ns::SASL)
|
.ns(ns::SASL)
|
||||||
|
|
|
@ -12,5 +12,6 @@ pub mod plugin;
|
||||||
pub mod event;
|
pub mod event;
|
||||||
pub mod plugins;
|
pub mod plugins;
|
||||||
pub mod connection;
|
pub mod connection;
|
||||||
|
pub mod sasl;
|
||||||
|
|
||||||
mod locked_io;
|
mod locked_io;
|
||||||
|
|
60
src/sasl.rs
Normal file
60
src/sasl.rs
Normal file
|
@ -0,0 +1,60 @@
|
||||||
|
//! Provides the `SaslMechanism` trait and some implementations.
|
||||||
|
|
||||||
|
pub trait SaslMechanism {
|
||||||
|
/// The name of the mechanism.
|
||||||
|
fn name() -> &'static str;
|
||||||
|
|
||||||
|
/// Provides initial payload of the SASL mechanism.
|
||||||
|
fn initial(&mut self) -> Vec<u8> {
|
||||||
|
Vec::new()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Creates a response to the SASL challenge.
|
||||||
|
fn respond(&mut self, _challenge: &[u8]) -> Vec<u8> {
|
||||||
|
Vec::new()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A few SASL mechanisms.
|
||||||
|
pub mod mechanisms {
|
||||||
|
use super::SaslMechanism;
|
||||||
|
|
||||||
|
pub struct Anonymous;
|
||||||
|
|
||||||
|
impl Anonymous {
|
||||||
|
pub fn new() -> Anonymous {
|
||||||
|
Anonymous
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl SaslMechanism for Anonymous {
|
||||||
|
fn name() -> &'static str { "ANONYMOUS" }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Plain {
|
||||||
|
name: String,
|
||||||
|
password: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Plain {
|
||||||
|
pub fn new<N: Into<String>, P: Into<String>>(name: N, password: P) -> Plain {
|
||||||
|
Plain {
|
||||||
|
name: name.into(),
|
||||||
|
password: password.into(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl SaslMechanism for Plain {
|
||||||
|
fn name() -> &'static str { "PLAIN" }
|
||||||
|
|
||||||
|
fn initial(&mut self) -> Vec<u8> {
|
||||||
|
let mut auth = Vec::new();
|
||||||
|
auth.push(0);
|
||||||
|
auth.extend(self.name.bytes());
|
||||||
|
auth.push(0);
|
||||||
|
auth.extend(self.password.bytes());
|
||||||
|
auth
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue