diff --git a/Cargo.toml b/Cargo.toml index 7df7f69a2..ae6c325f6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,12 +9,10 @@ license = "GPL-3.0-or-later" repository = "https://github.com/tazjin/crimp" [features] -default = [ "json", "basic_auth" ] +default = [ "json" ] json = [ "serde", "serde_json"] -basic_auth = [ "base64" ] [dependencies] curl = "0.4" serde = { version = "1.0", optional = true } serde_json = { version = "1.0", optional = true } -base64 = { version = "0.10", optional = true } diff --git a/src/lib.rs b/src/lib.rs index ae5003a42..9366a2c3d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -52,18 +52,13 @@ //! //! ## Cargo features //! -//! `crimp` has several optional features, all of which are enabled by -//! default: +//! All optional features are enabled by default. //! //! * `json`: Adds `Request::json` and `Response::as_json` methods //! which can be used for convenient serialisation of //! request/response bodies using `serde_json`. This feature adds a //! dependency on the `serde` and `serde_json` crates. //! -//! * `basic_auth`: Adds a `Request::basic_auth` utility method to set -//! a basic authentication header on the request. This feature adds -//! a dependency on the `base64` crate. -//! //! [cURL Rust bindings]: https://docs.rs/curl //! [reqwest]: https://docs.rs/reqwest //! [file an issue]: https://github.com/tazjin/crimp/issues @@ -72,9 +67,8 @@ extern crate curl; #[cfg(feature = "json")] extern crate serde; #[cfg(feature = "json")] extern crate serde_json; -#[cfg(feature = "basic_auth")] extern crate base64; -use curl::easy::{Easy, Form, List, ReadError}; +use curl::easy::{Auth, Easy, Form, List, ReadError}; use std::collections::HashMap; use std::io::Write; use std::path::Path; @@ -173,6 +167,17 @@ impl <'a> Request<'a> { Ok(self) } + /// Set the `Authorization` header to a basic authentication value + /// from the supplied username and password. + pub fn basic_auth(mut self, username: &str, password: &str) -> Result { + let mut auth = Auth::new(); + auth.basic(true); + self.handle.username(username)?; + self.handle.password(password)?; + self.handle.http_auth(&auth)?; + Ok(self) + } + /// Configure a TLS client certificate on the request. /// /// Depending on whether the certificate file contains the private @@ -215,15 +220,6 @@ impl <'a> Request<'a> { Ok(self) } - #[cfg(feature = "basic_auth")] - /// Set the `Authorization` header to a basic authentication value - /// from the supplied username and password. - pub fn basic_auth(mut self, username: &str, password: &str) -> Result { - let auth = base64::encode(format!("{}:{}", username, password).as_bytes()); - self.headers.append(&format!("Authorization: Basic {}", auth))?; - Ok(self) - } - /// Add a byte-array body to a request using the specified /// `Content-Type`. pub fn body(mut self, content_type: &'a str, data: &'a [u8]) -> Self { diff --git a/src/tests.rs b/src/tests.rs index 9fce04b15..97798dbcd 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -101,7 +101,7 @@ fn test_bearer_auth() { assert!(response.is_success(), "authorized request should succeed"); } -#[cfg(feature = "basic_auth")] #[test] +#[test] fn test_basic_auth() { let request = Request::new( Method::Get, "https://httpbin.org/basic-auth/alan_watts/oneness"