#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct Caller {
pub subject: String,
pub via: Via,
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum Via {
OAuth {
client_id: Option<String>,
},
Credential,
StaticToken,
}
pub const STATIC_SUBJECT: &str = "static";
impl Caller {
pub fn new(subject: impl Into<String>, via: Via) -> Self {
Caller {
subject: subject.into(),
via,
}
}
pub fn audit_label(&self) -> String {
match &self.via {
Via::OAuth {
client_id: Some(id),
} => format!("oauth:{id}"),
Via::OAuth { client_id: None } => "oauth".into(),
Via::Credential => "credential".into(),
Via::StaticToken => STATIC_SUBJECT.into(),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn audit_labels_name_the_way_in() {
let c = |via| Caller::new("owner", via).audit_label();
assert_eq!(
c(Via::OAuth {
client_id: Some("c1".into())
}),
"oauth:c1"
);
assert_eq!(c(Via::OAuth { client_id: None }), "oauth");
assert_eq!(c(Via::Credential), "credential");
assert_eq!(c(Via::StaticToken), "static");
}
}