vaultrs/auth/
oidc.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
use crate::{
    api::{
        self,
        auth::oidc::{
            requests::{JWTLoginRequest, OIDCAuthRequest, OIDCCallbackRequest},
            responses::OIDCAuthResponse,
        },
        AuthInfo,
    },
    client::Client,
    error::ClientError,
};

/// Obtain an authorization URL from Vault to start an OIDC login flow
///
/// See [OIDCAuthRequest]
pub async fn auth(
    client: &impl Client,
    mount: &str,
    redirect_uri: &str,
    role: Option<String>,
) -> Result<OIDCAuthResponse, ClientError> {
    let mut endpoint = OIDCAuthRequest::builder();
    if let Some(r) = role {
        endpoint.role(r);
    }
    api::exec_with_result(
        client,
        endpoint
            .mount(mount)
            .redirect_uri(redirect_uri)
            .build()
            .unwrap(),
    )
    .await
}

/// Exchange an authorization code for an OIDC ID Token
///
/// See [OIDCCallbackRequest]
pub async fn callback(
    client: &impl Client,
    mount: &str,
    state: &str,
    nonce: &str,
    code: &str,
) -> Result<AuthInfo, ClientError> {
    let endpoint = OIDCCallbackRequest::builder()
        .mount(mount)
        .state(state)
        .nonce(nonce)
        .code(code)
        .build()
        .unwrap();
    api::auth(client, endpoint).await
}

/// Fetch a token using a JWT token
///
/// See [JWTLoginRequest]
pub async fn login(
    client: &impl Client,
    mount: &str,
    jwt: &str,
    role: Option<String>,
) -> Result<AuthInfo, ClientError> {
    let mut endpoint = JWTLoginRequest::builder();
    if let Some(r) = role {
        endpoint.role(r);
    }
    api::auth(client, endpoint.mount(mount).jwt(jwt).build().unwrap()).await
}

pub mod config {
    use crate::{
        api::{
            self,
            auth::oidc::{
                requests::{
                    ReadConfigurationRequest, SetConfigurationRequest,
                    SetConfigurationRequestBuilder,
                },
                responses::ReadConfigurationResponse,
            },
        },
        client::Client,
        error::ClientError,
    };

    /// Read the configuration of the mounted KV engine
    ///
    /// See [ReadConfigurationResponse]
    pub async fn read(
        client: &impl Client,
        mount: &str,
    ) -> Result<ReadConfigurationResponse, ClientError> {
        let endpoint = ReadConfigurationRequest::builder()
            .mount(mount)
            .build()
            .unwrap();
        api::exec_with_result(client, endpoint).await
    }

    /// Update the configuration of the mounted KV engine
    ///
    /// See [SetConfigurationRequest]
    pub async fn set(
        client: &impl Client,
        mount: &str,
        opts: Option<&mut SetConfigurationRequestBuilder>,
    ) -> Result<(), ClientError> {
        let mut t = SetConfigurationRequest::builder();
        let endpoint = opts.unwrap_or(&mut t).mount(mount).build().unwrap();
        api::exec_with_empty(client, endpoint).await
    }
}

pub mod role {
    use crate::api;
    use crate::api::auth::oidc::{
        requests::{
            DeleteRoleRequest, ListRolesRequest, ReadRoleRequest, SetRoleRequest,
            SetRoleRequestBuilder,
        },
        responses::{ListRolesResponse, ReadRoleResponse},
    };
    use crate::client::Client;
    use crate::error::ClientError;

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

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

    /// Reads a role
    ///
    /// See [ReadRoleRequest]
    pub async fn read(
        client: &impl Client,
        mount: &str,
        name: &str,
    ) -> Result<ReadRoleResponse, ClientError> {
        let endpoint = ReadRoleRequest::builder()
            .mount(mount)
            .name(name)
            .build()
            .unwrap();
        api::exec_with_result(client, endpoint).await
    }

    /// Creates or updates a role
    ///
    /// See [SetRoleRequest]
    pub async fn set(
        client: &impl Client,
        mount: &str,
        name: &str,
        user_claim: &str,
        allowed_redirect_uris: Vec<String>,
        opts: Option<&mut SetRoleRequestBuilder>,
    ) -> Result<(), ClientError> {
        let mut t = SetRoleRequest::builder();
        let endpoint = opts
            .unwrap_or(&mut t)
            .mount(mount)
            .name(name)
            .user_claim(user_claim)
            .allowed_redirect_uris(allowed_redirect_uris)
            .build()
            .unwrap();
        api::exec_with_empty(client, endpoint).await
    }
}