Fixed not building when default-features = false

This commit is contained in:
Gustav Palmqvist 2022-05-20 07:51:51 +02:00
commit c9931f12a9
5 changed files with 28 additions and 4 deletions

View file

@ -15,8 +15,9 @@ edition = "2018"
gitlab = { repository = "lumi/sasl-rs" } gitlab = { repository = "lumi/sasl-rs" }
[features] [features]
default = ["scram"] default = ["scram", "anonymous"]
scram = ["base64", "getrandom", "sha-1", "sha2", "hmac", "pbkdf2"] scram = ["base64", "getrandom", "sha-1", "sha2", "hmac", "pbkdf2"]
anonymous = ["getrandom"]
[dependencies] [dependencies]
base64 = { version = "0.13", optional = true } base64 = { version = "0.13", optional = true }

View file

@ -1,8 +1,12 @@
use crate::common::scram::DeriveError;
use crate::common::Credentials;
use hmac::digest::InvalidLength;
use std::fmt; use std::fmt;
use crate::common::Credentials;
#[cfg(feature = "scram")]
use crate::common::scram::DeriveError;
#[cfg(feature = "scram")]
use hmac::digest::InvalidLength;
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
pub enum MechanismError { pub enum MechanismError {
AnonymousRequiresNoCredentials, AnonymousRequiresNoCredentials,
@ -18,7 +22,9 @@ pub enum MechanismError {
NoServerNonce, NoServerNonce,
NoServerSalt, NoServerSalt,
NoServerIterations, NoServerIterations,
#[cfg(feature = "scram")]
DeriveError(DeriveError), DeriveError(DeriveError),
#[cfg(feature = "scram")]
InvalidKeyLength(InvalidLength), InvalidKeyLength(InvalidLength),
InvalidState, InvalidState,
@ -27,12 +33,14 @@ pub enum MechanismError {
NoSignatureInSuccessResponse, NoSignatureInSuccessResponse,
} }
#[cfg(feature = "scram")]
impl From<DeriveError> for MechanismError { impl From<DeriveError> for MechanismError {
fn from(err: DeriveError) -> MechanismError { fn from(err: DeriveError) -> MechanismError {
MechanismError::DeriveError(err) MechanismError::DeriveError(err)
} }
} }
#[cfg(feature = "scram")]
impl From<InvalidLength> for MechanismError { impl From<InvalidLength> for MechanismError {
fn from(err: InvalidLength) -> MechanismError { fn from(err: InvalidLength) -> MechanismError {
MechanismError::InvalidKeyLength(err) MechanismError::InvalidKeyLength(err)
@ -60,7 +68,9 @@ impl fmt::Display for MechanismError {
MechanismError::NoServerNonce => "no server nonce", MechanismError::NoServerNonce => "no server nonce",
MechanismError::NoServerSalt => "no server salt", MechanismError::NoServerSalt => "no server salt",
MechanismError::NoServerIterations => "no server iterations", MechanismError::NoServerIterations => "no server iterations",
#[cfg(feature = "scram")]
MechanismError::DeriveError(err) => return write!(fmt, "derive error: {}", err), MechanismError::DeriveError(err) => return write!(fmt, "derive error: {}", err),
#[cfg(feature = "scram")]
MechanismError::InvalidKeyLength(err) => MechanismError::InvalidKeyLength(err) =>
return write!(fmt, "invalid key length: {}", err), return write!(fmt, "invalid key length: {}", err),
MechanismError::InvalidState => "not in the right state to receive this response", MechanismError::InvalidState => "not in the right state to receive this response",

View file

@ -1,5 +1,6 @@
use crate::common::Identity; use crate::common::Identity;
use crate::server::{Mechanism, MechanismError, Response}; use crate::server::{Mechanism, MechanismError, Response};
use getrandom::getrandom; use getrandom::getrandom;
pub struct Anonymous; pub struct Anonymous;

View file

@ -1,8 +1,10 @@
#[cfg(feature = "anonymous")]
mod anonymous; mod anonymous;
mod plain; mod plain;
#[cfg(feature = "scram")] #[cfg(feature = "scram")]
mod scram; mod scram;
#[cfg(feature = "anonymous")]
pub use self::anonymous::Anonymous; pub use self::anonymous::Anonymous;
pub use self::plain::Plain; pub use self::plain::Plain;
#[cfg(feature = "scram")] #[cfg(feature = "scram")]

View file

@ -1,8 +1,10 @@
use crate::common::scram::DeriveError;
use crate::common::Identity; use crate::common::Identity;
use crate::secret::Secret; use crate::secret::Secret;
use std::fmt; use std::fmt;
#[cfg(feature = "scram")]
use crate::common::scram::DeriveError;
#[macro_export] #[macro_export]
macro_rules! impl_validator_using_provider { macro_rules! impl_validator_using_provider {
( $validator:ty, $secret:ty ) => { ( $validator:ty, $secret:ty ) => {
@ -33,6 +35,7 @@ pub trait Validator<S: Secret> {
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
pub enum ProviderError { pub enum ProviderError {
AuthenticationFailed, AuthenticationFailed,
#[cfg(feature = "scram")]
DeriveError(DeriveError), DeriveError(DeriveError),
} }
@ -61,7 +64,9 @@ pub enum MechanismError {
ProviderError(ProviderError), ProviderError(ProviderError),
CannotDecodeResponse, CannotDecodeResponse,
#[cfg(feature = "scram")]
InvalidKeyLength(hmac::digest::InvalidLength), InvalidKeyLength(hmac::digest::InvalidLength),
#[cfg(any(feature = "scram", feature = "anonymous"))]
RandomFailure(getrandom::Error), RandomFailure(getrandom::Error),
NoProof, NoProof,
CannotDecodeProof, CannotDecodeProof,
@ -69,6 +74,7 @@ pub enum MechanismError {
SaslSessionAlreadyOver, SaslSessionAlreadyOver,
} }
#[cfg(feature = "scram")]
impl From<DeriveError> for ProviderError { impl From<DeriveError> for ProviderError {
fn from(err: DeriveError) -> ProviderError { fn from(err: DeriveError) -> ProviderError {
ProviderError::DeriveError(err) ProviderError::DeriveError(err)
@ -93,12 +99,14 @@ impl From<ValidatorError> for MechanismError {
} }
} }
#[cfg(feature = "scram")]
impl From<hmac::digest::InvalidLength> for MechanismError { impl From<hmac::digest::InvalidLength> for MechanismError {
fn from(err: hmac::digest::InvalidLength) -> MechanismError { fn from(err: hmac::digest::InvalidLength) -> MechanismError {
MechanismError::InvalidKeyLength(err) MechanismError::InvalidKeyLength(err)
} }
} }
#[cfg(any(feature = "scram", feature = "anonymous"))]
impl From<getrandom::Error> for MechanismError { impl From<getrandom::Error> for MechanismError {
fn from(err: getrandom::Error) -> MechanismError { fn from(err: getrandom::Error) -> MechanismError {
MechanismError::RandomFailure(err) MechanismError::RandomFailure(err)
@ -145,7 +153,9 @@ impl fmt::Display for MechanismError {
MechanismError::ProviderError(err) => write!(fmt, "provider error: {}", err), MechanismError::ProviderError(err) => write!(fmt, "provider error: {}", err),
MechanismError::CannotDecodeResponse => write!(fmt, "can’t decode response"), MechanismError::CannotDecodeResponse => write!(fmt, "can’t decode response"),
#[cfg(feature = "scram")]
MechanismError::InvalidKeyLength(err) => write!(fmt, "invalid key length: {}", err), MechanismError::InvalidKeyLength(err) => write!(fmt, "invalid key length: {}", err),
#[cfg(any(feature = "scram", feature = "anonymous"))]
MechanismError::RandomFailure(err) => { MechanismError::RandomFailure(err) => {
write!(fmt, "failure to get random data: {}", err) write!(fmt, "failure to get random data: {}", err)
} }