Skip to content

Commit

Permalink
Initialize rustls crypto provider if not otherwise done
Browse files Browse the repository at this point in the history
  • Loading branch information
dermesser committed Jun 11, 2024
1 parent a55609d commit 41d37ca
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ 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"]

[dependencies]
Expand All @@ -43,7 +43,7 @@ hyper-rustls = { version = "0.27", optional = true, default-features = false, fe
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 }
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() {

Check failure on line 1071 in src/authenticator.rs

View workflow job for this annotation

GitHub Actions / yup-oauth2 (hyper-rustls)

failed to resolve: use of undeclared crate or module `rustls`

Check failure on line 1071 in src/authenticator.rs

View workflow job for this annotation

GitHub Actions / yup-oauth2 (hyper-rustls)

failed to resolve: use of undeclared crate or module `rustls`
let _ = rustls::crypto::ring::default_provider().install_default();

Check failure on line 1072 in src/authenticator.rs

View workflow job for this annotation

GitHub Actions / yup-oauth2 (hyper-rustls)

failed to resolve: use of undeclared crate or module `rustls`

Check failure on line 1072 in src/authenticator.rs

View workflow job for this annotation

GitHub Actions / yup-oauth2 (hyper-rustls)

failed to resolve: use of undeclared crate or module `rustls`
}
}

#[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 41d37ca

Please sign in to comment.