kanidm_proto/v1/
unix.rs

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)]
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    pub kind: KeyTypeKind,
25    pub plain: &'static str,
26}
27
28#[allow(dead_code)]
29#[derive(ToSchema)]
30#[schema(as = PublicKeyKind)]
31pub struct PublicKeyKindSchema(PublicKeyKind);
32
33#[derive(ToSchema)]
34#[schema(as = SshPublicKey)]
35pub struct SshPublicKeySchema {
36    pub key_type: KeyType,
37    pub kind: PublicKeyKind,
38    pub comment: Option<String>,
39}
40
41/// A token representing the details of a unix group
42#[derive(Debug, Serialize, Deserialize, Clone, ToSchema)]
43pub struct UnixGroupToken {
44    pub name: String,
45    pub spn: String,
46    pub uuid: Uuid,
47    pub gidnumber: u32,
48}
49
50impl Display for UnixGroupToken {
51    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
52        write!(
53            f,
54            "[ spn: {}, gidnumber: {}, name: {}, uuid: {} ]",
55            self.spn, self.gidnumber, self.name, self.uuid
56        )
57    }
58}
59
60/// Request addition of unix attributes to a group.
61#[derive(Debug, Serialize, Deserialize, Clone, ToSchema)]
62pub struct GroupUnixExtend {
63    pub gidnumber: Option<u32>,
64}
65
66/// A token representing the details of a unix user
67#[skip_serializing_none]
68#[derive(Debug, Serialize, Deserialize, Clone, ToSchema)]
69pub struct UnixUserToken {
70    pub name: String,
71    pub spn: String,
72    pub displayname: String,
73    pub gidnumber: u32,
74    pub uuid: Uuid,
75    pub shell: Option<String>,
76    pub groups: Vec<UnixGroupToken>,
77    pub sshkeys: Vec<SshPublicKey>,
78    // The default value of bool is false.
79    #[serde(default)]
80    pub valid: bool,
81}
82
83impl Display for UnixUserToken {
84    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
85        writeln!(f, "---")?;
86        writeln!(f, "spn: {}", self.spn)?;
87        writeln!(f, "name: {}", self.name)?;
88        writeln!(f, "displayname: {}", self.displayname)?;
89        writeln!(f, "uuid: {}", self.uuid)?;
90        writeln!(f, "gidnumber: {}", self.gidnumber)?;
91        match &self.shell {
92            Some(s) => writeln!(f, "shell: {}", s)?,
93            None => writeln!(f, "shell: <none>")?,
94        }
95        self.sshkeys
96            .iter()
97            .try_for_each(|s| writeln!(f, "{}: {}", ATTR_LDAP_SSHPUBLICKEY, s))?;
98        self.groups
99            .iter()
100            .try_for_each(|g| writeln!(f, "{}: {}", ATTR_GROUP, g))
101    }
102}
103
104/// Request addition of unix attributes to an account
105#[derive(Debug, Serialize, Deserialize, Clone, ToSchema)]
106#[serde(deny_unknown_fields)]
107pub struct AccountUnixExtend {
108    pub gidnumber: Option<u32>,
109    // TODO: rename shell to loginshell everywhere we can find
110    /// The internal attribute is "loginshell" but we use shell in the API currently
111    #[serde(alias = "loginshell")]
112    pub shell: Option<String>,
113}