vtcode_auth/credentials/mode.rs
1//! Storage backend selection for credentials.
2
3/// Preferred storage backend for credentials.
4///
5/// - `Keyring`: Use OS-specific secure storage (macOS Keychain, Windows Credential Manager,
6/// Linux Secret Service). This is the default as it's the most secure option.
7/// - `File`: Use an AES-256-GCM encrypted file whose key is stored in the OS keyring.
8/// - `Auto`: Try keyring first, then file storage; both require an accessible
9/// OS keyring key, so storage fails closed when the keyring is unavailable.
10#[derive(Debug, Copy, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
11#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
12#[serde(rename_all = "lowercase")]
13pub enum AuthCredentialsStoreMode {
14 /// Use OS-specific keyring service.
15 /// This is the most secure option as credentials are managed by the OS
16 /// and are not accessible to other users or applications.
17 Keyring,
18 /// Persist credentials in an encrypted file.
19 /// The AES-256-GCM key is stored separately in the OS keyring.
20 File,
21 /// Try keyring first, then encrypted-file storage using a key held by the
22 /// OS keyring.
23 Auto,
24}
25
26impl Default for AuthCredentialsStoreMode {
27 /// Platform-aware default:
28 ///
29 /// - **macOS**: `File` — AES-256-GCM encrypted file with its key held in
30 /// Keychain. Users can opt into storing credentials directly in Keychain
31 /// via `credential_storage_mode` in `vtcode.toml`.
32 ///
33 /// - **Linux / Windows / others**: `Auto` — try OS keyring (Secret Service
34 /// / Windows Credential Manager), then an encrypted file. Both modes
35 /// require an accessible OS keyring key and fail closed when unavailable.
36 fn default() -> Self {
37 #[cfg(target_os = "macos")]
38 {
39 Self::File
40 }
41 #[cfg(not(target_os = "macos"))]
42 {
43 Self::Auto
44 }
45 }
46}
47
48impl AuthCredentialsStoreMode {
49 /// Resolve `Auto` to the best available concrete backend.
50 ///
51 /// Follows the "parse, don't validate" pattern: the return type is
52 /// [`ResolvedStoreMode`], which has no `Auto` variant, so downstream
53 /// code matches exhaustively and never needs an `unreachable!` arm.
54 pub(crate) fn effective_mode(self) -> ResolvedStoreMode {
55 match self {
56 Self::Auto => {
57 if super::keyring::is_functional() {
58 ResolvedStoreMode::Keyring
59 } else {
60 tracing::debug!(
61 "Keyring not available; selecting encrypted-file storage, which still requires a keyring key"
62 );
63 ResolvedStoreMode::File
64 }
65 }
66 Self::Keyring => ResolvedStoreMode::Keyring,
67 Self::File => ResolvedStoreMode::File,
68 }
69 }
70}
71
72/// A concrete credential backend, with [`AuthCredentialsStoreMode::Auto`] resolved.
73///
74/// [`AuthCredentialsStoreMode::effective_mode`] parses the possibly-`Auto`
75/// configuration into this type once; every consumer then handles both
76/// backends exhaustively without a runtime `unreachable!` guard.
77#[derive(Debug, Copy, Clone, PartialEq, Eq)]
78pub(crate) enum ResolvedStoreMode {
79 /// Use the OS-specific keyring service.
80 Keyring,
81 /// Persist credentials in an encrypted file using a key held by the OS keyring.
82 File,
83}
84
85#[cfg(test)]
86mod tests {
87 use super::{AuthCredentialsStoreMode, ResolvedStoreMode};
88
89 #[test]
90 fn explicit_modes_pass_through_unchanged() {
91 assert_eq!(AuthCredentialsStoreMode::Keyring.effective_mode(), ResolvedStoreMode::Keyring);
92 assert_eq!(AuthCredentialsStoreMode::File.effective_mode(), ResolvedStoreMode::File);
93 }
94
95 #[test]
96 fn auto_resolves_to_a_concrete_backend() {
97 // `Auto` must never survive resolution: the match is exhaustive over
98 // the concrete variants, so there is no `Auto` case to assert against.
99 match AuthCredentialsStoreMode::Auto.effective_mode() {
100 ResolvedStoreMode::Keyring | ResolvedStoreMode::File => {}
101 }
102 }
103}