Skip to main content

vault_client_rs/api/auth/
github.rs

1use reqwest::Method;
2use secrecy::{ExposeSecret, SecretString};
3
4use crate::VaultClient;
5use crate::api::traits::GithubAuthOperations;
6use crate::client::{encode_path, to_body};
7use crate::types::auth::{GithubConfig, GithubConfigRequest, GithubTeamInfo, GithubTeamMapping};
8use crate::types::error::VaultError;
9use crate::types::response::AuthInfo;
10
11#[derive(Debug)]
12pub struct GithubAuthHandler<'a> {
13    pub(crate) client: &'a VaultClient,
14    pub(crate) mount: String,
15}
16
17impl GithubAuthOperations for GithubAuthHandler<'_> {
18    async fn login(&self, token: &SecretString) -> Result<AuthInfo, VaultError> {
19        let body = serde_json::json!({ "token": token.expose_secret() });
20        let resp = self
21            .client
22            .exec_with_auth::<serde_json::Value>(
23                Method::POST,
24                &format!("auth/{}/login", self.mount),
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 configure(&self, config: &GithubConfigRequest) -> Result<(), VaultError> {
34        let body = to_body(config)?;
35        self.client
36            .exec_empty(
37                Method::POST,
38                &format!("auth/{}/config", self.mount),
39                Some(&body),
40            )
41            .await
42    }
43
44    async fn read_config(&self) -> Result<GithubConfig, VaultError> {
45        self.client
46            .exec_with_data(Method::GET, &format!("auth/{}/config", self.mount), None)
47            .await
48    }
49
50    async fn map_team(&self, team: &str, params: &GithubTeamMapping) -> Result<(), VaultError> {
51        let body = to_body(params)?;
52        self.client
53            .exec_empty(
54                Method::POST,
55                &format!("auth/{}/map/teams/{}", self.mount, encode_path(team)),
56                Some(&body),
57            )
58            .await
59    }
60
61    async fn read_team_mapping(&self, team: &str) -> Result<GithubTeamInfo, VaultError> {
62        self.client
63            .exec_with_data(
64                Method::GET,
65                &format!("auth/{}/map/teams/{}", self.mount, encode_path(team)),
66                None,
67            )
68            .await
69    }
70
71    async fn list_teams(&self) -> Result<Vec<String>, VaultError> {
72        self.client
73            .exec_list(&format!("auth/{}/map/teams", self.mount))
74            .await
75    }
76}