use std::collections::HashMap;
use std::path::Path;
use std::sync::Arc;
use crate::error::{AuthError, CloudError, Result};
#[derive(Debug, Clone)]
pub enum Credentials {
None,
ApiKey {
key: String,
},
AccessKey {
access_key: String,
secret_key: String,
session_token: Option<String>,
},
OAuth2 {
access_token: String,
refresh_token: Option<String>,
expires_at: Option<chrono::DateTime<chrono::Utc>>,
},
ServiceAccount {
key_json: String,
project_id: Option<String>,
},
SasToken {
token: String,
expires_at: Option<chrono::DateTime<chrono::Utc>>,
},
IamRole {
role_arn: String,
session_name: String,
},
Custom {
data: HashMap<String, String>,
},
}
impl Credentials {
#[must_use]
pub fn api_key(key: impl Into<String>) -> Self {
Self::ApiKey { key: key.into() }
}
#[must_use]
pub fn access_key(access_key: impl Into<String>, secret_key: impl Into<String>) -> Self {
Self::AccessKey {
access_key: access_key.into(),
secret_key: secret_key.into(),
session_token: None,
}
}
#[must_use]
pub fn access_key_with_session(
access_key: impl Into<String>,
secret_key: impl Into<String>,
session_token: impl Into<String>,
) -> Self {
Self::AccessKey {
access_key: access_key.into(),
secret_key: secret_key.into(),
session_token: Some(session_token.into()),
}
}
#[must_use]
pub fn oauth2(access_token: impl Into<String>) -> Self {
Self::OAuth2 {
access_token: access_token.into(),
refresh_token: None,
expires_at: None,
}
}
#[must_use]
pub fn oauth2_with_refresh(
access_token: impl Into<String>,
refresh_token: impl Into<String>,
) -> Self {
Self::OAuth2 {
access_token: access_token.into(),
refresh_token: Some(refresh_token.into()),
expires_at: None,
}
}
pub fn service_account_from_json(json: impl Into<String>) -> Result<Self> {
let json_str = json.into();
let parsed: serde_json::Value = serde_json::from_str(&json_str).map_err(|e| {
CloudError::Auth(AuthError::ServiceAccountKey {
message: format!("Invalid JSON: {e}"),
})
})?;
let project_id = parsed
.get("project_id")
.and_then(|v| v.as_str())
.map(|s| s.to_string());
Ok(Self::ServiceAccount {
key_json: json_str,
project_id,
})
}
pub fn service_account_from_file(path: impl AsRef<Path>) -> Result<Self> {
let content = std::fs::read_to_string(path.as_ref()).map_err(|e| {
CloudError::Auth(AuthError::ServiceAccountKey {
message: format!("Failed to read service account key file: {e}"),
})
})?;
Self::service_account_from_json(content)
}
#[must_use]
pub fn sas_token(token: impl Into<String>) -> Self {
Self::SasToken {
token: token.into(),
expires_at: None,
}
}
#[must_use]
pub fn iam_role(role_arn: impl Into<String>, session_name: impl Into<String>) -> Self {
Self::IamRole {
role_arn: role_arn.into(),
session_name: session_name.into(),
}
}
#[must_use]
pub fn is_expired(&self) -> bool {
let now = chrono::Utc::now();
match self {
Self::OAuth2 {
expires_at: Some(expiry),
..
} => *expiry <= now,
Self::SasToken {
expires_at: Some(expiry),
..
} => *expiry <= now,
_ => false,
}
}
#[must_use]
pub fn needs_refresh(&self) -> bool {
let now = chrono::Utc::now();
let buffer = chrono::Duration::minutes(5);
match self {
Self::OAuth2 {
expires_at: Some(expiry),
..
} => *expiry <= now + buffer,
Self::SasToken {
expires_at: Some(expiry),
..
} => *expiry <= now + buffer,
_ => false,
}
}
#[must_use]
pub fn variant_name(&self) -> &'static str {
match self {
Self::None => "None",
Self::ApiKey { .. } => "ApiKey",
Self::AccessKey { .. } => "AccessKey",
Self::OAuth2 { .. } => "OAuth2",
Self::ServiceAccount { .. } => "ServiceAccount",
Self::SasToken { .. } => "SasToken",
Self::IamRole { .. } => "IamRole",
Self::Custom { .. } => "Custom",
}
}
}
#[cfg(feature = "async")]
#[async_trait::async_trait]
pub trait CredentialProvider: Send + Sync {
async fn load(&self) -> Result<Credentials>;
async fn refresh(&self, _credentials: &Credentials) -> Result<Credentials> {
self.load().await
}
}
#[cfg(feature = "async")]
pub struct RefreshingCredentials {
current: tokio::sync::RwLock<Credentials>,
provider: Option<Arc<dyn CredentialProvider>>,
}
#[cfg(feature = "async")]
impl std::fmt::Debug for RefreshingCredentials {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("RefreshingCredentials")
.field("has_provider", &self.provider.is_some())
.finish_non_exhaustive()
}
}
#[cfg(feature = "async")]
impl RefreshingCredentials {
#[must_use]
pub fn new(initial: Credentials, provider: Option<Arc<dyn CredentialProvider>>) -> Self {
Self {
current: tokio::sync::RwLock::new(initial),
provider,
}
}
#[must_use]
pub fn static_credentials(credentials: Credentials) -> Self {
Self::new(credentials, None)
}
pub async fn ensure_fresh(&self) -> Result<Credentials> {
{
let guard = self.current.read().await;
if !guard.needs_refresh() {
return Ok(guard.clone());
}
}
let Some(provider) = &self.provider else {
let guard = self.current.read().await;
tracing::warn!(
"Credentials ({}) are expiring or already expired and no CredentialProvider \
is configured to refresh them; continuing with the current (possibly stale) \
credentials. Configure a CredentialProvider (e.g. OAuth2RefreshProvider) via \
RefreshingCredentials::new to enable automatic refresh.",
guard.variant_name(),
);
return Ok(guard.clone());
};
let mut guard = self.current.write().await;
if !guard.needs_refresh() {
return Ok(guard.clone());
}
let refreshed = provider.refresh(&guard).await?;
*guard = refreshed.clone();
Ok(refreshed)
}
pub async fn current(&self) -> Credentials {
self.current.read().await.clone()
}
}
#[cfg(all(feature = "http", feature = "async"))]
pub struct OAuth2RefreshProvider {
token_url: String,
client_id: String,
client_secret: Option<String>,
http_client: reqwest::Client,
}
#[cfg(all(feature = "http", feature = "async"))]
impl OAuth2RefreshProvider {
#[must_use]
pub fn new(token_url: impl Into<String>, client_id: impl Into<String>) -> Self {
Self {
token_url: token_url.into(),
client_id: client_id.into(),
client_secret: None,
http_client: reqwest::Client::new(),
}
}
#[must_use]
pub fn with_client_secret(mut self, client_secret: impl Into<String>) -> Self {
self.client_secret = Some(client_secret.into());
self
}
}
#[derive(Debug, serde::Deserialize)]
struct OAuth2TokenResponse {
access_token: String,
#[serde(default)]
refresh_token: Option<String>,
#[serde(default)]
expires_in: Option<i64>,
}
#[cfg(all(feature = "http", feature = "async"))]
#[async_trait::async_trait]
impl CredentialProvider for OAuth2RefreshProvider {
async fn load(&self) -> Result<Credentials> {
Err(CloudError::Auth(AuthError::OAuth2 {
message: "OAuth2RefreshProvider only supports refresh() of an existing \
refresh_token, not an initial load(); obtain the first access/refresh \
token pair from your application's own authorization flow and \
construct `Credentials::OAuth2` directly."
.to_string(),
}))
}
async fn refresh(&self, credentials: &Credentials) -> Result<Credentials> {
let refresh_token = match credentials {
Credentials::OAuth2 {
refresh_token: Some(rt),
..
} => rt.clone(),
Credentials::OAuth2 {
refresh_token: None,
..
} => {
return Err(CloudError::Auth(AuthError::OAuth2 {
message: "Cannot refresh an OAuth2 credential that has no refresh_token"
.to_string(),
}));
}
other => {
return Err(CloudError::Auth(AuthError::OAuth2 {
message: format!(
"OAuth2RefreshProvider can only refresh OAuth2 credentials, got '{}'",
other.variant_name()
),
}));
}
};
let mut form: Vec<(&str, String)> = vec![
("grant_type", "refresh_token".to_string()),
("refresh_token", refresh_token.clone()),
("client_id", self.client_id.clone()),
];
if let Some(secret) = &self.client_secret {
form.push(("client_secret", secret.clone()));
}
let response = self
.http_client
.post(&self.token_url)
.form(&form)
.send()
.await
.map_err(|e| {
CloudError::Auth(AuthError::OAuth2 {
message: format!("Token refresh request to '{}' failed: {e}", self.token_url),
})
})?;
let status = response.status();
if !status.is_success() {
let body = response.text().await.unwrap_or_default();
return Err(CloudError::Auth(AuthError::OAuth2 {
message: format!(
"Token endpoint '{}' rejected the refresh grant (HTTP {status}): {body}",
self.token_url
),
}));
}
let parsed: OAuth2TokenResponse = response.json().await.map_err(|e| {
CloudError::Auth(AuthError::OAuth2 {
message: format!("Failed to parse token endpoint response: {e}"),
})
})?;
let expires_at = parsed
.expires_in
.map(|secs| chrono::Utc::now() + chrono::Duration::seconds(secs));
Ok(Credentials::OAuth2 {
access_token: parsed.access_token,
refresh_token: parsed.refresh_token.or(Some(refresh_token)),
expires_at,
})
}
}
pub struct EnvCredentialProvider {
credential_type: CredentialType,
}
#[derive(Debug, Clone, Copy)]
pub enum CredentialType {
Aws,
Azure,
Gcp,
ApiKey,
}
impl EnvCredentialProvider {
#[must_use]
pub const fn new(credential_type: CredentialType) -> Self {
Self { credential_type }
}
fn load_aws() -> Result<Credentials> {
let access_key = std::env::var("AWS_ACCESS_KEY_ID").map_err(|_| {
CloudError::Auth(AuthError::CredentialsNotFound {
message: "AWS_ACCESS_KEY_ID not found".to_string(),
})
})?;
let secret_key = std::env::var("AWS_SECRET_ACCESS_KEY").map_err(|_| {
CloudError::Auth(AuthError::CredentialsNotFound {
message: "AWS_SECRET_ACCESS_KEY not found".to_string(),
})
})?;
let session_token = std::env::var("AWS_SESSION_TOKEN").ok();
Ok(Credentials::AccessKey {
access_key,
secret_key,
session_token,
})
}
fn load_azure() -> Result<Credentials> {
let account_name = std::env::var("AZURE_STORAGE_ACCOUNT").map_err(|_| {
CloudError::Auth(AuthError::CredentialsNotFound {
message: "AZURE_STORAGE_ACCOUNT not found".to_string(),
})
})?;
if let Ok(account_key) = std::env::var("AZURE_STORAGE_KEY") {
let mut data = HashMap::new();
data.insert("account_name".to_string(), account_name);
data.insert("account_key".to_string(), account_key);
Ok(Credentials::Custom { data })
} else if let Ok(sas_token) = std::env::var("AZURE_STORAGE_SAS_TOKEN") {
Ok(Credentials::SasToken {
token: sas_token,
expires_at: None,
})
} else {
Err(CloudError::Auth(AuthError::CredentialsNotFound {
message: "Neither AZURE_STORAGE_KEY nor AZURE_STORAGE_SAS_TOKEN found".to_string(),
}))
}
}
fn load_gcp() -> Result<Credentials> {
let key_file = std::env::var("GOOGLE_APPLICATION_CREDENTIALS").map_err(|_| {
CloudError::Auth(AuthError::CredentialsNotFound {
message: "GOOGLE_APPLICATION_CREDENTIALS not found".to_string(),
})
})?;
Credentials::service_account_from_file(&key_file)
}
fn load_api_key() -> Result<Credentials> {
let key = std::env::var("API_KEY")
.or_else(|_| std::env::var("APIKEY"))
.map_err(|_| {
CloudError::Auth(AuthError::CredentialsNotFound {
message: "API_KEY or APIKEY not found".to_string(),
})
})?;
Ok(Credentials::ApiKey { key })
}
}
#[cfg(feature = "async")]
#[async_trait::async_trait]
impl CredentialProvider for EnvCredentialProvider {
async fn load(&self) -> Result<Credentials> {
match self.credential_type {
CredentialType::Aws => Self::load_aws(),
CredentialType::Azure => Self::load_azure(),
CredentialType::Gcp => Self::load_gcp(),
CredentialType::ApiKey => Self::load_api_key(),
}
}
}
pub struct FileCredentialProvider {
path: std::path::PathBuf,
}
impl FileCredentialProvider {
#[must_use]
pub fn new(path: impl AsRef<Path>) -> Self {
Self {
path: path.as_ref().to_path_buf(),
}
}
}
#[cfg(feature = "async")]
#[async_trait::async_trait]
impl CredentialProvider for FileCredentialProvider {
async fn load(&self) -> Result<Credentials> {
Credentials::service_account_from_file(&self.path)
}
}
pub struct ChainCredentialProvider {
providers: Vec<Box<dyn CredentialProvider>>,
}
impl ChainCredentialProvider {
#[must_use]
pub fn new() -> Self {
Self {
providers: Vec::new(),
}
}
#[must_use]
pub fn with_provider(mut self, provider: Box<dyn CredentialProvider>) -> Self {
self.providers.push(provider);
self
}
}
impl Default for ChainCredentialProvider {
fn default() -> Self {
Self::new()
}
}
#[cfg(feature = "async")]
#[async_trait::async_trait]
impl CredentialProvider for ChainCredentialProvider {
async fn load(&self) -> Result<Credentials> {
for provider in &self.providers {
if let Ok(credentials) = provider.load().await {
return Ok(credentials);
}
}
Err(CloudError::Auth(AuthError::CredentialsNotFound {
message: "No credential provider succeeded".to_string(),
}))
}
}
#[cfg(test)]
#[allow(clippy::panic)]
mod tests {
use super::*;
#[test]
fn test_credentials_api_key() {
let creds = Credentials::api_key("test-key");
match creds {
Credentials::ApiKey { key } => assert_eq!(key, "test-key"),
_ => panic!("Expected ApiKey credentials"),
}
}
#[test]
fn test_credentials_access_key() {
let creds = Credentials::access_key("access", "secret");
match creds {
Credentials::AccessKey {
access_key,
secret_key,
session_token,
} => {
assert_eq!(access_key, "access");
assert_eq!(secret_key, "secret");
assert!(session_token.is_none());
}
_ => panic!("Expected AccessKey credentials"),
}
}
#[test]
fn test_credentials_oauth2() {
let creds = Credentials::oauth2("token");
match creds {
Credentials::OAuth2 { access_token, .. } => assert_eq!(access_token, "token"),
_ => panic!("Expected OAuth2 credentials"),
}
}
#[test]
fn test_credentials_sas_token() {
let creds = Credentials::sas_token("token");
match creds {
Credentials::SasToken { token, .. } => assert_eq!(token, "token"),
_ => panic!("Expected SasToken credentials"),
}
}
#[test]
fn test_credentials_iam_role() {
let creds = Credentials::iam_role("arn:aws:iam::123:role/test", "session");
match creds {
Credentials::IamRole {
role_arn,
session_name,
} => {
assert_eq!(role_arn, "arn:aws:iam::123:role/test");
assert_eq!(session_name, "session");
}
_ => panic!("Expected IamRole credentials"),
}
}
#[test]
fn test_credentials_service_account_from_json() {
let json = r#"{"type":"service_account","project_id":"test-project"}"#;
let creds = Credentials::service_account_from_json(json);
assert!(creds.is_ok());
match creds.ok() {
Some(Credentials::ServiceAccount {
project_id: Some(project_id),
..
}) => {
assert_eq!(project_id, "test-project");
}
_ => panic!("Expected ServiceAccount credentials with project_id"),
}
}
#[test]
fn test_credentials_is_expired() {
let now = chrono::Utc::now();
let past = now - chrono::Duration::hours(1);
let future = now + chrono::Duration::hours(1);
let expired = Credentials::OAuth2 {
access_token: "token".to_string(),
refresh_token: None,
expires_at: Some(past),
};
assert!(expired.is_expired());
let valid = Credentials::OAuth2 {
access_token: "token".to_string(),
refresh_token: None,
expires_at: Some(future),
};
assert!(!valid.is_expired());
}
#[test]
fn test_credentials_needs_refresh() {
let now = chrono::Utc::now();
let soon = now + chrono::Duration::minutes(3); let later = now + chrono::Duration::hours(1);
let needs_refresh = Credentials::OAuth2 {
access_token: "token".to_string(),
refresh_token: None,
expires_at: Some(soon),
};
assert!(needs_refresh.needs_refresh());
let valid = Credentials::OAuth2 {
access_token: "token".to_string(),
refresh_token: None,
expires_at: Some(later),
};
assert!(!valid.needs_refresh());
}
#[cfg(feature = "async")]
#[tokio::test]
async fn test_refreshing_credentials_no_refresh_needed_returns_current() {
let creds = Credentials::api_key("static-key");
let refreshing = RefreshingCredentials::static_credentials(creds);
let result = refreshing
.ensure_fresh()
.await
.expect("ensure_fresh should succeed for non-expiring credentials");
match result {
Credentials::ApiKey { key } => assert_eq!(key, "static-key"),
other => panic!("expected ApiKey credentials, got {other:?}"),
}
}
#[cfg(feature = "async")]
#[tokio::test]
async fn test_refreshing_credentials_without_provider_warns_and_returns_stale() {
let expired = Credentials::OAuth2 {
access_token: "old-token".to_string(),
refresh_token: Some("rt".to_string()),
expires_at: Some(chrono::Utc::now() - chrono::Duration::hours(1)),
};
let refreshing = RefreshingCredentials::new(expired, None);
let result = refreshing
.ensure_fresh()
.await
.expect("ensure_fresh without a provider should still succeed");
match result {
Credentials::OAuth2 { access_token, .. } => assert_eq!(access_token, "old-token"),
other => panic!("expected OAuth2 credentials, got {other:?}"),
}
}
#[cfg(feature = "async")]
struct CountingProvider {
calls: std::sync::atomic::AtomicUsize,
}
#[cfg(feature = "async")]
#[async_trait::async_trait]
impl CredentialProvider for CountingProvider {
async fn load(&self) -> Result<Credentials> {
Err(CloudError::Auth(AuthError::CredentialsNotFound {
message: "load() not supported by CountingProvider".to_string(),
}))
}
async fn refresh(&self, _credentials: &Credentials) -> Result<Credentials> {
self.calls.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
Ok(Credentials::OAuth2 {
access_token: "refreshed-token".to_string(),
refresh_token: Some("rt".to_string()),
expires_at: Some(chrono::Utc::now() + chrono::Duration::hours(1)),
})
}
}
#[cfg(feature = "async")]
#[tokio::test]
async fn test_refreshing_credentials_calls_provider_and_caches_result() {
let expired = Credentials::OAuth2 {
access_token: "old-token".to_string(),
refresh_token: Some("rt".to_string()),
expires_at: Some(chrono::Utc::now() - chrono::Duration::hours(1)),
};
let provider = Arc::new(CountingProvider {
calls: std::sync::atomic::AtomicUsize::new(0),
});
let refreshing = RefreshingCredentials::new(expired, Some(provider.clone()));
let first = refreshing
.ensure_fresh()
.await
.expect("first ensure_fresh should refresh");
match first {
Credentials::OAuth2 { access_token, .. } => {
assert_eq!(access_token, "refreshed-token")
}
other => panic!("expected OAuth2 credentials, got {other:?}"),
}
assert_eq!(provider.calls.load(std::sync::atomic::Ordering::SeqCst), 1);
let second = refreshing
.ensure_fresh()
.await
.expect("second ensure_fresh should succeed without refreshing");
match second {
Credentials::OAuth2 { access_token, .. } => {
assert_eq!(access_token, "refreshed-token")
}
other => panic!("expected OAuth2 credentials, got {other:?}"),
}
assert_eq!(
provider.calls.load(std::sync::atomic::Ordering::SeqCst),
1,
"provider must not be called again once credentials are fresh"
);
}
#[cfg(all(feature = "http", feature = "async"))]
mod oauth2_refresh_provider_tests {
use super::*;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
async fn serve_one_capturing(
listener: tokio::net::TcpListener,
status_line: &'static str,
response_body: String,
) -> String {
let (mut socket, _) = listener.accept().await.expect("accept failed");
let mut buf = vec![0u8; 8192];
let mut received = Vec::new();
loop {
let n = socket.read(&mut buf).await.expect("read failed");
received.extend_from_slice(&buf[..n]);
if received.windows(4).any(|w| w == b"\r\n\r\n") {
break;
}
if n == 0 {
break;
}
}
let header_text = String::from_utf8_lossy(&received).to_string();
let content_length: usize = header_text
.lines()
.find(|l| l.to_ascii_lowercase().starts_with("content-length:"))
.and_then(|l| l.split_once(':').map(|x| x.1))
.and_then(|v| v.trim().parse().ok())
.unwrap_or(0);
let header_end = received
.windows(4)
.position(|w| w == b"\r\n\r\n")
.map(|p| p + 4)
.unwrap_or(received.len());
let mut body = received[header_end..].to_vec();
while body.len() < content_length {
let n = socket.read(&mut buf).await.expect("read failed");
if n == 0 {
break;
}
body.extend_from_slice(&buf[..n]);
}
let response = format!(
"HTTP/1.1 {status_line}\r\nContent-Type: application/json\r\nContent-Length: {}\r\n\r\n{}",
response_body.len(),
response_body
);
socket
.write_all(response.as_bytes())
.await
.expect("write response failed");
socket.flush().await.expect("flush failed");
String::from_utf8_lossy(&body).to_string()
}
#[tokio::test]
async fn test_refresh_performs_real_http_grant_and_parses_response() {
let listener = tokio::net::TcpListener::bind("127.0.0.1:0")
.await
.expect("bind failed");
let addr = listener.local_addr().expect("local_addr failed");
let token_url = format!("http://{addr}/token");
let response_body =
r#"{"access_token":"new-access","refresh_token":"new-refresh","expires_in":3600}"#
.to_string();
let server = tokio::spawn(serve_one_capturing(
listener,
"200 OK",
response_body.clone(),
));
let provider = OAuth2RefreshProvider::new(token_url, "client-123")
.with_client_secret("shh-secret");
let old = Credentials::OAuth2 {
access_token: "old-access".to_string(),
refresh_token: Some("old-refresh".to_string()),
expires_at: None,
};
let refreshed = provider
.refresh(&old)
.await
.expect("refresh should succeed");
match refreshed {
Credentials::OAuth2 {
access_token,
refresh_token,
expires_at,
} => {
assert_eq!(access_token, "new-access");
assert_eq!(refresh_token.as_deref(), Some("new-refresh"));
let expires_at = expires_at.expect("expires_at should be set");
let expected = chrono::Utc::now() + chrono::Duration::seconds(3600);
let delta = (expires_at - expected).num_seconds().abs();
assert!(
delta < 5,
"expires_at should be ~3600s from now, delta={delta}"
);
}
other => panic!("expected OAuth2 credentials, got {other:?}"),
}
let captured_body = server.await.expect("server task panicked");
assert!(captured_body.contains("grant_type=refresh_token"));
assert!(captured_body.contains("refresh_token=old-refresh"));
assert!(captured_body.contains("client_id=client-123"));
assert!(captured_body.contains("client_secret=shh-secret"));
}
#[tokio::test]
async fn test_refresh_keeps_old_refresh_token_when_endpoint_omits_it() {
let listener = tokio::net::TcpListener::bind("127.0.0.1:0")
.await
.expect("bind failed");
let addr = listener.local_addr().expect("local_addr failed");
let token_url = format!("http://{addr}/token");
let response_body = r#"{"access_token":"new-access","expires_in":60}"#.to_string();
let server = tokio::spawn(serve_one_capturing(listener, "200 OK", response_body));
let provider = OAuth2RefreshProvider::new(token_url, "client-123");
let old = Credentials::OAuth2 {
access_token: "old-access".to_string(),
refresh_token: Some("old-refresh".to_string()),
expires_at: None,
};
let refreshed = provider
.refresh(&old)
.await
.expect("refresh should succeed");
match refreshed {
Credentials::OAuth2 { refresh_token, .. } => {
assert_eq!(refresh_token.as_deref(), Some("old-refresh"));
}
other => panic!("expected OAuth2 credentials, got {other:?}"),
}
server.await.expect("server task panicked");
}
#[tokio::test]
async fn test_refresh_rejects_non_oauth2_credentials() {
let provider = OAuth2RefreshProvider::new("http://127.0.0.1:1/token", "client-123");
let err = provider
.refresh(&Credentials::api_key("nope"))
.await
.expect_err("expected an error for non-OAuth2 credentials");
assert!(matches!(err, CloudError::Auth(AuthError::OAuth2 { .. })));
}
#[tokio::test]
async fn test_refresh_rejects_missing_refresh_token() {
let provider = OAuth2RefreshProvider::new("http://127.0.0.1:1/token", "client-123");
let no_rt = Credentials::OAuth2 {
access_token: "tok".to_string(),
refresh_token: None,
expires_at: None,
};
let err = provider
.refresh(&no_rt)
.await
.expect_err("expected an error for missing refresh_token");
assert!(matches!(err, CloudError::Auth(AuthError::OAuth2 { .. })));
}
#[tokio::test]
async fn test_load_is_unsupported() {
let provider = OAuth2RefreshProvider::new("http://127.0.0.1:1/token", "client-123");
let err = provider
.load()
.await
.expect_err("load() must be unsupported");
assert!(matches!(err, CloudError::Auth(AuthError::OAuth2 { .. })));
}
#[tokio::test]
async fn test_refresh_surfaces_error_response_from_token_endpoint() {
let listener = tokio::net::TcpListener::bind("127.0.0.1:0")
.await
.expect("bind failed");
let addr = listener.local_addr().expect("local_addr failed");
let token_url = format!("http://{addr}/token");
let response_body = r#"{"error":"invalid_grant"}"#.to_string();
let server = tokio::spawn(serve_one_capturing(
listener,
"400 Bad Request",
response_body,
));
let provider = OAuth2RefreshProvider::new(token_url, "client-123");
let old = Credentials::OAuth2 {
access_token: "old-access".to_string(),
refresh_token: Some("old-refresh".to_string()),
expires_at: None,
};
let err = provider
.refresh(&old)
.await
.expect_err("a 400 response should surface as an error");
assert!(matches!(err, CloudError::Auth(AuthError::OAuth2 { .. })));
server.await.expect("server task panicked");
}
}
}