From 97f597d89db1afef1cba155cf98153ef89152611 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 | 3 +-- sasl/src/server/mechanisms/scram.rs | 14 +++++----- sasl/src/server/mod.rs | 12 ++++----- 6 files changed, 34 insertions(+), 55 deletions(-) diff --git a/sasl/src/common/scram.rs b/sasl/src/common/scram.rs index 5862c31e..e6126b58 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; @@ -42,7 +42,7 @@ 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" @@ -105,7 +105,7 @@ 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 c454eef1..de542caf 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 171e2f92..61654edb 100644 --- a/sasl/src/secret.rs +++ b/sasl/src/secret.rs @@ -1,39 +1,26 @@ -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 } @@ -45,23 +32,16 @@ impl Pbkdf2SecretValue for Pbkdf2Sha1Value { } } +#[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 } diff --git a/sasl/src/server/mechanisms/plain.rs b/sasl/src/server/mechanisms/plain.rs index f43f68f5..1deebbe5 100644 --- a/sasl/src/server/mechanisms/plain.rs +++ b/sasl/src/server/mechanisms/plain.rs @@ -33,8 +33,7 @@ impl> Mechanism for Plain { 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 d888ebc8..9ff46f10 100644 --- a/sasl/src/server/mechanisms/scram.rs +++ b/sasl/src/server/mechanisms/scram.rs @@ -5,7 +5,7 @@ use base64; use common::scram::{generate_nonce, ScramProvider}; use common::{parse_frame, xor, ChannelBinding, Identity}; use secret; -use secret::Pbkdf2SecretValue; +use secret::Pbkdf2Secret; use server::{Mechanism, Provider, Response}; enum ScramState { @@ -24,8 +24,8 @@ enum ScramState { pub struct Scram where S: ScramProvider, - P: Provider, - ::Value: secret::Pbkdf2SecretValue, + P: Provider, + S::Secret: secret::Pbkdf2Secret, { name: String, state: ScramState, @@ -37,8 +37,8 @@ where 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 { @@ -54,8 +54,8 @@ where 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 f46c91d1..87b4b07b 100644 --- a/sasl/src/server/mod.rs +++ b/sasl/src/server/mod.rs @@ -1,5 +1,5 @@ use common::Identity; -use secret::SecretKind; +use secret::Secret; #[macro_export] macro_rules! impl_validator_using_provider { @@ -8,7 +8,7 @@ macro_rules! impl_validator_using_provider { fn validate( &self, identity: &$crate::common::Identity, - value: &<$secret as sasl::secret::SecretKind>::Value, + value: &$secret, ) -> Result<(), String> { if &(self as &$crate::server::Provider<$secret>).provide(identity)? == value { Ok(()) @@ -20,12 +20,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 {