more API simplifications

This commit is contained in:
lumi 2017-03-25 23:45:30 +01:00
parent d20e6d4b2b
commit cab6733b6f
6 changed files with 34 additions and 54 deletions

View file

@ -22,7 +22,7 @@ pub fn generate_nonce() -> Result<String, ErrorStack> {
/// 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" }

View file

@ -43,8 +43,8 @@
//! struct MyValidator;
//!
//! impl Validator<secret::Plain> 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<secret::Pbkdf2Sha1> for MyValidator {
//! fn provide(&self, identity: &Identity) -> Result<secret::Pbkdf2Sha1Value, String> {
//! fn provide(&self, identity: &Identity) -> Result<secret::Pbkdf2Sha1, String> {
//! 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<secret::Pbkdf2Sha256> for MyValidator {
//! fn provide(&self, identity: &Identity) -> Result<secret::Pbkdf2Sha256Value, String> {
//! fn provide(&self, identity: &Identity) -> Result<secret::Pbkdf2Sha256, String> {
//! 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,

View file

@ -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<u8>,
pub iterations: usize,
}
#[derive(PartialEq)]
pub struct Pbkdf2Sha1Value {
pub salt: Vec<u8>,
pub iterations: usize,
pub digest: Vec<u8>,
}
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<u8>,
pub iterations: usize,
}
#[derive(PartialEq)]
pub struct Pbkdf2Sha256Value {
pub salt: Vec<u8>,
pub iterations: usize,
pub digest: Vec<u8>,
}
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 }

View file

@ -25,7 +25,7 @@ impl<V: Validator<secret::Plain>> Mechanism for Plain<V> {
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()))
}
}

View file

@ -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<S, P>
where S: ScramProvider,
P: Provider<S::SecretKind>,
<S::SecretKind as secret::SecretKind>::Value: secret::Pbkdf2SecretValue {
P: Provider<S::Secret>,
S::Secret: secret::Pbkdf2Secret {
name: String,
state: ScramState,
channel_binding: ChannelBinding,
@ -32,8 +32,8 @@ pub struct Scram<S, P>
impl<S, P> Scram<S, P>
where S: ScramProvider,
P: Provider<S::SecretKind>,
<S::SecretKind as secret::SecretKind>::Value: secret::Pbkdf2SecretValue {
P: Provider<S::Secret>,
S::Secret: secret::Pbkdf2Secret {
pub fn new(provider: P, channel_binding: ChannelBinding) -> Scram<S, P> {
Scram {
name: format!("SCRAM-{}", S::name()),
@ -47,8 +47,8 @@ impl<S, P> Scram<S, P>
impl<S, P> Mechanism for Scram<S, P>
where S: ScramProvider,
P: Provider<S::SecretKind>,
<S::SecretKind as secret::SecretKind>::Value: secret::Pbkdf2SecretValue {
P: Provider<S::Secret>,
S::Secret: secret::Pbkdf2Secret {
fn name(&self) -> &str {
&self.name
}

View file

@ -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<S: SecretKind>: Validator<S> {
fn provide(&self, identity: &Identity) -> Result<S::Value, String>;
pub trait Provider<S: Secret>: Validator<S> {
fn provide(&self, identity: &Identity) -> Result<S, String>;
}
pub trait Validator<S: SecretKind> {
fn validate(&self, identity: &Identity, value: &S::Value) -> Result<(), String>;
pub trait Validator<S: Secret> {
fn validate(&self, identity: &Identity, value: &S) -> Result<(), String>;
}
pub trait Mechanism {