From 10e42b5d76b3a2fc560623392675f774969dc795 Mon Sep 17 00:00:00 2001 From: Daniel Freire Date: Fri, 20 Dec 2024 17:34:22 -0300 Subject: [PATCH] simplify parse_admin_password --- src/eth/rpc/rpc_http_middleware.rs | 25 +++++++++---------------- 1 file changed, 9 insertions(+), 16 deletions(-) diff --git a/src/eth/rpc/rpc_http_middleware.rs b/src/eth/rpc/rpc_http_middleware.rs index 15df2d617..c34de02d0 100644 --- a/src/eth/rpc/rpc_http_middleware.rs +++ b/src/eth/rpc/rpc_http_middleware.rs @@ -62,23 +62,16 @@ impl Authentication { /// Checks if the provided admin password is correct fn parse_admin_password(headers: &HeaderMap) -> Authentication { - let Ok(real_pass) = std::env::var("ADMIN_PASSWORD") else { - return Authentication::Admin; + let real_pass = match std::env::var("ADMIN_PASSWORD") { + Ok(pass) if !pass.is_empty() => pass, + _ => return Authentication::Admin, }; - if real_pass.is_empty() { - return Authentication::Admin; - } - let Some(value) = headers.get("Authorization") else { - return Authentication::None; - }; - let Ok(value) = value.to_str() else { return Authentication::None }; - let Some(password) = value.strip_prefix("Password ") else { - return Authentication::None; - }; - if real_pass == password { - Authentication::Admin - } else { - Authentication::None + + match headers.get("Authorization") + .and_then(|val| val.to_str().ok()) + .and_then(|val| val.strip_prefix("Password ")) { + Some(password) if password == real_pass => Authentication::Admin, + _ => Authentication::None, } }