1
0
mirror of https://github.com/pcvolkmer/mv64e-rest-to-kafka-gateway synced 2025-09-13 09:12:51 +00:00

refactor: combine nested if statements

This commit is contained in:
2025-09-09 20:10:53 +02:00
parent c36cff5409
commit b19f7fd2fe
2 changed files with 16 additions and 19 deletions

View File

@@ -4,21 +4,18 @@ use base64::Engine;
#[allow(clippy::module_name_repetitions)] #[allow(clippy::module_name_repetitions)]
pub fn check_basic_auth(auth_header: &str, expected_token: &str) -> bool { pub fn check_basic_auth(auth_header: &str, expected_token: &str) -> bool {
let split = auth_header.split(' ').collect::<Vec<_>>(); let split = auth_header.split(' ').collect::<Vec<_>>();
if split.len() == 2 && split.first().map(|first| first.to_lowercase()) == Some("basic".into()) { if split.len() == 2
if let Ok(auth) = BASE64_STANDARD.decode(split.last().unwrap_or(&"")) { && split.first().map(|first| first.to_lowercase()) == Some("basic".into())
if let Ok(auth) = String::from_utf8(auth) { && let Ok(auth) = BASE64_STANDARD.decode(split.last().unwrap_or(&""))
let split = auth.split(':').collect::<Vec<_>>(); && let Ok(auth) = String::from_utf8(auth)
if split.len() == 2 && split.first() == Some(&"token") { {
match split.last() { let split = auth.split(':').collect::<Vec<_>>();
None => {} if split.len() == 2
Some(&token) => { && split.first() == Some(&"token")
if let Ok(true) = bcrypt::verify(token, expected_token) { && let Some(&token) = split.last()
return true; && let Ok(true) = bcrypt::verify(token, expected_token)
} {
} return true;
}
}
}
} }
} }

View File

@@ -47,10 +47,10 @@ pub fn routes(sender: DynMtbFileSender) -> Router {
} }
async fn check_basic_auth(request: Request<Body>, next: Next) -> Response { async fn check_basic_auth(request: Request<Body>, next: Next) -> Response {
if let Some(Ok(auth_header)) = request.headers().get(AUTHORIZATION).map(|x| x.to_str()) { if let Some(Ok(auth_header)) = request.headers().get(AUTHORIZATION).map(|x| x.to_str())
if auth::check_basic_auth(auth_header, &CONFIG.token) { && auth::check_basic_auth(auth_header, &CONFIG.token)
return next.run(request).await; {
} return next.run(request).await;
} }
log::warn!("Invalid authentication used"); log::warn!("Invalid authentication used");
Unauthorized.into_response() Unauthorized.into_response()