casdoor_sdk_rust/
config.rs

1use std::{fs::File, io::Read};
2
3use serde::{Deserialize, Serialize};
4use openssl::{error::ErrorStack, pkey::{PKey, Public}, x509::X509};
5
6/// Config is the core configuration.
7#[derive(Debug, Clone, Serialize, Deserialize)]
8pub struct Config {
9    /// Casdoor Server Url, such as `http://localhost:8000`
10    pub endpoint: String,
11    /// Client ID for the Casdoor application
12    pub client_id: String,
13    /// Client secret for the Casdoor application
14    pub client_secret: String,
15    /// x509 certificate content of Application.cert
16    pub certificate: String,
17    /// The name for the Casdoor organization
18    pub org_name: String,
19    /// The name for the Casdoor application
20    pub app_name: Option<String>,
21}
22
23impl Config {
24    /// Create a new Config.
25    pub fn new(
26        endpoint: String,
27        client_id: String,
28        client_secret: String,
29        certificate: String,
30        org_name: String,
31        app_name: Option<String>,
32    ) -> Self {
33        Config {
34            endpoint,
35            client_id,
36            client_secret,
37            certificate,
38            org_name,
39            app_name
40        }
41    }
42
43    /// Create a new Config from a Toml file.
44    pub fn from_toml(path: &str) -> Result<Self, Box<dyn std::error::Error>> {
45        // read path file content
46        let mut file = File::open(path)?;
47        let mut content = String::new();
48        file.read_to_string(&mut content)?;
49
50        let conf: Config = toml::from_str(&content)?;
51
52        Ok(conf)
53    }
54
55    pub fn replace_cert_to_pub_key(&self) -> Result<PKey<Public>, ErrorStack> {
56        let cert_x509 = X509::from_pem(self.certificate.as_bytes())?;
57        cert_x509.public_key()
58    }
59}