Skip to content

Commit 2d70581

Browse files
committed
feature flag for authentication
1 parent 581d24f commit 2d70581

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

Cargo.toml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ edition = "2021"
1212

1313
[dependencies]
1414
unicase = "^2.8"
15-
base64 = "^0.22.1"
16-
zeroize = { version = "^1.8.1", features = ["zeroize_derive"] }
15+
base64 = { version = "^0.22.1", optional = true }
16+
zeroize = { version = "^1.8.1", features = ["zeroize_derive"], optional = true }
1717
native-tls = { version = "^0.2", optional = true }
1818
rustls = { version = "^0.23", optional = true }
1919
rustls-pemfile = { version = "^2.2", optional = true }
@@ -22,11 +22,13 @@ webpki = { version = "^0.22", optional = true }
2222
webpki-roots = { version = "^0.26", optional = true }
2323

2424
[features]
25-
default = ["native-tls"]
25+
default = ["native-tls", "auth"]
2626
rust-tls = [
2727
"rustls",
2828
"rustls-pki-types",
2929
"webpki",
3030
"webpki-roots",
3131
"rustls-pemfile",
32+
"auth",
3233
]
34+
auth = ["base64", "zeroize"]

src/request.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use crate::{
77
stream::{Stream, ThreadReceive, ThreadSend},
88
uri::Uri,
99
};
10+
#[cfg(feature = "auth")]
1011
use base64::prelude::*;
1112
use std::{
1213
convert::TryFrom,
@@ -17,6 +18,7 @@ use std::{
1718
thread,
1819
time::{Duration, Instant},
1920
};
21+
#[cfg(feature = "auth")]
2022
use zeroize::{Zeroize, ZeroizeOnDrop, Zeroizing};
2123

2224
const CR_LF: &str = "\r\n";
@@ -109,9 +111,11 @@ impl fmt::Display for HttpVersion {
109111
/// Authentication details:
110112
/// - Basic: username and password
111113
/// - Bearer: token
114+
#[cfg(feature = "auth")]
112115
#[derive(Debug, PartialEq, Zeroize, ZeroizeOnDrop)]
113116
pub struct Authentication(AuthenticationType);
114117

118+
#[cfg(feature = "auth")]
115119
impl Authentication {
116120
/// Creates a new `Authentication` of type `Basic`.
117121
///
@@ -172,11 +176,13 @@ impl Authentication {
172176

173177
/// Authentication types
174178
#[derive(Debug, PartialEq, Zeroize, ZeroizeOnDrop)]
179+
#[cfg(feature = "auth")]
175180
enum AuthenticationType {
176181
Basic { username: String, password: String },
177182
Bearer(String),
178183
}
179184

185+
#[cfg(feature = "auth")]
180186
impl AuthenticationType {
181187
/// Returns the authentication scheme as a string.
182188
const fn scheme(&self) -> &str {
@@ -409,6 +415,7 @@ impl<'a> RequestMessage<'a> {
409415
/// let request_msg = RequestMessage::new(&addr)
410416
/// .authentication(Authentication::bearer("secret456token123"));
411417
/// ```
418+
#[cfg(feature = "auth")]
412419
pub fn authentication<T>(&mut self, auth: T) -> &mut Self
413420
where
414421
Authentication: From<T>,
@@ -629,6 +636,7 @@ impl<'a> Request<'a> {
629636
/// let request = Request::new(&addr)
630637
/// .authentication(Authentication::bearer("secret456token123"));
631638
/// ```
639+
#[cfg(feature = "auth")]
632640
pub fn authentication<T>(&mut self, auth: T) -> &mut Self
633641
where
634642
Authentication: From<T>,
@@ -968,6 +976,7 @@ mod tests {
968976
}
969977

970978
#[test]
979+
#[cfg(feature = "auth")]
971980
fn authentication_basic() {
972981
let auth = Authentication::basic("user", "password123");
973982
assert_eq!(
@@ -980,6 +989,7 @@ mod tests {
980989
}
981990

982991
#[test]
992+
#[cfg(feature = "auth")]
983993
fn authentication_baerer() {
984994
let auth = Authentication::bearer("456secret123token");
985995
assert_eq!(
@@ -989,6 +999,7 @@ mod tests {
989999
}
9901000

9911001
#[test]
1002+
#[cfg(feature = "auth")]
9921003
fn authentication_header() {
9931004
{
9941005
let auth = Authentication::basic("user", "password123");
@@ -1052,6 +1063,7 @@ mod tests {
10521063
}
10531064

10541065
#[test]
1066+
#[cfg(feature = "auth")]
10551067
fn request_m_authentication() {
10561068
let uri = Uri::try_from(URI).unwrap();
10571069
let mut req = RequestMessage::new(&uri);

0 commit comments

Comments
 (0)