1use super::*;
3
4#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
5#[serde(rename_all = "snake_case")]
6pub enum LegacyProtocolPolicy {
7 Allow,
8 Deny,
9}
10
11impl LegacyProtocolPolicy {
12 pub fn is_allowed(self) -> bool {
13 matches!(self, Self::Allow)
14 }
15}
16
17#[derive(Clone, Debug)]
18pub struct AuthConfig {
19 pub state_dir: PathBuf,
20 pub max_temporary_keys: usize,
21 pub max_temporary_key_ttl: Duration,
22 pub legacy_protocol: LegacyProtocolPolicy,
23}
24
25#[derive(Clone, Debug, Serialize, Deserialize)]
26pub struct AuthFailure {
27 pub code: String,
28 pub message: String,
29 pub retryable: bool,
30}
31
32impl AuthFailure {
33 pub fn new(code: impl Into<String>, message: impl Into<String>, retryable: bool) -> Self {
34 Self {
35 code: code.into(),
36 message: message.into(),
37 retryable,
38 }
39 }
40
41 pub fn internal(message: impl Into<String>) -> Self {
42 Self::new(codes::AUTH_INTERNAL_ERROR, message, false)
43 }
44
45 pub(crate) fn key_expired() -> Self {
48 Self::new(
49 codes::TEMPORARY_KEY_EXPIRED,
50 "temporary key has expired",
51 false,
52 )
53 }
54
55 pub(crate) fn key_revoked() -> Self {
56 Self::new(
57 codes::TEMPORARY_KEY_REVOKED,
58 "temporary key was revoked",
59 false,
60 )
61 }
62
63 pub(crate) fn key_rotated() -> Self {
64 Self::new(
65 codes::TEMPORARY_KEY_ROTATED,
66 "temporary credential was invalidated by administrator root rotation or auth-state reset",
67 false,
68 )
69 }
70
71 pub(crate) fn key_material_mismatch() -> Self {
72 Self::new(
73 codes::TEMPORARY_KEY_INVALID,
74 "temporary credential does not match the active relay key material",
75 false,
76 )
77 }
78
79 pub(crate) fn key_lease_inactive() -> Self {
83 Self::new(
84 codes::TEMPORARY_KEY_INACTIVE,
85 "temporary key lease is no longer active",
86 true,
87 )
88 }
89
90 pub(crate) fn state_manager_stopped() -> Self {
91 Self::new(
92 codes::AUTH_STATE_UNAVAILABLE,
93 "authentication state manager is not running",
94 true,
95 )
96 }
97}
98
99impl fmt::Display for AuthFailure {
100 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
101 write!(formatter, "{}: {}", self.code, self.message)
102 }
103}
104
105impl std::error::Error for AuthFailure {}
106
107#[derive(Clone, Debug, Serialize, Deserialize)]
108pub struct TemporaryKeyMetadata {
109 pub key_id: KeyId,
110 pub state: String,
111 pub issued_at: u64,
112 pub expires_at: u64,
113 pub label: Option<String>,
114 #[serde(default, skip_serializing_if = "Option::is_none")]
118 pub namespace: Option<u64>,
119}
120
121impl TemporaryKeyMetadata {
122 pub fn effective_namespace(&self) -> u64 {
124 self.key_id.namespace(self.namespace)
125 }
126}
127
128#[derive(Clone, Serialize, Deserialize)]
129pub struct IssuedTemporaryKey {
130 #[serde(flatten)]
131 pub metadata: TemporaryKeyMetadata,
132 pub credential: String,
135}
136
137impl fmt::Debug for IssuedTemporaryKey {
138 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
139 let Self {
140 metadata,
141 credential,
142 } = self;
143 let credential = if credential.is_empty() {
145 ""
146 } else {
147 "[redacted]"
148 };
149 formatter
150 .debug_struct("IssuedTemporaryKey")
151 .field("metadata", metadata)
152 .field("credential", &credential)
153 .finish()
154 }
155}
156
157#[derive(Clone, Debug, Serialize, Deserialize)]
158pub struct KeyPage {
159 pub schema_version: u16,
160 pub items: Vec<TemporaryKeyMetadata>,
161 pub next_page: Option<u32>,
162}
163
164#[derive(Clone, Debug, Serialize, Deserialize)]
165pub struct AuthStatus {
166 pub schema_version: u16,
167 pub safe_mode: bool,
168 pub capacity: usize,
169 pub active_keys: usize,
170 pub expired_keys: usize,
171 pub revoked_keys: usize,
172 pub legacy_protocol: LegacyProtocolPolicy,
173 pub active_legacy_connections: u64,
174 pub last_legacy_connection_at: Option<u64>,
175 pub auth_successes: u64,
176 pub auth_failures: u64,
177 pub server_instance_id: String,
178 #[serde(default, skip_serializing_if = "Vec::is_empty")]
181 pub capabilities: Vec<String>,
182}