mirror of
https://gitlab.com/xmpp-rs/xmpp-rs.git
synced 2024-07-12 22:21:53 +00:00
more API simplifications
This commit is contained in:
parent
35fc26f378
commit
97f597d89d
6 changed files with 34 additions and 55 deletions
|
@ -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;
|
||||
|
@ -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"
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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<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
|
||||
}
|
||||
|
@ -45,23 +32,16 @@ impl Pbkdf2SecretValue for Pbkdf2Sha1Value {
|
|||
}
|
||||
}
|
||||
|
||||
#[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
|
||||
}
|
||||
|
|
|
@ -33,8 +33,7 @@ impl<V: Validator<secret::Plain>> Mechanism for Plain<V> {
|
|||
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()))
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<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,
|
||||
|
@ -37,8 +37,8 @@ where
|
|||
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 {
|
||||
|
@ -54,8 +54,8 @@ where
|
|||
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
|
||||
|
|
|
@ -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<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 {
|
||||
|
|
Loading…
Reference in a new issue