Skip to content

Commit

Permalink
Merge pull request #229 from dermesser/install-crypto-provider
Browse files Browse the repository at this point in the history
Initialize rustls crypto provider if not otherwise done
  • Loading branch information
dermesser authored Jun 14, 2024
2 parents e1a58b4 + 500417e commit e711b1b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
7 changes: 4 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ name = "tests"
required-features = ["hyper-rustls", "service_account"]

[features]
default = ["hyper-rustls", "service_account"]
default = ["hyper-rustls", "rustls", "service_account"]
service_account = ["hyper-rustls", "rustls", "rustls-pemfile"]
hyper-rustls = ["dep:hyper-rustls", "rustls"]

[dependencies]
anyhow = "1.0.38"
Expand All @@ -39,11 +40,11 @@ http = "1"
http-body-util = "0.1"
hyper = "1"
hyper-util = { version = "0.1.5", features = ["client-legacy", "server-auto", "http1", "http2", "server-graceful"] }
hyper-rustls = { version = "0.27", optional = true, default-features = false, features = ["http1", "http2", "rustls-native-certs", "ring"] }
hyper-rustls = { version = "0.27", optional = true, default-features = false, features = ["http1", "http2", "rustls-native-certs", "ring", "native-tokio"] }
hyper-tls = { version = "0.6.0", optional = true }
log = "0.4"
percent-encoding = "2"
rustls = { version = "^0.23", optional = true, features = ["ring"] }
rustls = { version = "^0.23", optional = true, default-features = false, features = ["ring", "std"] }
rustls-pemfile = { version = "2.0.0", optional = true }
seahash = "4"
serde = { version = "1.0", features = ["derive"] }
Expand Down
14 changes: 14 additions & 0 deletions src/authenticator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,10 @@ impl ExternalAccountAuthenticator {
/// # .expect("failed to create authenticator");
/// # }
/// ```
#[cfg(any(feature = "hyper-rustls", feature = "hyper-tls"))]
pub struct AccessTokenAuthenticator;

#[cfg(any(feature = "hyper-rustls", feature = "hyper-tls"))]
impl AccessTokenAuthenticator {
/// the builder pattern for the authenticator
pub fn builder(
Expand Down Expand Up @@ -576,6 +579,7 @@ impl<C, F> AuthenticatorBuilder<C, F> {
}

fn new(auth_flow: F, hyper_client_builder: C) -> AuthenticatorBuilder<C, F> {
install_crypto_provider_if_not_set();
AuthenticatorBuilder {
hyper_client_builder,
storage_type: StorageType::Memory,
Expand Down Expand Up @@ -1062,6 +1066,16 @@ enum StorageType {
Custom(Box<dyn TokenStorage>),
}

#[cfg(feature = "hyper-rustls")]
fn install_crypto_provider_if_not_set() {
if rustls::crypto::CryptoProvider::get_default().is_none() {
let _ = rustls::crypto::ring::default_provider().install_default();
}
}

#[cfg(not(feature = "hyper-rustls"))]
fn install_crypto_provider_if_not_set() {}

#[cfg(test)]
mod tests {
#[test]
Expand Down
10 changes: 7 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,15 @@ pub use hyper_rustls;
#[cfg(feature = "service_account")]
#[doc(inline)]
pub use crate::authenticator::ServiceAccountAuthenticator;

#[cfg(any(feature = "hyper-rustls", feature = "hyper-tls"))]
pub use crate::authenticator::AccessTokenAuthenticator;

#[doc(inline)]
pub use crate::authenticator::{
AccessTokenAuthenticator, ApplicationDefaultCredentialsAuthenticator,
AuthorizedUserAuthenticator, DeviceFlowAuthenticator, ExternalAccountAuthenticator,
InstalledFlowAuthenticator, ServiceAccountImpersonationAuthenticator,
ApplicationDefaultCredentialsAuthenticator, AuthorizedUserAuthenticator,
DeviceFlowAuthenticator, ExternalAccountAuthenticator, InstalledFlowAuthenticator,
ServiceAccountImpersonationAuthenticator,
};

pub use crate::helper::*;
Expand Down

0 comments on commit e711b1b

Please sign in to comment.