Skip to main content

kanidm_client/
person.rs

1use crate::{ClientError, KanidmClient};
2use kanidm_proto::{
3    constants::*,
4    internal::{CredentialStatus, IdentifyUserRequest, IdentifyUserResponse},
5    v1::{AccountUnixExtend, Entry, SingleStringRequest, UatStatus},
6};
7use std::collections::BTreeMap;
8use uuid::Uuid;
9
10impl KanidmClient {
11    pub async fn idm_person_account_list(&self) -> Result<Vec<Entry>, ClientError> {
12        self.perform_get_request("/v1/person").await
13    }
14
15    pub async fn idm_person_account_get(&self, id: &str) -> Result<Option<Entry>, ClientError> {
16        self.perform_get_request(format!("/v1/person/{id}").as_str())
17            .await
18    }
19
20    pub async fn idm_person_search(&self, id: &str) -> Result<Vec<Entry>, ClientError> {
21        self.perform_get_request(format!("/v1/person/_search/{id}").as_str())
22            .await
23    }
24
25    pub async fn idm_person_account_create(
26        &self,
27        name: &str,
28        displayname: &str,
29    ) -> Result<(), ClientError> {
30        let mut new_acct = Entry {
31            attrs: BTreeMap::new(),
32        };
33        new_acct
34            .attrs
35            .insert(ATTR_NAME.to_string(), vec![name.to_string()]);
36        new_acct
37            .attrs
38            .insert(ATTR_DISPLAYNAME.to_string(), vec![displayname.to_string()]);
39        self.perform_post_request("/v1/person", new_acct).await
40    }
41
42    pub async fn idm_person_account_update(
43        &self,
44        id: &str,
45        newname: Option<&str>,
46        displayname: Option<&str>,
47        legalname: Option<&str>,
48        mail: Option<&[String]>,
49    ) -> Result<(), ClientError> {
50        let mut update_entry = Entry {
51            attrs: BTreeMap::new(),
52        };
53
54        if let Some(newname) = newname {
55            update_entry
56                .attrs
57                .insert(ATTR_NAME.to_string(), vec![newname.to_string()]);
58        }
59        if let Some(newdisplayname) = displayname {
60            update_entry.attrs.insert(
61                ATTR_DISPLAYNAME.to_string(),
62                vec![newdisplayname.to_string()],
63            );
64        }
65        if let Some(newlegalname) = legalname {
66            // An empty string means we should purge the attribute, which
67            // is done by inserting an empty vec as the entry value
68            let val = if newlegalname.is_empty() {
69                vec![]
70            } else {
71                vec![newlegalname.to_string()]
72            };
73            update_entry.attrs.insert(ATTR_LEGALNAME.to_string(), val);
74        }
75        if let Some(mail) = mail {
76            update_entry
77                .attrs
78                .insert(ATTR_MAIL.to_string(), mail.to_vec());
79        }
80
81        self.perform_patch_request(format!("/v1/person/{id}").as_str(), update_entry)
82            .await
83    }
84
85    pub async fn idm_person_account_delete(&self, id: &str) -> Result<(), ClientError> {
86        self.perform_delete_request(format!("/v1/person/{id}").as_str())
87            .await
88    }
89
90    pub async fn idm_person_account_add_attr(
91        &self,
92        id: &str,
93        attr: &str,
94        values: &[&str],
95    ) -> Result<(), ClientError> {
96        let msg: Vec<_> = values.iter().map(|v| (*v).to_string()).collect();
97        self.perform_post_request(format!("/v1/person/{id}/_attr/{attr}").as_str(), msg)
98            .await
99    }
100
101    pub async fn idm_person_account_set_attr(
102        &self,
103        id: &str,
104        attr: &str,
105        values: &[&str],
106    ) -> Result<(), ClientError> {
107        let m: Vec<_> = values.iter().map(|v| (*v).to_string()).collect();
108        self.perform_put_request(format!("/v1/person/{id}/_attr/{attr}").as_str(), m)
109            .await
110    }
111
112    pub async fn idm_person_account_get_attr(
113        &self,
114        id: &str,
115        attr: &str,
116    ) -> Result<Option<Vec<String>>, ClientError> {
117        self.perform_get_request(format!("/v1/person/{id}/_attr/{attr}").as_str())
118            .await
119    }
120
121    pub async fn idm_person_account_purge_attr(
122        &self,
123        id: &str,
124        attr: &str,
125    ) -> Result<(), ClientError> {
126        self.perform_delete_request(format!("/v1/person/{id}/_attr/{attr}").as_str())
127            .await
128    }
129
130    pub async fn idm_person_account_get_credential_status(
131        &self,
132        id: &str,
133    ) -> Result<CredentialStatus, ClientError> {
134        let res: Result<CredentialStatus, ClientError> = self
135            .perform_get_request(format!("/v1/person/{id}/_credential/_status").as_str())
136            .await;
137        res.and_then(|cs| {
138            if cs.creds.is_empty() {
139                Err(ClientError::EmptyResponse)
140            } else {
141                Ok(cs)
142            }
143        })
144    }
145
146    // This helper calls through the credential update session wrappers to
147    pub async fn idm_person_account_primary_credential_set_password(
148        &self,
149        id: &str,
150        pw: &str,
151    ) -> Result<(), ClientError> {
152        let (session_tok, status) = self.idm_account_credential_update_begin(id).await?;
153        trace!(?status);
154
155        let status = self
156            .idm_account_credential_update_set_password(&session_tok, pw)
157            .await?;
158        trace!(?status);
159
160        self.idm_account_credential_update_commit(&session_tok)
161            .await
162    }
163
164    pub async fn idm_person_account_post_ssh_pubkey(
165        &self,
166        id: &str,
167        tag: &str,
168        pubkey: &str,
169    ) -> Result<(), ClientError> {
170        let tag = urlencoding::encode(tag);
171        let sk = (tag, pubkey.to_string());
172        self.perform_post_request(format!("/v1/person/{id}/_ssh_pubkeys").as_str(), sk)
173            .await
174    }
175
176    pub async fn idm_person_account_delete_ssh_pubkey(
177        &self,
178        id: &str,
179        tag: &str,
180    ) -> Result<(), ClientError> {
181        let tag = urlencoding::encode(tag);
182        self.perform_delete_request(format!("/v1/person/{id}/_ssh_pubkeys/{tag}").as_str())
183            .await
184    }
185
186    pub async fn idm_person_account_unix_extend(
187        &self,
188        id: &str,
189        gidnumber: Option<u32>,
190        shell: Option<&str>,
191    ) -> Result<(), ClientError> {
192        let ux = AccountUnixExtend {
193            shell: shell.map(str::to_string),
194            gidnumber,
195        };
196        self.perform_post_request(format!("/v1/person/{id}/_unix").as_str(), ux)
197            .await
198    }
199
200    pub async fn idm_person_account_unix_cred_put(
201        &self,
202        id: &str,
203        cred: &str,
204    ) -> Result<(), ClientError> {
205        let req = SingleStringRequest {
206            value: cred.to_string(),
207        };
208        self.perform_put_request(
209            ["/v1/person/", id, "/_unix/_credential"].concat().as_str(),
210            req,
211        )
212        .await
213    }
214
215    pub async fn idm_person_account_unix_cred_delete(&self, id: &str) -> Result<(), ClientError> {
216        self.perform_delete_request(["/v1/person/", id, "/_unix/_credential"].concat().as_str())
217            .await
218    }
219
220    pub async fn idm_person_identify_user(
221        &self,
222        id: &str,
223        request: IdentifyUserRequest,
224    ) -> Result<IdentifyUserResponse, ClientError> {
225        self.perform_post_request(
226            ["/v1/person/", id, "/_identify_user"].concat().as_str(),
227            request,
228        )
229        .await
230    }
231
232    pub async fn idm_account_radius_credential_get(
233        &self,
234        id: &str,
235    ) -> Result<Option<String>, ClientError> {
236        self.perform_get_request(format!("/v1/person/{id}/_radius").as_str())
237            .await
238    }
239
240    pub async fn idm_account_radius_credential_regenerate(
241        &self,
242        id: &str,
243    ) -> Result<String, ClientError> {
244        self.perform_post_request(format!("/v1/person/{id}/_radius").as_str(), ())
245            .await
246    }
247
248    pub async fn idm_account_radius_credential_delete(&self, id: &str) -> Result<(), ClientError> {
249        self.perform_delete_request(format!("/v1/person/{id}/_radius").as_str())
250            .await
251    }
252
253    pub async fn idm_account_list_user_auth_token(
254        &self,
255        id: &str,
256    ) -> Result<Vec<UatStatus>, ClientError> {
257        self.perform_get_request(format!("/v1/account/{id}/_user_auth_token").as_str())
258            .await
259    }
260
261    pub async fn idm_account_destroy_user_auth_token(
262        &self,
263        id: &str,
264        token_id: Uuid,
265    ) -> Result<(), ClientError> {
266        self.perform_delete_request(
267            format!("/v1/account/{}/_user_auth_token/{}", id, token_id).as_str(),
268        )
269        .await
270    }
271
272    pub async fn idm_person_certificate_list(&self, id: &str) -> Result<Vec<Entry>, ClientError> {
273        self.perform_get_request(format!("/v1/person/{id}/_certificate").as_str())
274            .await
275    }
276
277    pub async fn idm_person_certificate_create(
278        &self,
279        id: &str,
280        pem_data: &str,
281    ) -> Result<(), ClientError> {
282        let mut new_cert = Entry {
283            attrs: BTreeMap::new(),
284        };
285        new_cert
286            .attrs
287            .insert(ATTR_CERTIFICATE.to_string(), vec![pem_data.to_string()]);
288        self.perform_post_request(format!("/v1/person/{id}/_certificate").as_str(), new_cert)
289            .await
290    }
291}