Skip to main content

vtcode_config/core/
auth.rs

1//! Authentication configuration for VT Code.
2//!
3//! This module provides configuration for OAuth-based authentication
4//! with LLM providers that support it.
5
6use serde::{Deserialize, Serialize};
7
8#[cfg(feature = "schema")]
9use schemars::JsonSchema;
10
11/// Authentication configuration for all providers.
12#[derive(Debug, Clone, Default, Serialize, Deserialize)]
13#[cfg_attr(feature = "schema", derive(JsonSchema))]
14pub struct AuthConfig {
15    /// OpenRouter OAuth configuration
16    #[serde(default)]
17    pub openrouter: OpenRouterAuthConfig,
18}
19
20/// OpenRouter-specific authentication configuration.
21#[derive(Debug, Clone, Serialize, Deserialize)]
22#[cfg_attr(feature = "schema", derive(JsonSchema))]
23#[serde(default)]
24pub struct OpenRouterAuthConfig {
25    /// Whether to use OAuth instead of API key for authentication.
26    /// When enabled, VT Code will prompt for OAuth login if no valid token exists.
27    pub use_oauth: bool,
28
29    /// Port for the local OAuth callback server.
30    /// The server listens on localhost for the OAuth redirect.
31    pub callback_port: u16,
32
33    /// Whether to automatically refresh tokens when they expire.
34    /// If false, the user will be prompted to re-authenticate.
35    pub auto_refresh: bool,
36
37    /// Timeout in seconds for the OAuth flow.
38    /// If the user doesn't complete authentication within this time, the flow is cancelled.
39    pub flow_timeout_secs: u64,
40}
41
42impl Default for OpenRouterAuthConfig {
43    fn default() -> Self {
44        Self {
45            use_oauth: false,
46            callback_port: 8484,
47            auto_refresh: true,
48            flow_timeout_secs: 300,
49        }
50    }
51}
52
53impl OpenRouterAuthConfig {
54    /// Check if OAuth is enabled and should be used.
55    pub fn should_use_oauth(&self) -> bool {
56        self.use_oauth
57    }
58
59    /// Get the callback URL for the OAuth flow.
60    pub fn callback_url(&self) -> String {
61        format!("http://localhost:{}/callback", self.callback_port)
62    }
63}
64
65#[cfg(test)]
66mod tests {
67    use super::*;
68
69    #[test]
70    fn test_default_config() {
71        let config = OpenRouterAuthConfig::default();
72        assert!(!config.use_oauth);
73        assert_eq!(config.callback_port, 8484);
74        assert!(config.auto_refresh);
75        assert_eq!(config.flow_timeout_secs, 300);
76    }
77
78    #[test]
79    fn test_callback_url() {
80        let config = OpenRouterAuthConfig {
81            callback_port: 9000,
82            ..Default::default()
83        };
84        assert_eq!(config.callback_url(), "http://localhost:9000/callback");
85    }
86}