From 867c0fd29854d9d4b455f7d196dcbc4bf9617c43 Mon Sep 17 00:00:00 2001 From: SamTV12345 <40429738+samtv12345@users.noreply.github.com> Date: Wed, 1 Jan 2025 11:59:45 +0100 Subject: [PATCH 1/2] Fixed basic auth --- src/auth_middleware.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/auth_middleware.rs b/src/auth_middleware.rs index 17aa37ae..ef398140 100644 --- a/src/auth_middleware.rs +++ b/src/auth_middleware.rs @@ -117,8 +117,8 @@ where if let Some(admin_username) = ENVIRONMENT_SERVICE.username.clone() { if unwrapped_user.username.clone() == admin_username { - return if let Some(password) = &ENVIRONMENT_SERVICE.password { - if &digest(password) == password { + return if let Some(env_password) = &ENVIRONMENT_SERVICE.password { + if &digest(password) == env_password { req.extensions_mut().insert(unwrapped_user); let service = Rc::clone(&self.service); async move { From 83a79dbdb9e8f1b7830d3f7d7b20ec1c33b9ebef Mon Sep 17 00:00:00 2001 From: SamTV12345 <40429738+samtv12345@users.noreply.github.com> Date: Wed, 1 Jan 2025 12:09:21 +0100 Subject: [PATCH 2/2] Fixed basic auth --- src/auth_middleware.rs | 29 +++++++++++++++++++++++++++-- src/gpodder/auth/authentication.rs | 2 +- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/auth_middleware.rs b/src/auth_middleware.rs index ef398140..b0f7e1ec 100644 --- a/src/auth_middleware.rs +++ b/src/auth_middleware.rs @@ -345,9 +345,34 @@ impl AuthFilter { Ok((username.to_string(), password.to_string())) } - pub fn basic_auth_login(rq: String) -> Result<(String, String), CustomError> { - let (u, p) = Self::extract_basic_auth(rq.as_str())?; + pub fn basic_auth_login(rq: &str) -> Result<(String, String), CustomError> { + let (u, p) = Self::extract_basic_auth(rq)?; Ok((u.to_string(), p.to_string())) } } + + +#[cfg(test)] +mod test { + use crate::auth_middleware::AuthFilter; + + #[test] + fn test_basic_auth_login() { + let result = AuthFilter::extract_basic_auth("Bearer dGVzdDp0ZXN0"); + assert_eq!(result.is_err(), false); + let (u, p) = result.unwrap(); + assert_eq!(u, "test"); + assert_eq!(p, "test"); + } + + + #[test] + fn test_basic_auth_login_with_special_characters() { + let result = AuthFilter::extract_basic_auth("Bearer dGVzdCTDvMOWOnRlc3Q="); + assert_eq!(result.is_err(), false); + let (u, p) = result.unwrap(); + assert_eq!(u, "test$üÖ"); + assert_eq!(p, "test"); + } +} diff --git a/src/gpodder/auth/authentication.rs b/src/gpodder/auth/authentication.rs index aa4c43a3..19a12b16 100644 --- a/src/gpodder/auth/authentication.rs +++ b/src/gpodder/auth/authentication.rs @@ -87,7 +87,7 @@ fn handle_gpodder_basic_auth( let authorization = opt_authorization.unwrap().to_str().unwrap(); let unwrapped_username = username.into_inner(); - let (username_basic, password) = AuthFilter::basic_auth_login(authorization.to_string())?; + let (username_basic, password) = AuthFilter::basic_auth_login(authorization)?; if username_basic != unwrapped_username { return Err(CustomError::Forbidden); }