From cab6733b6f8582aad07d13d8d316d33a8700d570 Mon Sep 17 00:00:00 2001 From: lumi Date: Sat, 25 Mar 2017 23:45:30 +0100 Subject: [PATCH] more API simplifications --- sasl/src/common/scram.rs | 6 ++--- sasl/src/lib.rs | 12 ++++----- sasl/src/secret.rs | 42 ++++++++--------------------- sasl/src/server/mechanisms/plain.rs | 2 +- sasl/src/server/mechanisms/scram.rs | 14 +++++----- sasl/src/server/mod.rs | 12 ++++----- 6 files changed, 34 insertions(+), 54 deletions(-) diff --git a/sasl/src/common/scram.rs b/sasl/src/common/scram.rs index 5843b84..8ebe4aa 100644 --- a/sasl/src/common/scram.rs +++ b/sasl/src/common/scram.rs @@ -22,7 +22,7 @@ pub fn generate_nonce() -> Result { /// A trait which defines the needed methods for SCRAM. pub trait ScramProvider { /// The kind of secret this `ScramProvider` requires. - type SecretKind: secret::SecretKind; + type Secret: secret::Secret; /// The name of the hash function. fn name() -> &'static str; @@ -41,7 +41,7 @@ pub trait ScramProvider { pub struct Sha1; impl ScramProvider for Sha1 { // TODO: look at all these unwraps - type SecretKind = secret::Pbkdf2Sha1; + type Secret = secret::Pbkdf2Sha1; fn name() -> &'static str { "SHA-1" } @@ -85,7 +85,7 @@ impl ScramProvider for Sha1 { // TODO: look at all these unwraps pub struct Sha256; impl ScramProvider for Sha256 { // TODO: look at all these unwraps - type SecretKind = secret::Pbkdf2Sha256; + type Secret = secret::Pbkdf2Sha256; fn name() -> &'static str { "SHA-256" } diff --git a/sasl/src/lib.rs b/sasl/src/lib.rs index ac2b76f..9e33a7d 100644 --- a/sasl/src/lib.rs +++ b/sasl/src/lib.rs @@ -43,8 +43,8 @@ //! struct MyValidator; //! //! impl Validator for MyValidator { -//! fn validate(&self, identity: &Identity, value: &secret::PlainValue) -> Result<(), String> { -//! let &secret::PlainValue(ref password) = value; +//! fn validate(&self, identity: &Identity, value: &secret::Plain) -> Result<(), String> { +//! let &secret::Plain(ref password) = value; //! if identity != &Identity::Username(USERNAME.to_owned()) { //! Err("authentication failed".to_owned()) //! } @@ -58,7 +58,7 @@ //! } //! //! impl Provider for MyValidator { -//! fn provide(&self, identity: &Identity) -> Result { +//! fn provide(&self, identity: &Identity) -> Result { //! if identity != &Identity::Username(USERNAME.to_owned()) { //! Err("authentication failed".to_owned()) //! } @@ -67,7 +67,7 @@ //! ( &Password::Plain((PASSWORD.to_owned())) //! , &SALT[..] //! , ITERATIONS )?; -//! Ok(secret::Pbkdf2Sha1Value { +//! Ok(secret::Pbkdf2Sha1 { //! salt: SALT.to_vec(), //! iterations: ITERATIONS, //! digest: digest, @@ -79,7 +79,7 @@ //! impl_validator_using_provider!(MyValidator, secret::Pbkdf2Sha1); //! //! impl Provider for MyValidator { -//! fn provide(&self, identity: &Identity) -> Result { +//! fn provide(&self, identity: &Identity) -> Result { //! if identity != &Identity::Username(USERNAME.to_owned()) { //! Err("authentication failed".to_owned()) //! } @@ -88,7 +88,7 @@ //! ( &Password::Plain((PASSWORD.to_owned())) //! , &SALT[..] //! , ITERATIONS )?; -//! Ok(secret::Pbkdf2Sha256Value { +//! Ok(secret::Pbkdf2Sha256 { //! salt: SALT.to_vec(), //! iterations: ITERATIONS, //! digest: digest, diff --git a/sasl/src/secret.rs b/sasl/src/secret.rs index 47e8223..f2d9853 100644 --- a/sasl/src/secret.rs +++ b/sasl/src/secret.rs @@ -1,61 +1,41 @@ -pub trait SecretKind { - type Value: PartialEq; -} +pub trait Secret {} -pub trait Pbkdf2SecretValue { +pub trait Pbkdf2Secret { fn salt(&self) -> &[u8]; fn iterations(&self) -> usize; fn digest(&self) -> &[u8]; } -pub struct Plain; +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct Plain(pub String); -#[derive(PartialEq)] -pub struct PlainValue(pub String); - -impl SecretKind for Plain { - type Value = PlainValue; -} +impl Secret for Plain {} +#[derive(Clone, Debug, PartialEq, Eq)] pub struct Pbkdf2Sha1 { pub salt: Vec, pub iterations: usize, -} - -#[derive(PartialEq)] -pub struct Pbkdf2Sha1Value { - pub salt: Vec, - pub iterations: usize, pub digest: Vec, } -impl SecretKind for Pbkdf2Sha1 { - type Value = Pbkdf2Sha1Value; -} +impl Secret for Pbkdf2Sha1 {} -impl Pbkdf2SecretValue for Pbkdf2Sha1Value { +impl Pbkdf2Secret for Pbkdf2Sha1 { fn salt(&self) -> &[u8] { &self.salt } fn iterations(&self) -> usize { self.iterations } fn digest(&self) -> &[u8] { &self.digest } } +#[derive(Clone, Debug, PartialEq, Eq)] pub struct Pbkdf2Sha256 { pub salt: Vec, pub iterations: usize, -} - -#[derive(PartialEq)] -pub struct Pbkdf2Sha256Value { - pub salt: Vec, - pub iterations: usize, pub digest: Vec, } -impl SecretKind for Pbkdf2Sha256 { - type Value = Pbkdf2Sha256Value; -} +impl Secret for Pbkdf2Sha256 {} -impl Pbkdf2SecretValue for Pbkdf2Sha256Value { +impl Pbkdf2Secret for Pbkdf2Sha256 { fn salt(&self) -> &[u8] { &self.salt } fn iterations(&self) -> usize { self.iterations } fn digest(&self) -> &[u8] { &self.digest } diff --git a/sasl/src/server/mechanisms/plain.rs b/sasl/src/server/mechanisms/plain.rs index 1bf10aa..95b16b5 100644 --- a/sasl/src/server/mechanisms/plain.rs +++ b/sasl/src/server/mechanisms/plain.rs @@ -25,7 +25,7 @@ impl> Mechanism for Plain { let password = sp.next().ok_or_else(|| "no password specified".to_owned())?; let password = String::from_utf8(password.to_vec()).map_err(|_| "error decoding password")?; let ident = Identity::Username(username); - self.validator.validate(&ident, &secret::PlainValue(password))?; + self.validator.validate(&ident, &secret::Plain(password))?; Ok(Response::Success(ident, Vec::new())) } } diff --git a/sasl/src/server/mechanisms/scram.rs b/sasl/src/server/mechanisms/scram.rs index 7a817b7..908fd8a 100644 --- a/sasl/src/server/mechanisms/scram.rs +++ b/sasl/src/server/mechanisms/scram.rs @@ -6,7 +6,7 @@ use server::{Provider, Response, Mechanism}; use common::{Identity, ChannelBinding, parse_frame, xor}; use common::scram::{ScramProvider, generate_nonce}; use secret; -use secret::Pbkdf2SecretValue; +use secret::Pbkdf2Secret; enum ScramState { Init, @@ -21,8 +21,8 @@ enum ScramState { pub struct Scram where S: ScramProvider, - P: Provider, - ::Value: secret::Pbkdf2SecretValue { + P: Provider, + S::Secret: secret::Pbkdf2Secret { name: String, state: ScramState, channel_binding: ChannelBinding, @@ -32,8 +32,8 @@ pub struct Scram impl Scram where S: ScramProvider, - P: Provider, - ::Value: secret::Pbkdf2SecretValue { + P: Provider, + S::Secret: secret::Pbkdf2Secret { pub fn new(provider: P, channel_binding: ChannelBinding) -> Scram { Scram { name: format!("SCRAM-{}", S::name()), @@ -47,8 +47,8 @@ impl Scram impl Mechanism for Scram where S: ScramProvider, - P: Provider, - ::Value: secret::Pbkdf2SecretValue { + P: Provider, + S::Secret: secret::Pbkdf2Secret { fn name(&self) -> &str { &self.name } diff --git a/sasl/src/server/mod.rs b/sasl/src/server/mod.rs index 292dc00..926ed98 100644 --- a/sasl/src/server/mod.rs +++ b/sasl/src/server/mod.rs @@ -1,12 +1,12 @@ use common::Identity; -use secret::SecretKind; +use secret::Secret; #[macro_export] macro_rules! impl_validator_using_provider { ( $validator:ty, $secret:ty ) => { impl $crate::server::Validator<$secret> for $validator { fn validate(&self, identity: &$crate::common::Identity - , value: &<$secret as sasl::secret::SecretKind>::Value) -> Result<(), String> { + , value: &$secret) -> Result<(), String> { if &(self as &$crate::server::Provider<$secret>).provide(identity)? == value { Ok(()) } @@ -18,12 +18,12 @@ macro_rules! impl_validator_using_provider { } } -pub trait Provider: Validator { - fn provide(&self, identity: &Identity) -> Result; +pub trait Provider: Validator { + fn provide(&self, identity: &Identity) -> Result; } -pub trait Validator { - fn validate(&self, identity: &Identity, value: &S::Value) -> Result<(), String>; +pub trait Validator { + fn validate(&self, identity: &Identity, value: &S) -> Result<(), String>; } pub trait Mechanism {