Skip to main content

vault_client_rs/api/auth/
userpass.rs

1use reqwest::Method;
2use secrecy::{ExposeSecret, SecretString};
3
4use crate::VaultClient;
5use crate::api::traits::UserpassAuthOperations;
6use crate::client::{encode_path, to_body};
7use crate::types::auth::{UserpassUserInfo, UserpassUserRequest};
8use crate::types::error::VaultError;
9use crate::types::response::AuthInfo;
10
11#[derive(Debug)]
12pub struct UserpassAuthHandler<'a> {
13    pub(crate) client: &'a VaultClient,
14    pub(crate) mount: String,
15}
16
17impl UserpassAuthOperations for UserpassAuthHandler<'_> {
18    async fn login(&self, username: &str, password: &SecretString) -> Result<AuthInfo, VaultError> {
19        let body = serde_json::json!({ "password": password.expose_secret() });
20        let resp = self
21            .client
22            .exec_with_auth::<serde_json::Value>(
23                Method::POST,
24                &format!("auth/{}/login/{}", self.mount, encode_path(username)),
25                Some(&body),
26            )
27            .await?;
28        let auth = resp.auth.ok_or(VaultError::EmptyResponse)?;
29        self.client.update_token_from_auth(&auth)?;
30        Ok(auth)
31    }
32
33    async fn create_user(
34        &self,
35        username: &str,
36        params: &UserpassUserRequest,
37    ) -> Result<(), VaultError> {
38        let body = to_body(params)?;
39        self.client
40            .exec_empty(
41                Method::POST,
42                &format!("auth/{}/users/{}", self.mount, encode_path(username)),
43                Some(&body),
44            )
45            .await
46    }
47
48    async fn read_user(&self, username: &str) -> Result<UserpassUserInfo, VaultError> {
49        self.client
50            .exec_with_data(
51                Method::GET,
52                &format!("auth/{}/users/{}", self.mount, encode_path(username)),
53                None,
54            )
55            .await
56    }
57
58    async fn delete_user(&self, username: &str) -> Result<(), VaultError> {
59        self.client
60            .exec_empty(
61                Method::DELETE,
62                &format!("auth/{}/users/{}", self.mount, encode_path(username)),
63                None,
64            )
65            .await
66    }
67
68    async fn list_users(&self) -> Result<Vec<String>, VaultError> {
69        self.client
70            .exec_list(&format!("auth/{}/users", self.mount))
71            .await
72    }
73
74    async fn update_password(
75        &self,
76        username: &str,
77        password: &SecretString,
78    ) -> Result<(), VaultError> {
79        let body = serde_json::json!({ "password": password.expose_secret() });
80        self.client
81            .exec_empty(
82                Method::POST,
83                &format!(
84                    "auth/{}/users/{}/password",
85                    self.mount,
86                    encode_path(username)
87                ),
88                Some(&body),
89            )
90            .await
91    }
92}