pub mod browser;
pub mod config;
pub mod device;
pub mod pkce;
pub mod token;
mod user;
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
use std::time::Duration;
use subspace_core::Secret;
pub use config::{Endpoints, OAuthClient};
pub const SIGN_IN_TIME_LIMIT: Duration = Duration::from_secs(5 * 60);
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
pub enum AuthError {
#[error("Sign-in was cancelled.")]
Cancelled,
#[error("GitHub sign-in took too long. Try again.")]
TimedOut,
#[error("The browser's reply did not belong to this sign-in. Try again.")]
StateMismatch,
#[error("GitHub sign-in was declined in the browser.")]
Denied,
#[error("The code expired before it was entered on GitHub. Try again.")]
Expired,
#[error(
"GitHub does not allow sign-in with a code for this app yet. Use a release build of \
the app, or run `gh auth login`."
)]
DeviceFlowDisabled,
#[error("GitHub did not grant {0}. Sign in again and approve every permission.")]
MissingScopes(String),
#[error("Subspace cannot wait for the browser: {0}.")]
Listener(String),
#[error("GitHub could not complete the sign-in: {0}")]
Service(String),
#[error("GitHub could not be reached: {0}")]
Network(String),
}
#[derive(Debug, Clone, Default)]
pub struct Cancel(Arc<AtomicBool>);
impl Cancel {
pub fn cancel(&self) {
self.0.store(true, Ordering::Relaxed);
}
pub fn is_cancelled(&self) -> bool {
self.0.load(Ordering::Relaxed)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Prompt {
Browser { url: String },
Device {
user_code: String,
verification_uri: String,
},
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SignedIn {
pub login: String,
pub token: Secret,
}
pub trait Pending: Send {
fn finish(self: Box<Self>, cancel: &Cancel) -> Result<SignedIn, AuthError>;
}
pub struct Started {
pub prompt: Prompt,
pub pending: Box<dyn Pending>,
}
pub trait Authenticator: Send + Sync {
fn start(&self) -> Result<Started, AuthError>;
fn revoke(&self, token: &Secret);
}
#[derive(Debug, Clone)]
pub struct GitHubOAuth {
client: OAuthClient,
endpoints: Endpoints,
time_limit: Duration,
}
impl GitHubOAuth {
pub fn for_client(client: OAuthClient) -> Self {
Self::new(client, Endpoints::from_build())
}
pub fn new(client: OAuthClient, endpoints: Endpoints) -> Self {
Self {
client,
endpoints,
time_limit: SIGN_IN_TIME_LIMIT,
}
}
pub fn with_time_limit(mut self, time_limit: Duration) -> Self {
self.time_limit = time_limit;
self
}
}
impl Authenticator for GitHubOAuth {
fn start(&self) -> Result<Started, AuthError> {
match &self.client.secret {
Some(secret) => {
browser::start(&self.client.id, secret, &self.endpoints, self.time_limit)
}
None => device::start(&self.client.id, &self.endpoints, self.time_limit),
}
}
fn revoke(&self, token: &Secret) {
if let Some(secret) = &self.client.secret {
user::revoke(&self.endpoints, (&self.client.id, secret), token);
}
}
}
pub(crate) fn signed_in(
endpoints: &Endpoints,
granted: token::Granted,
) -> Result<SignedIn, AuthError> {
token::check_scopes(&granted.scopes)?;
let login = user::login(endpoints, &granted.token)?;
Ok(SignedIn {
login,
token: granted.token,
})
}
pub(crate) fn sleep(duration: Duration, cancel: &Cancel) -> Result<(), AuthError> {
const STEP: Duration = Duration::from_millis(100);
let mut left = duration;
while !left.is_zero() {
if cancel.is_cancelled() {
return Err(AuthError::Cancelled);
}
let step = left.min(STEP);
std::thread::sleep(step);
left -= step;
}
Ok(())
}