1use std::collections::BTreeMap;
2
3use kanidm_proto::{
4 constants::{ATTR_DISPLAYNAME, ATTR_ENTRY_MANAGED_BY, ATTR_MAIL, ATTR_NAME},
5 internal::{ApiToken, CredentialStatus},
6 v1::{AccountUnixExtend, ApiTokenGenerate, Entry},
7};
8use time::OffsetDateTime;
9use uuid::Uuid;
10
11use crate::{ClientError, KanidmClient};
12
13impl KanidmClient {
14 pub async fn idm_service_account_list(&self) -> Result<Vec<Entry>, ClientError> {
15 self.perform_get_request("/v1/service_account").await
16 }
17
18 pub async fn idm_service_account_get(&self, id: &str) -> Result<Option<Entry>, ClientError> {
19 self.perform_get_request(format!("/v1/service_account/{id}").as_str())
20 .await
21 }
22
23 pub async fn idm_service_account_create(
25 &self,
26 name: &str,
27 displayname: &str,
28 entry_managed_by: &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 new_acct.attrs.insert(
40 ATTR_ENTRY_MANAGED_BY.to_string(),
41 vec![entry_managed_by.to_string()],
42 );
43
44 self.perform_post_request("/v1/service_account", new_acct)
45 .await
46 }
47
48 pub async fn idm_service_account_delete(&self, id: &str) -> Result<(), ClientError> {
49 self.perform_delete_request(["/v1/service_account/", id].concat().as_str())
50 .await
51 }
52
53 pub async fn idm_service_account_update(
54 &self,
55 id: &str,
56 newname: Option<&str>,
57 displayname: Option<&str>,
58 entry_managed_by: Option<&str>,
59 mail: Option<&[String]>,
60 ) -> Result<(), ClientError> {
61 let mut update_entry = Entry {
62 attrs: BTreeMap::new(),
63 };
64
65 if let Some(newname) = newname {
66 update_entry
67 .attrs
68 .insert(ATTR_NAME.to_string(), vec![newname.to_string()]);
69 }
70
71 if let Some(newdisplayname) = displayname {
72 update_entry.attrs.insert(
73 ATTR_DISPLAYNAME.to_string(),
74 vec![newdisplayname.to_string()],
75 );
76 }
77
78 if let Some(entry_managed_by) = entry_managed_by {
79 update_entry.attrs.insert(
80 ATTR_ENTRY_MANAGED_BY.to_string(),
81 vec![entry_managed_by.to_string()],
82 );
83 }
84
85 if let Some(mail) = mail {
86 update_entry
87 .attrs
88 .insert(ATTR_MAIL.to_string(), mail.to_vec());
89 }
90
91 self.perform_patch_request(format!("/v1/service_account/{id}").as_str(), update_entry)
92 .await
93 }
94
95 pub async fn idm_service_account_add_attr(
96 &self,
97 id: &str,
98 attr: &str,
99 values: &[&str],
100 ) -> Result<(), ClientError> {
101 let msg: Vec<_> = values.iter().map(|v| (*v).to_string()).collect();
102 self.perform_post_request(
103 format!("/v1/service_account/{id}/_attr/{attr}").as_str(),
104 msg,
105 )
106 .await
107 }
108
109 pub async fn idm_service_account_set_attr(
110 &self,
111 id: &str,
112 attr: &str,
113 values: &[&str],
114 ) -> Result<(), ClientError> {
115 let m: Vec<_> = values.iter().map(|v| (*v).to_string()).collect();
116 self.perform_put_request(format!("/v1/service_account/{id}/_attr/{attr}").as_str(), m)
117 .await
118 }
119
120 pub async fn idm_service_account_get_attr(
121 &self,
122 id: &str,
123 attr: &str,
124 ) -> Result<Option<Vec<String>>, ClientError> {
125 self.perform_get_request(format!("/v1/service_account/{id}/_attr/{attr}").as_str())
126 .await
127 }
128
129 pub async fn idm_service_account_purge_attr(
130 &self,
131 id: &str,
132 attr: &str,
133 ) -> Result<(), ClientError> {
134 self.perform_delete_request(format!("/v1/service_account/{id}/_attr/{attr}").as_str())
135 .await
136 }
137
138 pub async fn idm_service_account_post_ssh_pubkey(
139 &self,
140 id: &str,
141 tag: &str,
142 pubkey: &str,
143 ) -> Result<(), ClientError> {
144 let sk = (tag.to_string(), pubkey.to_string());
145 self.perform_post_request(
146 format!("/v1/service_account/{id}/_ssh_pubkeys").as_str(),
147 sk,
148 )
149 .await
150 }
151
152 pub async fn idm_service_account_delete_ssh_pubkey(
153 &self,
154 id: &str,
155 tag: &str,
156 ) -> Result<(), ClientError> {
157 self.perform_delete_request(format!("/v1/service_account/{id}/_ssh_pubkeys/{tag}").as_str())
158 .await
159 }
160
161 pub async fn idm_service_account_unix_extend(
162 &self,
163 id: &str,
165 gidnumber: Option<u32>,
167 shell: Option<&str>,
169 ) -> Result<(), ClientError> {
170 let ux = AccountUnixExtend {
171 shell: shell.map(str::to_string),
172 gidnumber,
173 };
174 self.perform_post_request(format!("/v1/service_account/{id}/_unix").as_str(), ux)
175 .await
176 }
177
178 pub async fn idm_service_account_into_person(&self, id: &str) -> Result<(), ClientError> {
180 self.perform_post_request(
181 format!("/v1/service_account/{id}/_into_person").as_str(),
182 (),
183 )
184 .await
185 }
186
187 pub async fn idm_service_account_get_credential_status(
188 &self,
189 id: &str,
190 ) -> Result<CredentialStatus, ClientError> {
191 let res: Result<CredentialStatus, ClientError> = self
192 .perform_get_request(format!("/v1/service_account/{id}/_credential/_status").as_str())
193 .await;
194 res.and_then(|cs| {
195 if cs.creds.is_empty() {
196 Err(ClientError::EmptyResponse)
197 } else {
198 Ok(cs)
199 }
200 })
201 }
202
203 pub async fn idm_service_account_generate_password(
204 &self,
205 id: &str,
206 ) -> Result<String, ClientError> {
207 let res: Result<String, ClientError> = self
208 .perform_get_request(format!("/v1/service_account/{id}/_credential/_generate").as_str())
209 .await;
210 res.and_then(|pw| {
211 if pw.is_empty() {
212 Err(ClientError::EmptyResponse)
213 } else {
214 Ok(pw)
215 }
216 })
217 }
218
219 pub async fn idm_service_account_list_api_token(
220 &self,
221 id: &str,
222 ) -> Result<Vec<ApiToken>, ClientError> {
223 self.perform_get_request(format!("/v1/service_account/{id}/_api_token").as_str())
225 .await
226 }
227
228 pub async fn idm_service_account_generate_api_token(
229 &self,
230 id: &str,
231 label: &str,
232 expiry: Option<OffsetDateTime>,
233 read_write: bool,
234 compact: bool,
235 ) -> Result<String, ClientError> {
236 let new_token = ApiTokenGenerate {
237 label: label.to_string(),
238 expiry,
239 read_write,
240 compact,
241 };
242 self.perform_post_request(
243 format!("/v1/service_account/{id}/_api_token").as_str(),
244 new_token,
245 )
246 .await
247 }
248
249 pub async fn idm_service_account_destroy_api_token(
250 &self,
251 id: &str,
252 token_id: Uuid,
253 ) -> Result<(), ClientError> {
254 self.perform_delete_request(
255 format!("/v1/service_account/{}/_api_token/{}", id, token_id).as_str(),
256 )
257 .await
258 }
259}