From b19f7fd2fe53702b1d26a4d529b8724f7f68efb9 Mon Sep 17 00:00:00 2001 From: Paul-Christian Volkmer Date: Tue, 9 Sep 2025 20:10:53 +0200 Subject: [PATCH] refactor: combine nested if statements --- src/auth.rs | 27 ++++++++++++--------------- src/routes.rs | 8 ++++---- 2 files changed, 16 insertions(+), 19 deletions(-) diff --git a/src/auth.rs b/src/auth.rs index b4af9e0..a7a0211 100644 --- a/src/auth.rs +++ b/src/auth.rs @@ -4,21 +4,18 @@ use base64::Engine; #[allow(clippy::module_name_repetitions)] pub fn check_basic_auth(auth_header: &str, expected_token: &str) -> bool { let split = auth_header.split(' ').collect::>(); - if split.len() == 2 && split.first().map(|first| first.to_lowercase()) == Some("basic".into()) { - if let Ok(auth) = BASE64_STANDARD.decode(split.last().unwrap_or(&"")) { - if let Ok(auth) = String::from_utf8(auth) { - let split = auth.split(':').collect::>(); - if split.len() == 2 && split.first() == Some(&"token") { - match split.last() { - None => {} - Some(&token) => { - if let Ok(true) = bcrypt::verify(token, expected_token) { - return true; - } - } - } - } - } + if split.len() == 2 + && split.first().map(|first| first.to_lowercase()) == Some("basic".into()) + && let Ok(auth) = BASE64_STANDARD.decode(split.last().unwrap_or(&"")) + && let Ok(auth) = String::from_utf8(auth) + { + let split = auth.split(':').collect::>(); + if split.len() == 2 + && split.first() == Some(&"token") + && let Some(&token) = split.last() + && let Ok(true) = bcrypt::verify(token, expected_token) + { + return true; } } diff --git a/src/routes.rs b/src/routes.rs index a070647..13e7d36 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -47,10 +47,10 @@ pub fn routes(sender: DynMtbFileSender) -> Router { } async fn check_basic_auth(request: Request, next: Next) -> Response { - if let Some(Ok(auth_header)) = request.headers().get(AUTHORIZATION).map(|x| x.to_str()) { - if auth::check_basic_auth(auth_header, &CONFIG.token) { - return next.run(request).await; - } + if let Some(Ok(auth_header)) = request.headers().get(AUTHORIZATION).map(|x| x.to_str()) + && auth::check_basic_auth(auth_header, &CONFIG.token) + { + return next.run(request).await; } log::warn!("Invalid authentication used"); Unauthorized.into_response()