casdoor_sdk_rust/
config.rs1use std::{fs::File, io::Read};
2
3use serde::{Deserialize, Serialize};
4use openssl::{error::ErrorStack, pkey::{PKey, Public}, x509::X509};
5
6#[derive(Debug, Clone, Serialize, Deserialize)]
8pub struct Config {
9 pub endpoint: String,
11 pub client_id: String,
13 pub client_secret: String,
15 pub certificate: String,
17 pub org_name: String,
19 pub app_name: Option<String>,
21}
22
23impl Config {
24 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 pub fn from_toml(path: &str) -> Result<Self, Box<dyn std::error::Error>> {
45 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}