From c92e45f2da629140cdfc806e333ceeefc25dd613 Mon Sep 17 00:00:00 2001 From: sigoden Date: Sun, 12 Mar 2023 12:58:36 +0800 Subject: [PATCH] fix: basic auth sometimes does not work (#194) --- src/auth.rs | 4 ++-- tests/auth.rs | 8 ++++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/auth.rs b/src/auth.rs index 018a7a7..ecd9f90 100644 --- a/src/auth.rs +++ b/src/auth.rs @@ -211,7 +211,7 @@ impl AuthMethod { pub fn get_user(&self, authorization: &HeaderValue) -> Option { match self { AuthMethod::Basic => { - let value: Vec = general_purpose::STANDARD_NO_PAD + let value: Vec = general_purpose::STANDARD .decode(strip_prefix(authorization.as_bytes(), b"Basic ")?) .ok()?; let parts: Vec<&str> = std::str::from_utf8(&value).ok()?.split(':').collect(); @@ -236,7 +236,7 @@ impl AuthMethod { ) -> Option<()> { match self { AuthMethod::Basic => { - let basic_value: Vec = general_purpose::STANDARD_NO_PAD + let basic_value: Vec = general_purpose::STANDARD .decode(strip_prefix(authorization.as_bytes(), b"Basic ")?) .ok()?; let parts: Vec<&str> = std::str::from_utf8(&basic_value).ok()?.split(':').collect(); diff --git a/tests/auth.rs b/tests/auth.rs index 077523b..8d98b89 100644 --- a/tests/auth.rs +++ b/tests/auth.rs @@ -106,15 +106,19 @@ fn auth_nest_share( } #[rstest] +#[case(server(&["--auth", "/@user:pass", "--auth-method", "basic", "-A"]), "user", "pass")] +#[case(server(&["--auth", "/@u1:p1", "--auth-method", "basic", "-A"]), "u1", "p1")] fn auth_basic( - #[with(&["--auth", "/@user:pass", "--auth-method", "basic", "-A"])] server: TestServer, + #[case] server: TestServer, + #[case] user: &str, + #[case] pass: &str, ) -> Result<(), Error> { let url = format!("{}file1", server.url()); let resp = fetch!(b"PUT", &url).body(b"abc".to_vec()).send()?; assert_eq!(resp.status(), 401); let resp = fetch!(b"PUT", &url) .body(b"abc".to_vec()) - .basic_auth("user", Some("pass")) + .basic_auth(user, Some(pass)) .send()?; assert_eq!(resp.status(), 201); Ok(())