casdoor-rust-sdk 1.6.2

Rust client SDK for Casdoor
Documentation
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
// Copyright 2026 The Casdoor Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use std::collections::HashMap;

use reqwest::{multipart, RequestBuilder, StatusCode};
use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};
use serde_json::Value;

use crate::config::AuthConfig;
use crate::error::{CasdoorError, Result};

/// Response is the general response returned by the Casdoor server APIs.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct Response {
    #[serde(default)]
    pub status: String,
    #[serde(default)]
    pub msg: String,
    #[serde(default)]
    pub sub: String,
    #[serde(default)]
    pub name: String,
    #[serde(default)]
    pub data: Value,
    #[serde(default)]
    pub data2: Value,
    #[serde(default)]
    pub data3: Value,
}

/// The body of a POST request sent to the Casdoor server.
pub enum PostBody<'a> {
    /// A raw body posted as `text/plain;charset=UTF-8`, it's what most of the Casdoor
    /// APIs expect.
    Raw(Vec<u8>),
    /// A `multipart/form-data` body made of text fields.
    Form(HashMap<String, String>),
    /// A `multipart/form-data` body made of a single file field named `file`.
    File(&'a [u8]),
}

/// Client is the Casdoor client, it holds the config of the Casdoor application and sends
/// all the API requests to the Casdoor server.
///
/// ```no_run
/// use casdoor_rust_sdk::Client;
///
/// # async fn f() -> casdoor_rust_sdk::Result<()> {
/// let client = Client::new(
///     "http://localhost:8000",
///     "<client-id>",
///     "<client-secret>",
///     "<certificate>",
///     "built-in",
///     "app-built-in",
/// );
///
/// let users = client.get_users().await?;
/// # Ok(())
/// # }
/// ```
#[derive(Debug, Clone)]
pub struct Client {
    /// The config of the Casdoor application.
    pub config: AuthConfig,
    /// The extra HTTP headers added to every request sent to the Casdoor server.
    pub custom_headers: HashMap<String, String>,
    /// The user's access token. If it's not empty, all the API requests sent by this
    /// client are authenticated as the user who owns the access token (via the
    /// `Authorization: Bearer` header), instead of as the application itself (via the
    /// client ID and client secret's Basic Auth).
    /// Use [`Client::with_access_token()`] to get such a client.
    pub access_token: Option<String>,
    http: reqwest::Client,
}

impl Client {
    /// Create a new client from the 6 config values.
    pub fn new(
        endpoint: impl Into<String>,
        client_id: impl Into<String>,
        client_secret: impl Into<String>,
        certificate: impl Into<String>,
        organization_name: impl Into<String>,
        application_name: impl Into<String>,
    ) -> Self {
        Self::from_config(AuthConfig::new(
            endpoint,
            client_id,
            client_secret,
            certificate,
            organization_name,
            application_name,
        ))
    }

    /// Create a new client from an [`AuthConfig`].
    pub fn from_config(config: AuthConfig) -> Self {
        Self {
            config,
            custom_headers: HashMap::new(),
            access_token: None,
            http: reqwest::Client::new(),
        }
    }

    /// Create a new client from a TOML config file, see [`AuthConfig::from_toml()`].
    pub fn from_toml(path: &str) -> Result<Self> {
        Ok(Self::from_config(AuthConfig::from_toml(path)?))
    }

    /// Return a copy of the client that calls all the Casdoor APIs as the user who owns
    /// the given access token, rather than as the application. The access token is the
    /// user's OAuth access token returned by [`Client::get_oauth_token()`],
    /// [`Client::refresh_oauth_token()`], [`Client::get_oauth_token_by_password()`] or
    /// [`Client::impersonate_user()`].
    ///
    /// The returned client is independent from the original one, so the original client
    /// keeps using the application's client ID and client secret, and it's safe to create
    /// one client per user request:
    ///
    /// ```no_run
    /// # async fn f(client: &casdoor_rust_sdk::Client, code: &str) -> casdoor_rust_sdk::Result<()> {
    /// let token = client.get_oauth_token(code).await?;
    /// let user = client.with_access_token(token.access_token).get_account().await?;
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// Note that the APIs are still subject to Casdoor's permission check, so a non-admin
    /// user can only access their own data.
    pub fn with_access_token(&self, access_token: impl Into<String>) -> Self {
        Self {
            access_token: Some(access_token.into()),
            ..self.clone()
        }
    }

    /// Return a copy of the client that adds the given HTTP header to every request.
    pub fn with_custom_header(&self, key: impl Into<String>, value: impl Into<String>) -> Self {
        let mut client = self.clone();
        client.custom_headers.insert(key.into(), value.into());
        client
    }

    /// Return a copy of the client that uses the given [`reqwest::Client`], so that the
    /// timeout, the proxy, the TLS options, etc. can be customized.
    pub fn with_http_client(&self, http: reqwest::Client) -> Self {
        Self {
            http,
            ..self.clone()
        }
    }

    /// The underlying HTTP client, shared by all the requests sent by this client.
    pub(crate) fn http(&self) -> &reqwest::Client {
        &self.http
    }

    /// Return the URL of an API action, such as
    /// `http://localhost:8000/api/get-user?id=built-in%2Fadmin`.
    pub fn get_url(&self, action: &str, query: &[(&str, &str)]) -> String {
        let query = query
            .iter()
            .map(|(k, v)| format!("{}={}", urlencoding::encode(k), urlencoding::encode(v)))
            .collect::<Vec<_>>()
            .join("&");

        format!("{}/api/{}?{}", self.config.endpoint, action, query)
    }

    /// Return the Casdoor object ID (`"owner/name"`) of an object owned by an
    /// organization. If `name` is already a qualified `"owner/name"` ID, it is returned
    /// as-is, so that a client can also address objects that live outside of its own
    /// organization. Object names never contain a slash in Casdoor.
    pub fn get_id(&self, name: &str) -> String {
        get_id(name, &self.config.organization_name)
    }

    /// The general function to send a GET request and get the whole [`Response`].
    pub async fn do_get_response(&self, url: &str) -> Result<Response> {
        let bytes = self.do_get_bytes_raw_without_check(url).await?;
        let response: Response = serde_json::from_slice(&bytes)?;

        if response.status != "ok" {
            return Err(CasdoorError::Casdoor(response.msg));
        }

        Ok(response)
    }

    /// The general function to send a GET request and get the raw response body.
    pub async fn do_get_bytes_raw(&self, url: &str) -> Result<Vec<u8>> {
        let bytes = self.do_get_bytes_raw_without_check(url).await?;

        if let Ok(response) = serde_json::from_slice::<Response>(&bytes) {
            if response.status == "error" {
                return Err(CasdoorError::Casdoor(response.msg));
            }
        }

        Ok(bytes)
    }

    /// The general function to send a POST request and get the whole [`Response`].
    pub async fn do_post(
        &self,
        action: &str,
        query: &[(&str, &str)],
        body: PostBody<'_>,
    ) -> Result<Response> {
        let url = self.get_url(action, query);
        let bytes = self.do_post_bytes_raw(&url, body).await?;

        let response: Response = serde_json::from_slice(&bytes)?;
        if response.status != "ok" {
            return Err(CasdoorError::Casdoor(response.msg));
        }

        Ok(response)
    }

    /// The general function to send a POST request and get the raw response body.
    pub async fn do_post_bytes_raw(&self, url: &str, body: PostBody<'_>) -> Result<Vec<u8>> {
        let mut req = self.http.post(url);

        req = match body {
            PostBody::Raw(bytes) => req
                .header(reqwest::header::CONTENT_TYPE, "text/plain;charset=UTF-8")
                .body(bytes),
            PostBody::Form(fields) => {
                let form = fields
                    .into_iter()
                    .fold(multipart::Form::new(), |form, (k, v)| form.text(k, v));
                req.multipart(form)
            }
            PostBody::File(bytes) => {
                let part = multipart::Part::bytes(bytes.to_vec()).file_name("file");
                req.multipart(multipart::Form::new().part("file", part))
            }
        };

        read_body(self.set_headers(req).send().await?).await
    }

    async fn do_get_bytes_raw_without_check(&self, url: &str) -> Result<Vec<u8>> {
        let req = self.set_headers(self.http.get(url));
        read_body(req.send().await?).await
    }

    /// Set the `Authorization` header and the custom headers of a request. The user's
    /// access token is used when the client has one, so that the API is called as the
    /// user instead of as the application. Otherwise the application's client ID and
    /// client secret are used.
    fn set_headers(&self, req: RequestBuilder) -> RequestBuilder {
        let mut req = match &self.access_token {
            Some(token) if !token.is_empty() => req.bearer_auth(token),
            _ => req.basic_auth(&self.config.client_id, Some(&self.config.client_secret)),
        };

        for (key, value) in &self.custom_headers {
            req = req.header(key, value);
        }

        req
    }

    /// Get one object from an API action, `None` is returned when the Casdoor server
    /// answers with a `null` data, which means the object doesn't exist.
    pub(crate) async fn get_object<T: DeserializeOwned>(
        &self,
        action: &str,
        query: &[(&str, &str)],
    ) -> Result<Option<T>> {
        let url = self.get_url(action, query);
        let response = self.do_get_response(&url).await?;

        if response.data.is_null() {
            return Ok(None);
        }

        Ok(Some(serde_json::from_value(response.data)?))
    }

    /// Get a list of objects from an API action.
    pub(crate) async fn get_list<T: DeserializeOwned>(
        &self,
        action: &str,
        query: &[(&str, &str)],
    ) -> Result<Vec<T>> {
        let url = self.get_url(action, query);
        let response = self.do_get_response(&url).await?;

        if response.data.is_null() {
            return Ok(Vec::new());
        }

        Ok(serde_json::from_value(response.data)?)
    }

    /// Get one page of objects from an API action, the returned count is the total number
    /// of the objects.
    pub(crate) async fn get_pagination<T: DeserializeOwned>(
        &self,
        action: &str,
        owner: &str,
        p: i32,
        page_size: i32,
        query: &[(&str, &str)],
    ) -> Result<(Vec<T>, i32)> {
        let (p, page_size) = (p.to_string(), page_size.to_string());

        let mut full_query = vec![
            ("owner", owner),
            ("p", p.as_str()),
            ("pageSize", page_size.as_str()),
        ];
        full_query.extend_from_slice(query);

        let url = self.get_url(action, &full_query);
        let response = self.do_get_response(&url).await?;

        let objects = if response.data.is_null() {
            Vec::new()
        } else {
            serde_json::from_value(response.data).map_err(|_| CasdoorError::InvalidData)?
        };

        let count = response.data2.as_f64().ok_or(CasdoorError::InvalidData)? as i32;

        Ok((objects, count))
    }

    /// The encapsulation of the CUD (Create, Update, Delete) operations, the returned bool
    /// tells whether the object was really affected.
    pub(crate) async fn modify_object<T: Serialize>(
        &self,
        action: &str,
        id: &str,
        object: &T,
        columns: &[&str],
    ) -> Result<(Response, bool)> {
        let columns = columns.join(",");

        let mut query = vec![("id", id)];
        if !columns.is_empty() {
            query.push(("columns", columns.as_str()));
        }

        let post_bytes = serde_json::to_vec(object)?;
        let response = self
            .do_post(action, &query, PostBody::Raw(post_bytes))
            .await?;

        let affected = response.data.as_str() == Some("Affected");
        Ok((response, affected))
    }
}

/// `get_admin_id` is [`Client::get_id()`] for the object types that are owned by `admin`
/// instead of by an organization: organization, application, token and LDAP server.
pub(crate) fn get_admin_id(name: &str) -> String {
    get_id(name, "admin")
}

fn get_id(name: &str, default_owner: &str) -> String {
    if name.contains('/') {
        return name.to_string();
    }

    format!("{default_owner}/{name}")
}

/// `get_owner` returns the owner of an object about to be sent to the server: the one set
/// by the caller, falling back to `default_owner` when the caller left it empty. Callers
/// must apply it before building the request ID, so that the ID in the query string and
/// the owner in the request body always agree.
pub(crate) fn get_owner(owner: &str, default_owner: &str) -> String {
    if owner.is_empty() {
        return default_owner.to_string();
    }

    owner.to_string()
}

async fn read_body(response: reqwest::Response) -> Result<Vec<u8>> {
    let status = response.status();
    let bytes = response.bytes().await?;

    if status != StatusCode::OK && status != StatusCode::FORBIDDEN {
        return Err(CasdoorError::HttpStatus {
            status: status.as_u16(),
            body: String::from_utf8_lossy(&bytes).into_owned(),
        });
    }

    Ok(bytes.to_vec())
}

#[cfg(test)]
mod tests {
    use super::*;

    fn client() -> Client {
        Client::new(
            "http://localhost:8000",
            "client-id",
            "client-secret",
            "",
            "built-in",
            "app-built-in",
        )
    }

    #[test]
    fn get_url_escapes_the_query_parameters() {
        assert_eq!(
            client().get_url("get-user", &[("id", "built-in/admin")]),
            "http://localhost:8000/api/get-user?id=built-in%2Fadmin"
        );

        assert_eq!(
            client().get_url("get-global-users", &[]),
            "http://localhost:8000/api/get-global-users?"
        );
    }

    #[test]
    fn get_id_only_adds_the_owner_of_an_unqualified_name() {
        assert_eq!(client().get_id("admin"), "built-in/admin");
        assert_eq!(client().get_id("other-org/alice"), "other-org/alice");
        assert_eq!(get_admin_id("app-built-in"), "admin/app-built-in");
    }

    #[test]
    fn get_owner_falls_back_to_the_default_owner() {
        assert_eq!(get_owner("", "built-in"), "built-in");
        assert_eq!(get_owner("other-org", "built-in"), "other-org");
    }

    #[test]
    fn with_access_token_keeps_the_original_client_unchanged() {
        let client = client();
        let user_client = client.with_access_token("access-token");

        assert_eq!(user_client.access_token.as_deref(), Some("access-token"));
        assert_eq!(client.access_token, None);
    }
}