vaultrs/auth/
userpass.rs

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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
use crate::{
    api::{self, auth::userpass::requests::LoginRequest, AuthInfo},
    client::Client,
    error::ClientError,
};

// Fetch a token with policies corresponding to the username.
//
// See [LoginRequest]
pub async fn login(
    client: &impl Client,
    mount: &str,
    username: &str,
    password: &str,
) -> Result<AuthInfo, ClientError> {
    let endpoint = LoginRequest::builder()
        .mount(mount)
        .username(username)
        .password(password)
        .build()
        .unwrap();
    api::auth(client, endpoint).await
}

pub mod user {
    use crate::{
        api::{
            self,
            auth::userpass::{
                requests::{
                    CreateUserRequest, CreateUserRequestBuilder, DeleteUserRequest,
                    ListUsersRequest, ReadUserRequest, UpdatePasswordRequest,
                    UpdatePoliciesRequest,
                },
                responses::{ListUsersResponse, ReadUserResponse},
            },
        },
        client::Client,
        error::ClientError,
    };

    /// Deletes a user.
    ///
    /// See [DeleteUserRequest]
    pub async fn delete(
        client: &impl Client,
        mount: &str,
        username: &str,
    ) -> Result<(), ClientError> {
        let endpoint = DeleteUserRequest::builder()
            .mount(mount)
            .username(username)
            .build()
            .unwrap();
        api::exec_with_empty(client, endpoint).await
    }

    /// Lists users.
    ///
    /// See [ListUsersRequest]
    pub async fn list(client: &impl Client, mount: &str) -> Result<ListUsersResponse, ClientError> {
        let endpoint = ListUsersRequest::builder().mount(mount).build().unwrap();
        api::exec_with_result(client, endpoint).await
    }

    /// Reads information about a user.
    ///
    /// See [ReadUserRequest]
    pub async fn read(
        client: &impl Client,
        mount: &str,
        username: &str,
    ) -> Result<ReadUserResponse, ClientError> {
        let endpoint = ReadUserRequest::builder()
            .mount(mount)
            .username(username)
            .build()
            .unwrap();
        api::exec_with_result(client, endpoint).await
    }

    /// Crates or updates a new user.
    ///
    /// See [CreateUserRequest]
    pub async fn set(
        client: &impl Client,
        mount: &str,
        username: &str,
        password: &str,
        opts: Option<&mut CreateUserRequestBuilder>,
    ) -> Result<(), ClientError> {
        let mut t = CreateUserRequest::builder();
        let endpoint = opts
            .unwrap_or(&mut t)
            .mount(mount)
            .username(username)
            .password(password)
            .build()
            .unwrap();
        api::exec_with_empty(client, endpoint).await
    }

    /// Updates a user's password.
    ///
    /// See [UpdatePasswordRequest]
    pub async fn update_password(
        client: &impl Client,
        mount: &str,
        username: &str,
        password: &str,
    ) -> Result<(), ClientError> {
        let endpoint = UpdatePasswordRequest::builder()
            .mount(mount)
            .username(username)
            .password(password)
            .build()
            .unwrap();
        api::exec_with_empty(client, endpoint).await
    }

    /// Updates a user's policies.
    ///
    /// See [UpdatePoliciesRequest]
    pub async fn update_policies(
        client: &impl Client,
        mount: &str,
        username: &str,
        policies: &str,
    ) -> Result<(), ClientError> {
        let endpoint = UpdatePoliciesRequest::builder()
            .mount(mount)
            .username(username)
            .policies(policies)
            .build()
            .unwrap();
        api::exec_with_empty(client, endpoint).await
    }
}