Skip to content

Commit

Permalink
Add DeviceCodes::poll_until_available method
Browse files Browse the repository at this point in the history
  • Loading branch information
mkrasnitski committed Aug 15, 2024
1 parent 70e9b9e commit 4141a62
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 27 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ serde_json = "1.0.64"
serde_path_to_error = "0.1.4"
serde_urlencoded = "0.7.1"
snafu = "0.8"
tokio = { version = "1.17.0", default-features = false, optional = true }
tokio = { version = "1.17.0", default-features = false, features = ["time"], optional = true }
tower = { version = "0.4.13", default-features = false, features = ["util", "buffer"] }
tower-http = { version = "0.5.1", features = ["map-response-body", "trace"] }
tracing = { version = "0.1.37", features = ["log"], optional = true }
Expand Down
25 changes: 1 addition & 24 deletions examples/device_flow.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use either::Either;
use http::header::ACCEPT;
use std::time::Duration;

#[tokio::main]
async fn main() -> octocrab::Result<()> {
Expand All @@ -17,28 +15,7 @@ async fn main() -> octocrab::Result<()> {
"Go to {} and enter code {}",
codes.verification_uri, codes.user_code
);
let mut interval = Duration::from_secs(codes.interval);
let mut clock = tokio::time::interval(interval);
let auth = loop {
clock.tick().await;
match codes.poll_once(&crab, &client_id).await? {
Either::Left(auth) => break auth,
Either::Right(cont) => match cont {
octocrab::auth::Continue::SlowDown => {
// We were request to slow down. We add five seconds to the polling
// duration.
interval += Duration::from_secs(5);
clock = tokio::time::interval(interval);
// The first tick happens instantly, so we tick that off immediately.
clock.tick().await;
}
octocrab::auth::Continue::AuthorizationPending => {
// The user has not clicked authorize yet, but nothing has gone wrong.
// We keep polling.
}
},
}
};
let auth = codes.poll_until_available(&crab, &client_id).await?;

println!("Authorization succeeded with access to {:?}", auth.scope);
Ok(())
Expand Down
35 changes: 33 additions & 2 deletions src/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use jsonwebtoken::{Algorithm, EncodingKey, Header};
use secrecy::{ExposeSecret, SecretString};
use serde::{Deserialize, Serialize};
use std::fmt;
use std::time::SystemTime;
use std::time::{Duration, SystemTime};

use snafu::*;

Expand Down Expand Up @@ -204,7 +204,7 @@ pub struct DeviceCodes {
impl DeviceCodes {
/// Poll Github to see if authentication codes are available.
///
/// See `https://docs.github.com/en/developers/apps/building-oauth-apps/authorizing-oauth-apps#response-parameters` for details.
/// See `https://docs.github.com/en/developers/apps/building-oauth-apps/authorizing-oauth-apps` for details.
pub async fn poll_once(
&self,
crab: &crate::Octocrab,
Expand All @@ -225,6 +225,37 @@ impl DeviceCodes {
TokenResponse::Continue { error } => Either::Right(error),
})
}

/// Poll Github in a loop until authentication codes become available.
#[cfg(feature = "tokio")]
pub async fn poll_until_available(
&self,
crab: &crate::Octocrab,
client_id: &SecretString,
) -> Result<OAuth> {
let mut interval = Duration::from_secs(self.interval);
let mut clock = tokio::time::interval(interval);

loop {
clock.tick().await;
match self.poll_once(crab, client_id).await? {
Either::Left(auth) => return Ok(auth),
Either::Right(cont) => match cont {
Continue::SlowDown => {
// We were requested to slow down, so add five seconds to the polling
// duration.
interval += Duration::from_secs(5);
clock = tokio::time::interval(interval);
// The first tick happens instantly, so we tick that off immediately.
clock.tick().await;
}
Continue::AuthorizationPending => {
// The user has not clicked authorize yet, so we keep polling as normal.
}
},
}
}
}
}

/// See https://docs.github.com/en/developers/apps/building-oauth-apps/authorizing-oauth-apps#input-parameters
Expand Down

0 comments on commit 4141a62

Please sign in to comment.