use std::sync::LazyLock;
use std::time::Duration;
use anyhow::Result;
use serde::Deserialize;
use crate::claude::{LinkState, classify_credentials_link};
use crate::lock::with_state_lock;
use crate::profile::{AppConfig, save_app_state, save_profile};
use crate::usage::{UsageStore, now_ms};
const TOKEN_ENDPOINT: &str = "https://api.anthropic.com/v1/oauth/token";
const CLIENT_ID: &str = "9d1c250a-e61b-44d9-88ed-5944d1962f5e";
const MESSAGES_ENDPOINT: &str = "https://api.anthropic.com/v1/messages";
const KICK_MODEL: &str = "claude-haiku-4-5-20251001";
const KICK_SYSTEM_PROMPT: &str = "You are Claude Code, Anthropic's official CLI for Claude.";
const AUTO_START_COOLDOWN_MS: u64 = 4 * 3600 * 1000 + 30 * 60 * 1000;
#[derive(Deserialize)]
pub(crate) struct TokenResponse {
pub(crate) access_token: String,
pub(crate) refresh_token: String,
pub(crate) expires_in: u64,
#[serde(default)]
pub(crate) scope: Option<String>,
}
static AGENT: LazyLock<ureq::Agent> = LazyLock::new(|| {
ureq::Agent::config_builder()
.timeout_connect(Some(Duration::from_secs(4)))
.timeout_recv_response(Some(Duration::from_secs(15)))
.build()
.into()
});
pub(crate) fn refresh(refresh_token: &str) -> Result<TokenResponse> {
let body = serde_json::to_string(&serde_json::json!({
"grant_type": "refresh_token",
"refresh_token": refresh_token,
"client_id": CLIENT_ID,
}))?;
let text = AGENT
.post(TOKEN_ENDPOINT)
.header("Content-Type", "application/json")
.send(&body)
.map_err(crate::ureq_error::into_anyhow)?
.body_mut()
.read_to_string()
.map_err(crate::ureq_error::into_anyhow)?;
serde_json::from_str(&text).map_err(|e| anyhow::anyhow!("{e}: {text}"))
}
fn kick(access_token: &str) -> Result<()> {
let body = serde_json::to_string(&serde_json::json!({
"model": KICK_MODEL,
"max_tokens": 1,
"system": [{ "type": "text", "text": KICK_SYSTEM_PROMPT }],
"messages": [{ "role": "user", "content": "x" }],
}))?;
AGENT
.post(MESSAGES_ENDPOINT)
.header("Content-Type", "application/json")
.header("Authorization", &format!("Bearer {access_token}"))
.header("anthropic-version", "2023-06-01")
.header("anthropic-beta", "oauth-2025-04-20")
.send(&body)
.map_err(crate::ureq_error::into_anyhow)?;
Ok(())
}
pub(crate) fn refresh_all(config: &mut AppConfig) -> Vec<String> {
let skip_active = active_link_diverged(config);
let snapshots: Vec<(String, String)> = config
.profiles
.iter()
.filter_map(|p| {
if skip_active && config.is_active(&p.name) {
return None;
}
Some((p.name.clone(), p.refresh_token()?.to_string()))
})
.collect();
if snapshots.is_empty() {
return Vec::new();
}
let handles: Vec<_> = snapshots
.into_iter()
.map(|(name, rt)| std::thread::spawn(move || (name, refresh(&rt))))
.collect();
let mut refreshed = Vec::new();
for h in handles {
let Ok((name, Ok(tok))) = h.join() else {
continue;
};
let saved = with_state_lock(|| {
let Some(profile) = config.find_mut(&name) else {
return Ok::<_, anyhow::Error>(false);
};
let Some(creds) = profile.credentials.as_mut() else {
return Ok(false);
};
let Some(oauth) = creds.claude_ai_oauth.as_mut() else {
return Ok(false);
};
oauth.access_token = tok.access_token;
oauth.refresh_token = Some(tok.refresh_token);
oauth.expires_at = Some((now_ms() + tok.expires_in * 1000) as i64);
if let Some(scope) = tok.scope {
oauth.scopes = Some(scope.split_whitespace().map(String::from).collect());
}
Ok(save_profile(profile).is_ok())
})
.unwrap_or(false);
if saved {
refreshed.push(name);
}
}
refreshed
}
pub(crate) fn auto_start_windows(config: &mut AppConfig, store: &UsageStore) -> Vec<String> {
let skip_active = active_link_diverged(config);
let snapshots: Vec<(String, String)> = match with_state_lock(|| {
let now = now_ms();
let mut claimed = Vec::new();
for profile in &config.profiles {
if !profile.auto_start {
continue;
}
if skip_active && config.is_active(&profile.name) {
continue;
}
let resets_at = {
let usage = store.lock().ok();
usage
.as_ref()
.and_then(|s| s.get(&profile.name))
.and_then(|u| u.five_hour.as_ref())
.and_then(|w| w.resets_at.clone())
};
if resets_at.is_some() {
continue;
}
let last = config
.state
.last_auto_start_at
.get(&profile.name)
.copied()
.unwrap_or(0);
if now.saturating_sub(last) < AUTO_START_COOLDOWN_MS {
continue;
}
let Some(token) = profile.refresh_token().map(str::to_string) else {
continue;
};
claimed.push((profile.name.clone(), token));
}
for (name, _) in &claimed {
config.state.last_auto_start_at.insert(name.clone(), now);
}
if !claimed.is_empty() {
let _ = save_app_state(&config.state);
}
Ok::<_, anyhow::Error>(claimed)
}) {
Ok(s) => s,
Err(_) => return Vec::new(),
};
let handles: Vec<_> = snapshots
.into_iter()
.map(|(name, rt)| std::thread::spawn(move || (name, refresh(&rt))))
.collect();
let mut kicked = Vec::new();
for h in handles {
let Ok((name, Ok(tok))) = h.join() else {
continue;
};
let saved_access = with_state_lock(|| {
let Some(profile) = config.find_mut(&name) else {
return Ok::<_, anyhow::Error>(None);
};
let Some(creds) = profile.credentials.as_mut() else {
return Ok(None);
};
let Some(oauth) = creds.claude_ai_oauth.as_mut() else {
return Ok(None);
};
oauth.access_token = tok.access_token.clone();
oauth.refresh_token = Some(tok.refresh_token);
oauth.expires_at = Some((now_ms() + tok.expires_in * 1000) as i64);
if let Some(scope) = tok.scope {
oauth.scopes = Some(scope.split_whitespace().map(String::from).collect());
}
if save_profile(profile).is_err() {
return Ok(None);
}
Ok(Some(tok.access_token))
})
.unwrap_or(None);
let Some(access_token) = saved_access else {
continue;
};
if kick(&access_token).is_ok() {
kicked.push(name);
}
}
kicked
}
pub(crate) fn auto_start_named(config: &mut AppConfig, name: &str) -> bool {
let now = now_ms();
let token = match with_state_lock(|| {
let Some(profile) = config.find(name) else {
return Ok::<_, anyhow::Error>(None);
};
if !profile.auto_start {
return Ok(None);
}
if active_link_diverged(config) && config.is_active(name) {
return Ok(None);
}
let last = config
.state
.last_auto_start_at
.get(name)
.copied()
.unwrap_or(0);
if now.saturating_sub(last) < AUTO_START_COOLDOWN_MS {
return Ok(None);
}
let Some(rt) = profile.refresh_token().map(str::to_string) else {
return Ok(None);
};
config
.state
.last_auto_start_at
.insert(name.to_string(), now);
let _ = save_app_state(&config.state);
Ok(Some(rt))
}) {
Ok(Some(t)) => t,
_ => return false,
};
let Ok(tok) = refresh(&token) else {
return false;
};
let access_token = tok.access_token.clone();
if !apply_rotated_tokens(config, name, tok) {
return false;
}
kick(&access_token).is_ok()
}
pub(crate) fn apply_rotated_tokens(config: &mut AppConfig, name: &str, tok: TokenResponse) -> bool {
with_state_lock(|| {
let Some(profile) = config.find_mut(name) else {
return Ok::<_, anyhow::Error>(false);
};
let Some(creds) = profile.credentials.as_mut() else {
return Ok(false);
};
let Some(oauth) = creds.claude_ai_oauth.as_mut() else {
return Ok(false);
};
oauth.access_token = tok.access_token;
oauth.refresh_token = Some(tok.refresh_token);
oauth.expires_at = Some((now_ms() + tok.expires_in * 1000) as i64);
if let Some(scope) = tok.scope {
oauth.scopes = Some(scope.split_whitespace().map(String::from).collect());
}
Ok(save_profile(profile).is_ok())
})
.unwrap_or(false)
}
fn active_link_diverged(config: &AppConfig) -> bool {
config.state.active_profile.as_deref().is_some_and(|name| {
matches!(
classify_credentials_link(name).ok(),
Some(LinkState::Diverged)
)
})
}