1use crate::{prelude::*, value::CredentialType};
2use kanidm_lib_crypto::{PW_MAX_LENGTH_NIST, PW_MFA_MIN_LENGTH, PW_SFA_MIN_LENGTH_NIST};
3use webauthn_rs::prelude::AttestationCaList;
4
5#[derive(Clone)]
6#[cfg_attr(test, derive(Default))]
7pub(crate) struct AccountPolicy {
8 privilege_expiry: u32,
9 authsession_expiry: u32,
10 pw_min_length: u32,
11 credential_policy: CredentialType,
12 webauthn_att_ca_list: Option<AttestationCaList>,
13 limit_search_max_filter_test: Option<u64>,
14 limit_search_max_results: Option<u64>,
15 allow_primary_cred_fallback: Option<bool>,
16}
17
18impl From<&EntrySealedCommitted> for Option<AccountPolicy> {
19 fn from(val: &EntrySealedCommitted) -> Self {
20 if !val.attribute_equality(
21 Attribute::Class,
22 &EntryClass::AccountPolicy.to_partialvalue(),
23 ) {
24 return None;
25 }
26
27 let authsession_expiry = val
28 .get_ava_single_uint32(Attribute::AuthSessionExpiry)
29 .unwrap_or(MAXIMUM_AUTH_SESSION_EXPIRY);
30
31 let privilege_expiry = val
32 .get_ava_single_uint32(Attribute::PrivilegeExpiry)
33 .unwrap_or(MAXIMUM_AUTH_PRIVILEGE_EXPIRY);
34
35 let pw_min_length = val
36 .get_ava_single_uint32(Attribute::AuthPasswordMinimumLength)
37 .unwrap_or(PW_MFA_MIN_LENGTH);
38
39 let credential_policy = val
40 .get_ava_single_credential_type(Attribute::CredentialTypeMinimum)
41 .unwrap_or(CredentialType::Any);
42
43 let webauthn_att_ca_list = val
44 .get_ava_webauthn_attestation_ca_list(Attribute::WebauthnAttestationCaList)
45 .cloned();
46
47 let limit_search_max_results = val
48 .get_ava_single_uint32(Attribute::LimitSearchMaxResults)
49 .map(|u| u as u64);
50
51 let limit_search_max_filter_test = val
52 .get_ava_single_uint32(Attribute::LimitSearchMaxFilterTest)
53 .map(|u| u as u64);
54
55 let allow_primary_cred_fallback =
56 val.get_ava_single_bool(Attribute::AllowPrimaryCredFallback);
57
58 Some(AccountPolicy {
59 privilege_expiry,
60 authsession_expiry,
61 pw_min_length,
62 credential_policy,
63 webauthn_att_ca_list,
64 limit_search_max_filter_test,
65 limit_search_max_results,
66 allow_primary_cred_fallback,
67 })
68 }
69}
70
71#[derive(Clone, Debug)]
72#[cfg_attr(test, derive(Default))]
73pub(crate) struct ResolvedAccountPolicy {
74 privilege_expiry: u32,
75 authsession_expiry: u32,
76 pw_min_length: u32,
77 pw_max_length: u32,
78 credential_policy: CredentialType,
79 webauthn_att_ca_list: Option<AttestationCaList>,
80 limit_search_max_filter_test: Option<u64>,
81 limit_search_max_results: Option<u64>,
82 allow_primary_cred_fallback: Option<bool>,
83}
84
85impl ResolvedAccountPolicy {
86 #[cfg(test)]
87 pub(crate) fn test_policy() -> Self {
88 ResolvedAccountPolicy {
89 privilege_expiry: DEFAULT_AUTH_PRIVILEGE_EXPIRY,
90 authsession_expiry: DEFAULT_AUTH_SESSION_EXPIRY,
91 pw_min_length: PW_MFA_MIN_LENGTH,
92 pw_max_length: PW_MAX_LENGTH_NIST,
93 credential_policy: CredentialType::Any,
94 webauthn_att_ca_list: None,
95 limit_search_max_filter_test: Some(DEFAULT_LIMIT_SEARCH_MAX_FILTER_TEST),
96 limit_search_max_results: Some(DEFAULT_LIMIT_SEARCH_MAX_RESULTS),
97 allow_primary_cred_fallback: None,
98 }
99 }
100
101 pub(crate) fn fold_from<I>(iter: I) -> Self
102 where
103 I: Iterator<Item = AccountPolicy>,
104 {
105 let mut accumulate = ResolvedAccountPolicy {
107 privilege_expiry: MAXIMUM_AUTH_PRIVILEGE_EXPIRY,
108 authsession_expiry: MAXIMUM_AUTH_SESSION_EXPIRY,
109 pw_min_length: PW_MFA_MIN_LENGTH,
110 pw_max_length: PW_MAX_LENGTH_NIST,
111 credential_policy: CredentialType::Any,
112 webauthn_att_ca_list: None,
113 limit_search_max_filter_test: None,
114 limit_search_max_results: None,
115 allow_primary_cred_fallback: None,
116 };
117
118 iter.for_each(|acc_pol| {
119 if acc_pol.privilege_expiry < accumulate.privilege_expiry {
121 accumulate.privilege_expiry = acc_pol.privilege_expiry
122 }
123
124 if acc_pol.authsession_expiry < accumulate.authsession_expiry {
126 accumulate.authsession_expiry = acc_pol.authsession_expiry
127 }
128
129 if acc_pol.pw_min_length > accumulate.pw_min_length {
131 accumulate.pw_min_length = acc_pol.pw_min_length
132 }
133
134 if acc_pol.credential_policy > accumulate.credential_policy {
136 accumulate.credential_policy = acc_pol.credential_policy
137 }
138
139 if let Some(pol_lim) = acc_pol.limit_search_max_results {
140 if let Some(acc_lim) = accumulate.limit_search_max_results {
141 if pol_lim > acc_lim {
142 accumulate.limit_search_max_results = Some(pol_lim);
143 }
144 } else {
145 accumulate.limit_search_max_results = Some(pol_lim);
146 }
147 }
148
149 if let Some(pol_lim) = acc_pol.limit_search_max_filter_test {
150 if let Some(acc_lim) = accumulate.limit_search_max_filter_test {
151 if pol_lim > acc_lim {
152 accumulate.limit_search_max_filter_test = Some(pol_lim);
153 }
154 } else {
155 accumulate.limit_search_max_filter_test = Some(pol_lim);
156 }
157 }
158
159 if let Some(acc_pol_w_att_ca) = acc_pol.webauthn_att_ca_list {
160 if let Some(res_w_att_ca) = accumulate.webauthn_att_ca_list.as_mut() {
161 res_w_att_ca.intersection(&acc_pol_w_att_ca);
162 } else {
163 accumulate.webauthn_att_ca_list = Some(acc_pol_w_att_ca);
164 }
165 }
166
167 if let Some(allow_primary_cred_fallback) = acc_pol.allow_primary_cred_fallback {
168 accumulate.allow_primary_cred_fallback =
169 match accumulate.allow_primary_cred_fallback {
170 Some(acc_fallback) => Some(allow_primary_cred_fallback && acc_fallback),
171 None => Some(allow_primary_cred_fallback),
172 };
173 }
174 });
175
176 if accumulate.credential_policy < CredentialType::Mfa
179 && accumulate.pw_min_length < PW_SFA_MIN_LENGTH_NIST
180 {
181 accumulate.pw_min_length = PW_SFA_MIN_LENGTH_NIST;
182 }
183
184 accumulate
185 }
186
187 pub(crate) fn privilege_expiry(&self) -> u32 {
188 self.privilege_expiry
189 }
190
191 pub(crate) fn authsession_expiry(&self) -> u32 {
192 self.authsession_expiry
193 }
194
195 pub(crate) fn pw_min_length(&self) -> u32 {
196 self.pw_min_length
197 }
198
199 pub(crate) fn pw_max_length(&self) -> u32 {
200 self.pw_max_length
201 }
202
203 pub(crate) fn credential_policy(&self) -> CredentialType {
204 self.credential_policy
205 }
206
207 pub(crate) fn webauthn_attestation_ca_list(&self) -> Option<&AttestationCaList> {
208 self.webauthn_att_ca_list.as_ref()
209 }
210
211 pub(crate) fn limit_search_max_results(&self) -> Option<u64> {
212 self.limit_search_max_results
213 }
214
215 pub(crate) fn limit_search_max_filter_test(&self) -> Option<u64> {
216 self.limit_search_max_filter_test
217 }
218
219 pub(crate) fn allow_primary_cred_fallback(&self) -> Option<bool> {
220 self.allow_primary_cred_fallback
221 }
222}
223
224#[cfg(test)]
225mod tests {
226 use super::{AccountPolicy, CredentialType, ResolvedAccountPolicy};
227 use crate::prelude::*;
228 use webauthn_rs_core::proto::AttestationCaListBuilder;
229
230 #[test]
231 fn test_idm_account_policy_resolve() {
232 sketching::test_init();
233
234 let ca_root_a: &[u8] = b"-----BEGIN CERTIFICATE-----
236MIIDHjCCAgagAwIBAgIEG0BT9zANBgkqhkiG9w0BAQsFADAuMSwwKgYDVQQDEyNZ
237dWJpY28gVTJGIFJvb3QgQ0EgU2VyaWFsIDQ1NzIwMDYzMTAgFw0xNDA4MDEwMDAw
238MDBaGA8yMDUwMDkwNDAwMDAwMFowLjEsMCoGA1UEAxMjWXViaWNvIFUyRiBSb290
239IENBIFNlcmlhbCA0NTcyMDA2MzEwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEK
240AoIBAQC/jwYuhBVlqaiYWEMsrWFisgJ+PtM91eSrpI4TK7U53mwCIawSDHy8vUmk
2415N2KAj9abvT9NP5SMS1hQi3usxoYGonXQgfO6ZXyUA9a+KAkqdFnBnlyugSeCOep
2428EdZFfsaRFtMjkwz5Gcz2Py4vIYvCdMHPtwaz0bVuzneueIEz6TnQ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iA2IABCJCQ2pTVhzjl4Wo6IhHtMSAzO2cv+H9DQKev3//fG59G11k
263xu9eI0/7o6V5uShBpe1u6l6mS19S1FEh6yGljnZAJ+2GNP1mi/YK2kSXIuTHjxA/
264pcoRf7XkOtO4o1qlcaNCMEAwDwYDVR0TAQH/BAUwAwEB/zAdBgNVHQ4EFgQUJtdk
2652cV4wlpn0afeaxLQG2PxxtcwDgYDVR0PAQH/BAQDAgEGMAoGCCqGSM49BAMDA2cA
266MGQCMFrZ+9DsJ1PW9hfNdBywZDsWDbWFp28it1d/5w2RPkRX3Bbn/UbDTNLx7Jr3
267jAGGiQIwHFj+dJZYUJR786osByBelJYsVZd2GbHQu209b5RCmGQ21gpSAk9QZW4B
2681bWeT0vT
269-----END CERTIFICATE-----";
270
271 let aaguid_a = Uuid::new_v4();
272 let aaguid_b = Uuid::new_v4();
273 let aaguid_c = Uuid::new_v4();
274 let aaguid_d = Uuid::new_v4();
275 let aaguid_e = Uuid::new_v4();
276
277 let mut att_ca_builder = AttestationCaListBuilder::new();
278
279 att_ca_builder
280 .insert_device_pem(ca_root_a, aaguid_a, "A".to_string(), Default::default())
281 .unwrap();
282 att_ca_builder
283 .insert_device_pem(ca_root_a, aaguid_b, "B".to_string(), Default::default())
284 .unwrap();
285 att_ca_builder
286 .insert_device_pem(ca_root_a, aaguid_c, "C".to_string(), Default::default())
287 .unwrap();
288 att_ca_builder
289 .insert_device_pem(ca_root_b, aaguid_d, "D".to_string(), Default::default())
290 .unwrap();
291
292 let att_ca_list_a = att_ca_builder.build();
293
294 let policy_a = AccountPolicy {
295 privilege_expiry: 100,
296 authsession_expiry: 100,
297 pw_min_length: 11,
298 credential_policy: CredentialType::Mfa,
299 webauthn_att_ca_list: Some(att_ca_list_a),
300 limit_search_max_filter_test: Some(10),
301 limit_search_max_results: Some(10),
302 allow_primary_cred_fallback: None,
303 };
304
305 let mut att_ca_builder = AttestationCaListBuilder::new();
306
307 att_ca_builder
308 .insert_device_pem(ca_root_a, aaguid_b, "B".to_string(), Default::default())
309 .unwrap();
310 att_ca_builder
311 .insert_device_pem(ca_root_b, aaguid_e, "E".to_string(), Default::default())
312 .unwrap();
313
314 let att_ca_list_b = att_ca_builder.build();
315
316 let policy_b = AccountPolicy {
317 privilege_expiry: 150,
318 authsession_expiry: 50,
319 pw_min_length: 15,
320 credential_policy: CredentialType::Passkey,
321 webauthn_att_ca_list: Some(att_ca_list_b),
322 limit_search_max_filter_test: Some(5),
323 limit_search_max_results: Some(15),
324 allow_primary_cred_fallback: Some(false),
325 };
326
327 let rap = ResolvedAccountPolicy::fold_from([policy_a, policy_b].into_iter());
328
329 assert_eq!(rap.privilege_expiry(), 100);
330 assert_eq!(rap.authsession_expiry(), 50);
331 assert_eq!(rap.pw_min_length(), 15);
332 assert_eq!(rap.credential_policy, CredentialType::Passkey);
333 assert_eq!(rap.limit_search_max_results(), Some(15));
334 assert_eq!(rap.limit_search_max_filter_test(), Some(10));
335 assert_eq!(rap.allow_primary_cred_fallback(), Some(false));
336
337 let mut att_ca_builder = AttestationCaListBuilder::new();
338
339 att_ca_builder
340 .insert_device_pem(ca_root_a, aaguid_b, "B".to_string(), Default::default())
341 .unwrap();
342
343 let att_ca_list_ex = att_ca_builder.build();
344
345 assert_eq!(rap.webauthn_att_ca_list, Some(att_ca_list_ex));
346 }
347}