2
0

fix mcp token file

This commit is contained in:
2026-03-24 20:33:30 +09:00
parent a55e4eced1
commit bf33706688
2 changed files with 45 additions and 11 deletions

View File

@@ -37,13 +37,30 @@ pub fn bot_token_path() -> Result<PathBuf> {
Ok(config_dir.join("bot.json"))
}
/// Load session from token file
/// Load session from token file, fallback to oauth_session.json
pub fn load_session() -> Result<Session> {
let path = token_path()?;
let content = fs::read_to_string(&path)
.with_context(|| format!("Token file not found: {:?}. Run 'ailog login' first.", path))?;
let session: Session = serde_json::from_str(&content)?;
Ok(session)
if let Ok(content) = fs::read_to_string(&path) {
if let Ok(session) = serde_json::from_str::<Session>(&content) {
return Ok(session);
}
}
// Fallback: oauth_session.json (different field names)
let oauth_path = path.with_file_name("oauth_session.json");
if let Ok(content) = fs::read_to_string(&oauth_path) {
if let Ok(oauth) = serde_json::from_str::<serde_json::Value>(&content) {
if let (Some(did), Some(handle)) = (oauth["did"].as_str(), oauth["handle"].as_str()) {
return Ok(Session {
did: did.to_string(),
handle: handle.to_string(),
access_jwt: oauth["access_token"].as_str().unwrap_or("").to_string(),
refresh_jwt: oauth["refresh_token"].as_str().unwrap_or("").to_string(),
pds: oauth["pds"].as_str().map(|s| s.to_string()),
});
}
}
}
Err(anyhow::anyhow!("Token file not found: {:?}. Run 'ailog login' or 'ailog oauth' first.", path))
}
/// Save session to token file
@@ -54,13 +71,30 @@ pub fn save_session(session: &Session) -> Result<()> {
Ok(())
}
/// Load bot session from bot token file
/// Load bot session from bot token file, fallback to oauth_bot_session.json
pub fn load_bot_session() -> Result<Session> {
let path = bot_token_path()?;
let content = fs::read_to_string(&path)
.with_context(|| format!("Bot token file not found: {:?}. Run 'ailog login --bot' first.", path))?;
let session: Session = serde_json::from_str(&content)?;
Ok(session)
if let Ok(content) = fs::read_to_string(&path) {
if let Ok(session) = serde_json::from_str::<Session>(&content) {
return Ok(session);
}
}
// Fallback: oauth_bot_session.json
let oauth_path = path.with_file_name("oauth_bot_session.json");
if let Ok(content) = fs::read_to_string(&oauth_path) {
if let Ok(oauth) = serde_json::from_str::<serde_json::Value>(&content) {
if let (Some(did), Some(handle)) = (oauth["did"].as_str(), oauth["handle"].as_str()) {
return Ok(Session {
did: did.to_string(),
handle: handle.to_string(),
access_jwt: oauth["access_token"].as_str().unwrap_or("").to_string(),
refresh_jwt: oauth["refresh_token"].as_str().unwrap_or("").to_string(),
pds: oauth["pds"].as_str().map(|s| s.to_string()),
});
}
}
}
Err(anyhow::anyhow!("Bot token file not found: {:?}. Run 'ailog login --bot' or 'ailog oauth --bot' first.", path))
}
/// Save bot session to bot token file

View File

@@ -419,7 +419,7 @@ fn handle_post_create(text: &str) -> Result<String> {
&oauth_session, "POST", &full_url, dpop_nonce.as_deref(),
)?;
let res = client.post(&url)
.header("Authorization", format!("DPoP {}", bot_session.access_jwt))
.header("Authorization", format!("DPoP {}", oauth_session.access_token))
.header("DPoP", dpop_proof)
.json(&body)
.send()