oxi-ai 0.6.3

Unified LLM API — multi-provider streaming interface for AI coding assistants
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
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
//! OAuth authentication system for oxi-ai
//!
//! Supports PKCE-based OAuth flows for:
//! - Anthropic (authorization code + PKCE)
//! - OpenAI Codex (authorization code + PKCE)
//! - GitHub Copilot (device flow)
//!
//! Token persistence to `~/.oxi/auth.json` with secure file permissions.

use base64::engine::general_purpose::URL_SAFE_NO_PAD;
use base64::Engine;
use chrono::{DateTime, Utc};
use rand::RngCore;
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
use std::collections::HashMap;
use std::fs;
use std::io::{self, Write};
use std::path::PathBuf;

// ---------------------------------------------------------------------------
// Error types
// ---------------------------------------------------------------------------

/// Errors produced by the OAuth subsystem.
#[derive(Debug, thiserror::Error)]
pub enum OAuthError {
    #[error("IO error: {0}")]
/// io variant.
    Io(#[from] io::Error),

    #[error("HTTP request failed: {0}")]
/// http variant.
    Http(#[from] reqwest::Error),

    #[error("JSON error: {0}")]
/// json variant.
    Json(#[from] serde_json::Error),

    #[error("Token expired and no refresh_token available")]
/// no refresh token variant.
    NoRefreshToken,

    #[error("Token refresh failed: {0}")]
/// refresh failed variant.
    RefreshFailed(String),

    #[error("Device flow polling timed out after {0}s")]
/// device flow timeout variant.
    DeviceFlowTimeout(u64),

    #[error("Device flow authorization pending")]
/// device flow pending variant.
    DeviceFlowPending,

    #[error("Device flow rejected by user")]
/// device flow rejected variant.
    DeviceFlowRejected,

    #[error("Missing environment variable: {0}")]
/// missing env variant.
    MissingEnv(String),

    #[error("Invalid state: {0}")]
/// invalid state variant.
    InvalidState(String),
}

type Result<T> = std::result::Result<T, OAuthError>;

// ---------------------------------------------------------------------------
// Token data model
// ---------------------------------------------------------------------------

/// An OAuth token bundle as stored on disk.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TokenBundle {
    /// The bearer access token.
    pub access_token: String,
    /// Optional refresh token.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub refresh_token: Option<String>,
    /// Token type, e.g. "Bearer".
    #[serde(default = "default_token_type")]
    pub token_type: String,
    /// Time at which the token was obtained.
    pub obtained_at: DateTime<Utc>,
    /// Seconds until expiry (0 = unknown / never expires).
    #[serde(default)]
    pub expires_in: u64,
    /// Granted scopes (space-separated).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub scope: Option<String>,
}

fn default_token_type() -> String {
    "Bearer".to_string()
}

impl TokenBundle {
    /// Returns true when the token is expired (with a 60-second safety margin).
    pub fn is_expired(&self) -> bool {
        if self.expires_in == 0 {
            return false; // never expires / unknown
        }
        let expires_at = self.obtained_at + chrono::Duration::seconds(self.expires_in as i64);
        Utc::now() >= expires_at - chrono::Duration::seconds(60)
    }
}

/// The on-disk auth file structure: a map from provider name to token bundle.
#[derive(Debug, Default, Serialize, Deserialize)]
pub struct AuthStore {
    /// Map from provider name to token bundle.
    #[serde(flatten)]
    pub tokens: HashMap<String, TokenBundle>,
}

// ---------------------------------------------------------------------------
// File-based token persistence
// ---------------------------------------------------------------------------

/// Returns the default path for the auth store: `~/.oxi/auth.json`.
pub fn default_auth_path() -> Result<PathBuf> {
    let home = dirs_home()?;
    let dir = home.join(".oxi");
    if !dir.exists() {
        fs::create_dir_all(&dir)?;
    }
    Ok(dir.join("auth.json"))
}

fn dirs_home() -> Result<PathBuf> {
    // Try HOME env var (unix), then fallback.
    if let Ok(h) = std::env::var("HOME") {
        return Ok(PathBuf::from(h));
    }
    // On Windows try USERPROFILE.
    if let Ok(h) = std::env::var("USERPROFILE") {
        return Ok(PathBuf::from(h));
    }
    Err(OAuthError::InvalidState(
        "Cannot determine home directory".into(),
    ))
}

/// Load the auth store from disk.
pub fn load_auth_store() -> Result<AuthStore> {
    let path = default_auth_path()?;
    if !path.exists() {
        return Ok(AuthStore::default());
    }
    let data = fs::read_to_string(&path)?;
    let store: AuthStore = serde_json::from_str(&data)?;
    Ok(store)
}

/// Persist the auth store to disk with mode 0o600 where possible.
pub fn save_auth_store(store: &AuthStore) -> Result<()> {
    let path = default_auth_path()?;
    let json = serde_json::to_string_pretty(store)?;

    // Write atomically: temp file → rename.
    let tmp_path = path.with_extension("json.tmp");
    {
        let mut file = fs::File::create(&tmp_path)?;
        file.write_all(json.as_bytes())?;
        file.flush()?;
        // Set permissions on unix.
        #[cfg(unix)]
        {
            use std::os::unix::fs::PermissionsExt;
            let perms = fs::Permissions::from_mode(0o600);
            fs::set_permissions(&tmp_path, perms)?;
        }
    }
    fs::rename(&tmp_path, &path)?;
    Ok(())
}

/// Convenience: load a token for a provider key (e.g. "anthropic").
pub fn load_token(provider: &str) -> Result<Option<TokenBundle>> {
    let store = load_auth_store()?;
    Ok(store.tokens.get(provider).cloned())
}

/// Convenience: save a token for a provider key.
pub fn save_token(provider: &str, token: &TokenBundle) -> Result<()> {
    let mut store = load_auth_store()?;
    store.tokens.insert(provider.to_string(), token.clone());
    save_auth_store(&store)
}

/// Remove a stored token.
pub fn remove_token(provider: &str) -> Result<()> {
    let mut store = load_auth_store()?;
    store.tokens.remove(provider);
    save_auth_store(&store)
}

// ---------------------------------------------------------------------------
// PKCE helpers
// ---------------------------------------------------------------------------

/// Generate a cryptographically-random code_verifier (43 chars, RFC 7636 §4.1).
pub fn generate_code_verifier() -> String {
    let mut bytes = [0u8; 32]; // 32 bytes → 43 base64url chars
    rand::thread_rng().fill_bytes(&mut bytes);
    URL_SAFE_NO_PAD.encode(bytes)
}

/// Derive the code_challenge from a code_verifier using S256 (SHA-256 + base64url).
pub fn derive_code_challenge(verifier: &str) -> String {
    let mut hasher = Sha256::new();
    hasher.update(verifier.as_bytes());
    let hash = hasher.finalize();
    URL_SAFE_NO_PAD.encode(hash)
}

// ---------------------------------------------------------------------------
// Provider configuration
// ---------------------------------------------------------------------------

/// Configuration for a PKCE-based authorization-code OAuth provider.
#[derive(Debug, Clone)]
pub struct OAuthConfig {
    /// OAuth authorization endpoint URL.
    pub authorization_endpoint: String,
    /// OAuth token endpoint URL.
    pub token_endpoint: String,
    /// OAuth client identifier.
    pub client_id: String,
    /// OAuth redirect URI.
    pub redirect_uri: String,
    /// Space-separated scopes.
    pub scopes: String,
}

/// Anthropic OAuth configuration.
pub fn anthropic_config() -> Result<OAuthConfig> {
    let client_id = std::env::var("ANTHROPIC_OAUTH_CLIENT_ID")
        .map_err(|_| OAuthError::MissingEnv("ANTHROPIC_OAUTH_CLIENT_ID".into()))?;
    Ok(OAuthConfig {
        authorization_endpoint: "https://console.anthropic.com/api/oauth".into(),
        token_endpoint: "https://console.anthropic.com/api/oauth/token".into(),
        client_id,
        redirect_uri: "http://localhost:8787/callback".into(),
        scopes: "org.api.read org.api.write".into(),
    })
}

/// OpenAI Codex OAuth configuration.
pub fn openai_codex_config() -> Result<OAuthConfig> {
    let client_id = std::env::var("OPENAI_OAUTH_CLIENT_ID")
        .map_err(|_| OAuthError::MissingEnv("OPENAI_OAUTH_CLIENT_ID".into()))?;
    Ok(OAuthConfig {
        authorization_endpoint: "https://auth.openai.com/authorize".into(),
        token_endpoint: "https://auth.openai.com/oauth/token".into(),
        client_id,
        redirect_uri: "http://localhost:8787/callback".into(),
        scopes: "".into(),
    })
}

// ---------------------------------------------------------------------------
// Authorization URL builder (PKCE)
// ---------------------------------------------------------------------------

/// PKCE state produced when starting an authorization flow.
#[derive(Debug, Clone)]
pub struct PkceState {
    /// PKCE code verifier.
    pub code_verifier: String,
    /// PKCE code challenge (S256).
    pub code_challenge: String,
    /// Full authorization URL to redirect the user to.
    pub authorization_url: String,
    /// Opaque state parameter for CSRF protection.
    pub state: String,
}

/// Build a PKCE authorization URL for the given provider config.
pub fn build_authorization_url(config: &OAuthConfig) -> PkceState {
    let code_verifier = generate_code_verifier();
    let code_challenge = derive_code_challenge(&code_verifier);
    let state = generate_state_token();

    let mut url =
        url::Url::parse(&config.authorization_endpoint).expect("invalid authorization endpoint");
    url.query_pairs_mut()
        .append_pair("response_type", "code")
        .append_pair("client_id", &config.client_id)
        .append_pair("redirect_uri", &config.redirect_uri)
        .append_pair("code_challenge", &code_challenge)
        .append_pair("code_challenge_method", "S256")
        .append_pair("state", &state);

    if !config.scopes.is_empty() {
        url.query_pairs_mut().append_pair("scope", &config.scopes);
    }

    PkceState {
        code_verifier,
        code_challenge,
        authorization_url: url.to_string(),
        state,
    }
}

/// Generate an opaque state parameter (22 random base64url chars).
fn generate_state_token() -> String {
    let mut bytes = [0u8; 16];
    rand::thread_rng().fill_bytes(&mut bytes);
    URL_SAFE_NO_PAD.encode(bytes)
}

// ---------------------------------------------------------------------------
// Token exchange (authorization code → access token)
// ---------------------------------------------------------------------------

/// Exchange an authorization code for a token bundle.
pub async fn exchange_code(
    client: &reqwest::Client,
    config: &OAuthConfig,
    pkce: &PkceState,
    code: &str,
) -> Result<TokenBundle> {
    #[derive(Serialize)]
    struct TokenRequest {
        grant_type: String,
        code: String,
        redirect_uri: String,
        client_id: String,
        code_verifier: String,
    }

    #[derive(Deserialize)]
    struct TokenResponse {
        access_token: String,
        #[serde(default)]
        refresh_token: Option<String>,
        #[serde(default = "default_token_type")]
        token_type: String,
        #[serde(default)]
        expires_in: u64,
        #[serde(default)]
        scope: Option<String>,
    }

    let body = TokenRequest {
        grant_type: "authorization_code".into(),
        code: code.into(),
        redirect_uri: config.redirect_uri.clone(),
        client_id: config.client_id.clone(),
        code_verifier: pkce.code_verifier.clone(),
    };

    let resp = client
        .post(&config.token_endpoint)
        .header("content-type", "application/json")
        .header("accept", "application/json")
        .json(&body)
        .send()
        .await?;

    let status = resp.status();
    if !status.is_success() {
        let text = resp.text().await.unwrap_or_default();
        return Err(OAuthError::RefreshFailed(format!(
            "Token exchange failed ({status}): {text}"
        )));
    }

    let tr: TokenResponse = resp.json().await?;
    Ok(TokenBundle {
        access_token: tr.access_token,
        refresh_token: tr.refresh_token,
        token_type: tr.token_type,
        obtained_at: Utc::now(),
        expires_in: tr.expires_in,
        scope: tr.scope,
    })
}

// ---------------------------------------------------------------------------
// Token refresh
// ---------------------------------------------------------------------------

/// Attempt to refresh an expired token.
pub async fn refresh_token(
    client: &reqwest::Client,
    config: &OAuthConfig,
    bundle: &TokenBundle,
) -> Result<TokenBundle> {
    let refresh = bundle
        .refresh_token
        .as_ref()
        .ok_or(OAuthError::NoRefreshToken)?;

    #[derive(Serialize)]
    struct RefreshRequest {
        grant_type: String,
        refresh_token: String,
        client_id: String,
    }

    #[derive(Deserialize)]
    struct TokenResponse {
        access_token: String,
        #[serde(default)]
        refresh_token: Option<String>,
        #[serde(default = "default_token_type")]
        token_type: String,
        #[serde(default)]
        expires_in: u64,
        #[serde(default)]
        scope: Option<String>,
    }

    let body = RefreshRequest {
        grant_type: "refresh_token".into(),
        refresh_token: refresh.clone(),
        client_id: config.client_id.clone(),
    };

    let resp = client
        .post(&config.token_endpoint)
        .header("content-type", "application/json")
        .header("accept", "application/json")
        .json(&body)
        .send()
        .await?;

    let status = resp.status();
    if !status.is_success() {
        let text = resp.text().await.unwrap_or_default();
        return Err(OAuthError::RefreshFailed(format!(
            "Refresh failed ({status}): {text}"
        )));
    }

    let tr: TokenResponse = resp.json().await?;
    Ok(TokenBundle {
        access_token: tr.access_token,
        refresh_token: tr.refresh_token.or_else(|| Some(refresh.clone())),
        token_type: tr.token_type,
        obtained_at: Utc::now(),
        expires_in: tr.expires_in,
        scope: tr.scope,
    })
}

/// Ensure a valid token is available, refreshing if necessary.
/// Returns a token bundle (possibly refreshed).
pub async fn ensure_valid_token(
    client: &reqwest::Client,
    config: &OAuthConfig,
    provider_key: &str,
) -> Result<TokenBundle> {
    let bundle = load_token(provider_key)?.ok_or(OAuthError::InvalidState(format!(
        "No token stored for {provider_key}"
    )))?;

    if !bundle.is_expired() {
        return Ok(bundle);
    }

    let refreshed = refresh_token(client, config, &bundle).await?;
    save_token(provider_key, &refreshed)?;
    Ok(refreshed)
}

// ---------------------------------------------------------------------------
// GitHub device flow
// ---------------------------------------------------------------------------

/// Response from GitHub's device-code endpoint.
#[derive(Debug, Deserialize)]
pub struct DeviceCodeResponse {
    /// Device verification code.
    pub device_code: String,
    /// User-visible code to enter on the verification page.
    pub user_code: String,
    /// URL the user should visit to authorize.
    pub verification_uri: String,
    /// Optional complete verification URL (includes user code).
    #[serde(default)]
    pub verification_uri_complete: Option<String>,
    /// Polling interval in seconds.
    pub interval: u64,
    /// Seconds until the device code expires.
    pub expires_in: u64,
}

/// Result of the device-flow token polling.
#[derive(Debug)]
pub enum DeviceFlowResult {
/// success variant.
    Success(TokenBundle),
/// pending variant.
    Pending,
/// rejected variant.
    Rejected,
/// timeout variant.
    Timeout(u64),
}

/// Step 1: Request a device code from GitHub.
pub async fn github_request_device_code(
    client: &reqwest::Client,
    client_id: &str,
    scope: &str,
) -> Result<DeviceCodeResponse> {
    #[derive(Serialize)]
    struct Body {
        client_id: String,
        scope: String,
    }

    let resp = client
        .post("https://github.com/login/device/code")
        .header("accept", "application/json")
        .json(&Body {
            client_id: client_id.into(),
            scope: scope.into(),
        })
        .send()
        .await?;

    let status = resp.status();
    if !status.is_success() {
        let text = resp.text().await.unwrap_or_default();
        return Err(OAuthError::RefreshFailed(format!(
            "Device code request failed ({status}): {text}"
        )));
    }

    Ok(resp.json().await?)
}

/// Step 2: Poll GitHub for the access token.
///
/// `timeout_secs` caps total polling duration. Returns `DeviceFlowResult::Success`
/// once the user authorizes, `Pending` if still waiting, or `Rejected` on error.
pub async fn github_poll_for_token(
    client: &reqwest::Client,
    client_id: &str,
    device_code: &str,
    timeout_secs: u64,
) -> Result<DeviceFlowResult> {
    #[derive(Serialize)]
    struct Body {
        client_id: String,
        device_code: String,
        grant_type: String,
    }

    #[derive(Deserialize)]
    struct TokenResponse {
        #[serde(default)]
        access_token: Option<String>,
        #[serde(default)]
        error: Option<String>,
        #[serde(default)]
        token_type: Option<String>,
        #[serde(default)]
        scope: Option<String>,
    }

    let deadline = std::time::Instant::now() + std::time::Duration::from_secs(timeout_secs);

    loop {
        if std::time::Instant::now() > deadline {
            return Ok(DeviceFlowResult::Timeout(timeout_secs));
        }

        let resp = client
            .post("https://github.com/login/oauth/access_token")
            .header("accept", "application/json")
            .json(&Body {
                client_id: client_id.into(),
                device_code: device_code.into(),
                grant_type: "urn:ietf:params:oauth:grant-type:device_code".into(),
            })
            .send()
            .await?;

        let tr: TokenResponse = resp.json().await?;

        if let Some(token) = tr.access_token {
            return Ok(DeviceFlowResult::Success(TokenBundle {
                access_token: token,
                refresh_token: None,
                token_type: tr.token_type.unwrap_or_else(|| "Bearer".into()),
                obtained_at: Utc::now(),
                expires_in: 0, // GitHub tokens don't expire by default
                scope: tr.scope,
            }));
        }

        match tr.error.as_deref() {
            Some("authorization_pending") => {
                tokio::time::sleep(std::time::Duration::from_secs(5)).await;
                continue;
            }
            Some("slow_down") => {
                tokio::time::sleep(std::time::Duration::from_secs(10)).await;
                continue;
            }
            Some("expired_token") => return Ok(DeviceFlowResult::Rejected),
            Some("access_denied") => return Ok(DeviceFlowResult::Rejected),
            Some(other) => {
                return Err(OAuthError::RefreshFailed(format!(
                    "Device flow error: {other}"
                )));
            }
            None => {
                // No token and no error — keep polling briefly then bail.
                tokio::time::sleep(std::time::Duration::from_secs(5)).await;
                continue;
            }
        }
    }
}

/// High-level convenience: run the full GitHub device flow and persist the token.
pub async fn github_device_flow(
    client: &reqwest::Client,
    client_id: &str,
    scope: &str,
    timeout_secs: u64,
) -> Result<TokenBundle> {
    let dc = github_request_device_code(client, client_id, scope).await?;

    println!();
    println!("=== GitHub Device Authorization ===");
    println!("  1. Open: {}", dc.verification_uri);
    println!("  2. Enter code: {}", dc.user_code);
    if let Some(ref url) = dc.verification_uri_complete {
        println!("  Or visit: {url}");
    }
    println!();

    let result = github_poll_for_token(client, client_id, &dc.device_code, timeout_secs).await?;

    match result {
        DeviceFlowResult::Success(token) => {
            save_token("github", &token)?;
            println!("✓ GitHub authentication successful.");
            Ok(token)
        }
        DeviceFlowResult::Pending => Err(OAuthError::DeviceFlowPending),
        DeviceFlowResult::Rejected => Err(OAuthError::DeviceFlowRejected),
        DeviceFlowResult::Timeout(s) => Err(OAuthError::DeviceFlowTimeout(s)),
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use tempfile::TempDir;

    // ---- PKCE tests ----

    #[test]
    fn test_code_verifier_length() {
        let v = generate_code_verifier();
        assert!((43..=128).contains(&v.len()), "verifier length {}", v.len());
    }

    #[test]
    fn test_code_verifier_is_base64url() {
        let v = generate_code_verifier();
        // base64url chars: A-Z a-z 0-9 - _
        assert!(v
            .chars()
            .all(|c| c.is_ascii_alphanumeric() || c == '-' || c == '_'));
    }

    #[test]
    fn test_code_verifier_uniqueness() {
        let a = generate_code_verifier();
        let b = generate_code_verifier();
        assert_ne!(a, b, "two verifiers should differ");
    }

    #[test]
    fn test_code_challenge_deterministic() {
        let v = generate_code_verifier();
        let c1 = derive_code_challenge(&v);
        let c2 = derive_code_challenge(&v);
        assert_eq!(c1, c2);
    }

    #[test]
    fn test_code_challenge_differs_from_verifier() {
        let v = generate_code_verifier();
        let c = derive_code_challenge(&v);
        assert_ne!(v, c);
    }

    #[test]
    fn test_code_challenge_is_base64url() {
        let v = generate_code_verifier();
        let c = derive_code_challenge(&v);
        assert!(c
            .chars()
            .all(|ch| ch.is_ascii_alphanumeric() || ch == '-' || ch == '_'));
    }

    #[test]
    fn test_known_pkce_vector() {
        // RFC 7636 Appendix B reference vector
        let verifier = "dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk";
        let challenge = derive_code_challenge(verifier);
        assert_eq!(challenge, "E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM");
    }

    // ---- TokenBundle / AuthStore tests ----

    #[test]
    fn test_token_bundle_not_expired_when_no_expiry() {
        let bundle = TokenBundle {
            access_token: "abc".into(),
            refresh_token: None,
            token_type: "Bearer".into(),
            obtained_at: Utc::now(),
            expires_in: 0,
            scope: None,
        };
        assert!(!bundle.is_expired());
    }

    #[test]
    fn test_token_bundle_expired() {
        let bundle = TokenBundle {
            access_token: "abc".into(),
            refresh_token: None,
            token_type: "Bearer".into(),
            obtained_at: Utc::now() - chrono::Duration::seconds(3600),
            expires_in: 1800, // expired 30 min ago
            scope: None,
        };
        assert!(bundle.is_expired());
    }

    #[test]
    fn test_token_bundle_not_yet_expired() {
        let bundle = TokenBundle {
            access_token: "abc".into(),
            refresh_token: None,
            token_type: "Bearer".into(),
            obtained_at: Utc::now(),
            expires_in: 3600, // expires in 1 hour
            scope: None,
        };
        assert!(!bundle.is_expired());
    }

    // ---- Auth store round-trip tests ----

    fn setup_temp_store() -> TempDir {
        tempfile::tempdir().expect("tempdir")
    }

    fn with_temp_auth_store<F>(f: F)
    where
        F: FnOnce(&PathBuf),
    {
        let dir = setup_temp_store();
        let path = dir.path().join("auth.json");
        // Monkey-patch default_auth_path by using save/load directly with a known path.
        // For tests we directly exercise the serde round-trip.
        let mut store = AuthStore::default();
        store.tokens.insert(
            "test-provider".into(),
            TokenBundle {
                access_token: "tok_abc123".into(),
                refresh_token: Some("ref_xyz".into()),
                token_type: "Bearer".into(),
                obtained_at: Utc::now(),
                expires_in: 3600,
                scope: Some("read write".into()),
            },
        );
        let json = serde_json::to_string_pretty(&store).unwrap();
        fs::write(&path, &json).unwrap();

        f(&path);

        // Verify round-trip
        let loaded: AuthStore = serde_json::from_str(&fs::read_to_string(&path).unwrap()).unwrap();
        assert_eq!(loaded.tokens["test-provider"].access_token, "tok_abc123");
        assert_eq!(
            loaded.tokens["test-provider"].refresh_token.as_deref(),
            Some("ref_xyz")
        );
    }

    #[test]
    fn test_auth_store_round_trip() {
        with_temp_auth_store(|_| {});
    }

    #[test]
    fn test_auth_store_missing_file() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("nonexistent.json");
        assert!(!path.exists());
        // Loading from a missing file should yield an empty store via serde
        let result = fs::read_to_string(&path);
        assert!(result.is_err());
    }

    // ---- Authorization URL tests ----

    #[test]
    fn test_build_authorization_url_contains_pkce_params() {
        let config = OAuthConfig {
            authorization_endpoint: "https://example.com/authorize".into(),
            token_endpoint: "https://example.com/token".into(),
            client_id: "my-client".into(),
            redirect_uri: "http://localhost:8787/callback".into(),
            scopes: "read write".into(),
        };
        let pkce = build_authorization_url(&config);

        assert!(pkce.authorization_url.contains("code_challenge="));
        assert!(pkce
            .authorization_url
            .contains("code_challenge_method=S256"));
        assert!(pkce.authorization_url.contains("client_id=my-client"));
        assert!(pkce.authorization_url.contains("response_type=code"));
        assert!(pkce.authorization_url.contains("state="));
        assert!(pkce.authorization_url.contains("scope="));
        assert_eq!(pkce.code_verifier.len(), 43);
    }

    #[test]
    fn test_state_token_length() {
        let state = generate_state_token();
        assert!(state.len() >= 16, "state token should be at least 16 chars");
    }

    // ---- Serialization tests ----

    #[test]
    fn test_token_bundle_serialize_deserialize() {
        let bundle = TokenBundle {
            access_token: "at_123".into(),
            refresh_token: None,
            token_type: "Bearer".into(),
            obtained_at: "2025-01-01T00:00:00Z".parse().unwrap(),
            expires_in: 3600,
            scope: Some("org.api.read".into()),
        };
        let json = serde_json::to_string(&bundle).unwrap();
        let back: TokenBundle = serde_json::from_str(&json).unwrap();
        assert_eq!(back.access_token, "at_123");
        assert!(back.refresh_token.is_none());
        assert_eq!(back.expires_in, 3600);
    }

    #[test]
    fn test_auth_store_multiple_providers() {
        let mut store = AuthStore::default();
        for name in &["anthropic", "openai", "github"] {
            store.tokens.insert(
                (*name).into(),
                TokenBundle {
                    access_token: format!("tok_{name}"),
                    refresh_token: None,
                    token_type: "Bearer".into(),
                    obtained_at: Utc::now(),
                    expires_in: 0,
                    scope: None,
                },
            );
        }
        let json = serde_json::to_string(&store).unwrap();
        let back: AuthStore = serde_json::from_str(&json).unwrap();
        assert_eq!(back.tokens.len(), 3);
        assert_eq!(back.tokens["openai"].access_token, "tok_openai");
    }
}