use std::collections::HashMap;
use std::time::Duration;
use adk_core::{AgentId, CredentialSecret, RevocationScope, ScopedToken, Url};
use chrono::{DateTime, Utc};
use tokio::sync::RwLock;
use super::{CloudProvider, FederationError};
pub(crate) const REQUEST_TIMEOUT: Duration = Duration::from_secs(30);
const EXPIRY_SKEW: Duration = Duration::from_secs(30);
const USER_AGENT: &str = concat!("adk-auth/", env!("CARGO_PKG_VERSION"));
pub(crate) fn shorten(message: &str) -> String {
message.chars().take(512).collect()
}
pub(crate) async fn send(
provider: CloudProvider,
endpoint: &str,
request: reqwest::RequestBuilder,
) -> Result<(u16, reqwest::header::HeaderMap, String), FederationError> {
let transport = |error: reqwest::Error| FederationError::Transport {
provider,
endpoint: endpoint.to_string(),
reason: error.without_url().to_string(),
};
let response = request.header("user-agent", USER_AGENT).send().await.map_err(transport)?;
let status = response.status().as_u16();
let headers = response.headers().clone();
let body = response.text().await.map_err(transport)?;
Ok((status, headers, body))
}
pub(crate) async fn oauth_token(
provider: CloudProvider,
client: &reqwest::Client,
endpoint: &str,
form: &[(&str, &str)],
) -> Result<(CredentialSecret, DateTime<Utc>), FederationError> {
let (status, _, body) = send(provider, endpoint, client.post(endpoint).form(form)).await?;
let json: serde_json::Value = serde_json::from_str(&body).unwrap_or_default();
if !(200..300).contains(&status) {
let (code, message) = match &json["error"] {
serde_json::Value::String(code) => {
(code.clone(), json["error_description"].as_str().unwrap_or_default().to_string())
}
serde_json::Value::Object(error) => (
error.get("status").and_then(|v| v.as_str()).unwrap_or("error").to_string(),
error.get("message").and_then(|v| v.as_str()).unwrap_or_default().to_string(),
),
_ => (format!("http_{status}"), body.clone()),
};
return Err(FederationError::Refused {
provider,
status,
code,
message: shorten(&message),
});
}
let invalid = |reason: &str| FederationError::InvalidResponse {
provider,
reason: format!("the token response {reason}"),
};
let access_token = json["access_token"]
.as_str()
.filter(|token| !token.is_empty())
.ok_or_else(|| invalid("has no `access_token`"))?;
let expires_in = json["expires_in"]
.as_i64()
.or_else(|| json["expires_in"].as_str().and_then(|text| text.parse().ok()))
.filter(|seconds| *seconds > 0)
.ok_or_else(|| invalid("has no `expires_in`"))?;
Ok((CredentialSecret::new(access_token), Utc::now() + chrono::Duration::seconds(expires_in)))
}
pub(crate) type CacheKey = (AgentId, Url, Vec<String>);
#[derive(Debug, Default)]
pub(crate) struct TokenCache(RwLock<HashMap<CacheKey, ScopedToken>>);
impl TokenCache {
pub(crate) async fn live(&self, key: &CacheKey) -> Option<ScopedToken> {
let refresh_at = Utc::now() + chrono::Duration::from_std(EXPIRY_SKEW).unwrap_or_default();
self.0.read().await.get(key).filter(|token| token.is_live(refresh_at)).cloned()
}
pub(crate) async fn store(&self, key: CacheKey, token: ScopedToken) {
self.0.write().await.insert(key, token);
}
pub(crate) async fn revoke(&self, scope: &RevocationScope) -> u64 {
let mut cache = self.0.write().await;
let before = cache.len();
cache.retain(|(agent, resource, _), _| !scope.covers(agent, resource));
(before - cache.len()) as u64
}
}