1use serde::{Deserialize, Serialize};
2use sshkey_attest::proto::PublicKey as SshPublicKey;
3use sshkeys::{KeyType, KeyTypeKind, PublicKeyKind};
4use std::fmt::{self, Display};
5use utoipa::ToSchema;
6use uuid::Uuid;
7
8use serde_with::skip_serializing_none;
9
10use crate::constants::{ATTR_GROUP, ATTR_LDAP_SSHPUBLICKEY};
11
12#[allow(dead_code)]
13#[derive(ToSchema)]
14#[schema(as = KeyTypeKind, value_type = String)]
15pub struct KeyTypeKindSchema(KeyTypeKind);
16
17#[derive(ToSchema)]
18#[schema(as = KeyType)]
19pub struct KeyTypeSchema {
20 pub name: &'static str,
21 pub short_name: &'static str,
22 pub is_cert: bool,
23 pub is_sk: bool,
24 #[schema(value_type = String)]
25 pub kind: KeyTypeKind,
26 pub plain: &'static str,
27}
28
29#[allow(dead_code)]
30#[derive(ToSchema)]
31#[schema(as = PublicKeyKind, value_type = String)]
32pub struct PublicKeyKindSchema(PublicKeyKind);
33
34#[derive(ToSchema)]
35#[schema(as = SshPublicKey)]
36pub struct SshPublicKeySchema {
37 #[schema(value_type = String)]
38 pub key_type: KeyType,
39 #[schema(value_type = String)]
40 pub kind: PublicKeyKind,
41 pub comment: Option<String>,
42}
43
44#[derive(Debug, Serialize, Deserialize, Clone, ToSchema)]
46pub struct UnixGroupToken {
47 pub name: String,
48 pub spn: String,
49 pub uuid: Uuid,
50 pub gidnumber: u32,
51}
52
53impl Display for UnixGroupToken {
54 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
55 write!(
56 f,
57 "[ spn: {}, gidnumber: {}, name: {}, uuid: {} ]",
58 self.spn, self.gidnumber, self.name, self.uuid
59 )
60 }
61}
62
63#[derive(Debug, Serialize, Deserialize, Clone, ToSchema)]
65pub struct GroupUnixExtend {
66 pub gidnumber: Option<u32>,
67}
68
69#[skip_serializing_none]
71#[derive(Debug, Serialize, Deserialize, Clone, ToSchema)]
72pub struct UnixUserToken {
73 pub name: String,
74 pub spn: String,
75 pub displayname: String,
76 pub gidnumber: u32,
77 pub uuid: Uuid,
78 pub shell: Option<String>,
79 pub groups: Vec<UnixGroupToken>,
80 #[schema(value_type = Vec<String>)]
81 pub sshkeys: Vec<SshPublicKey>,
82 #[serde(default)]
84 pub valid: bool,
85}
86
87impl Display for UnixUserToken {
88 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
89 writeln!(f, "---")?;
90 writeln!(f, "spn: {}", self.spn)?;
91 writeln!(f, "name: {}", self.name)?;
92 writeln!(f, "displayname: {}", self.displayname)?;
93 writeln!(f, "uuid: {}", self.uuid)?;
94 writeln!(f, "gidnumber: {}", self.gidnumber)?;
95 match &self.shell {
96 Some(s) => writeln!(f, "shell: {s}")?,
97 None => writeln!(f, "shell: <none>")?,
98 }
99 self.sshkeys
100 .iter()
101 .try_for_each(|s| writeln!(f, "{ATTR_LDAP_SSHPUBLICKEY}: {s}"))?;
102 self.groups
103 .iter()
104 .try_for_each(|g| writeln!(f, "{ATTR_GROUP}: {g}"))
105 }
106}
107
108#[derive(Debug, Serialize, Deserialize, Clone, ToSchema)]
110#[serde(deny_unknown_fields)]
111pub struct AccountUnixExtend {
112 pub gidnumber: Option<u32>,
113 #[serde(alias = "loginshell")]
116 pub shell: Option<String>,
117}