From 3b642c8545c600fdf8ca03dc48a3e8a5ee66823e Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Sun, 7 Mar 2021 11:32:53 +0000 Subject: [PATCH] basic auth password is not Option --- awc/CHANGES.md | 2 ++ awc/src/request.rs | 20 ++++++++++---------- awc/tests/test_client.rs | 4 ++-- 3 files changed, 14 insertions(+), 12 deletions(-) diff --git a/awc/CHANGES.md b/awc/CHANGES.md index 9fbc6d042..04f1a0dbe 100644 --- a/awc/CHANGES.md +++ b/awc/CHANGES.md @@ -8,6 +8,7 @@ ### Changed * Feature `cookies` is now optional and enabled by default. [#1981] * `ClientBuilder::connector` method would take `actix_http::client::Connector` type. [#2008] +* Basic auth password now takes blank passwords as an empty string instead of Option. [#2050] ### Removed * `ClientBuilder::default` function [#2008] @@ -17,6 +18,7 @@ [#1981]: https://github.com/actix/actix-web/pull/1981 [#2008]: https://github.com/actix/actix-web/pull/2008 [#2024]: https://github.com/actix/actix-web/pull/2024 +[#2050]: https://github.com/actix/actix-web/pull/2050 ## 3.0.0-beta.2 - 2021-02-10 ### Added diff --git a/awc/src/request.rs b/awc/src/request.rs index c39e0b4cd..16b4fb584 100644 --- a/awc/src/request.rs +++ b/awc/src/request.rs @@ -252,16 +252,16 @@ impl ClientRequest { self.insert_header((header::CONTENT_LENGTH, buf.format(len))) } - /// Set HTTP basic authorization header + /// Set HTTP basic authorization header. + /// + /// If no password is needed, just provide an empty string. pub fn basic_auth( self, username: impl fmt::Display, - password: Option, + password: impl fmt::Display, ) -> Self { - let auth = match password { - Some(password) => format!("{}:{}", username, password), - None => format!("{}:", username), - }; + let auth = format!("{}:{}", username, password); + self.insert_header(( header::AUTHORIZATION, format!("Basic {}", base64::encode(&auth)), @@ -269,8 +269,8 @@ impl ClientRequest { } /// Set HTTP bearer authentication header - pub fn insert_auth(self, token: impl fmt::Display) -> Self { - self.append_header((header::AUTHORIZATION, format!("Bearer {}", token))) + pub fn bearer_auth(self, token: impl fmt::Display) -> Self { + self.insert_header((header::AUTHORIZATION, format!("Bearer {}", token))) } /// Set a cookie @@ -644,7 +644,7 @@ mod tests { async fn client_basic_auth() { let req = Client::new() .get("/") - .basic_auth("username", Some("password")); + .basic_auth("username", "password"); assert_eq!( req.head .headers @@ -655,7 +655,7 @@ mod tests { "Basic dXNlcm5hbWU6cGFzc3dvcmQ=" ); - let req = Client::new().get("/").basic_auth("username", None); + let req = Client::new().get("/").basic_auth("username", ""); assert_eq!( req.head .headers diff --git a/awc/tests/test_client.rs b/awc/tests/test_client.rs index c7fa82de8..50d2b5eac 100644 --- a/awc/tests/test_client.rs +++ b/awc/tests/test_client.rs @@ -829,7 +829,7 @@ async fn client_basic_auth() { .unwrap() .to_str() .unwrap() - == "Basic dXNlcm5hbWU6cGFzc3dvcmQ=" + == format!("Basic {}", base64::encode("username:password")) { HttpResponse::Ok() } else { @@ -840,7 +840,7 @@ async fn client_basic_auth() { }); // set authorization header to Basic - let request = srv.get("/").basic_auth("username", Some("password")); + let request = srv.get("/").basic_auth("username", "password"); let response = request.send().await.unwrap(); assert!(response.status().is_success()); }