use std::borrow::Cow;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CredentialProbe {
pub(crate) place: Cow<'static, str>,
pub fix: Option<Cow<'static, str>>,
}
impl CredentialProbe {
pub fn with_fix(
place: impl Into<Cow<'static, str>>,
fix: impl Into<Cow<'static, str>>,
) -> Self {
Self {
place: place.into(),
fix: Some(fix.into()),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum CredentialSource {
AuthModeNone,
KeylessRoute {
base_url: String,
},
CliOverride,
ProviderConfigApiKey {
table: String,
},
ProviderConfigEnv {
var: String,
},
AmbientEnv {
var: String,
},
SecretStore {
slot: String,
},
ExternalGrant {
cli: String,
path: String,
},
OAuth {
flow: String,
account: Option<String>,
},
UserGlobalConfig,
AccountSession,
ClaudeOrgLink,
SessionKey,
Missing {
probed: Vec<CredentialProbe>,
},
}
impl CredentialSource {
pub fn is_present(&self) -> bool {
!matches!(self, Self::Missing { .. })
}
pub fn label(&self) -> Cow<'static, str> {
match self {
Self::AuthModeNone => Cow::Borrowed("auth_mode = \"none\""),
Self::KeylessRoute { base_url } => Cow::Owned(format!(
"keyless route {}",
codewhale_config::structural_url_authority(base_url)
)),
Self::CliOverride => Cow::Borrowed("--api-key"),
Self::ProviderConfigApiKey { table } => Cow::Owned(format!("[{table}] api_key")),
Self::ProviderConfigEnv { var } => Cow::Owned(format!("api_key_env {var}")),
Self::AmbientEnv { var } => Cow::Owned(var.clone()),
Self::SecretStore { slot } => Cow::Owned(format!("secret store \"{slot}\"")),
Self::ExternalGrant { cli, path } => {
Cow::Owned(format!("{cli} credentials (read-only) {path}"))
}
Self::OAuth { flow, .. } => Cow::Owned(format!("{flow} OAuth")),
Self::AccountSession => Cow::Borrowed("Codewhale account"),
Self::ClaudeOrgLink => {
Cow::Borrowed(codewhale_config::credentials::CLAUDE_ORG_KEY_SOURCE)
}
Self::SessionKey => Cow::Borrowed("session key"),
Self::UserGlobalConfig => Cow::Borrowed("~/.codewhale/config.toml api_key"),
Self::Missing { .. } => Cow::Borrowed("not found"),
}
}
pub fn probed(&self) -> &[CredentialProbe] {
match self {
Self::Missing { probed } => probed,
_ => &[],
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CredentialResolution {
pub source: CredentialSource,
}
impl CredentialResolution {
pub fn found(source: CredentialSource) -> Self {
debug_assert!(source.is_present());
Self { source }
}
pub fn missing(probed: Vec<CredentialProbe>) -> Self {
Self {
source: CredentialSource::Missing { probed },
}
}
pub fn is_present(&self) -> bool {
self.source.is_present()
}
pub fn checked_places(&self) -> String {
self.source
.probed()
.iter()
.map(|probe| probe.place.as_ref())
.collect::<Vec<_>>()
.join(", ")
}
pub fn first_fix(&self) -> Option<&str> {
self.source
.probed()
.iter()
.find_map(|probe| probe.fix.as_deref())
}
}
#[cfg(test)]
mod private_label_tests {
use super::CredentialSource;
#[test]
fn keyless_label_omits_credentials_and_still_names_the_endpoint() {
for url in [
"https://user:label-s10-synthetic@gateway.invalid/v1",
"https://label-s10-synthetic@gateway.invalid/v1",
"https://gateway.invalid/key/label-s10-synthetic/v1",
"https://gateway.invalid/v1?token=label-s10-synthetic",
"https://gateway.invalid/v1#label-s10-synthetic",
] {
let label = CredentialSource::KeylessRoute {
base_url: url.into(),
}
.label()
.into_owned();
assert_eq!(label, "keyless route https://gateway.invalid");
}
let label = CredentialSource::KeylessRoute {
base_url: "http://localhost:11434/v1".into(),
}
.label()
.into_owned();
assert_eq!(label, "keyless route http://localhost:11434");
for url in ["not a url", "file:///etc/passwd", ""] {
let label = CredentialSource::KeylessRoute {
base_url: url.into(),
}
.label()
.into_owned();
assert!(label.contains("configured value omitted"));
assert!(!label.contains("passwd"));
}
}
}