-
-
Notifications
You must be signed in to change notification settings - Fork 331
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
client::ConfigExt
to extend Config
for Client
- Move TLS methods to `ConfigExt` - Prepare to move `Auth` method to `ConfigExt` - `.option_layer(config.auth_layer()?)`
- Loading branch information
Showing
8 changed files
with
99 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
use std::convert::TryFrom; | ||
|
||
use tower::util::Either; | ||
|
||
#[cfg(any(feature = "native-tls", feature = "rustls-tls"))] use super::tls; | ||
use super::{ | ||
auth::{AddAuthorizationLayer, RefreshingTokenLayer}, | ||
Auth, | ||
}; | ||
use crate::{Config, Result}; | ||
|
||
/// Extensions to `Config` for `Client`. | ||
pub trait ConfigExt { | ||
/// Create `native_tls::TlsConnector` | ||
#[cfg_attr(docsrs, doc(cfg(feature = "native-tls")))] | ||
#[cfg(feature = "native-tls")] | ||
fn native_tls_connector(&self) -> Result<tokio_native_tls::native_tls::TlsConnector>; | ||
|
||
/// Create `hyper_tls::HttpsConnector` | ||
#[cfg_attr(docsrs, doc(cfg(feature = "native-tls")))] | ||
#[cfg(feature = "native-tls")] | ||
fn native_tls_https_connector(&self) -> Result<hyper_tls::HttpsConnector<hyper::client::HttpConnector>>; | ||
|
||
/// Create `rustls::ClientConfig` | ||
#[cfg_attr(docsrs, doc(cfg(feature = "rustls-tls")))] | ||
#[cfg(feature = "rustls-tls")] | ||
fn rustls_client_config(&self) -> Result<rustls::ClientConfig>; | ||
|
||
/// Create `hyper_rustls::HttpsConnector` | ||
#[cfg_attr(docsrs, doc(cfg(feature = "rustls-tls")))] | ||
#[cfg(feature = "rustls-tls")] | ||
fn rustls_https_connector(&self) -> Result<hyper_rustls::HttpsConnector<hyper::client::HttpConnector>>; | ||
} | ||
|
||
impl ConfigExt for Config { | ||
#[cfg(feature = "native-tls")] | ||
fn native_tls_connector(&self) -> Result<tokio_native_tls::native_tls::TlsConnector> { | ||
tls::native_tls::native_tls_connector( | ||
self.identity_pem.as_ref(), | ||
self.root_cert.as_ref(), | ||
self.accept_invalid_certs, | ||
) | ||
} | ||
|
||
#[cfg(feature = "native-tls")] | ||
fn native_tls_https_connector(&self) -> Result<hyper_tls::HttpsConnector<hyper::client::HttpConnector>> { | ||
let tls = tokio_native_tls::TlsConnector::from(self.native_tls_connector()?); | ||
let mut http = hyper::client::HttpConnector::new(); | ||
http.enforce_http(false); | ||
Ok(hyper_tls::HttpsConnector::from((http, tls))) | ||
} | ||
|
||
#[cfg(feature = "rustls-tls")] | ||
fn rustls_client_config(&self) -> Result<rustls::ClientConfig> { | ||
tls::rustls_tls::rustls_client_config( | ||
self.identity_pem.as_ref(), | ||
self.root_cert.as_ref(), | ||
self.accept_invalid_certs, | ||
) | ||
} | ||
|
||
#[cfg(feature = "rustls-tls")] | ||
fn rustls_https_connector(&self) -> Result<hyper_rustls::HttpsConnector<hyper::client::HttpConnector>> { | ||
let rustls_config = std::sync::Arc::new(self.rustls_client_config()?); | ||
let mut http = hyper::client::HttpConnector::new(); | ||
http.enforce_http(false); | ||
Ok(hyper_rustls::HttpsConnector::from((http, rustls_config))) | ||
} | ||
} | ||
|
||
// A separate extension since it's not ready for public | ||
pub(crate) trait ConfigAuthExt { | ||
// TODO Try reducing exported types to minimize API surface before making this public. | ||
fn auth_layer(&self) -> Result<Option<Either<AddAuthorizationLayer, RefreshingTokenLayer>>>; | ||
} | ||
|
||
impl ConfigAuthExt for Config { | ||
fn auth_layer(&self) -> Result<Option<Either<AddAuthorizationLayer, RefreshingTokenLayer>>> { | ||
Ok(match Auth::try_from(&self.auth_info)? { | ||
Auth::None => None, | ||
Auth::Basic(user, pass) => Some(Either::A(AddAuthorizationLayer::basic(&user, &pass))), | ||
Auth::Bearer(token) => Some(Either::A(AddAuthorizationLayer::bearer(&token))), | ||
Auth::RefreshableToken(r) => Some(Either::B(RefreshingTokenLayer::new(r))), | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters