1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
use crate::{ClientError, KanidmClient};
use kanidm_proto::constants::{ATTR_DESCRIPTION, ATTR_NAME};
use kanidm_proto::v1::Entry;
use std::collections::BTreeMap;
use url::Url;

impl KanidmClient {
    pub async fn idm_sync_account_list(&self) -> Result<Vec<Entry>, ClientError> {
        self.perform_get_request("/v1/sync_account").await
    }

    pub async fn idm_sync_account_get(&self, id: &str) -> Result<Option<Entry>, ClientError> {
        self.perform_get_request(format!("/v1/sync_account/{}", id).as_str())
            .await
    }

    pub async fn idm_sync_account_set_credential_portal(
        &self,
        id: &str,
        url: Option<&Url>,
    ) -> Result<(), ClientError> {
        let m = if let Some(url) = url {
            vec![url.to_owned()]
        } else {
            vec![]
        };

        self.perform_put_request(
            format!("/v1/sync_account/{}/_attr/sync_credential_portal", id).as_str(),
            m,
        )
        .await
    }

    pub async fn idm_sync_account_get_credential_portal(
        &self,
        id: &str,
    ) -> Result<Option<Url>, ClientError> {
        self.perform_get_request(
            format!("/v1/sync_account/{}/_attr/sync_credential_portal", id).as_str(),
        )
        .await
        .map(|values: Vec<Url>| values.first().cloned())
    }

    pub async fn idm_sync_account_set_yield_attributes(
        &self,
        id: &str,
        attrs: &Vec<String>,
    ) -> Result<(), ClientError> {
        // let m: Vec<_> = members.iter().map(|v| (*v).to_string()).collect();
        self.perform_put_request(
            format!("/v1/sync_account/{}/_attr/sync_yield_authority", id).as_str(),
            &attrs,
        )
        .await
    }

    pub async fn idm_sync_account_create(
        &self,
        name: &str,
        description: Option<&str>,
    ) -> Result<(), ClientError> {
        let mut new_acct = Entry {
            attrs: BTreeMap::new(),
        };

        new_acct
            .attrs
            .insert(ATTR_NAME.to_string(), vec![name.to_string()]);
        if let Some(description) = description {
            new_acct
                .attrs
                .insert(ATTR_DESCRIPTION.to_string(), vec![description.to_string()]);
        }

        self.perform_post_request("/v1/sync_account", new_acct)
            .await
    }

    /// Creates a sync token for a given sync account
    pub async fn idm_sync_account_generate_token(
        &self,
        id: &str,
        label: &str,
    ) -> Result<String, ClientError> {
        self.perform_post_request(
            format!("/v1/sync_account/{}/_sync_token", id).as_str(),
            label,
        )
        .await
    }

    pub async fn idm_sync_account_destroy_token(&self, id: &str) -> Result<(), ClientError> {
        self.perform_delete_request(format!("/v1/sync_account/{}/_sync_token", id,).as_str())
            .await
    }

    pub async fn idm_sync_account_force_refresh(&self, id: &str) -> Result<(), ClientError> {
        let mut update_entry = Entry {
            attrs: BTreeMap::new(),
        };

        update_entry
            .attrs
            .insert("sync_cookie".to_string(), Vec::with_capacity(0));

        self.perform_patch_request(format!("/v1/sync_account/{}", id).as_str(), update_entry)
            .await
    }

    pub async fn idm_sync_account_finalise(&self, id: &str) -> Result<(), ClientError> {
        self.perform_get_request(format!("/v1/sync_account/{}/_finalise", id).as_str())
            .await
    }

    pub async fn idm_sync_account_terminate(&self, id: &str) -> Result<(), ClientError> {
        self.perform_get_request(format!("/v1/sync_account/{}/_terminate", id).as_str())
            .await
    }
}