Skip to main content

casdoor_rust_sdk/
cert.rs

1// Copyright 2026 The Casdoor Authors. All Rights Reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//      http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use serde::{Deserialize, Serialize};
16
17use crate::client::{get_owner, Client, Response};
18use crate::error::Result;
19
20/// Cert has the same definition as
21/// <https://github.com/casdoor/casdoor/blob/master/object/cert.go>.
22#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
23#[serde(rename_all = "camelCase", default)]
24pub struct Cert {
25    pub owner: String,
26    pub name: String,
27    pub created_time: String,
28
29    pub display_name: String,
30    pub scope: String,
31    pub r#type: String,
32    pub crypto_algorithm: String,
33    pub bit_size: i32,
34    pub expire_in_years: i32,
35
36    pub expire_time: String,
37    pub domain_expire_time: String,
38    pub provider: String,
39    pub account: String,
40    pub access_key: String,
41    pub access_secret: String,
42
43    pub certificate: String,
44    pub private_key: String,
45}
46
47impl Client {
48    /// Get the certs shared by all the organizations.
49    pub async fn get_global_certs(&self) -> Result<Vec<Cert>> {
50        self.get_list("get-global-certs", &[]).await
51    }
52
53    /// Get the certs of the client's organization.
54    pub async fn get_certs(&self) -> Result<Vec<Cert>> {
55        self.get_list("get-certs", &[("owner", &self.config.organization_name)])
56            .await
57    }
58
59    /// Get one cert by name.
60    pub async fn get_cert(&self, name: &str) -> Result<Option<Cert>> {
61        self.get_object("get-cert", &[("id", &self.get_id(name))])
62            .await
63    }
64
65    /// Add a cert.
66    pub async fn add_cert(&self, cert: &Cert) -> Result<bool> {
67        Ok(self.modify_cert("add-cert", cert, &[]).await?.1)
68    }
69
70    /// Update a cert.
71    pub async fn update_cert(&self, cert: &Cert) -> Result<bool> {
72        Ok(self.modify_cert("update-cert", cert, &[]).await?.1)
73    }
74
75    /// Delete a cert.
76    pub async fn delete_cert(&self, cert: &Cert) -> Result<bool> {
77        Ok(self.modify_cert("delete-cert", cert, &[]).await?.1)
78    }
79
80    async fn modify_cert(
81        &self,
82        action: &str,
83        cert: &Cert,
84        columns: &[&str],
85    ) -> Result<(Response, bool)> {
86        let mut cert = cert.clone();
87        cert.owner = get_owner(&cert.owner, &self.config.organization_name);
88
89        let id = format!("{}/{}", cert.owner, cert.name);
90        self.modify_object(action, &id, &cert, columns).await
91    }
92}