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:
23
src/auth.rs
23
src/auth.rs
@@ -4,23 +4,20 @@ 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 Ok(auth) = String::from_utf8(auth)
|
||||||
|
{
|
||||||
let split = auth.split(':').collect::<Vec<_>>();
|
let split = auth.split(':').collect::<Vec<_>>();
|
||||||
if split.len() == 2 && split.first() == Some(&"token") {
|
if split.len() == 2
|
||||||
match split.last() {
|
&& split.first() == Some(&"token")
|
||||||
None => {}
|
&& let Some(&token) = split.last()
|
||||||
Some(&token) => {
|
&& let Ok(true) = bcrypt::verify(token, expected_token)
|
||||||
if let Ok(true) = bcrypt::verify(token, expected_token) {
|
{
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
|
@@ -47,11 +47,11 @@ 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()
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user