pub mod providers;
use std::collections::HashMap;
use std::sync::Arc;
use std::time::Duration;
use base64::Engine;
use sha2::Digest;
use tokio::sync::{Mutex, RwLock, oneshot};
use tracing::{info, warn};
use aidaemon_secrets::{SecretStore, names};
use serde::Deserialize;
use crate::http_request::{HttpAuthProfile, HttpAuthType};
use crate::services::{OAuthConnection, OAuthGatewayStore, PendingOAuthFlow};
#[derive(Debug, Deserialize, Clone)]
pub struct OAuthProviderConfig {
#[serde(default)]
pub display_name: Option<String>,
pub auth_type: String,
pub authorize_url: String,
pub token_url: String,
#[serde(default)]
pub scopes: Vec<String>,
#[serde(default)]
pub allowed_domains: Vec<String>,
}
const FLOW_TIMEOUT_SECS: u64 = 600;
const RECENT_FLOW_RESULT_TTL_SECS: i64 = 900;
const FLOW_EXPIRED_MESSAGE: &str = "OAuth flow expired (10 minutes). Please try again.";
const INVALID_OR_USED_FLOW_MESSAGE: &str =
"OAuth flow expired or was already used. Please start a new connection attempt.";
const ACCESS_TOKEN_REFRESH_SKEW_SECS: i64 = 60;
fn access_token_needs_refresh(
connection: &OAuthConnection,
now: chrono::DateTime<chrono::Utc>,
) -> bool {
let Some(expires_at) = connection.token_expires_at.as_deref() else {
return false;
};
chrono::DateTime::parse_from_rfc3339(expires_at)
.map(|expires_at| {
expires_at.with_timezone(&chrono::Utc)
<= now + chrono::Duration::seconds(ACCESS_TOKEN_REFRESH_SKEW_SECS)
})
.unwrap_or(true)
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CredentialRefreshPath {
RefreshToken,
ClientCredentials,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum CredentialReadiness {
Ready { valid_until: Option<String> },
Refreshable {
expired_at: Option<String>,
path: CredentialRefreshPath,
},
ReauthRequired { missing: Vec<&'static str> },
}
impl CredentialReadiness {
pub fn for_connection(
connection: &OAuthConnection,
now: chrono::DateTime<chrono::Utc>,
secrets: &dyn SecretStore,
) -> Self {
let service = connection.service.as_str();
let stored = |suffix: &str| secrets.contains(&names::oauth(service, suffix));
let has_access_token = stored("access_token");
let has_refresh_token = stored("refresh_token");
let has_client_id = stored("client_id");
let has_client_secret = stored("client_secret");
let client_credentials_grant = connection.auth_type == "oauth2_client_credentials";
if has_access_token && !access_token_needs_refresh(connection, now) {
return Self::Ready {
valid_until: connection.token_expires_at.clone(),
};
}
let expired_at = has_access_token
.then(|| connection.token_expires_at.clone())
.flatten();
if has_client_id && has_client_secret {
if client_credentials_grant {
return Self::Refreshable {
expired_at,
path: CredentialRefreshPath::ClientCredentials,
};
}
if has_refresh_token {
return Self::Refreshable {
expired_at,
path: CredentialRefreshPath::RefreshToken,
};
}
}
let mut missing = Vec::new();
if !has_access_token {
missing.push("access token");
}
if !client_credentials_grant && !has_refresh_token {
missing.push("refresh token");
}
if !has_client_id {
missing.push("client id");
}
if !has_client_secret {
missing.push("client secret");
}
Self::ReauthRequired { missing }
}
pub fn state(&self) -> &'static str {
match self {
Self::Ready { .. } => "ready",
Self::Refreshable { .. } => "ready-via-refresh",
Self::ReauthRequired { .. } => "reauth-required",
}
}
pub fn describe(&self) -> String {
let detail = match self {
Self::Ready {
valid_until: Some(valid_until),
} => format!("bearer valid until {valid_until}"),
Self::Ready { valid_until: None } => "bearer stored, no expiry recorded".to_string(),
Self::Refreshable { expired_at, path } => {
let bearer = match expired_at {
Some(expired_at) => format!("bearer expired at {expired_at}"),
None => "no bearer stored".to_string(),
};
let path = match path {
CredentialRefreshPath::RefreshToken => "refresh token stored",
CredentialRefreshPath::ClientCredentials => "client-credentials grant",
};
format!(
"{bearer}; {path}; refreshed automatically before the next authenticated call"
)
}
Self::ReauthRequired { missing } if missing.is_empty() => {
"stored bearer is expired and no refresh path exists".to_string()
}
Self::ReauthRequired { missing } => format!("missing: {}", missing.join(", ")),
};
format!("{} ({detail})", self.state())
}
}
#[derive(Debug, Clone, PartialEq)]
pub enum OAuthType {
OAuth2Pkce,
OAuth2AuthorizationCode,
OAuth2ClientCredentials,
OAuth1a,
}
#[derive(Debug, Clone)]
pub struct OAuthProvider {
pub name: String,
pub display_name: String,
pub auth_type: OAuthType,
pub authorize_url: String,
pub token_url: String,
pub scopes: Vec<String>,
pub allowed_domains: Vec<String>,
}
#[derive(Debug, Clone)]
#[allow(dead_code)]
pub struct OAuthFlowResult {
pub service: String,
pub username: Option<String>,
pub message: String,
}
struct PendingFlow {
provider_name: String,
code_verifier: Option<String>,
#[allow(dead_code)]
session_id: String,
#[allow(dead_code)]
created_at: chrono::DateTime<chrono::Utc>,
result_tx: oneshot::Sender<OAuthFlowResult>,
}
struct RecentFlowResult {
message: String,
recorded_at: chrono::DateTime<chrono::Utc>,
}
struct ResolvedPendingFlow {
state: String,
provider_name: String,
code_verifier: Option<String>,
created_at: chrono::DateTime<chrono::Utc>,
result_tx: Option<oneshot::Sender<OAuthFlowResult>>,
}
pub use crate::http_request::SharedHttpProfiles;
#[derive(Clone)]
pub struct OAuthGateway {
providers: Arc<RwLock<HashMap<String, OAuthProvider>>>,
pending_flows: Arc<RwLock<HashMap<String, PendingFlow>>>,
recent_flow_results: Arc<RwLock<HashMap<String, RecentFlowResult>>>,
state_store: Arc<dyn OAuthGatewayStore>,
secrets: Arc<dyn SecretStore>,
http_profiles: SharedHttpProfiles,
callback_base_url: String,
secret_set_command: Option<String>,
refresh_lock: Arc<Mutex<()>>,
}
impl OAuthGateway {
pub fn new(
state_store: Arc<dyn OAuthGatewayStore>,
secrets: Arc<dyn SecretStore>,
http_profiles: SharedHttpProfiles,
callback_base_url: String,
) -> Self {
Self {
providers: Arc::new(RwLock::new(HashMap::new())),
pending_flows: Arc::new(RwLock::new(HashMap::new())),
recent_flow_results: Arc::new(RwLock::new(HashMap::new())),
state_store,
secrets,
http_profiles,
callback_base_url,
secret_set_command: None,
refresh_lock: Arc::new(Mutex::new(())),
}
}
pub fn with_secret_set_command(mut self, command: impl Into<String>) -> Self {
self.secret_set_command = Some(command.into());
self
}
pub fn secrets(&self) -> &Arc<dyn SecretStore> {
&self.secrets
}
pub async fn register_provider(&self, provider: OAuthProvider) {
let mut providers = self.providers.write().await;
providers.insert(provider.name.clone(), provider);
}
pub async fn register_config_provider(&self, name: &str, config: &OAuthProviderConfig) {
let auth_type = match config.auth_type.as_str() {
"oauth2_authorization_code" | "authorization_code" | "auth_code" => {
OAuthType::OAuth2AuthorizationCode
}
"oauth2_client_credentials" | "client_credentials" => {
OAuthType::OAuth2ClientCredentials
}
"oauth1a" => OAuthType::OAuth1a,
_ => OAuthType::OAuth2Pkce,
};
let provider = OAuthProvider {
name: name.to_string(),
display_name: config
.display_name
.clone()
.filter(|value| !value.trim().is_empty())
.unwrap_or_else(|| name.to_string()),
auth_type,
authorize_url: config.authorize_url.clone(),
token_url: config.token_url.clone(),
scopes: config.scopes.clone(),
allowed_domains: config.allowed_domains.clone(),
};
self.register_provider(provider).await;
}
pub async fn unregister_provider(&self, name: &str) -> bool {
let mut providers = self.providers.write().await;
providers.remove(name).is_some()
}
pub async fn list_providers(&self) -> Vec<(String, String)> {
let providers = self.providers.read().await;
providers
.values()
.map(|p| (p.name.clone(), p.display_name.clone()))
.collect()
}
pub async fn get_provider(&self, name: &str) -> Option<OAuthProvider> {
let providers = self.providers.read().await;
providers.get(name).cloned()
}
pub fn callback_url(&self) -> String {
Self::normalize_callback_url(&self.callback_base_url)
}
fn normalize_callback_url(callback_base_url: &str) -> String {
let trimmed = callback_base_url.trim().trim_end_matches('/');
if trimmed.ends_with("/oauth/callback") {
trimmed.to_string()
} else {
format!("{trimmed}/oauth/callback")
}
}
fn generate_code_verifier() -> String {
use rand::RngExt;
let mut rng = rand::rng();
let bytes: Vec<u8> = (0..32).map(|_| rng.random()).collect();
base64::engine::general_purpose::URL_SAFE_NO_PAD.encode(&bytes)
}
fn generate_code_challenge(verifier: &str) -> String {
let hash = sha2::Sha256::digest(verifier.as_bytes());
base64::engine::general_purpose::URL_SAFE_NO_PAD.encode(hash)
}
fn generate_state() -> String {
uuid::Uuid::new_v4().to_string()
}
pub fn expired_flow_message() -> &'static str {
FLOW_EXPIRED_MESSAGE
}
fn parse_pending_flow_created_at(raw: &str) -> Option<chrono::DateTime<chrono::Utc>> {
chrono::DateTime::parse_from_rfc3339(raw)
.ok()
.map(|parsed| parsed.with_timezone(&chrono::Utc))
}
fn flow_cutoff() -> chrono::DateTime<chrono::Utc> {
chrono::Utc::now() - chrono::Duration::seconds(FLOW_TIMEOUT_SECS as i64)
}
fn flow_is_expired(created_at: chrono::DateTime<chrono::Utc>) -> bool {
created_at < Self::flow_cutoff()
}
async fn remember_recent_flow_result(&self, state: &str, message: String) {
let cutoff = chrono::Utc::now() - chrono::Duration::seconds(RECENT_FLOW_RESULT_TTL_SECS);
let mut recent = self.recent_flow_results.write().await;
recent.retain(|_, result| result.recorded_at >= cutoff);
recent.insert(
state.to_string(),
RecentFlowResult {
message,
recorded_at: chrono::Utc::now(),
},
);
}
async fn recent_flow_result(&self, state: &str) -> Option<String> {
let cutoff = chrono::Utc::now() - chrono::Duration::seconds(RECENT_FLOW_RESULT_TTL_SECS);
let mut recent = self.recent_flow_results.write().await;
recent.retain(|_, result| result.recorded_at >= cutoff);
recent.get(state).map(|result| result.message.clone())
}
async fn finalize_flow_result(&self, flow: ResolvedPendingFlow, message: String) -> String {
self.remember_recent_flow_result(&flow.state, message.clone())
.await;
if let Some(result_tx) = flow.result_tx {
let _ = result_tx.send(OAuthFlowResult {
service: flow.provider_name,
username: None,
message: message.clone(),
});
}
message
}
async fn resolve_pending_flow(
&self,
state: &str,
) -> anyhow::Result<Option<ResolvedPendingFlow>> {
if let Some(stored) = self.state_store.get_pending_oauth_flow(state).await? {
self.state_store.delete_pending_oauth_flow(state).await?;
let result_tx = {
let mut flows = self.pending_flows.write().await;
flows.remove(state).map(|flow| flow.result_tx)
};
let created_at = match Self::parse_pending_flow_created_at(&stored.created_at) {
Some(created_at) => created_at,
None => {
warn!(
state = %state,
stored_created_at = %stored.created_at,
"Pending OAuth flow has an invalid timestamp; treating it as expired"
);
chrono::Utc::now() - chrono::Duration::seconds((FLOW_TIMEOUT_SECS as i64) + 1)
}
};
return Ok(Some(ResolvedPendingFlow {
state: stored.state,
provider_name: stored.service,
code_verifier: stored.code_verifier,
created_at,
result_tx,
}));
}
let flow = {
let mut flows = self.pending_flows.write().await;
flows.remove(state)
};
Ok(flow.map(|flow| ResolvedPendingFlow {
state: state.to_string(),
provider_name: flow.provider_name,
code_verifier: flow.code_verifier,
created_at: flow.created_at,
result_tx: Some(flow.result_tx),
}))
}
pub async fn expire_pending_flow(
&self,
state: &str,
message: Option<String>,
) -> anyhow::Result<bool> {
let Some(flow) = self.resolve_pending_flow(state).await? else {
return Ok(false);
};
let service = flow.provider_name.clone();
let message = message.unwrap_or_else(|| Self::expired_flow_message().to_string());
let final_message = self.finalize_flow_result(flow, message).await;
info!(
service = %service,
state = %state,
message = %final_message,
"Expired pending OAuth flow"
);
Ok(true)
}
pub fn has_credentials(&self, service: &str) -> bool {
self.secrets.contains(&names::oauth(service, "client_id"))
}
fn missing_credential(&self, what: &str, key: &str) -> anyhow::Error {
match self.secret_set_command.as_deref() {
Some(command) => anyhow::anyhow!(
"{} not found in keychain. Set it with: {} {}",
what,
command,
key
),
None => anyhow::anyhow!("{} not found in the secret store under '{}'", what, key),
}
}
fn get_credentials(&self, service: &str) -> anyhow::Result<(String, String)> {
let client_id_key = names::oauth(service, "client_id");
let client_secret_key = names::oauth(service, "client_secret");
let client_id = self
.secrets
.get(&client_id_key)
.map_err(|_| self.missing_credential("Client ID", &client_id_key))?
.into_exposed();
let client_secret = self
.secrets
.get(&client_secret_key)
.map_err(|_| self.missing_credential("Client secret", &client_secret_key))?
.into_exposed();
Ok((client_id, client_secret))
}
pub async fn start_oauth2_flow(
&self,
service: &str,
session_id: &str,
) -> anyhow::Result<(String, oneshot::Receiver<OAuthFlowResult>)> {
let provider = self
.get_provider(service)
.await
.ok_or_else(|| anyhow::anyhow!("Unknown OAuth provider: {}", service))?;
if !matches!(
provider.auth_type,
OAuthType::OAuth2Pkce | OAuthType::OAuth2AuthorizationCode
) {
return Err(anyhow::anyhow!(
"Provider '{}' does not support interactive OAuth authorization flows",
service
));
}
let (client_id, _) = self.get_credentials(service)?;
let state = Self::generate_state();
let code_verifier = if provider.auth_type == OAuthType::OAuth2Pkce {
Some(Self::generate_code_verifier())
} else {
None
};
let callback_url = self.callback_url();
let scopes = provider.scopes.join(" ");
let mut authorize_url = format!(
"{}?response_type=code&client_id={}&redirect_uri={}&scope={}&state={}",
provider.authorize_url,
urlencoded(&client_id),
urlencoded(&callback_url),
urlencoded(&scopes),
urlencoded(&state),
);
if let Some(ref verifier) = code_verifier {
let code_challenge = Self::generate_code_challenge(verifier);
authorize_url.push_str(&format!(
"&code_challenge={}&code_challenge_method=S256",
urlencoded(&code_challenge)
));
}
let (result_tx, result_rx) = oneshot::channel();
let created_at = chrono::Utc::now();
let flow = PendingFlow {
provider_name: service.to_string(),
code_verifier,
session_id: session_id.to_string(),
created_at,
result_tx,
};
let pending_flow = PendingOAuthFlow {
state: state.clone(),
service: service.to_string(),
code_verifier: flow.code_verifier.clone(),
session_id: session_id.to_string(),
created_at: created_at.to_rfc3339(),
};
self.state_store
.save_pending_oauth_flow(&pending_flow)
.await?;
{
let mut flows = self.pending_flows.write().await;
flows.insert(state.clone(), flow);
}
Ok((authorize_url, result_rx))
}
fn oauth_type_label(auth_type: &OAuthType) -> &'static str {
match auth_type {
OAuthType::OAuth2Pkce => "oauth2_pkce",
OAuthType::OAuth2AuthorizationCode => "oauth2_authorization_code",
OAuthType::OAuth2ClientCredentials => "oauth2_client_credentials",
OAuthType::OAuth1a => "oauth1a",
}
}
async fn store_connected_bearer_profile(
&self,
service: &str,
provider: &OAuthProvider,
access_token: &str,
refresh_token: Option<&str>,
expires_in: Option<u64>,
preserve_account_binding: bool,
) -> anyhow::Result<String> {
let at_key = names::oauth(service, "access_token");
self.secrets.set(&at_key, access_token)?;
if let Some(rt) = refresh_token {
let rt_key = names::oauth(service, "refresh_token");
self.secrets.set(&rt_key, rt)?;
}
let expires_at = expires_in
.map(|secs| (chrono::Utc::now() + chrono::Duration::seconds(secs as i64)).to_rfc3339());
let account_id = if preserve_account_binding {
self.state_store
.get_oauth_connection(service)
.await?
.and_then(|connection| connection.account_id)
} else {
None
};
let conn = OAuthConnection {
id: 0,
service: service.to_string(),
auth_type: Self::oauth_type_label(&provider.auth_type).to_string(),
username: None,
account_id: account_id.clone(),
scopes: serde_json::to_string(&provider.scopes).unwrap_or_default(),
token_expires_at: expires_at,
created_at: chrono::Utc::now().to_rfc3339(),
updated_at: chrono::Utc::now().to_rfc3339(),
};
self.state_store.save_oauth_connection(&conn).await?;
let profile = HttpAuthProfile {
auth_type: HttpAuthType::Bearer,
allowed_domains: provider.allowed_domains.clone(),
api_key: None,
api_secret: None,
access_token: None,
access_token_secret: None,
user_id: account_id,
token: Some(access_token.to_string()),
header_name: None,
header_value: None,
username: None,
password: None,
};
{
let mut profiles = self.http_profiles.write().await;
profiles.insert(service.to_string(), profile);
}
Ok(format!(
"Connected to {}! Use `http_request` with auth_profile=\"{}\" to make API calls.",
provider.display_name, service
))
}
async fn exchange_client_credentials_token(
&self,
service: &str,
provider: &OAuthProvider,
) -> anyhow::Result<(String, Option<String>, Option<u64>)> {
let (client_id, client_secret) = self.get_credentials(service)?;
let mut params = HashMap::new();
params.insert("grant_type", "client_credentials".to_string());
if !provider.scopes.is_empty() {
params.insert("scope", provider.scopes.join(" "));
}
let client = reqwest::Client::new();
let resp = client
.post(&provider.token_url)
.basic_auth(&client_id, Some(&client_secret))
.form(¶ms)
.timeout(Duration::from_secs(30))
.send()
.await
.map_err(|e| anyhow::anyhow!("Client credentials token request failed: {}", e))?;
if !resp.status().is_success() {
let status = resp.status();
let body = resp.text().await.unwrap_or_default();
anyhow::bail!(
"Client credentials token exchange failed (HTTP {}): {}",
status,
body
);
}
let token_data: serde_json::Value = resp
.json()
.await
.map_err(|e| anyhow::anyhow!("Failed to parse token response: {}", e))?;
let access_token = token_data["access_token"]
.as_str()
.ok_or_else(|| anyhow::anyhow!("No access_token in response"))?
.to_string();
let refresh_token = token_data["refresh_token"]
.as_str()
.map(ToString::to_string);
let expires_in = token_data["expires_in"].as_u64();
Ok((access_token, refresh_token, expires_in))
}
pub async fn connect_client_credentials(&self, service: &str) -> anyhow::Result<String> {
let provider = self
.get_provider(service)
.await
.ok_or_else(|| anyhow::anyhow!("Unknown OAuth provider: {}", service))?;
anyhow::ensure!(
provider.auth_type == OAuthType::OAuth2ClientCredentials,
"Provider '{}' is not configured for oauth2_client_credentials",
service
);
let (access_token, refresh_token, expires_in) = self
.exchange_client_credentials_token(service, &provider)
.await?;
self.store_connected_bearer_profile(
service,
&provider,
&access_token,
refresh_token.as_deref(),
expires_in,
false,
)
.await
}
pub async fn handle_callback(
&self,
state: &str,
code: Option<&str>,
error: Option<&str>,
) -> anyhow::Result<String> {
let Some(flow) = self.resolve_pending_flow(state).await? else {
if let Some(message) = self.recent_flow_result(state).await {
return Ok(message);
}
return Ok(INVALID_OR_USED_FLOW_MESSAGE.to_string());
};
if Self::flow_is_expired(flow.created_at) {
return Ok(self
.finalize_flow_result(flow, Self::expired_flow_message().to_string())
.await);
}
if let Some(err) = error {
let msg = format!("OAuth authorization denied: {}", err);
return Ok(self.finalize_flow_result(flow, msg).await);
}
let service = flow.provider_name.clone();
let message = match async {
let code = code.ok_or_else(|| anyhow::anyhow!("No authorization code in callback"))?;
let provider = self
.get_provider(&flow.provider_name)
.await
.ok_or_else(|| anyhow::anyhow!("Provider '{}' not found", flow.provider_name))?;
let (client_id, client_secret) = self.get_credentials(&flow.provider_name)?;
let callback_url = self.callback_url();
let mut params = HashMap::new();
params.insert("grant_type", "authorization_code".to_string());
params.insert("code", code.to_string());
params.insert("redirect_uri", callback_url);
if let Some(ref verifier) = flow.code_verifier {
params.insert("code_verifier", verifier.clone());
}
let client = reqwest::Client::new();
let resp = client
.post(&provider.token_url)
.basic_auth(&client_id, Some(&client_secret))
.form(¶ms)
.timeout(Duration::from_secs(30))
.send()
.await
.map_err(|e| anyhow::anyhow!("Token exchange request failed: {}", e))?;
if !resp.status().is_success() {
let status = resp.status();
let body = resp.text().await.unwrap_or_default();
anyhow::bail!("Token exchange failed (HTTP {}): {}", status, body);
}
let token_data: serde_json::Value = resp
.json()
.await
.map_err(|e| anyhow::anyhow!("Failed to parse token response: {}", e))?;
let access_token = token_data["access_token"]
.as_str()
.ok_or_else(|| anyhow::anyhow!("No access_token in response"))?;
let refresh_token = token_data["refresh_token"].as_str();
let expires_in = token_data["expires_in"].as_u64();
self.store_connected_bearer_profile(
&flow.provider_name,
&provider,
access_token,
refresh_token,
expires_in,
false,
)
.await
}
.await
{
Ok(message) => {
info!(service = %service, "OAuth connection established");
message
}
Err(err) => {
warn!(
service = %service,
state = %state,
error = %err,
"OAuth callback failed"
);
err.to_string()
}
};
Ok(self.finalize_flow_result(flow, message).await)
}
pub async fn cleanup_expired_flows(&self) {
let cutoff = Self::flow_cutoff();
let mut expired_states = Vec::new();
match self.state_store.list_pending_oauth_flows().await {
Ok(flows) => {
for flow in flows {
let created_at = Self::parse_pending_flow_created_at(&flow.created_at)
.unwrap_or_else(|| {
warn!(
state = %flow.state,
stored_created_at = %flow.created_at,
"Pending OAuth flow has an invalid timestamp; expiring it"
);
chrono::Utc::now()
- chrono::Duration::seconds((FLOW_TIMEOUT_SECS as i64) + 1)
});
if created_at < cutoff {
expired_states.push(flow.state);
}
}
}
Err(err) => {
warn!(error = %err, "Failed to enumerate pending OAuth flows for cleanup");
}
}
let in_memory_expired: Vec<String> = {
let flows = self.pending_flows.read().await;
flows
.iter()
.filter(|(_, flow)| flow.created_at < cutoff)
.map(|(state, _)| state.clone())
.collect()
};
for state in in_memory_expired {
if !expired_states.contains(&state) {
expired_states.push(state);
}
}
for state in &expired_states {
if let Err(err) = self
.expire_pending_flow(state, Some(Self::expired_flow_message().to_string()))
.await
{
warn!(state = %state, error = %err, "Failed to expire pending OAuth flow");
}
}
if !expired_states.is_empty() {
info!(
count = expired_states.len(),
"Cleaned up expired OAuth flows"
);
}
}
pub async fn restore_connections(&self) {
match self.state_store.list_oauth_connections().await {
Ok(connections) => {
for conn in connections {
if access_token_needs_refresh(&conn, chrono::Utc::now()) {
match self.refresh_token(&conn.service).await {
Ok(_) => {
info!(
service = %conn.service,
"Restored OAuth connection via proactive token refresh"
);
}
Err(error) => {
warn!(
service = %conn.service,
%error,
"Expired OAuth connection could not be refreshed; stale access token was not loaded"
);
}
}
continue;
}
let at_key = names::oauth(&conn.service, "access_token");
match self.secrets.get(&at_key) {
Ok(token) => {
let allowed_domains = self
.get_provider(&conn.service)
.await
.map(|p| p.allowed_domains.clone())
.unwrap_or_default();
let profile = HttpAuthProfile {
auth_type: HttpAuthType::Bearer,
allowed_domains,
api_key: None,
api_secret: None,
access_token: None,
access_token_secret: None,
user_id: conn.account_id.clone(),
token: Some(token.into_exposed()),
header_name: None,
header_value: None,
username: None,
password: None,
};
{
let mut profiles = self.http_profiles.write().await;
profiles.insert(conn.service.clone(), profile);
}
info!(service = %conn.service, "Restored OAuth connection");
}
Err(_) => {
match self.refresh_token(&conn.service).await {
Ok(_) => {
info!(
service = %conn.service,
"Restored OAuth connection via token refresh (access token was unavailable)"
);
}
Err(e) => {
warn!(
service = %conn.service,
error = %e,
"OAuth connection in DB but access token unavailable and refresh failed"
);
}
}
}
}
}
}
Err(e) => {
warn!("Failed to restore OAuth connections: {}", e);
}
}
}
pub async fn refresh_access_token_if_needed(&self, service: &str) -> anyhow::Result<bool> {
let _refresh_guard = self.refresh_lock.lock().await;
let Some(connection) = self.state_store.get_oauth_connection(service).await? else {
return Ok(false);
};
if !access_token_needs_refresh(&connection, chrono::Utc::now()) {
return Ok(false);
}
self.refresh_token_locked(service).await?;
Ok(true)
}
pub async fn refresh_token(&self, service: &str) -> anyhow::Result<String> {
let _refresh_guard = self.refresh_lock.lock().await;
self.refresh_token_locked(service).await
}
async fn refresh_token_locked(&self, service: &str) -> anyhow::Result<String> {
let provider = self
.get_provider(service)
.await
.ok_or_else(|| anyhow::anyhow!("Unknown OAuth provider: {}", service))?;
if provider.auth_type == OAuthType::OAuth2ClientCredentials {
let (access_token, refresh_token, expires_in) = self
.exchange_client_credentials_token(service, &provider)
.await?;
self.store_connected_bearer_profile(
service,
&provider,
&access_token,
refresh_token.as_deref(),
expires_in,
true,
)
.await?;
info!(service = %service, "OAuth client-credentials token refreshed");
return Ok(format!("Token refreshed for {}", service));
}
let rt_key = names::oauth(service, "refresh_token");
let refresh_token = self
.secrets
.get(&rt_key)
.map_err(|_| anyhow::anyhow!("No refresh token found for '{}'", service))?
.into_exposed();
let (client_id, client_secret) = self.get_credentials(service)?;
let mut params = HashMap::new();
params.insert("grant_type", "refresh_token".to_string());
params.insert("refresh_token", refresh_token);
let client = reqwest::Client::new();
let resp = client
.post(&provider.token_url)
.basic_auth(&client_id, Some(&client_secret))
.form(¶ms)
.timeout(Duration::from_secs(30))
.send()
.await
.map_err(|e| anyhow::anyhow!("Refresh request failed: {}", e))?;
if !resp.status().is_success() {
let body = resp.text().await.unwrap_or_default();
return Err(anyhow::anyhow!("Token refresh failed: {}", body));
}
let token_data: serde_json::Value = resp.json().await?;
let new_access_token = token_data["access_token"]
.as_str()
.ok_or_else(|| anyhow::anyhow!("No access_token in refresh response"))?;
let at_key = names::oauth(service, "access_token");
self.secrets.set(&at_key, new_access_token)?;
if let Some(new_rt) = token_data["refresh_token"].as_str() {
let rt_key = names::oauth(service, "refresh_token");
self.secrets.set(&rt_key, new_rt)?;
}
let expires_in = token_data["expires_in"].as_u64();
let expires_at = expires_in
.map(|secs| (chrono::Utc::now() + chrono::Duration::seconds(secs as i64)).to_rfc3339());
self.state_store
.update_oauth_token_expiry(service, expires_at.as_deref())
.await?;
let account_id = self
.state_store
.get_oauth_connection(service)
.await?
.and_then(|connection| connection.account_id);
{
let mut profiles = self.http_profiles.write().await;
let had_profile = profiles.contains_key(service);
profiles.insert(
service.to_string(),
HttpAuthProfile {
auth_type: HttpAuthType::Bearer,
allowed_domains: provider.allowed_domains.clone(),
api_key: None,
api_secret: None,
access_token: None,
access_token_secret: None,
user_id: account_id,
token: Some(new_access_token.to_string()),
header_name: None,
header_value: None,
username: None,
password: None,
},
);
if !had_profile {
info!(
service = %service,
"Rebuilt missing OAuth auth profile during token refresh"
);
}
}
info!(service = %service, "OAuth token refreshed");
Ok(format!("Token refreshed for {}", service))
}
pub async fn bind_account_id(&self, service: &str, account_id: &str) -> anyhow::Result<()> {
anyhow::ensure!(
self.state_store
.get_oauth_connection(service)
.await?
.is_some(),
"No OAuth connection found for '{}'",
service
);
{
let profiles = self.http_profiles.read().await;
anyhow::ensure!(
profiles.contains_key(service),
"OAuth profile '{}' is not loaded; reconnect or refresh it before binding identity",
service
);
}
anyhow::ensure!(
self.state_store
.update_oauth_account_id(service, Some(account_id))
.await?,
"OAuth connection '{}' disappeared before identity binding",
service
);
let mut profiles = self.http_profiles.write().await;
let profile = profiles
.get_mut(service)
.ok_or_else(|| anyhow::anyhow!("OAuth profile '{}' disappeared", service))?;
profile.user_id = Some(account_id.to_string());
info!(service = %service, account_id = %account_id, "Bound verified OAuth account identity");
Ok(())
}
pub async fn remove_connection(&self, service: &str) -> anyhow::Result<String> {
self.state_store.delete_oauth_connection(service).await?;
for suffix in &["access_token", "refresh_token"] {
let key = names::oauth(service, suffix);
let _ = self.secrets.delete(&key);
}
{
let mut profiles = self.http_profiles.write().await;
profiles.remove(service);
}
info!(service = %service, "OAuth connection removed");
Ok(format!("Disconnected from {}", service))
}
pub fn flow_timeout() -> Duration {
Duration::from_secs(FLOW_TIMEOUT_SECS)
}
}
fn urlencoded(s: &str) -> String {
let mut result = String::with_capacity(s.len() * 2);
for byte in s.bytes() {
match byte {
b'A'..=b'Z' | b'a'..=b'z' | b'0'..=b'9' | b'-' | b'_' | b'.' | b'~' => {
result.push(byte as char);
}
_ => {
result.push_str(&format!("%{:02X}", byte));
}
}
}
result
}
#[async_trait::async_trait]
impl crate::http_request::OAuthTokens for OAuthGateway {
async fn managed_scopes(&self, profile: &str) -> Option<Vec<String>> {
self.get_provider(profile)
.await
.map(|provider| provider.scopes)
}
async fn refresh_token(&self, profile: &str) -> anyhow::Result<String> {
OAuthGateway::refresh_token(self, profile).await
}
async fn refresh_access_token_if_needed(&self, profile: &str) -> anyhow::Result<bool> {
OAuthGateway::refresh_access_token_if_needed(self, profile).await
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::collections::HashMap;
use std::net::SocketAddr;
use std::sync::Arc;
use axum::{Json, Router, extract::Form, routing::post};
use tempfile::NamedTempFile;
use tokio::net::TcpListener;
use aidaemon_secrets::EnvSecretStore;
use crate::services::InMemoryOAuthStore;
struct TestSecrets {
env_file: NamedTempFile,
store: Arc<dyn SecretStore>,
}
impl TestSecrets {
fn new() -> Self {
let env_file = NamedTempFile::new().expect("create env file");
let store = Arc::new(EnvSecretStore::at(env_file.path()));
Self { env_file, store }
}
fn env_file_path(&self) -> &std::path::Path {
self.env_file.path()
}
}
fn state_from_authorize_url(authorize_url: &str) -> String {
reqwest::Url::parse(authorize_url)
.unwrap()
.query_pairs()
.find_map(|(key, value)| (key == "state").then(|| value.into_owned()))
.unwrap()
}
async fn test_gateway(secrets: &TestSecrets) -> anyhow::Result<OAuthGateway> {
let state = Arc::new(InMemoryOAuthStore::new());
let profiles: SharedHttpProfiles = Arc::new(RwLock::new(HashMap::new()));
Ok(OAuthGateway::new(
state as Arc<dyn OAuthGatewayStore>,
secrets.store.clone(),
profiles,
"http://localhost:8080".to_string(),
))
}
#[test]
fn startup_refreshes_expired_or_invalid_expiry_but_preserves_nonexpiring_tokens() {
let now = chrono::Utc::now();
let mut connection = OAuthConnection {
id: 0,
service: "twitter".to_string(),
auth_type: "oauth2_pkce".to_string(),
username: None,
account_id: Some("synthetic-account".to_string()),
scopes: r#"["tweet.read"]"#.to_string(),
token_expires_at: Some((now - chrono::Duration::minutes(1)).to_rfc3339()),
created_at: now.to_rfc3339(),
updated_at: now.to_rfc3339(),
};
assert!(access_token_needs_refresh(&connection, now));
connection.token_expires_at = Some("not-a-timestamp".to_string());
assert!(access_token_needs_refresh(&connection, now));
connection.token_expires_at = Some((now + chrono::Duration::minutes(5)).to_rfc3339());
assert!(!access_token_needs_refresh(&connection, now));
connection.token_expires_at = None;
assert!(!access_token_needs_refresh(&connection, now));
}
#[test]
fn test_pkce_code_verifier_length() {
let verifier = OAuthGateway::generate_code_verifier();
assert!(verifier.len() >= 43);
assert!(verifier.len() <= 128);
}
#[test]
fn test_pkce_code_challenge_is_deterministic() {
let verifier = "test_verifier_string";
let c1 = OAuthGateway::generate_code_challenge(verifier);
let c2 = OAuthGateway::generate_code_challenge(verifier);
assert_eq!(c1, c2);
}
#[test]
fn test_pkce_code_challenge_uses_sha256() {
let challenge = OAuthGateway::generate_code_challenge("test");
assert_eq!(challenge, "n4bQgYhMfWWaL-qgxVrQFaO_TxsrC4Is0V1sFbDwCgg");
}
#[test]
fn test_state_parameter_uniqueness() {
let s1 = OAuthGateway::generate_state();
let s2 = OAuthGateway::generate_state();
assert_ne!(s1, s2);
assert!(uuid::Uuid::parse_str(&s1).is_ok());
}
#[tokio::test]
async fn bind_account_id_updates_persistence_and_live_profile() {
let gateway = test_gateway(&TestSecrets::new()).await.unwrap();
gateway
.state_store
.save_oauth_connection(&OAuthConnection {
id: 0,
service: "synthetic".to_string(),
auth_type: "oauth2_pkce".to_string(),
username: None,
account_id: None,
scopes: "[]".to_string(),
token_expires_at: None,
created_at: chrono::Utc::now().to_rfc3339(),
updated_at: chrono::Utc::now().to_rfc3339(),
})
.await
.unwrap();
gateway.http_profiles.write().await.insert(
"synthetic".to_string(),
HttpAuthProfile {
auth_type: HttpAuthType::Bearer,
allowed_domains: vec!["api.example.test".to_string()],
api_key: None,
api_secret: None,
access_token: None,
access_token_secret: None,
user_id: None,
token: Some("synthetic-token".to_string()),
header_name: None,
header_value: None,
username: None,
password: None,
},
);
gateway
.bind_account_id("synthetic", "stable-remote-account")
.await
.unwrap();
let stored = gateway
.state_store
.get_oauth_connection("synthetic")
.await
.unwrap()
.unwrap();
assert_eq!(stored.account_id.as_deref(), Some("stable-remote-account"));
let profiles = gateway.http_profiles.read().await;
assert_eq!(
profiles
.get("synthetic")
.and_then(|profile| profile.user_id.as_deref()),
Some("stable-remote-account")
);
}
#[test]
fn test_urlencoded() {
assert_eq!(urlencoded("hello"), "hello");
assert_eq!(urlencoded("hello world"), "hello%20world");
assert_eq!(urlencoded("a=b&c=d"), "a%3Db%26c%3Dd");
assert_eq!(
urlencoded("https://example.com"),
"https%3A%2F%2Fexample.com"
);
}
#[test]
fn test_callback_url_accepts_base_url() {
assert_eq!(
OAuthGateway::normalize_callback_url("http://localhost:8080"),
"http://localhost:8080/oauth/callback"
);
}
#[test]
fn test_callback_url_accepts_full_callback_url() {
assert_eq!(
OAuthGateway::normalize_callback_url("http://localhost:8080/oauth/callback"),
"http://localhost:8080/oauth/callback"
);
}
#[tokio::test]
async fn authorization_code_flow_omits_pkce_challenge() {
let isolation = TestSecrets::new();
let gateway = test_gateway(&isolation).await.unwrap();
gateway
.register_provider(OAuthProvider {
name: "linear".to_string(),
display_name: "Linear".to_string(),
auth_type: OAuthType::OAuth2AuthorizationCode,
authorize_url: "https://linear.app/oauth/authorize".to_string(),
token_url: "https://api.linear.app/oauth/token".to_string(),
scopes: vec!["read".to_string()],
allowed_domains: vec!["api.linear.app".to_string()],
})
.await;
std::fs::write(
isolation.env_file_path(),
"OAUTH_LINEAR_CLIENT_ID=abc\nOAUTH_LINEAR_CLIENT_SECRET=def\n",
)
.unwrap();
let (authorize_url, _) = gateway.start_oauth2_flow("linear", "test").await.unwrap();
assert!(authorize_url.contains("response_type=code"));
assert!(!authorize_url.contains("code_challenge="));
}
#[tokio::test]
async fn client_credentials_flow_stores_connection_and_profile() {
let isolation = TestSecrets::new();
async fn token_handler(
Form(form): Form<HashMap<String, String>>,
) -> Json<serde_json::Value> {
assert_eq!(
form.get("grant_type").map(String::as_str),
Some("client_credentials")
);
Json(serde_json::json!({
"access_token": "test-access",
"expires_in": 3600
}))
}
let app = Router::new().route("/oauth/token", post(token_handler));
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
let addr: SocketAddr = listener.local_addr().unwrap();
tokio::spawn(async move {
let _ = axum::serve(listener, app).await;
});
let gateway = test_gateway(&isolation).await.unwrap();
gateway
.register_provider(OAuthProvider {
name: "service".to_string(),
display_name: "Service".to_string(),
auth_type: OAuthType::OAuth2ClientCredentials,
authorize_url: String::new(),
token_url: format!("http://{}/oauth/token", addr),
scopes: vec!["read".to_string()],
allowed_domains: vec!["api.example.com".to_string()],
})
.await;
std::fs::write(
isolation.env_file_path(),
"OAUTH_SERVICE_CLIENT_ID=abc\nOAUTH_SERVICE_CLIENT_SECRET=def\n",
)
.unwrap();
let result = gateway.connect_client_credentials("service").await.unwrap();
assert!(result.contains("Connected to Service"));
let conn = gateway
.state_store
.get_oauth_connection("service")
.await
.unwrap()
.unwrap();
assert_eq!(conn.auth_type, "oauth2_client_credentials");
let profiles = gateway.http_profiles.read().await;
let profile = profiles.get("service").unwrap();
assert_eq!(profile.token.as_deref(), Some("test-access"));
}
#[tokio::test]
async fn callback_survives_restart_using_persisted_pending_flow() {
let isolation = TestSecrets::new();
async fn token_handler(
Form(form): Form<HashMap<String, String>>,
) -> Json<serde_json::Value> {
assert_eq!(
form.get("grant_type").map(String::as_str),
Some("authorization_code")
);
assert_eq!(form.get("code").map(String::as_str), Some("auth-code"));
assert!(form.contains_key("code_verifier"));
Json(serde_json::json!({
"access_token": "restart-safe-token",
"refresh_token": "refresh-123",
"expires_in": 3600
}))
}
let app = Router::new().route("/oauth/token", post(token_handler));
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
let addr: SocketAddr = listener.local_addr().unwrap();
tokio::spawn(async move {
let _ = axum::serve(listener, app).await;
});
let gateway = test_gateway(&isolation).await.unwrap();
gateway
.register_provider(OAuthProvider {
name: "linear".to_string(),
display_name: "Linear".to_string(),
auth_type: OAuthType::OAuth2Pkce,
authorize_url: "https://linear.app/oauth/authorize".to_string(),
token_url: format!("http://{addr}/oauth/token"),
scopes: vec!["read".to_string()],
allowed_domains: vec!["api.linear.app".to_string()],
})
.await;
std::fs::write(
isolation.env_file_path(),
"OAUTH_LINEAR_CLIENT_ID=abc\nOAUTH_LINEAR_CLIENT_SECRET=def\n",
)
.unwrap();
let (authorize_url, _result_rx) =
gateway.start_oauth2_flow("linear", "test").await.unwrap();
let state = state_from_authorize_url(&authorize_url);
{
let mut flows = gateway.pending_flows.write().await;
flows.clear();
}
let result = gateway
.handle_callback(&state, Some("auth-code"), None)
.await
.unwrap();
assert!(result.contains("Connected to Linear"));
assert!(
gateway
.state_store
.get_pending_oauth_flow(&state)
.await
.unwrap()
.is_none()
);
assert!(
gateway
.state_store
.get_oauth_connection("linear")
.await
.unwrap()
.is_some()
);
}
#[tokio::test]
async fn callback_refresh_reuses_recent_result_message() {
let isolation = TestSecrets::new();
let gateway = test_gateway(&isolation).await.unwrap();
gateway
.register_provider(OAuthProvider {
name: "linear".to_string(),
display_name: "Linear".to_string(),
auth_type: OAuthType::OAuth2AuthorizationCode,
authorize_url: "https://linear.app/oauth/authorize".to_string(),
token_url: "https://api.linear.app/oauth/token".to_string(),
scopes: vec!["read".to_string()],
allowed_domains: vec!["api.linear.app".to_string()],
})
.await;
std::fs::write(
isolation.env_file_path(),
"OAUTH_LINEAR_CLIENT_ID=abc\nOAUTH_LINEAR_CLIENT_SECRET=def\n",
)
.unwrap();
let (authorize_url, _result_rx) =
gateway.start_oauth2_flow("linear", "test").await.unwrap();
let state = state_from_authorize_url(&authorize_url);
let first = gateway
.handle_callback(&state, None, Some("access_denied"))
.await
.unwrap();
let second = gateway.handle_callback(&state, None, None).await.unwrap();
assert_eq!(first, "OAuth authorization denied: access_denied");
assert_eq!(second, first);
}
#[tokio::test]
async fn cleanup_expires_persisted_flows_and_keeps_callback_message() {
let gateway = test_gateway(&TestSecrets::new()).await.unwrap();
gateway
.state_store
.save_pending_oauth_flow(&PendingOAuthFlow {
state: "expired-state".to_string(),
service: "linear".to_string(),
code_verifier: Some("verifier".to_string()),
session_id: "session-1".to_string(),
created_at: (chrono::Utc::now()
- chrono::Duration::seconds((FLOW_TIMEOUT_SECS as i64) + 30))
.to_rfc3339(),
})
.await
.unwrap();
gateway.cleanup_expired_flows().await;
assert!(
gateway
.state_store
.get_pending_oauth_flow("expired-state")
.await
.unwrap()
.is_none()
);
let message = gateway
.handle_callback("expired-state", Some("unused"), None)
.await
.unwrap();
assert_eq!(message, OAuthGateway::expired_flow_message());
}
#[tokio::test]
async fn refresh_token_rebuilds_missing_http_profile() {
let isolation = TestSecrets::new();
async fn token_handler(
Form(form): Form<HashMap<String, String>>,
) -> Json<serde_json::Value> {
assert_eq!(
form.get("grant_type").map(String::as_str),
Some("refresh_token")
);
assert_eq!(
form.get("refresh_token").map(String::as_str),
Some("refresh-123")
);
Json(serde_json::json!({
"access_token": "refreshed-access",
"expires_in": 3600
}))
}
let app = Router::new().route("/oauth/token", post(token_handler));
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
let addr: SocketAddr = listener.local_addr().unwrap();
tokio::spawn(async move {
let _ = axum::serve(listener, app).await;
});
let gateway = test_gateway(&isolation).await.unwrap();
gateway
.register_provider(OAuthProvider {
name: "twitter".to_string(),
display_name: "Twitter/X".to_string(),
auth_type: OAuthType::OAuth2Pkce,
authorize_url: "https://twitter.com/i/oauth2/authorize".to_string(),
token_url: format!("http://{addr}/oauth/token"),
scopes: vec!["tweet.read".to_string()],
allowed_domains: vec!["api.twitter.com".to_string(), "api.x.com".to_string()],
})
.await;
std::fs::write(
isolation.env_file_path(),
"OAUTH_TWITTER_CLIENT_ID=abc\nOAUTH_TWITTER_CLIENT_SECRET=def\nOAUTH_TWITTER_REFRESH_TOKEN=refresh-123\n",
)
.unwrap();
gateway
.state_store
.save_oauth_connection(&OAuthConnection {
id: 0,
service: "twitter".to_string(),
auth_type: "oauth2_pkce".to_string(),
username: None,
account_id: Some("stable-account-123".to_string()),
scopes: r#"["tweet.read"]"#.to_string(),
token_expires_at: None,
created_at: chrono::Utc::now().to_rfc3339(),
updated_at: chrono::Utc::now().to_rfc3339(),
})
.await
.unwrap();
let result = gateway.refresh_token("twitter").await.unwrap();
assert_eq!(result, "Token refreshed for twitter");
let profiles = gateway.http_profiles.read().await;
let profile = profiles.get("twitter").expect("twitter profile rebuilt");
assert_eq!(profile.token.as_deref(), Some("refreshed-access"));
assert_eq!(profile.user_id.as_deref(), Some("stable-account-123"));
assert!(
profile
.allowed_domains
.iter()
.any(|domain| domain == "api.x.com")
);
}
#[tokio::test]
async fn expired_token_preflight_is_serialized_and_preserves_account_binding() {
let isolation = TestSecrets::new();
async fn token_handler(
Form(form): Form<HashMap<String, String>>,
) -> Json<serde_json::Value> {
assert_eq!(
form.get("grant_type").map(String::as_str),
Some("refresh_token")
);
Json(serde_json::json!({
"access_token": "preflight-access",
"expires_in": 3600
}))
}
let app = Router::new().route("/oauth/token", post(token_handler));
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
let addr: SocketAddr = listener.local_addr().unwrap();
tokio::spawn(async move {
let _ = axum::serve(listener, app).await;
});
let gateway = test_gateway(&isolation).await.unwrap();
gateway
.register_provider(OAuthProvider {
name: "twitter".to_string(),
display_name: "Twitter/X".to_string(),
auth_type: OAuthType::OAuth2Pkce,
authorize_url: "https://twitter.com/i/oauth2/authorize".to_string(),
token_url: format!("http://{addr}/oauth/token"),
scopes: vec!["tweet.read".to_string()],
allowed_domains: vec!["api.twitter.com".to_string(), "api.x.com".to_string()],
})
.await;
std::fs::write(
isolation.env_file_path(),
"OAUTH_TWITTER_CLIENT_ID=abc\nOAUTH_TWITTER_CLIENT_SECRET=def\nOAUTH_TWITTER_REFRESH_TOKEN=refresh-123\n",
)
.unwrap();
gateway
.state_store
.save_oauth_connection(&OAuthConnection {
id: 0,
service: "twitter".to_string(),
auth_type: "oauth2_pkce".to_string(),
username: None,
account_id: Some("stable-account-789".to_string()),
scopes: r#"["tweet.read"]"#.to_string(),
token_expires_at: Some(
(chrono::Utc::now() - chrono::Duration::minutes(1)).to_rfc3339(),
),
created_at: chrono::Utc::now().to_rfc3339(),
updated_at: chrono::Utc::now().to_rfc3339(),
})
.await
.unwrap();
let (first, second) = tokio::join!(
gateway.refresh_access_token_if_needed("twitter"),
gateway.refresh_access_token_if_needed("twitter")
);
let refresh_results = [first.unwrap(), second.unwrap()];
assert_eq!(
refresh_results
.into_iter()
.filter(|refreshed| *refreshed)
.count(),
1,
"concurrent expiry checks must perform exactly one token exchange"
);
let profiles = gateway.http_profiles.read().await;
let profile = profiles.get("twitter").expect("refreshed HTTP profile");
assert_eq!(profile.token.as_deref(), Some("preflight-access"));
assert_eq!(profile.user_id.as_deref(), Some("stable-account-789"));
}
#[tokio::test]
async fn restore_connections_refreshes_when_access_token_missing() {
let isolation = TestSecrets::new();
async fn token_handler(
Form(form): Form<HashMap<String, String>>,
) -> Json<serde_json::Value> {
assert_eq!(
form.get("grant_type").map(String::as_str),
Some("refresh_token")
);
Json(serde_json::json!({
"access_token": "restored-via-refresh",
"expires_in": 3600
}))
}
let app = Router::new().route("/oauth/token", post(token_handler));
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
let addr: SocketAddr = listener.local_addr().unwrap();
tokio::spawn(async move {
let _ = axum::serve(listener, app).await;
});
let gateway = test_gateway(&isolation).await.unwrap();
gateway
.register_provider(OAuthProvider {
name: "twitter".to_string(),
display_name: "Twitter/X".to_string(),
auth_type: OAuthType::OAuth2Pkce,
authorize_url: "https://twitter.com/i/oauth2/authorize".to_string(),
token_url: format!("http://{addr}/oauth/token"),
scopes: vec!["tweet.read".to_string()],
allowed_domains: vec!["api.twitter.com".to_string(), "api.x.com".to_string()],
})
.await;
std::fs::write(
isolation.env_file_path(),
"OAUTH_TWITTER_CLIENT_ID=abc\nOAUTH_TWITTER_CLIENT_SECRET=def\nOAUTH_TWITTER_REFRESH_TOKEN=refresh-123\n",
)
.unwrap();
gateway
.state_store
.save_oauth_connection(&OAuthConnection {
id: 0,
service: "twitter".to_string(),
auth_type: "oauth2_pkce".to_string(),
username: None,
account_id: Some("stable-account-456".to_string()),
scopes: r#"["tweet.read"]"#.to_string(),
token_expires_at: None,
created_at: chrono::Utc::now().to_rfc3339(),
updated_at: chrono::Utc::now().to_rfc3339(),
})
.await
.unwrap();
gateway.restore_connections().await;
let profiles = gateway.http_profiles.read().await;
let profile = profiles
.get("twitter")
.expect("twitter profile should be restored via refresh");
assert_eq!(profile.token.as_deref(), Some("restored-via-refresh"));
assert_eq!(profile.user_id.as_deref(), Some("stable-account-456"));
}
fn synthetic_connection(auth_type: &str, expires_at: Option<&str>) -> OAuthConnection {
OAuthConnection {
id: 0,
service: "twitter".to_string(),
auth_type: auth_type.to_string(),
username: None,
account_id: Some("1000000000000000001".to_string()),
scopes: r#"["tweet.read"]"#.to_string(),
token_expires_at: expires_at.map(str::to_string),
created_at: chrono::Utc::now().to_rfc3339(),
updated_at: chrono::Utc::now().to_rfc3339(),
}
}
#[test]
fn credential_readiness_treats_an_expired_bearer_with_a_refresh_path_as_ready() {
let isolation = TestSecrets::new();
std::fs::write(
isolation.env_file_path(),
"OAUTH_TWITTER_ACCESS_TOKEN=stale\nOAUTH_TWITTER_REFRESH_TOKEN=rt\nOAUTH_TWITTER_CLIENT_ID=id\nOAUTH_TWITTER_CLIENT_SECRET=secret\n",
)
.unwrap();
let now = chrono::Utc::now();
let expired = (now - chrono::Duration::hours(3)).to_rfc3339();
let connection = synthetic_connection("oauth2_pkce", Some(&expired));
let readiness =
CredentialReadiness::for_connection(&connection, now, isolation.store.as_ref());
assert_eq!(
readiness,
CredentialReadiness::Refreshable {
expired_at: Some(expired.clone()),
path: CredentialRefreshPath::RefreshToken,
}
);
assert_eq!(readiness.state(), "ready-via-refresh");
let text = readiness.describe();
assert!(text.starts_with("ready-via-refresh ("), "{text}");
assert!(text.contains(&expired), "{text}");
assert!(text.contains("refresh token stored"), "{text}");
}
#[test]
fn credential_readiness_reports_a_live_bearer_as_ready() {
let isolation = TestSecrets::new();
std::fs::write(
isolation.env_file_path(),
"OAUTH_TWITTER_ACCESS_TOKEN=live\n",
)
.unwrap();
let now = chrono::Utc::now();
let valid_until = (now + chrono::Duration::hours(1)).to_rfc3339();
let connection = synthetic_connection("oauth2_pkce", Some(&valid_until));
let readiness =
CredentialReadiness::for_connection(&connection, now, isolation.store.as_ref());
assert_eq!(
readiness,
CredentialReadiness::Ready {
valid_until: Some(valid_until.clone()),
}
);
assert_eq!(readiness.state(), "ready");
assert!(readiness.describe().contains(&valid_until));
}
#[test]
fn credential_readiness_inside_the_refresh_skew_is_not_ready_without_a_refresh_path() {
let isolation = TestSecrets::new();
std::fs::write(
isolation.env_file_path(),
"OAUTH_TWITTER_ACCESS_TOKEN=nearly-stale\nOAUTH_TWITTER_CLIENT_ID=id\nOAUTH_TWITTER_CLIENT_SECRET=secret\n",
)
.unwrap();
let now = chrono::Utc::now();
let almost =
(now + chrono::Duration::seconds(ACCESS_TOKEN_REFRESH_SKEW_SECS / 2)).to_rfc3339();
let connection = synthetic_connection("oauth2_pkce", Some(&almost));
let readiness =
CredentialReadiness::for_connection(&connection, now, isolation.store.as_ref());
assert_eq!(
readiness,
CredentialReadiness::ReauthRequired {
missing: vec!["refresh token"],
}
);
assert_eq!(readiness.state(), "reauth-required");
assert!(
readiness
.describe()
.contains("reauth-required (missing: refresh token)")
);
}
#[test]
fn credential_readiness_uses_the_client_credentials_grant_when_no_bearer_is_stored() {
let isolation = TestSecrets::new();
std::fs::write(
isolation.env_file_path(),
"OAUTH_TWITTER_CLIENT_ID=id\nOAUTH_TWITTER_CLIENT_SECRET=secret\n",
)
.unwrap();
let connection = synthetic_connection("oauth2_client_credentials", None);
let readiness = CredentialReadiness::for_connection(
&connection,
chrono::Utc::now(),
isolation.store.as_ref(),
);
assert_eq!(
readiness,
CredentialReadiness::Refreshable {
expired_at: None,
path: CredentialRefreshPath::ClientCredentials,
}
);
let text = readiness.describe();
assert!(text.contains("no bearer stored"), "{text}");
assert!(text.contains("client-credentials grant"), "{text}");
}
#[test]
fn credential_readiness_lists_every_missing_secret_when_nothing_is_stored() {
let isolation = TestSecrets::new();
std::fs::write(isolation.env_file_path(), "").unwrap();
let connection = synthetic_connection("oauth2_pkce", None);
let readiness = CredentialReadiness::for_connection(
&connection,
chrono::Utc::now(),
isolation.store.as_ref(),
);
assert_eq!(
readiness,
CredentialReadiness::ReauthRequired {
missing: vec![
"access token",
"refresh token",
"client id",
"client secret"
],
}
);
}
}