casdoor_rust_sdk/entity/
config.rs1use serde_derive::Deserialize;
16use std::{fs::File, io::Read};
17
18#[derive(Debug, Clone, Deserialize)]
20pub struct CasdoorConfig {
21 pub(crate) endpoint: String,
22 pub(crate) client_id: String,
23 pub(crate) client_secret: String,
24 pub(crate) certificate: String,
25 pub(crate) org_name: String,
26 pub(crate) app_name: Option<String>,
27}
28
29impl CasdoorConfig {
30 #[allow(dead_code)]
32 pub fn new(
33 endpoint: String,
34 client_id: String,
35 client_secret: String,
36 certificate: String,
37 org_name: String,
38 app_name: Option<String>,
39 ) -> Self {
40 CasdoorConfig {
41 endpoint,
42 client_id,
43 client_secret,
44 certificate: Self::replace_cert_to_pub_key(certificate),
45 org_name,
46 app_name,
47 }
48 }
49
50 #[allow(dead_code)]
52 pub fn from_toml(path: &str) -> Result<Self, Box<dyn std::error::Error>> {
53 let mut file = File::open(path)?;
55 let mut content = String::new();
56 file.read_to_string(&mut content)?;
57
58 let mut conf: CasdoorConfig = toml::from_str(&content)?;
59
60 conf.certificate = Self::replace_cert_to_pub_key(conf.certificate);
62
63 Ok(conf)
64 }
65
66 fn replace_cert_to_pub_key(certificate: String) -> String {
67 certificate.replace("CERTIFICATE", "PUBLIC KEY")
68 }
69}