Files
hack/tools/scripts/llm-key-hunter/results/_cache/content/ayaka209_opencode-provider-manager.json

1 line
13 KiB
JSON

{"repo": "ayaka209/opencode-provider-manager", "files": {"crates/auth/src/status.rs": {"4474924f167a081adc47014782013f7ec5fb057d": {"t": 1785745837.9723916, "n": 6359, "h": "d1161ed6080d0628", "c": "//! API key status detection for providers.\n\nuse crate::parser::AuthEntry;\n\n/// The authentication status of a provider.\n#[derive(Debug, Clone, PartialEq, Eq)]\npub enum ProviderAuthStatus {\n /// API key is configured and has a recognized format.\n Configured {\n /// Whether the key format looks valid.\n format_valid: bool,\n },\n /// Auth is configured via environment variable reference.\n EnvVar {\n /// The environment variable name (e.g., \"ANTHROPIC_API_KEY\").\n var_name: String,\n },\n /// OAuth token is configured.\n OAuth,\n /// No authentication is configured for this provider.\n Missing,\n}\n\nimpl ProviderAuthStatus {\n /// Check the auth status for a given provider based on its auth entry\n /// and provider configuration.\n pub fn from_provider(provider_id: &str, auth_entry: Option<&AuthEntry>) -> Self {\n match auth_entry {\n Some(entry) => {\n match entry.auth_type.as_str() {\n \"api\" => {\n if let Some(key) = &entry.key {\n ProviderAuthStatus::Configured {\n format_valid: is_valid_key_format(provider_id, key),\n }\n } else {\n ProviderAuthStatus::Missing\n }\n }\n \"oauth\" => ProviderAuthStatus::OAuth,\n _other => {\n // Unknown auth type, but something is configured\n ProviderAuthStatus::Configured {\n format_valid: false,\n }\n }\n }\n }\n None => {\n // Check if there's a well-known env var for this provider\n if let Some(env_var) = provider_env_var(provider_id) {\n if std::env::var(env_var).is_ok() {\n ProviderAuthStatus::EnvVar {\n var_name: env_var.to_string(),\n }\n } else {\n ProviderAuthStatus::Missing\n }\n } else {\n ProviderAuthStatus::Missing\n }\n }\n }\n }\n\n /// Check auth status using env var fallback.\n pub fn from_env_var(provider_id: &str) -> Self {\n if let Some(env_var) = provider_env_var(provider_id) {\n if std::env::var(env_var).is_ok() {\n return ProviderAuthStatus::EnvVar {\n var_name: env_var.to_string(),\n };\n }\n }\n ProviderAuthStatus::Missing\n }\n}\n\n/// Check if an API key matches expected format patterns for a provider.\nfn is_valid_key_format(provider_id: &str, key: &str) -> bool {\n match provider_id {\n \"openai\" => key.starts_with(\"sk-\") && key.len() > 10,\n \"anthropic\" => key.starts_with(\"sk-ant-\") && key.len() > 10,\n \"google\" | \"gemini\" => key.len() > 10,\n \"deepseek\" => key.len() > 10,\n \"groq\" => key.starts_with(\"gsk_\") && key.len() > 10,\n \"openrouter\" => key.starts_with(\"sk-or-\") && key.len() > 10,\n \"xai\" => key.len() > 10,\n // For custom/unknown providers, just check it's not empty\n _ => !key.is_empty(),\n }\n}\n\n/// Get the environment variable name for a provider's API key.\npub fn provider_env_var(provider_id: &str) -> Option<&'static str> {\n match provider_id {\n \"openai\" => Some(\"OPENAI_API_KEY\"),\n \"anthropic\" => Some(\"ANTHROPIC_API_KEY\"),\n \"google\" | \"gemini\" => Some(\"GOOGLE_API_KEY\"),\n \"deepseek\" => Some(\"DEEPSEEK_API_KEY\"),\n \"groq\" => Some(\"GROQ_API_KEY\"),\n \"openrouter\" => Some(\"OPENROUTER_API_KEY\"),\n \"xai\" => Some(\"XAI_API_KEY\"),\n \"together\" | \"together-ai\" => Some(\"TOGETHER_API_KEY\"),\n \"fireworks\" | \"fireworks-ai\" => Some(\"FIREWORKS_API_KEY\"),\n \"cerebras\" => Some(\"CEREBRAS_API_KEY\"),\n \"mistral\" => Some(\"MISTRAL_API_KEY\"),\n \"perplexity\" => Some(\"PERPLEXITY_API_KEY\"),\n _ => None,\n }\n}\n\n#[cfg(test)]\nmod tests {\n use super::*;\n use crate::parser::AuthEntry;\n use std::collections::HashMap;\n\n #[test]\n fn test_configured_valid_key() {\n let entry = AuthEntry {\n auth_type: \"api\".to_string(),\n key: Some(\"sk-ant-api03-longkey\".to_string()),\n token: None,\n extra: HashMap::new(),\n };\n let status = ProviderAuthStatus::from_provider(\"anthropic\", Some(&entry));\n assert!(matches!(\n status,\n ProviderAuthStatus::Configured { format_valid: true }\n ));\n }\n\n #[test]\n fn test_configured_invalid_key_format() {\n let entry = AuthEntry {\n auth_type: \"api\".to_string(),\n key: Some(\"short\".to_string()),\n token: None,\n extra: HashMap::new(),\n };\n let status = ProviderAuthStatus::from_provider(\"anthropic\", Some(&entry));\n assert!(matches!(\n status,\n ProviderAuthStatus::Configured {\n format_valid: false\n }\n ));\n }\n\n #[test]\n fn test_oauth_status() {\n let entry = AuthEntry {\n auth_type: \"oauth\".to_string(),\n key: None,\n token: Some(\"gho_token\".to_string()),\n extra: HashMap::new(),\n };\n let status = ProviderAuthStatus::from_provider(\"github-copilot\", Some(&entry));\n assert_eq!(status, ProviderAuthStatus::OAuth);\n }\n\n #[test]\n fn test_missing_status() {\n let status = ProviderAuthStatus::from_provider(\"unknown-provider\", None);\n assert_eq!(status, ProviderAuthStatus::Missing);\n }\n\n #[test]\n fn test_key_format_openai() {\n assert!(is_valid_key_format(\"openai\", \"sk-proj-abc123def456\"));\n assert!(!is_valid_key_format(\"openai\", \"short\"));\n }\n\n #[test]\n fn test_key_format_anthropic() {\n assert!(is_valid_key_format(\"anthropic\", \"sk-ant-api03-longkey\"));\n assert!(!is_valid_key_format(\"anthropic\", \"sk-wrong-prefix\"));\n }\n\n #[test]\n fn test_key_format_groq() {\n assert!(is_valid_key_format(\"groq\", \"gsk_abc123longkey\"));\n assert!(!is_valid_key_format(\"groq\", \"short\"));\n }\n}\n"}, "78614314eedd767d5f635474b0fafdaad7860865": {"t": 1785757079.629562, "n": 6359, "h": "d1161ed6080d0628", "c": "//! API key status detection for providers.\n\nuse crate::parser::AuthEntry;\n\n/// The authentication status of a provider.\n#[derive(Debug, Clone, PartialEq, Eq)]\npub enum ProviderAuthStatus {\n /// API key is configured and has a recognized format.\n Configured {\n /// Whether the key format looks valid.\n format_valid: bool,\n },\n /// Auth is configured via environment variable reference.\n EnvVar {\n /// The environment variable name (e.g., \"ANTHROPIC_API_KEY\").\n var_name: String,\n },\n /// OAuth token is configured.\n OAuth,\n /// No authentication is configured for this provider.\n Missing,\n}\n\nimpl ProviderAuthStatus {\n /// Check the auth status for a given provider based on its auth entry\n /// and provider configuration.\n pub fn from_provider(provider_id: &str, auth_entry: Option<&AuthEntry>) -> Self {\n match auth_entry {\n Some(entry) => {\n match entry.auth_type.as_str() {\n \"api\" => {\n if let Some(key) = &entry.key {\n ProviderAuthStatus::Configured {\n format_valid: is_valid_key_format(provider_id, key),\n }\n } else {\n ProviderAuthStatus::Missing\n }\n }\n \"oauth\" => ProviderAuthStatus::OAuth,\n _other => {\n // Unknown auth type, but something is configured\n ProviderAuthStatus::Configured {\n format_valid: false,\n }\n }\n }\n }\n None => {\n // Check if there's a well-known env var for this provider\n if let Some(env_var) = provider_env_var(provider_id) {\n if std::env::var(env_var).is_ok() {\n ProviderAuthStatus::EnvVar {\n var_name: env_var.to_string(),\n }\n } else {\n ProviderAuthStatus::Missing\n }\n } else {\n ProviderAuthStatus::Missing\n }\n }\n }\n }\n\n /// Check auth status using env var fallback.\n pub fn from_env_var(provider_id: &str) -> Self {\n if let Some(env_var) = provider_env_var(provider_id) {\n if std::env::var(env_var).is_ok() {\n return ProviderAuthStatus::EnvVar {\n var_name: env_var.to_string(),\n };\n }\n }\n ProviderAuthStatus::Missing\n }\n}\n\n/// Check if an API key matches expected format patterns for a provider.\nfn is_valid_key_format(provider_id: &str, key: &str) -> bool {\n match provider_id {\n \"openai\" => key.starts_with(\"sk-\") && key.len() > 10,\n \"anthropic\" => key.starts_with(\"sk-ant-\") && key.len() > 10,\n \"google\" | \"gemini\" => key.len() > 10,\n \"deepseek\" => key.len() > 10,\n \"groq\" => key.starts_with(\"gsk_\") && key.len() > 10,\n \"openrouter\" => key.starts_with(\"sk-or-\") && key.len() > 10,\n \"xai\" => key.len() > 10,\n // For custom/unknown providers, just check it's not empty\n _ => !key.is_empty(),\n }\n}\n\n/// Get the environment variable name for a provider's API key.\npub fn provider_env_var(provider_id: &str) -> Option<&'static str> {\n match provider_id {\n \"openai\" => Some(\"OPENAI_API_KEY\"),\n \"anthropic\" => Some(\"ANTHROPIC_API_KEY\"),\n \"google\" | \"gemini\" => Some(\"GOOGLE_API_KEY\"),\n \"deepseek\" => Some(\"DEEPSEEK_API_KEY\"),\n \"groq\" => Some(\"GROQ_API_KEY\"),\n \"openrouter\" => Some(\"OPENROUTER_API_KEY\"),\n \"xai\" => Some(\"XAI_API_KEY\"),\n \"together\" | \"together-ai\" => Some(\"TOGETHER_API_KEY\"),\n \"fireworks\" | \"fireworks-ai\" => Some(\"FIREWORKS_API_KEY\"),\n \"cerebras\" => Some(\"CEREBRAS_API_KEY\"),\n \"mistral\" => Some(\"MISTRAL_API_KEY\"),\n \"perplexity\" => Some(\"PERPLEXITY_API_KEY\"),\n _ => None,\n }\n}\n\n#[cfg(test)]\nmod tests {\n use super::*;\n use crate::parser::AuthEntry;\n use std::collections::HashMap;\n\n #[test]\n fn test_configured_valid_key() {\n let entry = AuthEntry {\n auth_type: \"api\".to_string(),\n key: Some(\"sk-ant-api03-longkey\".to_string()),\n token: None,\n extra: HashMap::new(),\n };\n let status = ProviderAuthStatus::from_provider(\"anthropic\", Some(&entry));\n assert!(matches!(\n status,\n ProviderAuthStatus::Configured { format_valid: true }\n ));\n }\n\n #[test]\n fn test_configured_invalid_key_format() {\n let entry = AuthEntry {\n auth_type: \"api\".to_string(),\n key: Some(\"short\".to_string()),\n token: None,\n extra: HashMap::new(),\n };\n let status = ProviderAuthStatus::from_provider(\"anthropic\", Some(&entry));\n assert!(matches!(\n status,\n ProviderAuthStatus::Configured {\n format_valid: false\n }\n ));\n }\n\n #[test]\n fn test_oauth_status() {\n let entry = AuthEntry {\n auth_type: \"oauth\".to_string(),\n key: None,\n token: Some(\"gho_token\".to_string()),\n extra: HashMap::new(),\n };\n let status = ProviderAuthStatus::from_provider(\"github-copilot\", Some(&entry));\n assert_eq!(status, ProviderAuthStatus::OAuth);\n }\n\n #[test]\n fn test_missing_status() {\n let status = ProviderAuthStatus::from_provider(\"unknown-provider\", None);\n assert_eq!(status, ProviderAuthStatus::Missing);\n }\n\n #[test]\n fn test_key_format_openai() {\n assert!(is_valid_key_format(\"openai\", \"sk-proj-abc123def456\"));\n assert!(!is_valid_key_format(\"openai\", \"short\"));\n }\n\n #[test]\n fn test_key_format_anthropic() {\n assert!(is_valid_key_format(\"anthropic\", \"sk-ant-api03-longkey\"));\n assert!(!is_valid_key_format(\"anthropic\", \"sk-wrong-prefix\"));\n }\n\n #[test]\n fn test_key_format_groq() {\n assert!(is_valid_key_format(\"groq\", \"gsk_abc123longkey\"));\n assert!(!is_valid_key_format(\"groq\", \"short\"));\n }\n}\n"}}}}