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 52e85ae
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 26 deletions.
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
34 changes: 32 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,36 @@ impl DeviceCodes {
TokenResponse::Continue { error } => Either::Right(error),
})
}

/// Poll Github in a loop until authentication codes become available.
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 52e85ae

Please sign in to comment.