Skip to content

Commit

Permalink
Make rustls dependency optional by introducing "service_account" feature
Browse files Browse the repository at this point in the history
Now, service_account code must be (implicitly) enabled.

Asked for in feature #168
  • Loading branch information
dermesser committed Feb 22, 2022
1 parent d61ab10 commit 9b81a71
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 4 deletions.
11 changes: 8 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,21 @@ edition = "2018"
name = "custom_flow"
required-features = ["hyper-rustls"]

[[example]]
name = "custom_client"
required-features = ["hyper-rustls", "service_account"]

[[example]]
name = "custom_storage"
required-features = ["hyper-rustls"]

[[test]]
name = "tests"
required-features = ["hyper-rustls"]
required-features = ["hyper-rustls", "service_account"]

[features]
default = ["hyper-rustls"]
default = ["hyper-rustls", "service_account"]
service_account = ["rustls"]

[dependencies]
base64 = "0.13.0"
Expand All @@ -33,7 +38,7 @@ hyper = { version = "0.14", features = ["client", "server", "tcp", "http2"] }
hyper-rustls = { version = "0.22.1", optional = true }
hyper-tls = { version = "0.5.0", optional = true }
log = "0.4"
rustls = "0.19"
rustls = { version = "0.19", optional = true }
seahash = "4"
serde = {version = "1.0", features = ["derive"]}
serde_json = "1.0"
Expand Down
10 changes: 10 additions & 0 deletions src/authenticator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ use crate::device::DeviceFlow;
use crate::error::Error;
use crate::installed::{InstalledFlow, InstalledFlowReturnMethod};
use crate::refresh::RefreshFlow;

#[cfg(feature = "service_account")]
use crate::service_account::{ServiceAccountFlow, ServiceAccountFlowOpts, ServiceAccountKey};
use crate::storage::{self, Storage, TokenStorage};
use crate::types::{AccessToken, ApplicationSecret, TokenInfo};
Expand Down Expand Up @@ -242,7 +244,10 @@ impl DeviceFlowAuthenticator {
/// .expect("failed to create authenticator");
/// # }
/// ```
#[cfg(feature = "service_account")]
pub struct ServiceAccountAuthenticator;

#[cfg(feature = "service_account")]
impl ServiceAccountAuthenticator {
/// Use the builder pattern to create an Authenticator that uses a service account.
#[cfg(any(feature = "hyper-rustls", feature = "hyper-tls"))]
Expand Down Expand Up @@ -549,6 +554,7 @@ impl<C> AuthenticatorBuilder<C, InstalledFlow> {
/// .expect("failed to create authenticator");
/// # }
/// ```
#[cfg(feature = "service_account")]
impl<C> AuthenticatorBuilder<C, ServiceAccountFlowOpts> {
/// Use the provided subject.
pub fn subject(self, subject: impl Into<String>) -> Self {
Expand Down Expand Up @@ -598,12 +604,14 @@ mod private {
use crate::device::DeviceFlow;
use crate::error::Error;
use crate::installed::InstalledFlow;
#[cfg(feature = "service_account")]
use crate::service_account::ServiceAccountFlow;
use crate::types::{ApplicationSecret, TokenInfo};

pub enum AuthFlow {
DeviceFlow(DeviceFlow),
InstalledFlow(InstalledFlow),
#[cfg(feature = "service_account")]
ServiceAccountFlow(ServiceAccountFlow),
ApplicationDefaultCredentialsFlow(ApplicationDefaultCredentialsFlow),
}
Expand All @@ -613,6 +621,7 @@ mod private {
match self {
AuthFlow::DeviceFlow(device_flow) => Some(&device_flow.app_secret),
AuthFlow::InstalledFlow(installed_flow) => Some(&installed_flow.app_secret),
#[cfg(feature = "service_account")]
AuthFlow::ServiceAccountFlow(_) => None,
AuthFlow::ApplicationDefaultCredentialsFlow(_) => None,
}
Expand All @@ -632,6 +641,7 @@ mod private {
AuthFlow::InstalledFlow(installed_flow) => {
installed_flow.token(hyper_client, scopes).await
}
#[cfg(feature = "service_account")]
AuthFlow::ServiceAccountFlow(service_account_flow) => {
service_account_flow.token(hyper_client, scopes).await
}
Expand Down
2 changes: 2 additions & 0 deletions src/helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// Copyright (c) 2016 Google Inc ([email protected]).
//
// Refer to the project root for licensing information.
#[cfg(feature = "service_account")]
use crate::service_account::ServiceAccountKey;
use crate::types::{ApplicationSecret, ConsoleApplicationSecret};

Expand Down Expand Up @@ -39,6 +40,7 @@ pub fn parse_application_secret<S: AsRef<[u8]>>(secret: S) -> io::Result<Applica

/// Read a service account key from a JSON file. You can download the JSON keys from the Google
/// Cloud Console or the respective console of your service provider.
#[cfg(feature = "service_account")]
pub async fn read_service_account_key<P: AsRef<Path>>(path: P) -> io::Result<ServiceAccountKey> {
let key = tokio::fs::read(path).await?;
parse_service_account_key(key)
Expand Down
7 changes: 6 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ pub mod error;
mod helper;
mod installed;
mod refresh;

#[cfg(feature = "service_account")]
mod service_account;

/// Interface for storing tokens so that they can be re-used. There are built-in memory and
Expand All @@ -91,13 +93,16 @@ mod types;
#[doc(inline)]
pub use crate::authenticator::{
ApplicationDefaultCredentialsAuthenticator, DeviceFlowAuthenticator,
InstalledFlowAuthenticator, ServiceAccountAuthenticator,
InstalledFlowAuthenticator
};
#[cfg(feature = "service_account")]
pub use crate::authenticator::ServiceAccountAuthenticator;

pub use crate::helper::*;
pub use crate::installed::InstalledFlowReturnMethod;

pub use crate::application_default_credentials::ApplicationDefaultCredentialsFlowOpts;
#[cfg(feature = "service_account")]
pub use crate::service_account::ServiceAccountKey;

#[doc(inline)]
Expand Down
2 changes: 2 additions & 0 deletions src/service_account.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![cfg(feature = "service_account")]

//! This module provides a flow that obtains tokens for service accounts.
//!
//! Service accounts are usually used by software (i.e., non-human actors) to get access to
Expand Down

0 comments on commit 9b81a71

Please sign in to comment.