diff options
author | Sven-Hendrik Haase <svenstaro@gmail.com> | 2019-09-21 02:08:20 +0000 |
---|---|---|
committer | Sven-Hendrik Haase <svenstaro@gmail.com> | 2019-09-21 02:08:20 +0000 |
commit | 441b2863fd36a5a0a5be96915fa597f165d57232 (patch) | |
tree | 7a1e1caaebe27e84fafd762d147f088c04d222b8 /src | |
parent | Remove explicit clap dependency in favor of structopt's re-exported version (diff) | |
download | miniserve-441b2863fd36a5a0a5be96915fa597f165d57232.tar.gz miniserve-441b2863fd36a5a0a5be96915fa597f165d57232.zip |
Actually make use of pretty_assertions
Diffstat (limited to '')
-rw-r--r-- | src/args.rs | 1 | ||||
-rw-r--r-- | src/auth.rs | 32 |
2 files changed, 13 insertions, 20 deletions
diff --git a/src/args.rs b/src/args.rs index 47f4d1f..8fde78a 100644 --- a/src/args.rs +++ b/src/args.rs @@ -172,6 +172,7 @@ pub fn parse_args() -> crate::MiniserveConfig { mod tests { use super::*; use rstest::rstest_parametrize; + use pretty_assertions::assert_eq; /// Helper function that creates a `RequiredAuth` structure fn create_required_auth(username: &str, password: &str, encrypt: &str) -> auth::RequiredAuth { diff --git a/src/auth.rs b/src/auth.rs index 2c98622..1c98c10 100644 --- a/src/auth.rs +++ b/src/auth.rs @@ -55,19 +55,17 @@ pub fn parse_basic_auth( /// Return `true` if `basic_auth` is matches any of `required_auth` pub fn match_auth(basic_auth: BasicAuthParams, required_auth: &[RequiredAuth]) -> bool { - required_auth.iter().any( - |RequiredAuth { username, password }| - basic_auth.username == *username && - compare_password(&basic_auth.password, password) - ) + required_auth + .iter() + .any(|RequiredAuth { username, password }| { + basic_auth.username == *username && compare_password(&basic_auth.password, password) + }) } /// Return `true` if `basic_auth_pwd` meets `required_auth_pwd`'s requirement -pub fn compare_password (basic_auth_pwd: &str, required_auth_pwd: &RequiredAuthPassword) -> bool { +pub fn compare_password(basic_auth_pwd: &str, required_auth_pwd: &RequiredAuthPassword) -> bool { match &required_auth_pwd { - RequiredAuthPassword::Plain(required_password) => { - *basic_auth_pwd == *required_password - } + RequiredAuthPassword::Plain(required_password) => *basic_auth_pwd == *required_password, RequiredAuthPassword::Sha256(password_hash) => { compare_hash::<Sha256>(basic_auth_pwd, password_hash) } @@ -106,16 +104,9 @@ impl Middleware<crate::MiniserveConfig> for Auth { Ok(auth_req) => auth_req, Err(err) => { let auth_err = ContextualError::HTTPAuthenticationError(Box::new(err)); - return Ok(Response::Done( - HttpResponse::BadRequest().body( - build_unauthorized_response( - &req, - auth_err, - true, - StatusCode::BAD_REQUEST, - ), - ), - )); + return Ok(Response::Done(HttpResponse::BadRequest().body( + build_unauthorized_response(&req, auth_err, true, StatusCode::BAD_REQUEST), + ))); } }; @@ -135,7 +126,7 @@ impl Middleware<crate::MiniserveConfig> for Auth { ContextualError::InvalidHTTPCredentials, true, StatusCode::UNAUTHORIZED, - )) + )), )) } } @@ -178,6 +169,7 @@ fn build_unauthorized_response( mod tests { use super::*; use rstest::{rstest, rstest_parametrize, fixture}; + use pretty_assertions::assert_eq; /// Return a hashing function corresponds to given name fn get_hash_func(name: &str) -> impl FnOnce(&str) -> Vec<u8> { |