bitrouter 1.0.0-alpha.34

BitRouter: an LLM API router. CLI + assembly library.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
//! Import Claude Code's stored Claude Pro/Max OAuth credential.
//!
//! Claude Code persists its subscription OAuth token as JSON under a
//! `claudeAiOauth` key — in the macOS login Keychain (generic password,
//! service `Claude Code-credentials`) and/or `~/.claude/.credentials.json`.
//! We read the Keychain first (macOS), then the file. `expiresAt` is in
//! milliseconds since the epoch; the credential store tracks seconds.
//!
//! Reference: OpenClaw `src/agents/cli-credentials.ts`
//! (<https://github.com/openclaw/openclaw>).

use std::path::{Path, PathBuf};
use std::sync::Mutex;

static WRITES: Mutex<()> = Mutex::new(());

use serde::Deserialize;
use serde_json::{Value, json};

use crate::providers::import::{ImportError, ImportSource, Imported, home_dir, keychain};
use bitrouter_ai::auth::credentials::OAuthToken;

/// macOS Keychain generic-password service Claude Code stores its token under.
const KEYCHAIN_SERVICE: &str = "Claude Code-credentials";
/// Human name for diagnostics.
const CLI_NAME: &str = "Claude Code";

/// The `{ "claudeAiOauth": { … } }` envelope used by both the file and the
/// Keychain item.
#[derive(Deserialize)]
struct CredentialsFile {
    #[serde(rename = "claudeAiOauth")]
    claude_ai_oauth: Option<ClaudeAiOauth>,
}

#[derive(Deserialize)]
struct ClaudeAiOauth {
    #[serde(rename = "accessToken")]
    access_token: Option<String>,
    #[serde(rename = "refreshToken")]
    refresh_token: Option<String>,
    /// Milliseconds since the epoch.
    #[serde(rename = "expiresAt")]
    expires_at: Option<u64>,
}

/// Import the Claude Code credential from its default locations: the macOS
/// Keychain (`Claude Code-credentials`) first, then
/// `~/.claude/.credentials.json`. Returns `Ok(None)` when neither carries a
/// Claude Code OAuth credential.
pub fn import() -> Result<Option<Imported>, ImportError> {
    // Keychain first (macOS). A malformed or absent item falls through to the
    // file, which is the authoritative error surface.
    if let Some(blob) = keychain::read_generic_password(KEYCHAIN_SERVICE, None)
        && let Ok(Some(token)) = parse_blob(&blob, "keychain")
    {
        return Ok(Some(Imported {
            token,
            source: ImportSource::Keychain(KEYCHAIN_SERVICE),
        }));
    }
    let path = home_dir()
        .ok_or(ImportError::NoHome)?
        .join(".claude")
        .join(".credentials.json");
    from_file(&path)
}

/// Import from a specific `.credentials.json`. `Ok(None)` when the file is
/// absent or carries no `claudeAiOauth` block.
pub fn from_file(path: &Path) -> Result<Option<Imported>, ImportError> {
    let bytes = match std::fs::read(path) {
        Ok(b) => b,
        Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(None),
        Err(source) => {
            return Err(ImportError::Io {
                path: path.to_path_buf(),
                source,
            });
        }
    };
    let blob = String::from_utf8_lossy(&bytes);
    match parse_blob(blob.as_ref(), &path.display().to_string())? {
        Some(token) => Ok(Some(Imported {
            token,
            source: ImportSource::File(path.to_path_buf()),
        })),
        None => Ok(None),
    }
}

/// Parse a `{ "claudeAiOauth": { … } }` blob. `Ok(None)` when the block is
/// absent; errors when it is present but missing an access token.
fn parse_blob(blob: &str, origin: &str) -> Result<Option<OAuthToken>, ImportError> {
    let parsed: CredentialsFile =
        serde_json::from_str(blob).map_err(|source| ImportError::Json {
            origin: origin.to_string(),
            source,
        })?;
    let Some(oauth) = parsed.claude_ai_oauth else {
        return Ok(None);
    };
    let access_token = oauth
        .access_token
        .filter(|s| !s.is_empty())
        .ok_or(ImportError::MissingAccessToken { cli: CLI_NAME })?;
    // `expiresAt` is milliseconds since the epoch; the store tracks seconds.
    let expires_at = oauth.expires_at.map(|ms| ms / 1000).unwrap_or(0);
    Ok(Some(OAuthToken {
        access_token,
        expires_at,
        refresh_token: oauth.refresh_token,
    }))
}

/// Where a live Claude Code credential was read from — so a refresh write-back
/// can target the *same* place rather than diverging into a second store.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ClaudeCodeSource {
    /// The macOS login Keychain generic-password item.
    Keychain {
        /// The generic-password service (`Claude Code-credentials`).
        service: &'static str,
        /// The account the item is keyed under — required for an in-place
        /// upsert so we update Claude Code's item, not a duplicate.
        account: String,
    },
    /// A JSON file on disk (`~/.claude/.credentials.json`).
    File(PathBuf),
}

impl std::fmt::Display for ClaudeCodeSource {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ClaudeCodeSource::Keychain { service, .. } => {
                write!(f, "macOS Keychain ({service})")
            }
            ClaudeCodeSource::File(path) => write!(f, "{}", path.display()),
        }
    }
}

/// A live Claude Code OAuth credential plus the source it was read from.
#[derive(Debug, Clone)]
pub struct LiveCredential {
    /// The current OAuth token (`expires_at` in seconds).
    pub token: OAuthToken,
    /// Where it came from — the write-back target.
    pub source: ClaudeCodeSource,
}

/// A live, read-write view of Claude Code's own Claude Pro/Max OAuth credential.
///
/// Tokens are read and refreshed in the same source used by `claude`, without
/// copying a live adoption into BitRouter's store. Compare-and-commit rejects a
/// replaced credential and preserves unrelated envelope fields. Coordination is
/// in process; simultaneous writes/rotation by the external CLI require a shared
/// cross-process lock, which this adapter does not provide.
///
/// Locations mirror Claude Code: macOS Keychain generic password
/// `Claude Code-credentials`, else `~/.claude/.credentials.json`. Envelope:
/// `{ "claudeAiOauth": { accessToken, refreshToken, expiresAt(ms), … } }`,
/// with any other fields (`scopes`, `subscriptionType`, …) preserved verbatim
/// across a write-back.
#[derive(Debug, Clone)]
pub struct ClaudeCodeStore {
    /// `Some` on macOS (try the Keychain first); `None` elsewhere or in tests.
    keychain_service: Option<&'static str>,
    /// The on-disk credential file (used when the Keychain has no item).
    file_path: PathBuf,
}

impl ClaudeCodeStore {
    /// The store at Claude Code's default system locations: the macOS Keychain
    /// (`Claude Code-credentials`) plus `~/.claude/.credentials.json`. `None`
    /// when no home directory can be resolved.
    pub fn system() -> Option<Self> {
        let file_path = home_dir()?.join(".claude").join(".credentials.json");
        #[cfg(target_os = "macos")]
        let keychain_service = Some(KEYCHAIN_SERVICE);
        #[cfg(not(target_os = "macos"))]
        let keychain_service = None;
        Some(Self {
            keychain_service,
            file_path,
        })
    }

    /// A file-only store at `path`, never touching the Keychain. Used on
    /// non-macOS hosts and in tests so a real Keychain is never read/written.
    pub fn file_only(path: impl Into<PathBuf>) -> Self {
        Self {
            keychain_service: None,
            file_path: path.into(),
        }
    }

    pub(crate) fn coordination_identity(&self) -> (Option<&'static str>, PathBuf) {
        if let Some(service) = self.keychain_service {
            return (Some(service), PathBuf::new());
        }
        let path =
            std::fs::canonicalize(&self.file_path).unwrap_or_else(|_| self.file_path.clone());
        (None, path)
    }

    /// Commit a rotation only while this live source still holds the credential
    /// from which it was refreshed. External processes still need shared locking.
    pub(crate) fn write_back_checked(
        &self,
        token: &OAuthToken,
        original: &LiveCredential,
    ) -> std::result::Result<(), bitrouter_ai::auth::store::StoreError> {
        use bitrouter_ai::auth::store::StoreError;
        let _guard = WRITES.lock().map_err(|_| StoreError::Unavailable)?;
        let current = self.read().map_err(|_| StoreError::Unavailable)?;
        let Some(current) = current else {
            return Err(StoreError::Conflict);
        };
        if current.source != original.source
            || (current.token != original.token && current.token != *token)
        {
            return Err(StoreError::Conflict);
        }
        self.write_back_locked(token, &original.source)
            .map_err(|_| StoreError::Unavailable)
    }

    /// Read the current credential, Keychain first (macOS) then the file.
    /// `Ok(None)` when neither source carries a Claude Code OAuth credential.
    pub fn read(&self) -> Result<Option<LiveCredential>, ImportError> {
        if let Some(service) = self.keychain_service
            && let Some(blob) = keychain::read_generic_password(service, None)
            && let Some(token) = parse_blob(&blob, "keychain")?
        {
            // The account is needed to write the rotated token back in place.
            // Fall back to an empty account rather than failing the read — a
            // write-back would then create the item under that account.
            let account = keychain::find_account(service).ok_or_else(|| {
                ImportError::Keyring(
                    "selected Claude Keychain account could not be resolved".into(),
                )
            })?;
            return Ok(Some(LiveCredential {
                token,
                source: ClaudeCodeSource::Keychain { service, account },
            }));
        }
        match from_file(&self.file_path)? {
            Some(imported) => Ok(Some(LiveCredential {
                token: imported.token,
                source: ClaudeCodeSource::File(self.file_path.clone()),
            })),
            None => Ok(None),
        }
    }

    /// Persist a refreshed `token` back to `source`, preserving every other
    /// field Claude Code wrote. File writes are atomic and `0600`; Keychain
    /// writes upsert the existing `(service, account)` item.
    pub fn write_back(
        &self,
        token: &OAuthToken,
        source: &ClaudeCodeSource,
    ) -> Result<(), ImportError> {
        let _guard = WRITES.lock().map_err(|_| ImportError::Io {
            path: self.file_path.clone(),
            source: std::io::Error::other("credential write lock unavailable"),
        })?;
        self.write_back_locked(token, source)
    }

    fn write_back_locked(
        &self,
        token: &OAuthToken,
        source: &ClaudeCodeSource,
    ) -> Result<(), ImportError> {
        match source {
            ClaudeCodeSource::File(path) => {
                let existing = std::fs::read(path).ok();
                let bytes = updated_envelope_bytes(existing.as_deref(), token)?;
                write_file_atomic_0600(path, &bytes)
            }
            ClaudeCodeSource::Keychain { service, account } => {
                let existing = keychain::read_generic_password(service, Some(account));
                let bytes = updated_envelope_bytes(existing.as_deref().map(str::as_bytes), token)?;
                // `to_vec_pretty` always emits valid UTF-8, so the lossy
                // conversion is exact here.
                let value = String::from_utf8_lossy(&bytes).into_owned();
                if keychain::write_generic_password(service, account, &value) {
                    Ok(())
                } else {
                    Err(ImportError::Io {
                        path: PathBuf::from(format!("keychain:{service}")),
                        source: std::io::Error::other("security add-generic-password failed"),
                    })
                }
            }
        }
    }
}

/// Merge a refreshed `token` into Claude Code's credential envelope, preserving
/// every other field. `existing` is the current raw blob (file bytes or
/// Keychain value); when absent or unparseable a fresh `{ "claudeAiOauth": {} }`
/// envelope is built. `expiresAt` is written back in **milliseconds**.
fn updated_envelope_bytes(
    existing: Option<&[u8]>,
    token: &OAuthToken,
) -> Result<Vec<u8>, ImportError> {
    let mut root: Value = match existing {
        Some(b) if !b.is_empty() => serde_json::from_slice(b).unwrap_or_else(|_| json!({})),
        _ => json!({}),
    };
    if !root.is_object() {
        root = json!({});
    }
    let obj = root
        .as_object_mut()
        .ok_or(ImportError::MissingAccessToken { cli: CLI_NAME })?;
    let entry = obj.entry("claudeAiOauth").or_insert_with(|| json!({}));
    if !entry.is_object() {
        *entry = json!({});
    }
    let oauth = entry
        .as_object_mut()
        .ok_or(ImportError::MissingAccessToken { cli: CLI_NAME })?;
    oauth.insert("accessToken".to_string(), json!(token.access_token));
    if let Some(rt) = &token.refresh_token {
        oauth.insert("refreshToken".to_string(), json!(rt));
    } else {
        oauth.remove("refreshToken");
    }
    // Retain the replacement's expiry semantics, never the old token's TTL.
    if token.expires_at > 0 {
        oauth.insert(
            "expiresAt".to_string(),
            json!(token.expires_at.saturating_mul(1000)),
        );
    } else {
        oauth.remove("expiresAt");
    }
    serde_json::to_vec_pretty(&root).map_err(|source| ImportError::Json {
        origin: "claude code credentials write-back".to_string(),
        source,
    })
}

/// Write `bytes` to `path` atomically with `0600` permissions (Unix), mirroring
/// the credential store's flush so the token never sits on a world-readable
/// file even for the width of the write.
fn write_file_atomic_0600(path: &Path, bytes: &[u8]) -> Result<(), ImportError> {
    if let Some(parent) = path.parent() {
        std::fs::create_dir_all(parent).map_err(|source| ImportError::Io {
            path: parent.to_path_buf(),
            source,
        })?;
    }
    let mut tmp = path.as_os_str().to_owned();
    tmp.push(".tmp");
    let tmp = PathBuf::from(tmp);
    #[cfg(unix)]
    {
        use std::io::Write as _;
        use std::os::unix::fs::OpenOptionsExt as _;
        let _ = std::fs::remove_file(&tmp);
        let mut file = std::fs::OpenOptions::new()
            .write(true)
            .create_new(true)
            .mode(0o600)
            .open(&tmp)
            .map_err(|source| ImportError::Io {
                path: tmp.clone(),
                source,
            })?;
        file.write_all(bytes)
            .and_then(|()| file.sync_all())
            .map_err(|source| ImportError::Io {
                path: tmp.clone(),
                source,
            })?;
    }
    #[cfg(not(unix))]
    {
        std::fs::write(&tmp, bytes).map_err(|source| ImportError::Io {
            path: tmp.clone(),
            source,
        })?;
    }
    std::fs::rename(&tmp, path).map_err(|source| ImportError::Io {
        path: path.to_path_buf(),
        source,
    })?;
    #[cfg(unix)]
    if let Some(parent) = path.parent() {
        std::fs::File::open(parent)
            .and_then(|file| file.sync_all())
            .map_err(|source| ImportError::Io {
                path: parent.to_path_buf(),
                source,
            })?;
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use anyhow::Context;

    fn tmp_file(contents: &str) -> anyhow::Result<PathBuf> {
        use std::sync::atomic::{AtomicU64, Ordering};
        static COUNTER: AtomicU64 = AtomicU64::new(0);
        let id = COUNTER.fetch_add(1, Ordering::Relaxed);
        let dir = std::env::temp_dir().join(format!(
            "bitrouter-import-claude-{}-{id}",
            std::process::id()
        ));
        std::fs::create_dir_all(&dir).context("required fixture value")?;
        let path = dir.join(".credentials.json");
        std::fs::write(&path, contents).context("required fixture value")?;
        Ok(path)
    }

    #[test]
    fn parses_oauth_and_converts_expiry_ms_to_secs() -> anyhow::Result<()> {
        let path = tmp_file(
            r#"{"claudeAiOauth":{"accessToken":"sk-ant-oat-1","refreshToken":"r1","expiresAt":1700000000000}}"#,
        )?;
        let imported = from_file(&path)
            .context("required fixture value")?
            .context("required fixture value")?;
        assert_eq!(imported.token.access_token, "sk-ant-oat-1");
        assert_eq!(imported.token.refresh_token.as_deref(), Some("r1"));
        // 1_700_000_000_000 ms / 1000 = 1_700_000_000 s.
        assert_eq!(imported.token.expires_at, 1_700_000_000);
        assert!(matches!(imported.source, ImportSource::File(_)));
        Ok(())
    }

    #[test]
    fn missing_file_is_none() -> anyhow::Result<()> {
        let path = std::env::temp_dir().join("bitrouter-import-claude-absent/never.json");
        assert!(
            from_file(&path)
                .context("required fixture value")?
                .is_none()
        );
        Ok(())
    }

    #[test]
    fn no_oauth_block_is_none() -> anyhow::Result<()> {
        let path = tmp_file(r#"{"somethingElse":true}"#)?;
        assert!(
            from_file(&path)
                .context("required fixture value")?
                .is_none()
        );
        Ok(())
    }

    #[test]
    fn missing_access_token_errors() -> anyhow::Result<()> {
        let path = tmp_file(r#"{"claudeAiOauth":{"refreshToken":"r1"}}"#)?;
        let err = from_file(&path).err().context("expected fixture error")?;
        assert!(matches!(err, ImportError::MissingAccessToken { .. }));
        Ok(())
    }

    #[test]
    fn store_read_file_only_returns_token_and_file_source() -> anyhow::Result<()> {
        let path = tmp_file(
            r#"{"claudeAiOauth":{"accessToken":"a","refreshToken":"r","expiresAt":1700000000000}}"#,
        )?;
        let store = ClaudeCodeStore::file_only(&path);
        let live = store
            .read()
            .context("required fixture value")?
            .context("required fixture value")?;
        assert_eq!(live.token.access_token, "a");
        assert_eq!(live.token.refresh_token.as_deref(), Some("r"));
        assert_eq!(live.token.expires_at, 1_700_000_000);
        match &live.source {
            ClaudeCodeSource::File(p) => assert_eq!(p, &path),
            other => anyhow::bail!("expected File source, got {other:?}"),
        }
        Ok(())
    }

    #[test]
    fn store_read_file_only_absent_is_none() -> anyhow::Result<()> {
        let path = std::env::temp_dir().join("bitrouter-ccstore-absent/never.json");
        let store = ClaudeCodeStore::file_only(&path);
        assert!(store.read().context("required fixture value")?.is_none());
        Ok(())
    }

    #[test]
    fn write_back_preserves_unknown_keys_and_updates_token() -> anyhow::Result<()> {
        let path = tmp_file(
            r#"{"claudeAiOauth":{"accessToken":"old","refreshToken":"oldr","expiresAt":1700000000000,"scopes":["user:inference"],"subscriptionType":"max"},"otherTop":42}"#,
        )?;
        let store = ClaudeCodeStore::file_only(&path);
        store
            .write_back(
                &OAuthToken {
                    access_token: "new".into(),
                    expires_at: 1_700_000_500,
                    refresh_token: Some("newr".into()),
                },
                &ClaudeCodeSource::File(path.clone()),
            )
            .context("required fixture value")?;
        let raw: serde_json::Value =
            serde_json::from_slice(&std::fs::read(&path).context("required fixture value")?)
                .context("required fixture value")?;
        let o = &raw["claudeAiOauth"];
        assert_eq!(o["accessToken"], "new");
        assert_eq!(o["refreshToken"], "newr");
        // seconds → milliseconds on write-back.
        assert_eq!(o["expiresAt"], 1_700_000_500_000u64);
        // Unknown fields Claude Code wrote must survive the round-trip.
        assert_eq!(o["subscriptionType"], "max");
        assert_eq!(o["scopes"][0], "user:inference");
        assert_eq!(raw["otherTop"], 42);
        // And the rotated token reads back through the store.
        let live = store
            .read()
            .context("required fixture value")?
            .context("required fixture value")?;
        assert_eq!(live.token.access_token, "new");
        assert_eq!(live.token.refresh_token.as_deref(), Some("newr"));
        Ok(())
    }

    #[cfg(unix)]
    #[test]
    fn write_back_creates_fresh_file_0600() -> anyhow::Result<()> {
        use std::os::unix::fs::PermissionsExt;
        // Start from a unique path that does NOT yet exist (macOS Keychain-only
        // machines have no file until something writes one).
        let path = tmp_file("{}")?;
        std::fs::remove_file(&path).context("required fixture value")?;
        let store = ClaudeCodeStore::file_only(&path);
        store
            .write_back(
                &OAuthToken {
                    access_token: "a".into(),
                    expires_at: 1_700_000_000,
                    refresh_token: Some("r".into()),
                },
                &ClaudeCodeSource::File(path.clone()),
            )
            .context("required fixture value")?;
        let mode = std::fs::metadata(&path)
            .context("required fixture value")?
            .permissions()
            .mode()
            & 0o777;
        assert_eq!(mode, 0o600, "expected 0600, got {mode:o}");
        // A freshly-created file is still a valid, readable credential.
        let live = store
            .read()
            .context("required fixture value")?
            .context("required fixture value")?;
        assert_eq!(live.token.access_token, "a");
        Ok(())
    }
}