#![cfg_attr(not(test), forbid(unsafe_code))]
#![deny(missing_docs)]
#[doc = include_str!("../README.md")]
#[cfg(doctest)]
pub struct ReadmeDoctests;
mod credentials;
pub mod discovery;
#[cfg(feature = "encrypted-store")]
mod encrypted_store;
mod error;
pub mod flow;
mod pkce;
mod provider;
pub use credentials::Credential;
pub use credentials::CredentialKind;
pub use credentials::CredentialStore;
pub use credentials::MemoryStore;
pub use discovery::OidcMetadata;
pub use discovery::discover;
#[cfg(feature = "encrypted-store")]
pub use encrypted_store::EncryptedStore;
pub use error::Error;
pub use error::Result;
pub use flow::authorization_code::constant_time_eq;
pub use pkce::PkceCodes;
pub use pkce::generate_pkce;
pub use pkce::generate_state;
pub use provider::ApiKeyConfig;
pub use provider::AuthMethod;
pub use provider::DeviceCodeConfig;
pub use provider::OAuthConfig;
pub use provider::Provider;
pub use provider::Registry;
use std::time::Duration;
use crate::flow::authorization_code::BeganAuthorization;
use crate::flow::device_code::DeviceAuthorization;
#[derive(Clone)]
pub struct AuthClient<S: CredentialStore> {
registry: Registry,
store: S,
}
impl<S: CredentialStore> AuthClient<S> {
pub fn builtin(store: S) -> Result<Self> {
Ok(Self {
registry: Registry::builtin()?,
store,
})
}
pub fn new(registry: Registry, store: S) -> Self {
Self { registry, store }
}
pub fn registry(&self) -> &Registry {
&self.registry
}
pub fn providers(&self) -> &[Provider] {
self.registry.providers()
}
pub fn store(&self) -> &S {
&self.store
}
pub fn begin_oauth(&self, provider_id: &str) -> Result<BeganAuthorization> {
let provider = self.registry.require(provider_id)?;
let config = provider.oauth().ok_or_else(|| Error::UnsupportedMethod {
provider: provider_id.to_string(),
method: "oauth",
})?;
flow::authorization_code::begin(provider_id, config)
}
pub async fn begin_device(&self, provider_id: &str) -> Result<DeviceAuthorization> {
let provider = self.registry.require(provider_id)?;
let config = provider.device().ok_or_else(|| Error::UnsupportedMethod {
provider: provider_id.to_string(),
method: "device",
})?;
flow::device_code::request(provider_id, config).await
}
pub async fn complete_device(
&self,
auth: DeviceAuthorization,
max_wait: Duration,
) -> Result<Credential> {
auth.poll(max_wait).await
}
pub fn accept_api_key(&self, provider_id: &str, key: &str) -> Result<Credential> {
let provider = self.registry.require(provider_id)?;
let config = provider.api_key().ok_or_else(|| Error::UnsupportedMethod {
provider: provider_id.to_string(),
method: "api_key",
})?;
flow::api_key::accept(provider_id, config, key)
}
pub fn api_key_from_env(&self, provider_id: &str) -> Option<String> {
let config = self.registry.get(provider_id)?.api_key()?;
flow::api_key::from_env(config)
}
pub async fn refresh_stored(&self, provider_id: &str) -> Result<Credential> {
let provider = self.registry.require(provider_id)?;
let config = provider.oauth().ok_or_else(|| Error::UnsupportedMethod {
provider: provider_id.to_string(),
method: "oauth",
})?;
let existing = self
.store
.get(provider_id)?
.ok_or_else(|| Error::Store(format!("no stored credential for `{provider_id}`")))?;
let refresh_token = existing
.refresh_token()
.cloned()
.ok_or_else(|| Error::Store("stored credential has no refresh token".into()))?;
let mut refreshed = flow::refresh(
provider_id,
&config.token_url,
&config.client_id,
&refresh_token,
)
.await?;
if let (
Credential::OAuth {
refresh_token: new, ..
},
Some(old),
) = (&mut refreshed, existing.refresh_token())
{
if new.is_none() {
*new = Some(old.clone());
}
}
self.store.put(&refreshed)?;
Ok(refreshed)
}
pub fn logout(&self, provider_id: &str) -> Result<()> {
self.store.delete(provider_id)
}
}
pub const DEFAULT_SIGN_IN_TIMEOUT: Duration = Duration::from_secs(300);
#[cfg(test)]
mod tests;