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:
27
src/auth.rs
27
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::<Vec<_>>();
|
||||
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::<Vec<_>>();
|
||||
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::<Vec<_>>();
|
||||
if split.len() == 2
|
||||
&& split.first() == Some(&"token")
|
||||
&& let Some(&token) = split.last()
|
||||
&& let Ok(true) = bcrypt::verify(token, expected_token)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -47,10 +47,10 @@ pub fn routes(sender: DynMtbFileSender) -> Router {
|
||||
}
|
||||
|
||||
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 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()
|
||||
|
Reference in New Issue
Block a user