use crate::http_client::HttpClientExt;
use crate::wire::Secret;
use futures::lock::Mutex;
use std::fmt;
use std::path::PathBuf;
use std::sync::Arc;
pub use crate::providers::internal::auth::{DeviceCodeHandler, DeviceCodePrompt};
#[cfg(not(target_family = "wasm"))]
mod native;
#[derive(Clone)]
pub enum AuthSource {
AccessToken {
access_token: String,
account_id: Option<String>,
},
OAuth,
}
impl fmt::Debug for AuthSource {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::AccessToken { .. } => f.write_str("AccessToken(<redacted>)"),
Self::OAuth => f.write_str("OAuth"),
}
}
}
#[derive(Clone, Debug)]
#[cfg_attr(target_family = "wasm", allow(dead_code))]
pub struct Authenticator {
source: AuthSource,
auth_file: Option<PathBuf>,
device_code_handler: DeviceCodeHandler,
allow_device_flow: bool,
refresh_lock: Arc<Mutex<()>>,
}
pub use crate::providers::internal::auth::AuthError;
#[derive(Debug, Clone)]
pub struct AuthContext {
pub access_token: Secret,
pub account_id: Option<String>,
}
impl Authenticator {
pub fn new(
source: AuthSource,
auth_file: Option<PathBuf>,
device_code_handler: DeviceCodeHandler,
allow_device_flow: bool,
) -> Self {
Self {
source,
auth_file,
device_code_handler,
allow_device_flow,
refresh_lock: Arc::default(),
}
}
#[cfg_attr(target_family = "wasm", allow(unused_variables))]
pub async fn auth_context<H>(&self, http: &H) -> Result<AuthContext, AuthError>
where
H: HttpClientExt,
{
match &self.source {
AuthSource::AccessToken {
access_token,
account_id,
} => Ok(AuthContext {
access_token: access_token.clone().into(),
account_id: account_id.clone(),
}),
#[cfg(not(target_family = "wasm"))]
AuthSource::OAuth => self.auth_context_oauth(http).await,
#[cfg(target_family = "wasm")]
AuthSource::OAuth => Err(AuthError::Message(
"ChatGPT OAuth is not supported on wasm targets".into(),
)),
}
}
}