use super::*;
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum LegacyProtocolPolicy {
Allow,
Deny,
}
impl LegacyProtocolPolicy {
pub fn is_allowed(self) -> bool {
matches!(self, Self::Allow)
}
}
#[derive(Clone, Debug)]
pub struct AuthConfig {
pub state_dir: PathBuf,
pub max_temporary_keys: usize,
pub max_temporary_key_ttl: Duration,
pub legacy_protocol: LegacyProtocolPolicy,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct AuthFailure {
pub code: String,
pub message: String,
pub retryable: bool,
}
impl AuthFailure {
pub fn new(code: impl Into<String>, message: impl Into<String>, retryable: bool) -> Self {
Self {
code: code.into(),
message: message.into(),
retryable,
}
}
pub fn internal(message: impl Into<String>) -> Self {
Self::new(codes::AUTH_INTERNAL_ERROR, message, false)
}
pub(crate) fn key_expired() -> Self {
Self::new(
codes::TEMPORARY_KEY_EXPIRED,
"temporary key has expired",
false,
)
}
pub(crate) fn key_revoked() -> Self {
Self::new(
codes::TEMPORARY_KEY_REVOKED,
"temporary key was revoked",
false,
)
}
pub(crate) fn key_rotated() -> Self {
Self::new(
codes::TEMPORARY_KEY_ROTATED,
"temporary credential was invalidated by administrator root rotation or auth-state reset",
false,
)
}
pub(crate) fn key_material_mismatch() -> Self {
Self::new(
codes::TEMPORARY_KEY_INVALID,
"temporary credential does not match the active relay key material",
false,
)
}
pub(crate) fn key_lease_inactive() -> Self {
Self::new(
codes::TEMPORARY_KEY_INACTIVE,
"temporary key lease is no longer active",
true,
)
}
pub(crate) fn state_manager_stopped() -> Self {
Self::new(
codes::AUTH_STATE_UNAVAILABLE,
"authentication state manager is not running",
true,
)
}
}
impl fmt::Display for AuthFailure {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(formatter, "{}: {}", self.code, self.message)
}
}
impl std::error::Error for AuthFailure {}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct TemporaryKeyMetadata {
pub key_id: KeyId,
pub state: String,
pub issued_at: u64,
pub expires_at: u64,
pub label: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub namespace: Option<u64>,
}
impl TemporaryKeyMetadata {
pub fn effective_namespace(&self) -> u64 {
self.key_id.namespace(self.namespace)
}
}
#[derive(Clone, Serialize, Deserialize)]
pub struct IssuedTemporaryKey {
#[serde(flatten)]
pub metadata: TemporaryKeyMetadata,
pub credential: String,
}
impl fmt::Debug for IssuedTemporaryKey {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
let Self {
metadata,
credential,
} = self;
let credential = if credential.is_empty() {
""
} else {
"[redacted]"
};
formatter
.debug_struct("IssuedTemporaryKey")
.field("metadata", metadata)
.field("credential", &credential)
.finish()
}
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct KeyPage {
pub schema_version: u16,
pub items: Vec<TemporaryKeyMetadata>,
pub next_page: Option<u32>,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct AuthStatus {
pub schema_version: u16,
pub safe_mode: bool,
pub capacity: usize,
pub active_keys: usize,
pub expired_keys: usize,
pub revoked_keys: usize,
pub legacy_protocol: LegacyProtocolPolicy,
pub active_legacy_connections: u64,
pub last_legacy_connection_at: Option<u64>,
pub auth_successes: u64,
pub auth_failures: u64,
pub server_instance_id: String,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub capabilities: Vec<String>,
}