link-assistant-router 0.84.0

Link.Assistant.Router — Claude MAX OAuth proxy and token gateway for Anthropic APIs
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
//! Claude MAX OAuth credential reader.
//!
//! Reads Claude Code session credentials from the filesystem to obtain
//! the OAuth bearer token for upstream API requests.
//!
//! The credential file is never written back to: when the access token has
//! expired, [`OAuthProvider::get_fresh_token`] exchanges the stored
//! `refreshToken` via [`crate::refresh`] and keeps the result in memory. That
//! is what lets a container whose `CLAUDE_CODE_HOME` is mounted read-only
//! survive token expiry without a Claude CLI inside the image.

use serde::Deserialize;
use std::path::{Path, PathBuf};
use std::sync::{Arc, RwLock};

use crate::subscription::SubscriptionToken;

/// Cached OAuth credentials.
#[derive(Clone)]
pub struct OAuthProvider {
    claude_code_home: PathBuf,
    cached_token: Arc<RwLock<Option<String>>>,
    /// Token supplied through [`OAuthProvider::set_token`] rather than read
    /// from disk. Kept separate from `cached_token` so a refresh-aware read
    /// can still prefer an explicitly configured token over the file.
    manual_token: Arc<RwLock<Option<String>>>,
}

/// Structure of the Claude Code OAuth credentials file.
///
/// Two on-disk layouts are supported:
///
/// * The flat layout used by older or hand-written credential files:
///   `{"accessToken": "..."}`.
/// * The nested layout written by the official Claude Code CLI into
///   `~/.claude/.credentials.json`:
///   `{"claudeAiOauth": {"accessToken": "...", "expiresAt": 1234, ...}}`.
///
/// Real Claude MAX sessions use the nested layout, so it must be read for the
/// router to work against an actual Claude Code login.
#[derive(Debug, Default, Deserialize)]
struct ClaudeCredentials {
    /// The OAuth access token (flat layout).
    #[serde(alias = "accessToken", alias = "access_token")]
    access_token: Option<String>,
    /// The OAuth bearer token, alternative field name (flat layout).
    #[serde(alias = "oauthToken", alias = "oauth_token")]
    oauth_token: Option<String>,
    /// The OAuth refresh token (flat layout).
    #[serde(alias = "refreshToken", alias = "refresh_token")]
    refresh_token: Option<String>,
    /// Expiration timestamp in milliseconds since the Unix epoch (flat layout).
    #[serde(alias = "expiresAt", alias = "expires_at", alias = "expiryDate")]
    expires_at: Option<i64>,
    /// Nested OAuth block written by the Claude Code CLI.
    #[serde(alias = "claudeAiOauth", alias = "claude_ai_oauth")]
    claude_ai_oauth: Option<OAuthBlock>,
}

/// Nested OAuth credentials block (`claudeAiOauth`) written by Claude Code.
#[derive(Debug, Default, Deserialize)]
struct OAuthBlock {
    /// The OAuth access token.
    #[serde(alias = "accessToken", alias = "access_token")]
    access_token: Option<String>,
    /// The OAuth bearer token (alternative field name).
    #[serde(alias = "oauthToken", alias = "oauth_token")]
    oauth_token: Option<String>,
    /// The OAuth refresh token used to obtain a new access token.
    #[serde(alias = "refreshToken", alias = "refresh_token")]
    refresh_token: Option<String>,
    /// Expiration timestamp in milliseconds since the Unix epoch, if present.
    #[serde(alias = "expiresAt", alias = "expires_at")]
    expires_at: Option<i64>,
}

impl ClaudeCredentials {
    /// Normalize into a [`SubscriptionToken`], preferring the nested Claude
    /// Code block.
    ///
    /// The access token, refresh token, and expiry are taken from whichever
    /// layout supplied the access token, so a stale flat `refreshToken` is
    /// never paired with a nested access token.
    fn into_subscription_token(self) -> Option<SubscriptionToken> {
        fn non_empty(value: Option<String>) -> Option<String> {
            value.filter(|v| !v.is_empty())
        }

        if let Some(block) = self.claude_ai_oauth
            && let Some(access) =
                non_empty(block.access_token).or_else(|| non_empty(block.oauth_token))
        {
            return Some(SubscriptionToken {
                access_token: access,
                refresh_token: non_empty(block.refresh_token),
                expires_at_ms: block.expires_at,
                account_id: None,
                resource_url: None,
            });
        }
        let access = non_empty(self.access_token).or_else(|| non_empty(self.oauth_token))?;
        Some(SubscriptionToken {
            access_token: access,
            refresh_token: non_empty(self.refresh_token),
            expires_at_ms: self.expires_at,
            account_id: None,
            resource_url: None,
        })
    }
}

impl OAuthProvider {
    /// Create a new OAuth provider pointing at the given Claude Code home directory.
    #[must_use]
    pub fn new(claude_code_home: &str) -> Self {
        Self {
            claude_code_home: PathBuf::from(claude_code_home),
            cached_token: Arc::new(RwLock::new(None)),
            manual_token: Arc::new(RwLock::new(None)),
        }
    }

    /// Candidate file paths where Claude Code may store OAuth credentials.
    fn credential_paths(&self) -> Vec<PathBuf> {
        let base = &self.claude_code_home;
        vec![
            base.join("credentials.json"),
            base.join(".credentials.json"),
            base.join("auth.json"),
            base.join("oauth.json"),
            base.join("config.json"),
        ]
    }

    /// Return the first existing credential file path, if any.
    ///
    /// Useful for diagnostics (e.g. the `doctor` command) so the report
    /// matches the files the provider would actually read, including the
    /// dotfile `.credentials.json` written by the Claude Code CLI.
    #[must_use]
    pub fn discover_credential_path(&self) -> Option<PathBuf> {
        self.credential_paths().into_iter().find(|p| p.exists())
    }

    /// Try to read the OAuth token from Claude Code session files.
    ///
    /// Searches through known credential file locations and extracts the
    /// access token.
    fn read_token_from_files(&self) -> Result<String, OAuthError> {
        Ok(self.read_subscription_token()?.access_token)
    }

    /// Read the full Claude credential — access token, `refreshToken`, and
    /// expiry — from the first credential file that yields one.
    ///
    /// Unlike [`Self::get_token`] this always hits the filesystem, so a
    /// credential file refreshed by an outside process (a Claude CLI on the
    /// host, a re-mounted secret) is picked up rather than served from cache.
    ///
    /// # Errors
    ///
    /// Returns [`OAuthError`] when no credential file exists, one cannot be
    /// read, or none contains an access token.
    pub fn read_subscription_token(&self) -> Result<SubscriptionToken, OAuthError> {
        for path in self.credential_paths() {
            if let Some(token) = Self::try_read_credential_file(&path)? {
                return Ok(token);
            }
        }
        Err(OAuthError::NoCredentials(format!(
            "No credential files found in {}",
            self.claude_code_home.display()
        )))
    }

    /// Try to read a single credential file and extract the token.
    fn try_read_credential_file(path: &Path) -> Result<Option<SubscriptionToken>, OAuthError> {
        if !path.exists() {
            return Ok(None);
        }

        let content = std::fs::read_to_string(path).map_err(|e| {
            OAuthError::ReadError(format!("Failed to read {}: {e}", path.display()))
        })?;

        let creds: ClaudeCredentials = serde_json::from_str(&content).map_err(|e| {
            OAuthError::ParseError(format!("Failed to parse {}: {e}", path.display()))
        })?;

        // Prefer the nested `claudeAiOauth` block (real Claude Code layout),
        // then fall back to the flat `accessToken`/`oauthToken` fields.
        if let Some(token) = creds.into_subscription_token() {
            if let Some(exp_ms) = token.expires_at_ms {
                let now_ms = chrono::Utc::now().timestamp_millis();
                if exp_ms <= now_ms {
                    if token.refresh_token.is_some() {
                        tracing::debug!(
                            "Claude Code OAuth token in {} expired at {exp_ms} (now {now_ms}); \
                             the router will exchange its refresh token in memory.",
                            path.display()
                        );
                    } else {
                        tracing::warn!(
                            "Claude Code OAuth token in {} expired at {exp_ms} (now {now_ms}) \
                             and stores no refresh token; upstream requests may fail until you \
                             re-authenticate with `claude`.",
                            path.display()
                        );
                    }
                }
            }
            return Ok(Some(token));
        }

        Ok(None)
    }

    /// Get the OAuth token, using cache if available.
    ///
    /// Falls back to reading from files if the cache is empty.
    pub fn get_token(&self) -> Result<String, OAuthError> {
        // Check cache first
        if let Ok(guard) = self.cached_token.read()
            && let Some(ref token) = *guard
        {
            return Ok(token.clone());
        }

        // Read from files
        let token = self.read_token_from_files()?;

        // Cache it
        if let Ok(mut guard) = self.cached_token.write() {
            *guard = Some(token.clone());
        }

        Ok(token)
    }

    /// Force refresh the cached token by re-reading from files.
    pub fn refresh_token(&self) -> Result<String, OAuthError> {
        // Clear cache
        if let Ok(mut guard) = self.cached_token.write() {
            *guard = None;
        }

        self.get_token()
    }

    /// Get a non-expired OAuth token, exchanging the stored `refreshToken`
    /// when the on-disk access token has expired.
    ///
    /// Resolution order:
    /// 1. A token set explicitly via [`Self::set_token`].
    /// 2. The credential file, re-read on every call so an externally
    ///    refreshed file wins over anything cached here.
    /// 3. `cache`, which refreshes via Anthropic's token endpoint and keeps
    ///    the result in memory — the credential file is never written to.
    ///
    /// This is what allows a container with no Claude CLI (and a read-only
    /// `CLAUDE_CODE_HOME` mount) to keep serving requests past token expiry.
    ///
    /// # Errors
    ///
    /// Returns [`OAuthError`] when no token can be read from disk and none was
    /// set manually. A failed *refresh* is not an error: the expired disk
    /// token is returned so the upstream can surface its own error.
    pub async fn get_fresh_token(
        &self,
        client: &reqwest::Client,
        cache: &crate::refresh::TokenCache,
    ) -> Result<String, OAuthError> {
        if let Ok(guard) = self.manual_token.read()
            && let Some(ref token) = *guard
        {
            return Ok(token.clone());
        }

        let disk_token = match self.read_subscription_token() {
            Ok(token) => token,
            // No readable credential file: fall back to whatever `get_token`
            // can produce (a previously cached read) and its error otherwise.
            Err(e) => return self.get_token().map_err(|_| e),
        };

        let now_ms = chrono::Utc::now().timestamp_millis();
        let fresh = cache
            .get_fresh(
                client,
                crate::subscription::SubscriptionProvider::Claude,
                disk_token,
                now_ms,
            )
            .await;
        Ok(fresh.access_token)
    }

    /// Manually set the OAuth token (useful for testing or direct configuration).
    pub fn set_token(&self, token: &str) {
        if let Ok(mut guard) = self.manual_token.write() {
            *guard = Some(token.to_string());
        }
        if let Ok(mut guard) = self.cached_token.write() {
            *guard = Some(token.to_string());
        }
    }
}

/// Errors related to OAuth credential operations.
#[derive(Debug)]
pub enum OAuthError {
    /// No credential files were found.
    NoCredentials(String),
    /// Could not read a credential file.
    ReadError(String),
    /// Could not parse a credential file.
    ParseError(String),
}

impl std::fmt::Display for OAuthError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::NoCredentials(msg) | Self::ReadError(msg) | Self::ParseError(msg) => {
                write!(f, "{msg}")
            }
        }
    }
}

impl std::error::Error for OAuthError {}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;

    #[test]
    fn test_no_credential_files() {
        let provider = OAuthProvider::new("/tmp/nonexistent-claude-dir-test");
        let result = provider.get_token();
        assert!(result.is_err());
    }

    #[test]
    fn test_read_credential_file() {
        let dir = tempdir();
        let cred_file = dir.join("credentials.json");
        fs::write(&cred_file, r#"{"accessToken": "test-oauth-token-123"}"#).unwrap();

        let provider = OAuthProvider::new(dir.to_str().unwrap());
        let token = provider.get_token().expect("should read token");
        assert_eq!(token, "test-oauth-token-123");
    }

    #[test]
    fn test_read_nested_claude_code_credentials() {
        // Real Claude Code writes ~/.claude/.credentials.json in this nested
        // shape. The router must read it, not just the flat layout.
        let dir = tempdir();
        let cred_file = dir.join(".credentials.json");
        fs::write(
            &cred_file,
            r#"{"claudeAiOauth":{"accessToken":"sk-ant-oat-nested","refreshToken":"sk-ant-ort-x","expiresAt":9999999999999,"scopes":["user:inference"],"subscriptionType":"max"}}"#,
        )
        .unwrap();

        let provider = OAuthProvider::new(dir.to_str().unwrap());
        let token = provider.get_token().expect("should read nested token");
        assert_eq!(token, "sk-ant-oat-nested");
    }

    #[test]
    fn test_nested_credentials_preferred_over_flat() {
        let dir = tempdir();
        // A file that has both a flat and a nested token prefers the nested one.
        fs::write(
            dir.join("credentials.json"),
            r#"{"accessToken":"flat","claudeAiOauth":{"accessToken":"nested"}}"#,
        )
        .unwrap();
        let provider = OAuthProvider::new(dir.to_str().unwrap());
        assert_eq!(provider.get_token().unwrap(), "nested");
    }

    #[test]
    fn test_expired_nested_token_still_returned() {
        // An expired token is still returned (the caller decides what to do),
        // but reading must not fail.
        let dir = tempdir();
        fs::write(
            dir.join(".credentials.json"),
            r#"{"claudeAiOauth":{"accessToken":"sk-ant-oat-expired","expiresAt":1}}"#,
        )
        .unwrap();
        let provider = OAuthProvider::new(dir.to_str().unwrap());
        assert_eq!(provider.get_token().unwrap(), "sk-ant-oat-expired");
    }

    #[test]
    fn test_reads_refresh_token_and_expiry_from_nested_block() {
        // The refresh token is what lets a container renew without the CLI,
        // so it must survive the read alongside the access token.
        let dir = tempdir();
        fs::write(
            dir.join(".credentials.json"),
            r#"{"claudeAiOauth":{"accessToken":"sk-ant-oat-x","refreshToken":"sk-ant-ort-y","expiresAt":1700000000000}}"#,
        )
        .unwrap();
        let token = OAuthProvider::new(dir.to_str().unwrap())
            .read_subscription_token()
            .expect("should read credential");
        assert_eq!(token.access_token, "sk-ant-oat-x");
        assert_eq!(token.refresh_token.as_deref(), Some("sk-ant-ort-y"));
        assert_eq!(token.expires_at_ms, Some(1_700_000_000_000));
    }

    #[test]
    fn test_flat_layout_refresh_token_is_read() {
        let dir = tempdir();
        fs::write(
            dir.join("credentials.json"),
            r#"{"accessToken":"flat-access","refreshToken":"flat-refresh","expiresAt":42}"#,
        )
        .unwrap();
        let token = OAuthProvider::new(dir.to_str().unwrap())
            .read_subscription_token()
            .unwrap();
        assert_eq!(token.refresh_token.as_deref(), Some("flat-refresh"));
        assert_eq!(token.expires_at_ms, Some(42));
    }

    #[test]
    fn test_nested_block_does_not_borrow_flat_refresh_token() {
        // Mixing a nested access token with a flat refresh token would send a
        // mismatched pair to the token endpoint.
        let dir = tempdir();
        fs::write(
            dir.join("credentials.json"),
            r#"{"accessToken":"flat","refreshToken":"flat-refresh","claudeAiOauth":{"accessToken":"nested"}}"#,
        )
        .unwrap();
        let token = OAuthProvider::new(dir.to_str().unwrap())
            .read_subscription_token()
            .unwrap();
        assert_eq!(token.access_token, "nested");
        assert_eq!(token.refresh_token, None);
    }

    #[tokio::test]
    async fn test_get_fresh_token_returns_unexpired_disk_token() {
        let dir = tempdir();
        fs::write(
            dir.join(".credentials.json"),
            r#"{"claudeAiOauth":{"accessToken":"sk-ant-oat-valid","refreshToken":"r","expiresAt":99999999999999}}"#,
        )
        .unwrap();
        let provider = OAuthProvider::new(dir.to_str().unwrap());
        let token = provider
            .get_fresh_token(&reqwest::Client::new(), &crate::refresh::TokenCache::new())
            .await
            .unwrap();
        assert_eq!(token, "sk-ant-oat-valid");
    }

    #[tokio::test]
    async fn test_get_fresh_token_prefers_manual_token() {
        // An explicitly configured token must win over the credential file and
        // must never trigger a network refresh.
        let dir = tempdir();
        fs::write(
            dir.join(".credentials.json"),
            r#"{"claudeAiOauth":{"accessToken":"from-disk","expiresAt":1}}"#,
        )
        .unwrap();
        let provider = OAuthProvider::new(dir.to_str().unwrap());
        provider.set_token("manual");
        let token = provider
            .get_fresh_token(&reqwest::Client::new(), &crate::refresh::TokenCache::new())
            .await
            .unwrap();
        assert_eq!(token, "manual");
    }

    #[tokio::test]
    async fn test_get_fresh_token_uses_cached_refresh_result() {
        // With a valid cached token the expired disk token is never sent
        // upstream and no refresh request is made.
        let dir = tempdir();
        fs::write(
            dir.join(".credentials.json"),
            r#"{"claudeAiOauth":{"accessToken":"expired","refreshToken":"r","expiresAt":1}}"#,
        )
        .unwrap();
        let cache = crate::refresh::TokenCache::new();
        cache.store_refreshed(
            crate::subscription::SubscriptionProvider::Claude,
            "primary",
            SubscriptionToken {
                access_token: "refreshed".into(),
                refresh_token: Some("r".into()),
                expires_at_ms: Some(i64::MAX),
                account_id: None,
                resource_url: None,
            },
        );
        let provider = OAuthProvider::new(dir.to_str().unwrap());
        let token = provider
            .get_fresh_token(&reqwest::Client::new(), &cache)
            .await
            .unwrap();
        assert_eq!(token, "refreshed");
    }

    #[tokio::test]
    async fn test_get_fresh_token_errors_without_credentials() {
        let provider = OAuthProvider::new("/tmp/nonexistent-claude-dir-fresh");
        let result = provider
            .get_fresh_token(&reqwest::Client::new(), &crate::refresh::TokenCache::new())
            .await;
        assert!(result.is_err());
    }

    #[test]
    fn test_set_token_manually() {
        let provider = OAuthProvider::new("/tmp/nonexistent");
        provider.set_token("manual-token");
        let token = provider.get_token().expect("should return manual token");
        assert_eq!(token, "manual-token");
    }

    #[test]
    fn test_cached_token_returned() {
        let provider = OAuthProvider::new("/tmp/nonexistent");
        provider.set_token("cached");
        let t1 = provider.get_token().unwrap();
        let t2 = provider.get_token().unwrap();
        assert_eq!(t1, t2);
        assert_eq!(t1, "cached");
    }

    fn tempdir() -> PathBuf {
        let dir = std::env::temp_dir().join(format!("router-test-{}", uuid::Uuid::new_v4()));
        fs::create_dir_all(&dir).unwrap();
        dir
    }
}