use serde_derive::Deserialize;
use std::{fs::File, io::Read};
#[derive(Debug, Clone, Deserialize)]
pub struct CasdoorConfig {
pub(crate) endpoint: String,
pub(crate) client_id: String,
pub(crate) client_secret: String,
pub(crate) jwt_pub_key: String,
pub(crate) org_name: String,
pub(crate) app_name: Option<String>,
}
impl CasdoorConfig {
#[allow(dead_code)]
pub fn new(
endpoint: String,
client_id: String,
client_secret: String,
jwt_pub_key: String,
org_name: String,
app_name: Option<String>,
) -> Self {
CasdoorConfig {
endpoint,
client_id,
client_secret,
jwt_pub_key: Self::replace_cert_to_pub_key(jwt_pub_key),
org_name,
app_name,
}
}
#[allow(dead_code)]
pub fn from_toml(path: &str) -> Result<Self, Box<dyn std::error::Error>> {
let mut file = File::open(path)?;
let mut content = String::new();
file.read_to_string(&mut content)?;
let mut conf: CasdoorConfig = toml::from_str(&content)?;
conf.jwt_pub_key = Self::replace_cert_to_pub_key(conf.jwt_pub_key);
Ok(conf)
}
fn replace_cert_to_pub_key(jwt_pub_key: String) -> String {
jwt_pub_key.replace("CERTIFICATE", "PUBLIC KEY")
}
}