use std::sync::{Arc, 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, OAuthToken, clear_staged_credentials, save_profile, stage_rotated_credentials,
};
use crate::runtime::{RotationGuard, has_live_session};
use crate::usage::{
ActivityKind, ActivityStore, OpResult, OpResultSender, ProfileActivity, RefetchQueue,
clear_activity, mark_activity, 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 ROTATION_STEP_DELAY_MS: u64 = 2000;
#[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)))
.http_status_as_error(false)
.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 mut response = AGENT
.post(TOKEN_ENDPOINT)
.header("Content-Type", "application/json")
.send(&body)
.map_err(crate::ureq_error::into_anyhow)?;
let status = response.status().as_u16();
let text = response
.body_mut()
.read_to_string()
.map_err(crate::ureq_error::into_anyhow)?;
if status >= 400 {
anyhow::bail!("HTTP {status}: {text}");
}
serde_json::from_str(&text).map_err(|e| anyhow::anyhow!("{e}: {text}"))
}
enum KickError {
Status(u16),
Other(anyhow::Error),
}
impl From<KickError> for anyhow::Error {
fn from(e: KickError) -> Self {
match e {
KickError::Status(s) => anyhow::anyhow!("HTTP {s}"),
KickError::Other(e) => e,
}
}
}
fn kick(access_token: &str) -> std::result::Result<(), KickError> {
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" }],
}))
.map_err(|e| KickError::Other(e.into()))?;
let status = 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(|e| KickError::Other(crate::ureq_error::into_anyhow(e)))?
.status()
.as_u16();
if status >= 400 {
return Err(KickError::Status(status));
}
Ok(())
}
pub(crate) struct KickResult {
pub(crate) opened: bool,
pub(crate) rotated: Option<(String, Option<String>)>,
}
impl KickResult {
fn not_opened() -> Self {
Self {
opened: false,
rotated: None,
}
}
}
pub(crate) fn auto_start_kick(
config: &crate::profile::ConfigHandle,
name: &str,
access_token: &str,
refresh_token: Option<&str>,
access_expires_at: Option<i64>,
activity: Option<&ActivityStore>,
) -> KickResult {
match kick(access_token) {
Ok(()) => {
return KickResult {
opened: true,
rotated: None,
};
}
Err(KickError::Status(401)) => {}
Err(KickError::Status(429))
if access_expires_at.is_some_and(|exp| now_ms() as i64 >= exp) => {}
Err(_) => return KickResult::not_opened(),
}
let Some(rt) = refresh_token else {
return KickResult::not_opened();
};
std::thread::sleep(std::time::Duration::from_millis(ROTATION_STEP_DELAY_MS));
let Ok(rotation_guard) = RotationGuard::acquire(name) else {
return KickResult::not_opened();
};
if has_live_session(name) {
return KickResult::not_opened();
}
if let Some(activity) = activity {
mark_activity(activity, name, ProfileActivity::Refreshing);
}
let refreshed = refresh(rt);
if let Some(activity) = activity {
mark_activity(activity, name, ProfileActivity::Fetching);
}
let tok = match refreshed {
Ok(t) => t,
Err(_) => return KickResult::not_opened(),
};
let access = tok.access_token.clone();
let new_refresh = tok.refresh_token.clone();
if apply_rotated_tokens_locked(config, name, tok).is_err() {
return KickResult::not_opened();
}
let rotated = Some((access.clone(), Some(new_refresh)));
drop(rotation_guard);
std::thread::sleep(std::time::Duration::from_millis(ROTATION_STEP_DELAY_MS));
let opened = kick(&access).is_ok();
std::thread::sleep(std::time::Duration::from_millis(ROTATION_STEP_DELAY_MS));
KickResult { opened, rotated }
}
enum RotateOutcome {
GuardBusy,
Persisted(bool),
}
fn rotate_one_inner(
config: &crate::profile::ConfigHandle,
name: &str,
activity: Option<&ActivityStore>,
sender: &OpResultSender,
force: bool,
) -> RotateOutcome {
let Ok(_rotation_guard) = RotationGuard::acquire(name) else {
return RotateOutcome::GuardBusy;
};
let token = {
let cfg = config.lock().expect("config mutex poisoned");
with_state_lock(|| {
if !force && has_live_session(name) {
return Ok::<_, anyhow::Error>(None);
}
let rt = cfg
.find(name)
.and_then(|p| p.refresh_token().map(str::to_string));
if rt.is_some()
&& let Some(activity) = activity
{
mark_activity(activity, name, ProfileActivity::Refreshing);
}
Ok(rt)
})
.ok()
.flatten()
};
let Some(rt) = token else {
return RotateOutcome::Persisted(false);
};
let outcome = refresh(&rt).and_then(|tok| apply_rotated_tokens_locked(config, name, tok));
let applied = outcome.is_ok();
if let Some(activity) = activity {
clear_activity(activity, name);
}
let _ = sender.send(OpResult {
name: name.to_string(),
kind: ActivityKind::Refreshing,
outcome,
});
RotateOutcome::Persisted(applied)
}
pub(crate) fn rotation_candidates(config: &AppConfig, force: bool) -> Vec<(String, String)> {
let skip_active = !force && active_link_diverged(config);
config
.profiles
.iter()
.filter_map(|p| {
if skip_active && config.is_active(&p.name) {
return None;
}
if !force && has_live_session(&p.name) {
return None;
}
Some((p.name.to_string(), p.refresh_token()?.to_string()))
})
.collect()
}
pub(crate) fn refresh_all(
config: &crate::profile::ConfigHandle,
force: bool,
refetch: &RefetchQueue,
activity: &ActivityStore,
sender: &OpResultSender,
) -> Vec<String> {
let snapshots = {
let cfg = config.lock().expect("config mutex poisoned");
rotation_candidates(&cfg, force)
};
if snapshots.is_empty() {
return Vec::new();
}
for (name, _) in &snapshots {
mark_activity(activity, name, ProfileActivity::Refreshing);
}
let handles: Vec<(String, _)> = snapshots
.into_iter()
.map(|(name, _rt)| {
let config = Arc::clone(config);
let activity = Arc::clone(activity);
let sender = sender.clone();
let name_for_handle = name.clone();
let h = std::thread::spawn(move || {
let outcome = rotate_one_inner(&config, &name, Some(&activity), &sender, force);
(name, outcome)
});
(name_for_handle, h)
})
.collect();
let mut refreshed = Vec::new();
for (name, h) in handles {
match h.join() {
Ok((n, RotateOutcome::Persisted(true))) => refreshed.push(n),
Ok((n, RotateOutcome::GuardBusy)) => {
let _ = sender.send(OpResult {
name: n.clone(),
kind: ActivityKind::Refreshing,
outcome: Err(anyhow::anyhow!("failed to acquire rotation lock")),
});
clear_activity(activity, &n);
}
Ok((n, RotateOutcome::Persisted(false))) => clear_activity(activity, &n),
Err(_) => {
clear_activity(activity, &name);
}
}
}
if let Ok(mut q) = refetch.lock() {
for name in &refreshed {
q.insert(name.clone());
}
}
refreshed
}
pub(crate) fn rotate_one(
config: &crate::profile::ConfigHandle,
name: &str,
refetch: &RefetchQueue,
activity: &ActivityStore,
sender: &OpResultSender,
force: bool,
) -> bool {
mark_activity(activity, name, ProfileActivity::Refreshing);
let persisted = match rotate_one_inner(config, name, Some(activity), sender, force) {
RotateOutcome::Persisted(true) => true,
RotateOutcome::GuardBusy => {
let _ = sender.send(OpResult {
name: name.to_string(),
kind: ActivityKind::Refreshing,
outcome: Err(anyhow::anyhow!("failed to acquire rotation lock")),
});
clear_activity(activity, name);
false
}
RotateOutcome::Persisted(false) => {
clear_activity(activity, name);
false
}
};
if persisted && let Ok(mut q) = refetch.lock() {
q.insert(name.to_string());
}
persisted
}
pub(crate) fn prime_window(config: &crate::profile::ConfigHandle, name: &str) -> bool {
let (access_token, refresh_token, expires_at) = {
let cfg = config.lock().expect("config mutex poisoned");
match with_state_lock(|| {
let Some(profile) = cfg.find(name) else {
return Ok::<_, anyhow::Error>(None);
};
if !profile.is_oauth() || !profile.auto_start {
return Ok(None);
}
let Some(token) = profile.access_token().map(str::to_string) else {
return Ok(None);
};
let refresh = profile.refresh_token().map(str::to_string);
Ok(Some((token, refresh, profile.access_token_expires_at())))
}) {
Ok(Some(t)) => t,
_ => return false,
}
};
auto_start_kick(
config,
name,
&access_token,
refresh_token.as_deref(),
expires_at,
None,
)
.opened
}
fn write_token_fields(oauth: &mut OAuthToken, tok: TokenResponse) {
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());
}
}
pub(crate) fn apply_rotated_tokens_locked(
config: &crate::profile::ConfigHandle,
name: &str,
tok: TokenResponse,
) -> Result<()> {
let mut cfg = config.lock().expect("config mutex poisoned");
with_state_lock(|| {
let Some(profile) = cfg.find_mut(name) else {
return Err(anyhow::anyhow!("failed to persist rotated tokens"));
};
let Some(creds) = profile.credentials.as_mut() else {
return Err(anyhow::anyhow!("failed to persist rotated tokens"));
};
let Some(oauth) = creds.claude_ai_oauth.as_mut() else {
return Err(anyhow::anyhow!("failed to persist rotated tokens"));
};
write_token_fields(oauth, tok);
if let Some(creds) = profile.credentials.as_ref() {
let _ = stage_rotated_credentials(name, creds);
}
if save_profile(profile).is_err() {
return Err(anyhow::anyhow!("failed to persist rotated tokens"));
}
clear_staged_credentials(name);
Ok(())
})
}
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)
)
})
}
#[cfg(test)]
#[path = "../tests/inline/oauth.rs"]
mod tests;