pub struct AuthStorage { /* private fields */ }Expand description
Main auth storage struct.
Provides multi-layered credential lookup with the following priority:
- Runtime override (CLI –api-key)
- Stored API key from auth.json
- OAuth token from auth.json (with auto-refresh awareness)
- Session token from auth.json
- Environment variable
- Fallback resolver (e.g., custom provider config from models.json)
Implementations§
Source§impl AuthStorage
impl AuthStorage
Sourcepub fn with_backend(backend: impl AuthStorageBackend + 'static) -> Self
pub fn with_backend(backend: impl AuthStorageBackend + 'static) -> Self
Create with explicit storage backend
Sourcepub fn default_path() -> Option<PathBuf>
pub fn default_path() -> Option<PathBuf>
Get the default auth file path
Sourcepub fn set_runtime_key(&self, provider: &str, api_key: String)
pub fn set_runtime_key(&self, provider: &str, api_key: String)
Set a runtime API key override (from CLI –api-key)
Sourcepub fn remove_runtime_key(&self, provider: &str)
pub fn remove_runtime_key(&self, provider: &str)
Remove a runtime override
Sourcepub fn set_fallback_resolver(&self, resolver: Arc<dyn FallbackResolver>)
pub fn set_fallback_resolver(&self, resolver: Arc<dyn FallbackResolver>)
Set a fallback resolver for API keys not found in auth.json or env vars. Used for custom provider keys from models.json.
Sourcepub fn clear_fallback_resolver(&self)
pub fn clear_fallback_resolver(&self)
Clear the fallback resolver
Sourcepub fn get_status(&self, provider: &str) -> AuthStatus
pub fn get_status(&self, provider: &str) -> AuthStatus
Get auth status for a provider (without exposing credentials)
Sourcepub fn get_api_key(&self, provider: &str) -> Option<String>
pub fn get_api_key(&self, provider: &str) -> Option<String>
Get API key for a provider.
Priority:
- Runtime override (CLI –api-key)
- Stored API key from auth.json
- OAuth token from auth.json (auto-refreshed)
- Session token from auth.json
- Fallback resolver
Sourcepub fn get_api_key_with_options(
&self,
provider: &str,
include_fallback: bool,
) -> Option<String>
pub fn get_api_key_with_options( &self, provider: &str, include_fallback: bool, ) -> Option<String>
Get API key with option to include/exclude fallback resolver
Sourcepub fn set_api_key(&self, provider: &str, key: String)
pub fn set_api_key(&self, provider: &str, key: String)
Set API key for a provider
Sourcepub fn set_oauth(
&self,
provider: &str,
access_token: String,
refresh_token: Option<String>,
expires_at: u64,
)
pub fn set_oauth( &self, provider: &str, access_token: String, refresh_token: Option<String>, expires_at: u64, )
Set OAuth credential for a provider
Sourcepub fn set_oauth_full(
&self,
provider: &str,
access_token: String,
refresh_token: Option<String>,
expires_at: u64,
scopes: Option<String>,
provider_data: Option<Value>,
)
pub fn set_oauth_full( &self, provider: &str, access_token: String, refresh_token: Option<String>, expires_at: u64, scopes: Option<String>, provider_data: Option<Value>, )
Set OAuth credential with full details
Sourcepub fn set_session(
&self,
provider: &str,
token: String,
expires_at: u64,
metadata: Option<Value>,
)
pub fn set_session( &self, provider: &str, token: String, expires_at: u64, metadata: Option<Value>, )
Set session token for a provider
Sourcepub fn update_oauth_tokens(
&self,
provider: &str,
new_access_token: String,
new_refresh_token: Option<String>,
new_expires_at: u64,
) -> AuthResult<()>
pub fn update_oauth_tokens( &self, provider: &str, new_access_token: String, new_refresh_token: Option<String>, new_expires_at: u64, ) -> AuthResult<()>
Update an existing OAuth credential (for token refresh)
Sourcepub fn get(&self, provider: &str) -> Option<AuthCredential>
pub fn get(&self, provider: &str) -> Option<AuthCredential>
Get credential for a provider
Sourcepub fn get_oauth_credential(&self, provider: &str) -> Option<AuthCredential>
pub fn get_oauth_credential(&self, provider: &str) -> Option<AuthCredential>
Get OAuth credential for a provider (for token refresh)
Sourcepub fn has_oauth_with_refresh(&self, provider: &str) -> bool
pub fn has_oauth_with_refresh(&self, provider: &str) -> bool
Check if a provider has OAuth credentials that can be refreshed
Sourcepub fn set(&self, provider: &str, credential: AuthCredential)
pub fn set(&self, provider: &str, credential: AuthCredential)
Set a credential for a provider
Sourcepub fn list_providers(&self) -> Vec<String>
pub fn list_providers(&self) -> Vec<String>
List all providers with credentials
Sourcepub fn get_all(&self) -> HashMap<String, AuthCredential>
pub fn get_all(&self) -> HashMap<String, AuthCredential>
Get all credentials
Sourcepub fn drain_errors(&self) -> Vec<AuthError>
pub fn drain_errors(&self) -> Vec<AuthError>
Drain collected errors
Sourcepub fn load_error(&self) -> Option<AuthError>
pub fn load_error(&self) -> Option<AuthError>
Get the last load error
Sourcepub fn validate_all(&self) -> Vec<(String, CredentialValidationError)>
pub fn validate_all(&self) -> Vec<(String, CredentialValidationError)>
Validate all stored credentials
Sourcepub fn validate(&self, provider: &str) -> Result<(), CredentialValidationError>
pub fn validate(&self, provider: &str) -> Result<(), CredentialValidationError>
Validate credential for a specific provider
Sourcepub fn configured_providers(&self) -> Vec<String>
pub fn configured_providers(&self) -> Vec<String>
Get all configured provider IDs (sorted)
Sourcepub fn has_multiple_providers(&self) -> bool
pub fn has_multiple_providers(&self) -> bool
Check if multiple providers are configured
Sourcepub fn primary_provider(&self) -> Option<String>
pub fn primary_provider(&self) -> Option<String>
Get the primary provider (first configured, preferring stored over env)
Sourcepub fn migrate_provider(&self, from: &str, to: &str) -> AuthResult<()>
pub fn migrate_provider(&self, from: &str, to: &str) -> AuthResult<()>
Migrate credentials from one provider to another