Skip to main content

casdoor_rust_sdk/
provider.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 std::collections::HashMap;
16
17use serde::{Deserialize, Serialize};
18
19use crate::client::{get_owner, Client, Response};
20use crate::error::Result;
21use crate::serde_util::null_to_default;
22
23/// Provider has the same definition as
24/// <https://github.com/casdoor/casdoor/blob/master/object/provider.go>.
25#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
26#[serde(rename_all = "camelCase", default)]
27pub struct Provider {
28    pub owner: String,
29    pub name: String,
30    pub created_time: String,
31
32    pub display_name: String,
33    pub category: String,
34    pub r#type: String,
35    pub sub_type: String,
36    pub method: String,
37    pub client_id: String,
38    pub client_secret: String,
39    pub client_id2: String,
40    pub client_secret2: String,
41    pub cert: String,
42    pub custom_auth_url: String,
43    pub custom_token_url: String,
44    pub custom_user_info_url: String,
45    pub custom_logout_url: String,
46    pub custom_logo: String,
47    pub scopes: String,
48    #[serde(deserialize_with = "null_to_default")]
49    pub user_mapping: HashMap<String, String>,
50    #[serde(deserialize_with = "null_to_default")]
51    pub http_headers: HashMap<String, String>,
52
53    pub host: String,
54    pub port: i32,
55    /// Deprecated: use `ssl_mode` instead. If the provider type is WeChat, `disable_ssl`
56    /// means `enableQRCode`, if the type is Google, it means "sync phone number".
57    pub disable_ssl: bool,
58    /// `"Auto"` (an empty value means `"Auto"`), `"Enable"` or `"Disable"`.
59    pub ssl_mode: String,
60    pub title: String,
61    /// If the provider type is WeChat, `content` is the QR code string in Base64.
62    pub content: String,
63    pub receiver: String,
64
65    pub region_id: String,
66    pub sign_name: String,
67    pub template_code: String,
68    pub app_id: String,
69
70    pub endpoint: String,
71    pub intranet_endpoint: String,
72    pub domain: String,
73    pub bucket: String,
74    pub path_prefix: String,
75
76    pub metadata: String,
77    pub id_p: String,
78    pub issuer_url: String,
79    pub enable_sign_authn_request: bool,
80    pub email_regex: String,
81
82    pub provider_url: String,
83    pub enable_proxy: bool,
84    pub enable_pkce: bool,
85
86    pub state: String,
87}
88
89impl Client {
90    /// Get the providers of the client's organization.
91    pub async fn get_providers(&self) -> Result<Vec<Provider>> {
92        self.get_list(
93            "get-providers",
94            &[("owner", &self.config.organization_name)],
95        )
96        .await
97    }
98
99    /// Get one provider by name.
100    pub async fn get_provider(&self, name: &str) -> Result<Option<Provider>> {
101        self.get_object("get-provider", &[("id", &self.get_id(name))])
102            .await
103    }
104
105    /// Get one page of the providers of the client's organization, together with the total
106    /// number of the providers.
107    pub async fn get_pagination_providers(
108        &self,
109        p: i32,
110        page_size: i32,
111        query: &[(&str, &str)],
112    ) -> Result<(Vec<Provider>, i32)> {
113        self.get_pagination(
114            "get-providers",
115            &self.config.organization_name,
116            p,
117            page_size,
118            query,
119        )
120        .await
121    }
122
123    /// Add a provider.
124    pub async fn add_provider(&self, provider: &Provider) -> Result<bool> {
125        Ok(self.modify_provider("add-provider", provider, &[]).await?.1)
126    }
127
128    /// Update a provider.
129    pub async fn update_provider(&self, provider: &Provider) -> Result<bool> {
130        Ok(self
131            .modify_provider("update-provider", provider, &[])
132            .await?
133            .1)
134    }
135
136    /// Delete a provider.
137    pub async fn delete_provider(&self, provider: &Provider) -> Result<bool> {
138        Ok(self
139            .modify_provider("delete-provider", provider, &[])
140            .await?
141            .1)
142    }
143
144    async fn modify_provider(
145        &self,
146        action: &str,
147        provider: &Provider,
148        columns: &[&str],
149    ) -> Result<(Response, bool)> {
150        let mut provider = provider.clone();
151        provider.owner = get_owner(&provider.owner, &self.config.organization_name);
152
153        let id = format!("{}/{}", provider.owner, provider.name);
154        self.modify_object(action, &id, &provider, columns).await
155    }
156}