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:
parent
de86cc551a
commit
acc7e64a0d
3 changed files with 15 additions and 21 deletions
|
@ -9,12 +9,10 @@ license = "GPL-3.0-or-later"
|
||||||
repository = "https://github.com/tazjin/crimp"
|
repository = "https://github.com/tazjin/crimp"
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
default = [ "json", "basic_auth" ]
|
default = [ "json" ]
|
||||||
json = [ "serde", "serde_json"]
|
json = [ "serde", "serde_json"]
|
||||||
basic_auth = [ "base64" ]
|
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
curl = "0.4"
|
curl = "0.4"
|
||||||
serde = { version = "1.0", optional = true }
|
serde = { version = "1.0", optional = true }
|
||||||
serde_json = { version = "1.0", optional = true }
|
serde_json = { version = "1.0", optional = true }
|
||||||
base64 = { version = "0.10", optional = true }
|
|
||||||
|
|
30
src/lib.rs
30
src/lib.rs
|
@ -52,18 +52,13 @@
|
||||||
//!
|
//!
|
||||||
//! ## Cargo features
|
//! ## Cargo features
|
||||||
//!
|
//!
|
||||||
//! `crimp` has several optional features, all of which are enabled by
|
//! All optional features are enabled by default.
|
||||||
//! default:
|
|
||||||
//!
|
//!
|
||||||
//! * `json`: Adds `Request::json` and `Response::as_json` methods
|
//! * `json`: Adds `Request::json` and `Response::as_json` methods
|
||||||
//! which can be used for convenient serialisation of
|
//! which can be used for convenient serialisation of
|
||||||
//! request/response bodies using `serde_json`. This feature adds a
|
//! request/response bodies using `serde_json`. This feature adds a
|
||||||
//! dependency on the `serde` and `serde_json` crates.
|
//! 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
|
//! [cURL Rust bindings]: https://docs.rs/curl
|
||||||
//! [reqwest]: https://docs.rs/reqwest
|
//! [reqwest]: https://docs.rs/reqwest
|
||||||
//! [file an issue]: https://github.com/tazjin/crimp/issues
|
//! [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;
|
||||||
#[cfg(feature = "json")] extern crate serde_json;
|
#[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::collections::HashMap;
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
@ -173,6 +167,17 @@ impl <'a> Request<'a> {
|
||||||
Ok(self)
|
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.
|
/// Configure a TLS client certificate on the request.
|
||||||
///
|
///
|
||||||
/// Depending on whether the certificate file contains the private
|
/// Depending on whether the certificate file contains the private
|
||||||
|
@ -215,15 +220,6 @@ impl <'a> Request<'a> {
|
||||||
Ok(self)
|
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
|
/// Add a byte-array body to a request using the specified
|
||||||
/// `Content-Type`.
|
/// `Content-Type`.
|
||||||
pub fn body(mut self, content_type: &'a str, data: &'a [u8]) -> Self {
|
pub fn body(mut self, content_type: &'a str, data: &'a [u8]) -> Self {
|
||||||
|
|
|
@ -101,7 +101,7 @@ fn test_bearer_auth() {
|
||||||
assert!(response.is_success(), "authorized request should succeed");
|
assert!(response.is_success(), "authorized request should succeed");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "basic_auth")] #[test]
|
#[test]
|
||||||
fn test_basic_auth() {
|
fn test_basic_auth() {
|
||||||
let request = Request::new(
|
let request = Request::new(
|
||||||
Method::Get, "https://httpbin.org/basic-auth/alan_watts/oneness"
|
Method::Get, "https://httpbin.org/basic-auth/alan_watts/oneness"
|
||||||
|
|
Loading…
Add table
Reference in a new issue