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.
This commit is contained in:
Vincent Ambo 2019-02-26 17:54:11 +01:00
parent de86cc551a
commit acc7e64a0d
3 changed files with 15 additions and 21 deletions

View file

@ -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 }

View file

@ -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 {

View file

@ -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"