basic auth password is not Option

This commit is contained in:
Rob Ede 2021-03-07 11:32:53 +00:00
parent e2b83e3082
commit 3b642c8545
No known key found for this signature in database
GPG Key ID: 97C636207D3EF933
3 changed files with 14 additions and 12 deletions

View File

@ -8,6 +8,7 @@
### Changed ### Changed
* Feature `cookies` is now optional and enabled by default. [#1981] * Feature `cookies` is now optional and enabled by default. [#1981]
* `ClientBuilder::connector` method would take `actix_http::client::Connector<T, U>` type. [#2008] * `ClientBuilder::connector` method would take `actix_http::client::Connector<T, U>` type. [#2008]
* Basic auth password now takes blank passwords as an empty string instead of Option. [#2050]
### Removed ### Removed
* `ClientBuilder::default` function [#2008] * `ClientBuilder::default` function [#2008]
@ -17,6 +18,7 @@
[#1981]: https://github.com/actix/actix-web/pull/1981 [#1981]: https://github.com/actix/actix-web/pull/1981
[#2008]: https://github.com/actix/actix-web/pull/2008 [#2008]: https://github.com/actix/actix-web/pull/2008
[#2024]: https://github.com/actix/actix-web/pull/2024 [#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 ## 3.0.0-beta.2 - 2021-02-10
### Added ### Added

View File

@ -252,16 +252,16 @@ impl ClientRequest {
self.insert_header((header::CONTENT_LENGTH, buf.format(len))) 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( pub fn basic_auth(
self, self,
username: impl fmt::Display, username: impl fmt::Display,
password: Option<impl fmt::Display>, password: impl fmt::Display,
) -> Self { ) -> Self {
let auth = match password { let auth = format!("{}:{}", username, password);
Some(password) => format!("{}:{}", username, password),
None => format!("{}:", username),
};
self.insert_header(( self.insert_header((
header::AUTHORIZATION, header::AUTHORIZATION,
format!("Basic {}", base64::encode(&auth)), format!("Basic {}", base64::encode(&auth)),
@ -269,8 +269,8 @@ impl ClientRequest {
} }
/// Set HTTP bearer authentication header /// Set HTTP bearer authentication header
pub fn insert_auth(self, token: impl fmt::Display) -> Self { pub fn bearer_auth(self, token: impl fmt::Display) -> Self {
self.append_header((header::AUTHORIZATION, format!("Bearer {}", token))) self.insert_header((header::AUTHORIZATION, format!("Bearer {}", token)))
} }
/// Set a cookie /// Set a cookie
@ -644,7 +644,7 @@ mod tests {
async fn client_basic_auth() { async fn client_basic_auth() {
let req = Client::new() let req = Client::new()
.get("/") .get("/")
.basic_auth("username", Some("password")); .basic_auth("username", "password");
assert_eq!( assert_eq!(
req.head req.head
.headers .headers
@ -655,7 +655,7 @@ mod tests {
"Basic dXNlcm5hbWU6cGFzc3dvcmQ=" "Basic dXNlcm5hbWU6cGFzc3dvcmQ="
); );
let req = Client::new().get("/").basic_auth("username", None); let req = Client::new().get("/").basic_auth("username", "");
assert_eq!( assert_eq!(
req.head req.head
.headers .headers

View File

@ -829,7 +829,7 @@ async fn client_basic_auth() {
.unwrap() .unwrap()
.to_str() .to_str()
.unwrap() .unwrap()
== "Basic dXNlcm5hbWU6cGFzc3dvcmQ=" == format!("Basic {}", base64::encode("username:password"))
{ {
HttpResponse::Ok() HttpResponse::Ok()
} else { } else {
@ -840,7 +840,7 @@ async fn client_basic_auth() {
}); });
// set authorization header to Basic <base64 encoded username:password> // set authorization header to Basic <base64 encoded username:password>
let request = srv.get("/").basic_auth("username", Some("password")); let request = srv.get("/").basic_auth("username", "password");
let response = request.send().await.unwrap(); let response = request.send().await.unwrap();
assert!(response.status().is_success()); assert!(response.status().is_success());
} }