1use super::{ScimMail, ScimOauth2ClaimMapJoinChar, ScimSshPublicKey};
2use crate::{attribute::Attribute, internal::UiHint, v1::OutboundMessage};
3use crypto_glue::s256::Sha256Output;
4use scim_proto::{ScimEntry, ScimEntryHeader};
5use serde::Serialize;
6use serde_with::{base64, formats, hex::Hex, serde_as, skip_serializing_none};
7use std::{
8 collections::{BTreeMap, BTreeSet},
9 num::NonZeroU64,
10};
11use time::{format_description::well_known::Rfc3339, OffsetDateTime};
12use url::Url;
13use utoipa::ToSchema;
14use uuid::Uuid;
15
16#[serde_as]
20#[skip_serializing_none]
21#[derive(Serialize, Debug, Clone, ToSchema)]
22#[serde(deny_unknown_fields, rename_all = "camelCase")]
23pub struct ScimEntryKanidm {
24 #[serde(flatten)]
25 pub header: ScimEntryHeader,
26
27 pub ext_access_check: Option<ScimEffectiveAccess>,
28 #[serde(flatten)]
29 pub attrs: BTreeMap<Attribute, ScimValueKanidm>,
30}
31
32impl ScimEntryKanidm {
33 fn get_string_attr(&self, attr: &Attribute) -> Option<&String> {
34 self.attrs.get(attr).and_then(|v| match v {
35 ScimValueKanidm::String(s) => Some(s),
36 _ => None,
37 })
38 }
39
40 fn get_scim_refs_attr(&self, attr: &Attribute) -> Option<&Vec<ScimReference>> {
41 let option = self.attrs.get(attr);
42 option.and_then(|v| match v {
43 ScimValueKanidm::EntryReferences(s) => Some(s),
44 _ => None,
45 })
46 }
47}
48
49#[serde_as]
50#[skip_serializing_none]
51#[derive(Serialize, Clone, Debug, Default, ToSchema)]
52#[serde(deny_unknown_fields, rename_all = "camelCase")]
53pub struct ScimListResponse {
54 pub schemas: Vec<String>,
55 pub total_results: u64,
56 #[schema(value_type = u64)]
57 pub items_per_page: Option<NonZeroU64>,
58 #[schema(value_type = u64)]
59 pub start_index: Option<NonZeroU64>,
60 #[schema(value_type = Vec<ScimEntry>)]
61 pub resources: Vec<ScimEntryKanidm>,
62}
63
64#[derive(Serialize, Debug, Clone, ToSchema)]
65pub enum ScimAttributeEffectiveAccess {
66 Grant,
68 Deny,
70 Allow(BTreeSet<Attribute>),
72}
73
74impl ScimAttributeEffectiveAccess {
75 pub fn check(&self, attr: &Attribute) -> bool {
77 match self {
78 Self::Grant => true,
79 Self::Deny => false,
80 Self::Allow(set) => set.contains(attr),
81 }
82 }
83
84 pub fn check_any(&self, attrs: &BTreeSet<Attribute>) -> bool {
86 match self {
87 Self::Grant => true,
88 Self::Deny => false,
89 Self::Allow(set) => attrs.intersection(set).next().is_some(),
90 }
91 }
92}
93
94#[derive(Serialize, Debug, Clone, ToSchema)]
95#[serde(rename_all = "camelCase")]
96pub struct ScimEffectiveAccess {
97 pub ident: Uuid,
99 pub delete: bool,
101 pub search: ScimAttributeEffectiveAccess,
103 pub modify_present: ScimAttributeEffectiveAccess,
105 pub modify_remove: ScimAttributeEffectiveAccess,
107}
108
109#[derive(Serialize, Debug, Clone, ToSchema)]
110#[serde(rename_all = "camelCase")]
111pub struct ScimAddress {
112 pub formatted: String,
113 pub street_address: String,
114 pub locality: String,
115 pub region: String,
116 pub postal_code: String,
117 pub country: String,
118}
119
120#[derive(Serialize, Debug, Clone, ToSchema)]
121#[serde(rename_all = "camelCase")]
122pub struct ScimApplicationPasswordReference {
123 pub uuid: Uuid,
124 pub application_uuid: Uuid,
125 pub label: String,
126}
127
128#[serde_as]
129#[derive(Serialize, Debug, Clone, ToSchema)]
130#[serde(rename_all = "camelCase")]
131pub struct ScimBinary {
132 pub label: String,
133 #[serde_as(as = "base64::Base64<base64::UrlSafe, formats::Unpadded>")]
134 pub value: Vec<u8>,
135}
136
137#[serde_as]
138#[derive(Serialize, Debug, Clone, ToSchema)]
139#[serde(rename_all = "camelCase")]
140pub struct ScimCertificate {
141 #[serde_as(as = "Hex")]
142 pub s256: Vec<u8>,
143 #[serde_as(as = "base64::Base64<base64::UrlSafe, formats::Unpadded>")]
144 pub der: Vec<u8>,
145}
146
147#[serde_as]
148#[derive(Serialize, Debug, Clone, ToSchema)]
149#[serde(rename_all = "camelCase")]
150pub struct ScimAuditString {
151 #[serde_as(as = "Rfc3339")]
152 pub date_time: OffsetDateTime,
153 pub value: String,
154}
155
156#[derive(Serialize, Debug, Clone, ToSchema)]
157#[serde(rename_all = "camelCase")]
158pub enum ScimIntentTokenState {
159 Valid,
160 InProgress,
161 Consumed,
162}
163
164#[serde_as]
165#[derive(Serialize, Debug, Clone, ToSchema)]
166#[serde(rename_all = "camelCase")]
167pub struct ScimIntentToken {
168 pub token_id: String,
169 pub state: ScimIntentTokenState,
170 #[serde_as(as = "Rfc3339")]
171 pub expires: OffsetDateTime,
172}
173
174#[serde_as]
175#[derive(Serialize, Debug, Clone, ToSchema)]
176#[serde(rename_all = "camelCase")]
177pub struct ScimKeyInternal {
178 pub key_id: String,
179 pub status: String,
180 pub usage: String,
181 #[serde_as(as = "Rfc3339")]
182 pub valid_from: OffsetDateTime,
183}
184
185#[serde_as]
186#[skip_serializing_none]
187#[derive(Serialize, Debug, Clone, ToSchema)]
188#[serde(rename_all = "camelCase")]
189pub struct ScimAuthSession {
190 pub id: Uuid,
191 #[serde_as(as = "Option<Rfc3339>")]
192 pub expires: Option<OffsetDateTime>,
193 #[serde_as(as = "Option<Rfc3339>")]
194 pub revoked: Option<OffsetDateTime>,
195 #[serde_as(as = "Rfc3339")]
196 pub issued_at: OffsetDateTime,
197 pub issued_by: Uuid,
198 pub credential_id: Uuid,
199 pub auth_type: String,
200 pub session_scope: String,
201}
202
203#[serde_as]
204#[skip_serializing_none]
205#[derive(Serialize, Debug, Clone, ToSchema)]
206#[serde(rename_all = "camelCase")]
207pub struct ScimOAuth2Session {
208 pub id: Uuid,
209 pub parent_id: Option<Uuid>,
210 pub client_id: Uuid,
211 #[serde_as(as = "Rfc3339")]
212 pub issued_at: OffsetDateTime,
213 #[serde_as(as = "Option<Rfc3339>")]
214 pub expires: Option<OffsetDateTime>,
215 #[serde_as(as = "Option<Rfc3339>")]
216 pub revoked: Option<OffsetDateTime>,
217}
218
219#[serde_as]
220#[skip_serializing_none]
221#[derive(Serialize, Debug, Clone, ToSchema)]
222#[serde(rename_all = "camelCase")]
223pub struct ScimApiToken {
224 pub id: Uuid,
225 pub label: String,
226 #[serde_as(as = "Option<Rfc3339>")]
227 pub expires: Option<OffsetDateTime>,
228 #[serde_as(as = "Rfc3339")]
229 pub issued_at: OffsetDateTime,
230 pub issued_by: Uuid,
231 pub scope: String,
232}
233
234#[serde_as]
235#[derive(Serialize, Debug, Clone, ToSchema)]
236#[serde(rename_all = "camelCase")]
237pub struct ScimOAuth2ScopeMap {
238 pub group: String,
239 pub group_uuid: Uuid,
240 pub scopes: BTreeSet<String>,
241}
242
243#[serde_as]
244#[derive(Serialize, Debug, Clone, ToSchema)]
245#[serde(rename_all = "camelCase")]
246pub struct ScimOAuth2ClaimMap {
247 pub group: String,
248 pub group_uuid: Uuid,
249 pub claim: String,
250 pub join_char: ScimOauth2ClaimMapJoinChar,
251 pub values: BTreeSet<String>,
252}
253
254#[derive(Serialize, Debug, Clone, PartialEq, Eq, ToSchema)]
255#[serde(rename_all = "camelCase")]
256pub struct ScimReference {
257 pub uuid: Uuid,
258 pub value: String,
259}
260
261#[serde_as]
266#[derive(Serialize, Debug, Clone, ToSchema)]
267#[serde(untagged)]
268pub enum ScimValueKanidm {
269 Bool(bool),
270 Uint32(u32),
271 Int64(i64),
272 Uint64(u64),
273 Integer(i64),
274 Decimal(f64),
275 String(String),
276
277 DateTime(#[serde_as(as = "Rfc3339")] OffsetDateTime),
279 Reference(Url),
280 Uuid(Uuid),
281 EntryReference(ScimReference),
282 EntryReferences(Vec<ScimReference>),
283
284 ArrayString(Vec<String>),
285 ArrayDateTime(#[serde_as(as = "Vec<Rfc3339>")] Vec<OffsetDateTime>),
286 ArrayUuid(Vec<Uuid>),
287 ArrayBinary(Vec<ScimBinary>),
288 ArrayCertificate(Vec<ScimCertificate>),
289
290 Address(Vec<ScimAddress>),
291 Mail(Vec<ScimMail>),
292 ApplicationPassword(Vec<ScimApplicationPasswordReference>),
293 AuditString(Vec<ScimAuditString>),
294 SshPublicKey(Vec<ScimSshPublicKey>),
295 AuthSession(Vec<ScimAuthSession>),
296 OAuth2Session(Vec<ScimOAuth2Session>),
297 ApiToken(Vec<ScimApiToken>),
298 IntentToken(Vec<ScimIntentToken>),
299 OAuth2ScopeMap(Vec<ScimOAuth2ScopeMap>),
300 OAuth2ClaimMap(Vec<ScimOAuth2ClaimMap>),
301 KeyInternal(Vec<ScimKeyInternal>),
302 UiHints(Vec<UiHint>),
303
304 Message(OutboundMessage),
305
306 #[schema(value_type = Vec<String>)]
307 Sha256(#[serde_as(as = "Vec<Hex>")] Vec<Sha256Output>),
308}
309
310#[serde_as]
311#[derive(Serialize, Debug, Clone, ToSchema)]
312pub struct ScimPerson {
313 pub uuid: Uuid,
314 pub name: String,
315 pub displayname: String,
316 pub spn: String,
317 pub description: Option<String>,
318 pub mails: Vec<ScimMail>,
319 pub managed_by: Option<ScimReference>,
320 pub groups: Vec<ScimReference>,
321}
322
323impl ScimPerson {
324 pub fn get_primary_mail(&self) -> Option<String> {
325 self.mails
326 .iter()
327 .find(|m| m.primary)
328 .map(|primary_mail| primary_mail.value.clone())
329 }
330}
331
332impl TryFrom<ScimEntryKanidm> for ScimPerson {
333 type Error = ();
334
335 fn try_from(scim_entry: ScimEntryKanidm) -> Result<Self, Self::Error> {
336 let uuid = scim_entry.header.id;
337 let name = scim_entry
338 .get_string_attr(&Attribute::Name)
339 .cloned()
340 .ok_or(())?;
341 let displayname = scim_entry
342 .get_string_attr(&Attribute::DisplayName)
343 .cloned()
344 .ok_or(())?;
345 let spn = scim_entry
346 .get_string_attr(&Attribute::Spn)
347 .cloned()
348 .ok_or(())?;
349 let description = scim_entry.get_string_attr(&Attribute::Description).cloned();
350
351 let mails = scim_entry
352 .attrs
353 .get(&Attribute::Mail)
354 .and_then(|v| match v {
355 ScimValueKanidm::Mail(m) => Some(m.clone()),
356 _ => None,
357 })
358 .unwrap_or_default();
359
360 let groups = scim_entry
361 .get_scim_refs_attr(&Attribute::DirectMemberOf)
362 .cloned()
363 .unwrap_or_default();
364
365 let managed_by = scim_entry
366 .attrs
367 .get(&Attribute::EntryManagedBy)
368 .and_then(|v| match v {
369 ScimValueKanidm::EntryReference(v) => Some(v.clone()),
370 _ => None,
371 });
372
373 Ok(ScimPerson {
374 uuid,
375 name,
376 displayname,
377 spn,
378 description,
379 mails,
380 managed_by,
381 groups,
382 })
383 }
384}
385
386#[serde_as]
387#[derive(Serialize, Debug, Clone, ToSchema)]
388pub struct ScimGroup {
389 pub uuid: Uuid,
390 pub name: String,
391 pub description: Option<String>,
392 pub members: Vec<ScimReference>,
393}
394
395impl TryFrom<ScimEntryKanidm> for ScimGroup {
396 type Error = ();
397
398 fn try_from(scim_entry: ScimEntryKanidm) -> Result<Self, Self::Error> {
399 let uuid = scim_entry.header.id;
400 let name = scim_entry
401 .get_string_attr(&Attribute::Name)
402 .cloned()
403 .ok_or(())?;
404 let description = scim_entry.get_string_attr(&Attribute::Description).cloned();
405 let members = scim_entry
406 .get_scim_refs_attr(&Attribute::Member)
407 .cloned()
408 .unwrap_or_default();
409
410 Ok(ScimGroup {
411 uuid,
412 name,
413 description,
414 members,
415 })
416 }
417}
418
419impl From<bool> for ScimValueKanidm {
420 fn from(b: bool) -> Self {
421 Self::Bool(b)
422 }
423}
424
425impl From<OffsetDateTime> for ScimValueKanidm {
426 fn from(odt: OffsetDateTime) -> Self {
427 Self::DateTime(odt)
428 }
429}
430
431impl From<Vec<UiHint>> for ScimValueKanidm {
432 fn from(set: Vec<UiHint>) -> Self {
433 Self::UiHints(set)
434 }
435}
436
437impl From<Vec<OffsetDateTime>> for ScimValueKanidm {
438 fn from(set: Vec<OffsetDateTime>) -> Self {
439 Self::ArrayDateTime(set)
440 }
441}
442
443impl From<String> for ScimValueKanidm {
444 fn from(s: String) -> Self {
445 Self::String(s)
446 }
447}
448
449impl From<&str> for ScimValueKanidm {
450 fn from(s: &str) -> Self {
451 Self::String(s.to_string())
452 }
453}
454
455impl From<Vec<String>> for ScimValueKanidm {
456 fn from(set: Vec<String>) -> Self {
457 Self::ArrayString(set)
458 }
459}
460
461impl From<Uuid> for ScimValueKanidm {
462 fn from(u: Uuid) -> Self {
463 Self::Uuid(u)
464 }
465}
466
467impl From<Vec<Uuid>> for ScimValueKanidm {
468 fn from(set: Vec<Uuid>) -> Self {
469 Self::ArrayUuid(set)
470 }
471}
472
473impl From<u32> for ScimValueKanidm {
474 fn from(u: u32) -> Self {
475 Self::Uint32(u)
476 }
477}
478
479impl From<i64> for ScimValueKanidm {
480 fn from(u: i64) -> Self {
481 Self::Int64(u)
482 }
483}
484
485impl From<u64> for ScimValueKanidm {
486 fn from(u: u64) -> Self {
487 Self::Uint64(u)
488 }
489}
490
491impl From<Vec<ScimAddress>> for ScimValueKanidm {
492 fn from(set: Vec<ScimAddress>) -> Self {
493 Self::Address(set)
494 }
495}
496
497impl From<Vec<ScimMail>> for ScimValueKanidm {
498 fn from(set: Vec<ScimMail>) -> Self {
499 Self::Mail(set)
500 }
501}
502
503impl From<Vec<ScimApplicationPasswordReference>> for ScimValueKanidm {
504 fn from(set: Vec<ScimApplicationPasswordReference>) -> Self {
505 Self::ApplicationPassword(set)
506 }
507}
508
509impl From<Vec<ScimAuditString>> for ScimValueKanidm {
510 fn from(set: Vec<ScimAuditString>) -> Self {
511 Self::AuditString(set)
512 }
513}
514
515impl From<Vec<ScimBinary>> for ScimValueKanidm {
516 fn from(set: Vec<ScimBinary>) -> Self {
517 Self::ArrayBinary(set)
518 }
519}
520
521impl From<Vec<ScimCertificate>> for ScimValueKanidm {
522 fn from(set: Vec<ScimCertificate>) -> Self {
523 Self::ArrayCertificate(set)
524 }
525}
526
527impl From<Vec<ScimSshPublicKey>> for ScimValueKanidm {
528 fn from(set: Vec<ScimSshPublicKey>) -> Self {
529 Self::SshPublicKey(set)
530 }
531}
532
533impl From<Vec<ScimAuthSession>> for ScimValueKanidm {
534 fn from(set: Vec<ScimAuthSession>) -> Self {
535 Self::AuthSession(set)
536 }
537}
538
539impl From<Vec<ScimOAuth2Session>> for ScimValueKanidm {
540 fn from(set: Vec<ScimOAuth2Session>) -> Self {
541 Self::OAuth2Session(set)
542 }
543}
544
545impl From<Vec<ScimApiToken>> for ScimValueKanidm {
546 fn from(set: Vec<ScimApiToken>) -> Self {
547 Self::ApiToken(set)
548 }
549}
550
551impl From<Vec<ScimIntentToken>> for ScimValueKanidm {
552 fn from(set: Vec<ScimIntentToken>) -> Self {
553 Self::IntentToken(set)
554 }
555}
556
557impl From<Vec<ScimOAuth2ScopeMap>> for ScimValueKanidm {
558 fn from(set: Vec<ScimOAuth2ScopeMap>) -> Self {
559 Self::OAuth2ScopeMap(set)
560 }
561}
562
563impl From<Vec<ScimOAuth2ClaimMap>> for ScimValueKanidm {
564 fn from(set: Vec<ScimOAuth2ClaimMap>) -> Self {
565 Self::OAuth2ClaimMap(set)
566 }
567}
568
569impl From<Vec<ScimKeyInternal>> for ScimValueKanidm {
570 fn from(set: Vec<ScimKeyInternal>) -> Self {
571 Self::KeyInternal(set)
572 }
573}
574
575impl From<OutboundMessage> for ScimValueKanidm {
576 fn from(message: OutboundMessage) -> Self {
577 Self::Message(message)
578 }
579}
580
581impl From<Vec<Sha256Output>> for ScimValueKanidm {
582 fn from(set: Vec<Sha256Output>) -> Self {
583 Self::Sha256(set)
584 }
585}