Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add timeout option #239

Merged
merged 11 commits into from
Aug 6, 2024
19 changes: 15 additions & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ name: Actions CI

jobs:
build_and_test:
name: yup-oauth2
name: Run tests
runs-on: ubuntu-latest
strategy:
fail-fast: false
Expand All @@ -21,7 +21,7 @@ jobs:
- run: cargo test --no-default-features --features ${{ matrix.features }}

doc:
name: yup-oauth2
name: Create docs
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
Expand All @@ -30,14 +30,25 @@ jobs:
toolchain: nightly
- run: cargo doc --no-deps --all-features
env:
RUSTDOCFLAGS: --cfg yup_oauth2_docsrs
RUSTDOCFLAGS: --cfg docsrs -D warnings

fmt:
name: yup-oauth2
name: Check formatting
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
with:
toolchain: stable
- run: cargo fmt -- --check

clippy:
name: Run clippy lints
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
with:
toolchain: stable
components: clippy
- run: cargo clippy --all-features --all-targets -- -D warnings
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,4 @@ members = ["examples/test-installed/", "examples/test-svc-acct/", "examples/test

[package.metadata.docs.rs]
all-features = true
rustdoc-args = ["--cfg", "yup_oauth2_docsrs"]
rustdoc-args = ["--cfg", "docsrs"]
15 changes: 10 additions & 5 deletions examples/custom_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
//!
//! It is also a better use of resources (memory, sockets, etc.)

use std::time::Duration;

use hyper_util::client::legacy::connect::Connect;
use yup_oauth2::authenticator::HyperClientBuilder;

async fn r#use<C>(
client: hyper_util::client::legacy::Client<C, String>,
Expand Down Expand Up @@ -43,11 +46,13 @@ async fn main() {
.enable_http2()
.build(),
);
let authenticator =
yup_oauth2::ServiceAccountAuthenticator::with_client(secret, client.clone())
.build()
.await
.expect("could not create an authenticator");
let authenticator = yup_oauth2::ServiceAccountAuthenticator::with_client(
secret,
yup_oauth2::CustomHyperClient::from(client.clone()).with_timeout(Duration::from_secs(10)),
)
.build()
.await
.expect("could not create an authenticator");
r#use(client, authenticator)
.await
.expect("use is successful!");
Expand Down
7 changes: 3 additions & 4 deletions src/access_token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
//! The intention behind this is that if two services using the
//! same refresh token then each service will invalitate the
//! access token of the other service by generating a new token.
use crate::client::SendRequest;
use crate::error::Error;
use crate::types::TokenInfo;
use hyper_util::client::legacy::connect::Connect;

/// the flow for the access token authenticator
pub struct AccessTokenFlow {
Expand All @@ -16,14 +16,13 @@ pub struct AccessTokenFlow {

impl AccessTokenFlow {
/// just return the access token
pub(crate) async fn token<C, B, T>(
pub(crate) async fn token<T>(
&self,
_hyper_client: &hyper_util::client::legacy::Client<C, B>,
_hyper_client: &impl SendRequest,
_scopes: &[T],
) -> Result<TokenInfo, Error>
where
T: AsRef<str>,
C: Connect + Clone + Send + Sync + 'static,
{
Ok(TokenInfo {
access_token: Some(self.access_token.clone()),
Expand Down
7 changes: 3 additions & 4 deletions src/application_default_credentials.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::client::SendRequest;
use crate::error::Error;
use crate::types::TokenInfo;
use http_body_util::BodyExt;
use hyper_util::client::legacy::connect::Connect;

/// Provide options for the Application Default Credential Flow, mostly used for testing
#[derive(Default, Clone, Debug)]
Expand All @@ -20,14 +20,13 @@ impl ApplicationDefaultCredentialsFlow {
ApplicationDefaultCredentialsFlow { metadata_url }
}

pub(crate) async fn token<C, T>(
pub(crate) async fn token<T>(
&self,
hyper_client: &hyper_util::client::legacy::Client<C, String>,
hyper_client: &impl SendRequest,
scopes: &[T],
) -> Result<TokenInfo, Error>
where
T: AsRef<str>,
C: Connect + Clone + Send + Sync + 'static,
{
let scope = crate::helper::join(scopes, ",");
let token_uri = format!("{}?scopes={}", self.metadata_url, scope);
Expand Down
Loading