mirror of
https://github.com/OMGeeky/yup-oauth2.git
synced 2026-01-08 03:57:01 +01:00
Implement an ability to work without default client
This commit is contained in:
@@ -132,11 +132,6 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
enum StorageType {
|
||||
Memory,
|
||||
Disk(PathBuf),
|
||||
}
|
||||
|
||||
/// Configure an Authenticator using the builder pattern.
|
||||
pub struct AuthenticatorBuilder<C, F> {
|
||||
hyper_client_builder: C,
|
||||
@@ -171,9 +166,16 @@ impl InstalledFlowAuthenticator {
|
||||
app_secret: ApplicationSecret,
|
||||
method: InstalledFlowReturnMethod,
|
||||
) -> AuthenticatorBuilder<DefaultHyperClient, InstalledFlow> {
|
||||
AuthenticatorBuilder::<DefaultHyperClient, _>::with_auth_flow(InstalledFlow::new(
|
||||
app_secret, method,
|
||||
))
|
||||
Self::with_client(app_secret, method, DefaultHyperClient)
|
||||
}
|
||||
|
||||
/// Construct a new Authenticator that uses the installed flow and the provided http client.
|
||||
pub fn with_client<C>(
|
||||
app_secret: ApplicationSecret,
|
||||
method: InstalledFlowReturnMethod,
|
||||
client: C,
|
||||
) -> AuthenticatorBuilder<C, InstalledFlow> {
|
||||
AuthenticatorBuilder::new(InstalledFlow::new(app_secret, method), client)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -198,7 +200,15 @@ impl DeviceFlowAuthenticator {
|
||||
pub fn builder(
|
||||
app_secret: ApplicationSecret,
|
||||
) -> AuthenticatorBuilder<DefaultHyperClient, DeviceFlow> {
|
||||
AuthenticatorBuilder::<DefaultHyperClient, _>::with_auth_flow(DeviceFlow::new(app_secret))
|
||||
Self::with_client(app_secret, DefaultHyperClient)
|
||||
}
|
||||
|
||||
/// Construct a new Authenticator that uses the installed flow and the provided http client.
|
||||
pub fn with_client<C>(
|
||||
app_secret: ApplicationSecret,
|
||||
client: C,
|
||||
) -> AuthenticatorBuilder<C, DeviceFlow> {
|
||||
AuthenticatorBuilder::new(DeviceFlow::new(app_secret), client)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -223,10 +233,21 @@ impl ServiceAccountAuthenticator {
|
||||
pub fn builder(
|
||||
service_account_key: ServiceAccountKey,
|
||||
) -> AuthenticatorBuilder<DefaultHyperClient, ServiceAccountFlowOpts> {
|
||||
AuthenticatorBuilder::<DefaultHyperClient, _>::with_auth_flow(ServiceAccountFlowOpts {
|
||||
key: service_account_key,
|
||||
subject: None,
|
||||
})
|
||||
Self::with_client(service_account_key, DefaultHyperClient)
|
||||
}
|
||||
|
||||
/// Construct a new Authenticator that uses the installed flow and the provided http client.
|
||||
pub fn with_client<C>(
|
||||
service_account_key: ServiceAccountKey,
|
||||
client: C,
|
||||
) -> AuthenticatorBuilder<C, ServiceAccountFlowOpts> {
|
||||
AuthenticatorBuilder::new(
|
||||
ServiceAccountFlowOpts {
|
||||
key: service_account_key,
|
||||
subject: None,
|
||||
},
|
||||
client,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -270,14 +291,9 @@ impl<C, F> AuthenticatorBuilder<C, F> {
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(any(feature = "hyper-rustls", feature = "hyper-tls"))]
|
||||
#[cfg_attr(
|
||||
yup_oauth2_docsrs,
|
||||
doc(cfg(any(feature = "hyper-rustls", feature = "hyper-tls")))
|
||||
)]
|
||||
fn with_auth_flow(auth_flow: F) -> AuthenticatorBuilder<DefaultHyperClient, F> {
|
||||
fn new(auth_flow: F, hyper_client_builder: C) -> AuthenticatorBuilder<C, F> {
|
||||
AuthenticatorBuilder {
|
||||
hyper_client_builder: DefaultHyperClient,
|
||||
hyper_client_builder,
|
||||
storage_type: StorageType::Memory,
|
||||
auth_flow,
|
||||
}
|
||||
@@ -509,17 +525,6 @@ pub trait HyperClientBuilder {
|
||||
fn build_hyper_client(self) -> hyper::Client<Self::Connector>;
|
||||
}
|
||||
|
||||
impl<C> HyperClientBuilder for hyper::Client<C>
|
||||
where
|
||||
C: hyper::client::connect::Connect + Clone + Send + Sync + 'static,
|
||||
{
|
||||
type Connector = C;
|
||||
|
||||
fn build_hyper_client(self) -> hyper::Client<C> {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "hyper-rustls")]
|
||||
#[cfg_attr(
|
||||
yup_oauth2_docsrs,
|
||||
|
||||
@@ -211,9 +211,7 @@ impl ServiceAccountFlow {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
|
||||
use super::*;
|
||||
use crate::authenticator::HyperClientBuilder;
|
||||
use crate::helper::read_service_account_key;
|
||||
|
||||
// Valid but deactivated key.
|
||||
@@ -222,13 +220,13 @@ mod tests {
|
||||
// Uncomment this test to verify that we can successfully obtain tokens.
|
||||
//#[tokio::test]
|
||||
#[allow(dead_code)]
|
||||
#[cfg(any(feature = "hyper-rustls", feature = "hyper-tls"))]
|
||||
async fn test_service_account_e2e() {
|
||||
let key = read_service_account_key(TEST_PRIVATE_KEY_PATH)
|
||||
.await
|
||||
.unwrap();
|
||||
let acc = ServiceAccountFlow::new(ServiceAccountFlowOpts { key, subject: None }).unwrap();
|
||||
let client = crate::authenticator::DefaultHyperClient.build_hyper_client();
|
||||
let client =
|
||||
hyper::Client::builder().build(hyper_rustls::HttpsConnector::with_native_roots());
|
||||
println!(
|
||||
"{:?}",
|
||||
acc.token(&client, &["https://www.googleapis.com/auth/pubsub"])
|
||||
|
||||
Reference in New Issue
Block a user