clean up naming, add advertising that the client thinks channel binding is unsupported

This commit is contained in:
lumi 2017-03-07 17:02:57 +01:00
parent 9e38e85125
commit ea7479c78a
5 changed files with 50 additions and 46 deletions

View file

@ -1,8 +1,8 @@
[package] [package]
name = "sasl" name = "sasl"
version = "0.2.0" version = "0.3.0"
authors = ["lumi <lumi@pew.im>"] authors = ["lumi <lumi@pew.im>"]
description = "A crate for SASL authentication." description = "A crate for SASL authentication. Currently only does the client side."
homepage = "https://gitlab.com/lumi/sasl-rs" homepage = "https://gitlab.com/lumi/sasl-rs"
repository = "https://gitlab.com/lumi/sasl-rs" repository = "https://gitlab.com/lumi/sasl-rs"
documentation = "https://docs.rs/sasl" documentation = "https://docs.rs/sasl"

View file

@ -5,12 +5,12 @@
//! # Examples //! # Examples
//! //!
//! ```rust //! ```rust
//! use sasl::{SaslCredentials, SaslMechanism, Error}; //! use sasl::{Credentials, Mechanism, Error};
//! use sasl::mechanisms::Plain; //! use sasl::mechanisms::Plain;
//! //!
//! let creds = SaslCredentials::default() //! let creds = Credentials::default()
//! .with_username("user") //! .with_username("user")
//! .with_password("pencil"); //! .with_password("pencil");
//! //!
//! let mut mechanism = Plain::from_credentials(creds).unwrap(); //! let mut mechanism = Plain::from_credentials(creds).unwrap();
//! //!
@ -38,40 +38,40 @@ pub use error::Error;
/// A struct containing SASL credentials. /// A struct containing SASL credentials.
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub struct SaslCredentials { pub struct Credentials {
/// The requested username. /// The requested username.
pub username: Option<String>, pub username: Option<String>,
/// The secret used to authenticate. /// The secret used to authenticate.
pub secret: SaslSecret, pub secret: Secret,
/// Channel binding data, for *-PLUS mechanisms. /// Channel binding data, for *-PLUS mechanisms.
pub channel_binding: ChannelBinding, pub channel_binding: ChannelBinding,
} }
impl Default for SaslCredentials { impl Default for Credentials {
fn default() -> SaslCredentials { fn default() -> Credentials {
SaslCredentials { Credentials {
username: None, username: None,
secret: SaslSecret::None, secret: Secret::None,
channel_binding: ChannelBinding::None, channel_binding: ChannelBinding::None,
} }
} }
} }
impl SaslCredentials { impl Credentials {
/// Creates a new SaslCredentials with the specified username. /// Creates a new Credentials with the specified username.
pub fn with_username<N: Into<String>>(mut self, username: N) -> SaslCredentials { pub fn with_username<N: Into<String>>(mut self, username: N) -> Credentials {
self.username = Some(username.into()); self.username = Some(username.into());
self self
} }
/// Creates a new SaslCredentials with the specified password. /// Creates a new Credentials with the specified password.
pub fn with_password<P: Into<String>>(mut self, password: P) -> SaslCredentials { pub fn with_password<P: Into<String>>(mut self, password: P) -> Credentials {
self.secret = SaslSecret::Password(password.into()); self.secret = Secret::Password(password.into());
self self
} }
/// Creates a new SaslCredentials with the specified chanel binding. /// Creates a new Credentials with the specified chanel binding.
pub fn with_channel_binding(mut self, channel_binding: ChannelBinding) -> SaslCredentials { pub fn with_channel_binding(mut self, channel_binding: ChannelBinding) -> Credentials {
self.channel_binding = channel_binding; self.channel_binding = channel_binding;
self self
} }
@ -82,6 +82,8 @@ impl SaslCredentials {
pub enum ChannelBinding { pub enum ChannelBinding {
/// No channel binding data. /// No channel binding data.
None, None,
/// Advertise that the client does not think the server supports channel binding.
Unsupported,
/// p=tls-unique channel binding data. /// p=tls-unique channel binding data.
TlsUnique(Vec<u8>), TlsUnique(Vec<u8>),
} }
@ -91,6 +93,7 @@ impl ChannelBinding {
pub fn header(&self) -> &[u8] { pub fn header(&self) -> &[u8] {
match *self { match *self {
ChannelBinding::None => b"n,,", ChannelBinding::None => b"n,,",
ChannelBinding::Unsupported => b"y,,",
ChannelBinding::TlsUnique(_) => b"p=tls-unique,,", ChannelBinding::TlsUnique(_) => b"p=tls-unique,,",
} }
} }
@ -99,6 +102,7 @@ impl ChannelBinding {
pub fn data(&self) -> &[u8] { pub fn data(&self) -> &[u8] {
match *self { match *self {
ChannelBinding::None => &[], ChannelBinding::None => &[],
ChannelBinding::Unsupported => &[],
ChannelBinding::TlsUnique(ref data) => data, ChannelBinding::TlsUnique(ref data) => data,
} }
} }
@ -106,7 +110,7 @@ impl ChannelBinding {
/// Represents a SASL secret, like a password. /// Represents a SASL secret, like a password.
#[derive(Clone, Debug, PartialEq, Eq)] #[derive(Clone, Debug, PartialEq, Eq)]
pub enum SaslSecret { pub enum Secret {
/// No extra data needed. /// No extra data needed.
None, None,
/// Password required. /// Password required.
@ -114,12 +118,12 @@ pub enum SaslSecret {
} }
/// A trait which defines SASL mechanisms. /// A trait which defines SASL mechanisms.
pub trait SaslMechanism { pub trait Mechanism {
/// The name of the mechanism. /// The name of the mechanism.
fn name(&self) -> &str; fn name(&self) -> &str;
/// Creates this mechanism from `SaslCredentials`. /// Creates this mechanism from `Credentials`.
fn from_credentials(credentials: SaslCredentials) -> Result<Self, String> where Self: Sized; fn from_credentials(credentials: Credentials) -> Result<Self, String> where Self: Sized;
/// Provides initial payload of the SASL mechanism. /// Provides initial payload of the SASL mechanism.
fn initial(&mut self) -> Result<Vec<u8>, String> { fn initial(&mut self) -> Result<Vec<u8>, String> {

View file

@ -1,8 +1,8 @@
//! Provides the SASL "ANONYMOUS" mechanism. //! Provides the SASL "ANONYMOUS" mechanism.
use SaslMechanism; use Mechanism;
use SaslCredentials; use Credentials;
use SaslSecret; use Secret;
/// A struct for the SASL ANONYMOUS mechanism. /// A struct for the SASL ANONYMOUS mechanism.
pub struct Anonymous; pub struct Anonymous;
@ -10,18 +10,18 @@ pub struct Anonymous;
impl Anonymous { impl Anonymous {
/// Constructs a new struct for authenticating using the SASL ANONYMOUS mechanism. /// Constructs a new struct for authenticating using the SASL ANONYMOUS mechanism.
/// ///
/// It is recommended that instead you use a `SaslCredentials` struct and turn it into the /// It is recommended that instead you use a `Credentials` struct and turn it into the
/// requested mechanism using `from_credentials`. /// requested mechanism using `from_credentials`.
pub fn new() -> Anonymous { pub fn new() -> Anonymous {
Anonymous Anonymous
} }
} }
impl SaslMechanism for Anonymous { impl Mechanism for Anonymous {
fn name(&self) -> &str { "ANONYMOUS" } fn name(&self) -> &str { "ANONYMOUS" }
fn from_credentials(credentials: SaslCredentials) -> Result<Anonymous, String> { fn from_credentials(credentials: Credentials) -> Result<Anonymous, String> {
if let SaslSecret::None = credentials.secret { if let Secret::None = credentials.secret {
Ok(Anonymous) Ok(Anonymous)
} }
else { else {

View file

@ -1,8 +1,8 @@
//! Provides the SASL "PLAIN" mechanism. //! Provides the SASL "PLAIN" mechanism.
use SaslMechanism; use Mechanism;
use SaslCredentials; use Credentials;
use SaslSecret; use Secret;
/// A struct for the SASL PLAIN mechanism. /// A struct for the SASL PLAIN mechanism.
pub struct Plain { pub struct Plain {
@ -13,7 +13,7 @@ pub struct Plain {
impl Plain { impl Plain {
/// Constructs a new struct for authenticating using the SASL PLAIN mechanism. /// Constructs a new struct for authenticating using the SASL PLAIN mechanism.
/// ///
/// It is recommended that instead you use a `SaslCredentials` struct and turn it into the /// It is recommended that instead you use a `Credentials` struct and turn it into the
/// requested mechanism using `from_credentials`. /// requested mechanism using `from_credentials`.
pub fn new<N: Into<String>, P: Into<String>>(username: N, password: P) -> Plain { pub fn new<N: Into<String>, P: Into<String>>(username: N, password: P) -> Plain {
Plain { Plain {
@ -23,11 +23,11 @@ impl Plain {
} }
} }
impl SaslMechanism for Plain { impl Mechanism for Plain {
fn name(&self) -> &str { "PLAIN" } fn name(&self) -> &str { "PLAIN" }
fn from_credentials(credentials: SaslCredentials) -> Result<Plain, String> { fn from_credentials(credentials: Credentials) -> Result<Plain, String> {
if let SaslSecret::Password(password) = credentials.secret { if let Secret::Password(password) = credentials.secret {
if let Some(username) = credentials.username { if let Some(username) = credentials.username {
Ok(Plain::new(username, password)) Ok(Plain::new(username, password))
} }

View file

@ -2,9 +2,9 @@
use base64; use base64;
use SaslMechanism; use Mechanism;
use SaslCredentials; use Credentials;
use SaslSecret; use Secret;
use ChannelBinding; use ChannelBinding;
use error::Error; use error::Error;
@ -147,7 +147,7 @@ impl<S: ScramProvider> Scram<S> {
/// Constructs a new struct for authenticating using the SASL SCRAM-* and SCRAM-*-PLUS /// Constructs a new struct for authenticating using the SASL SCRAM-* and SCRAM-*-PLUS
/// mechanisms, depending on the passed channel binding. /// mechanisms, depending on the passed channel binding.
/// ///
/// It is recommended that instead you use a `SaslCredentials` struct and turn it into the /// It is recommended that instead you use a `Credentials` struct and turn it into the
/// requested mechanism using `from_credentials`. /// requested mechanism using `from_credentials`.
pub fn new<N: Into<String>, P: Into<String>>(username: N, password: P, channel_binding: ChannelBinding) -> Result<Scram<S>, Error> { pub fn new<N: Into<String>, P: Into<String>>(username: N, password: P, channel_binding: ChannelBinding) -> Result<Scram<S>, Error> {
Ok(Scram { Ok(Scram {
@ -177,13 +177,13 @@ impl<S: ScramProvider> Scram<S> {
} }
} }
impl<S: ScramProvider> SaslMechanism for Scram<S> { impl<S: ScramProvider> Mechanism for Scram<S> {
fn name(&self) -> &str { // TODO: this is quite the workaround… fn name(&self) -> &str { // TODO: this is quite the workaround…
&self.name &self.name
} }
fn from_credentials(credentials: SaslCredentials) -> Result<Scram<S>, String> { fn from_credentials(credentials: Credentials) -> Result<Scram<S>, String> {
if let SaslSecret::Password(password) = credentials.secret { if let Secret::Password(password) = credentials.secret {
if let Some(username) = credentials.username { if let Some(username) = credentials.username {
Scram::new(username, password, credentials.channel_binding) Scram::new(username, password, credentials.channel_binding)
.map_err(|_| "can't generate nonce".to_owned()) .map_err(|_| "can't generate nonce".to_owned())
@ -284,7 +284,7 @@ impl<S: ScramProvider> SaslMechanism for Scram<S> {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use ::SaslMechanism; use ::Mechanism;
use super::*; use super::*;