keystate_core/model/
envelope.rs1use std::collections::BTreeMap;
5
6use serde::{Deserialize, Serialize};
7use serde_json::Value;
8
9use crate::model::entities::{
10 AuthFlow, Client, ClientScope, Credential, Group, IdentityProvider, ProtocolMapper, Realm,
11 Role, User,
12};
13use crate::model::kind::EntityKind;
14use crate::version::Version;
15
16#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)]
20pub struct BackendInfo {
21 #[serde(default)]
23 pub backend: String,
24 pub detected_version: Version,
26 #[serde(default)]
30 pub extra: BTreeMap<String, Value>,
31}
32
33#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
37pub struct ExtractScope {
38 pub realm: String,
40}
41
42impl ExtractScope {
43 pub fn new(realm: impl Into<String>) -> Self {
45 Self {
46 realm: realm.into(),
47 }
48 }
49}
50
51#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)]
62pub struct VolatileSection(
63 pub BTreeMap<EntityKind, BTreeMap<String, BTreeMap<String, Value>>>,
65);
66
67impl VolatileSection {
68 pub fn new() -> Self {
70 Self::default()
71 }
72
73 pub fn is_empty(&self) -> bool {
75 self.0.is_empty()
76 }
77
78 pub fn insert(
80 &mut self,
81 entity: EntityKind,
82 row_id: impl Into<String>,
83 path: impl Into<String>,
84 value: Value,
85 ) {
86 self.0
87 .entry(entity)
88 .or_default()
89 .entry(row_id.into())
90 .or_default()
91 .insert(path.into(), value);
92 }
93
94 pub fn get(&self, entity: EntityKind, row_id: &str, path: &str) -> Option<&Value> {
96 self.0
97 .get(&entity)
98 .and_then(|by_id| by_id.get(row_id))
99 .and_then(|by_path| by_path.get(path))
100 }
101
102 pub fn row_ids<'a>(&'a self, entity: &'a EntityKind) -> impl Iterator<Item = &'a String> {
104 self.0.get(entity).into_iter().flat_map(|m| m.keys())
105 }
106}
107
108#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)]
114pub struct CanonicalRealm {
115 #[serde(default)]
117 pub backend: BackendInfo,
118 pub realm: Realm,
120 #[serde(default)]
122 pub clients: Vec<Client>,
123 #[serde(default)]
125 pub client_scopes: Vec<ClientScope>,
126 #[serde(default)]
128 pub groups: Vec<Group>,
129 #[serde(default)]
131 pub roles: Vec<Role>,
132 #[serde(default)]
134 pub users: Vec<User>,
135 #[serde(default)]
137 pub credentials: Vec<Credential>,
138 #[serde(default)]
140 pub protocol_mappers: Vec<ProtocolMapper>,
141 #[serde(default)]
143 pub auth_flows: Vec<AuthFlow>,
144 #[serde(default)]
146 pub identity_providers: Vec<IdentityProvider>,
147 #[serde(default)]
149 pub volatile: VolatileSection,
150}
151
152#[cfg(test)]
153mod tests {
154 use super::*;
155
156 #[test]
157 fn envelope_has_default_empty_state() {
158 let realm = CanonicalRealm::default();
159 assert!(realm.clients.is_empty());
160 assert!(realm.volatile.is_empty());
161 }
162
163 #[test]
164 fn volatile_section_roundtrips_and_is_ordered() {
165 let mut section = VolatileSection::new();
166 section.insert(
167 EntityKind::User,
168 "u1",
169 "native.last_login",
170 serde_json::json!("2026-01-01"),
171 );
172 section.insert(
173 EntityKind::User,
174 "u2",
175 "native.last_login",
176 serde_json::json!("2026-01-02"),
177 );
178 assert_eq!(
179 section
180 .get(EntityKind::User, "u1", "native.last_login")
181 .unwrap(),
182 &serde_json::json!("2026-01-01")
183 );
184 assert_eq!(
185 section.row_ids(&EntityKind::User).collect::<Vec<_>>(),
186 vec![&"u1".to_string(), &"u2".to_string()]
187 );
188
189 let json = serde_json::to_value(§ion).unwrap();
190 assert!(json.is_object());
191 let back: VolatileSection = serde_json::from_value(json).unwrap();
192 assert_eq!(back, section);
193 }
194
195 #[test]
196 fn backend_info_defaults_to_unknown_backend() {
197 let info = BackendInfo::default();
198 assert_eq!(info.backend, "");
199 assert_eq!(info.detected_version, Version::default());
200 }
201}