about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorVincent Ambo <mail@tazj.in>2019-02-26T16·54+0100
committerVincent Ambo <mail@tazj.in>2019-02-26T16·54+0100
commitacc7e64a0d0d26ad2c529b992b96b72f8029d29d (patch)
tree2c88b31b7e9e72ea1b46f5f9bbaaf2dec2cbe55d /src
parentde86cc551a75d37d551f6a7069067c235754b833 (diff)
refactor: Use cURL's own basic auth implementation
Drops the dependency on the base64-crate, which it turns out isn't
necessary as cURL has this built-in.
Diffstat (limited to 'src')
-rw-r--r--src/lib.rs30
-rw-r--r--src/tests.rs2
2 files changed, 14 insertions, 18 deletions
diff --git a/src/lib.rs b/src/lib.rs
index ae5003a42d60..9366a2c3d087 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<Self, curl::Error> {
+        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<Self, curl::Error> {
-        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 9fce04b15f21..97798dbcdd26 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"