openlark_auth/services/auth_service.rs
1//! 企业认证服务
2//!
3//! 提供企业应用的认证功能,包括自建应用和应用商店应用的访问令牌管理。
4
5use crate::auth::auth::v3::AuthServiceV3;
6use openlark_core::config::Config;
7
8/// 企业认证服务
9///
10/// 提供飞书开放平台的企业级认证功能,支持:
11/// - 企业自建应用认证
12/// - 应用商店应用认证
13/// - 访问令牌获取和管理
14/// - 应用票据处理
15///
16/// # 示例
17///
18/// ```rust
19/// use openlark_auth::AuthService;
20/// use openlark_core::config::Config;
21///
22/// let config = Config::builder()
23/// .app_id("your_app_id")
24/// .app_secret("your_app_secret")
25/// .base_url("https://open.feishu.cn")
26/// .build();
27/// let auth_service = AuthService::new(config);
28///
29/// // 构建「自建应用获取 app_access_token」请求(这里只演示构建,不发送网络请求)
30/// let _builder = auth_service
31/// .v3()
32/// .app_access_token_internal()
33/// .app_id("your_app_id")
34/// .app_secret("your_app_secret");
35///
36/// // 真正发送请求需要配合 `openlark_core::http::Transport` 进行请求执行。
37/// ```
38#[derive(Debug, Clone)]
39pub struct AuthService {
40 config: Config,
41}
42
43impl AuthService {
44 /// 创建新的企业认证服务实例
45 ///
46 /// # 参数
47 ///
48 /// * `config` - 飞书开放平台配置
49 ///
50 /// # 示例
51 ///
52 /// ```rust
53 /// use openlark_auth::AuthService;
54 /// use openlark_core::config::Config;
55 ///
56 /// let config = Config::default();
57 /// let auth_service = AuthService::new(config);
58 /// ```
59 pub fn new(config: Config) -> Self {
60 Self { config }
61 }
62
63 /// 获取配置的引用
64 pub fn config(&self) -> &Config {
65 &self.config
66 }
67
68 /// 获取v3版本的认证服务
69 ///
70 /// v3版本包含以下API:
71 /// - `app_access_token`: 商店应用获取app_access_token
72 /// - `app_access_token_internal`: 自建应用获取app_access_token
73 /// - `tenant_access_token`: 商店应用获取tenant_access_token
74 /// - `tenant_access_token_internal`: 自建应用获取tenant_access_token
75 /// - `app_ticket_resend`: 重新获取app_ticket
76 ///
77 /// # 示例
78 ///
79 /// ```rust
80 /// use openlark_auth::AuthService;
81 /// use openlark_core::config::Config;
82 ///
83 /// let config = Config::default();
84 /// let auth_service = AuthService::new(config);
85 /// let v3_service = auth_service.v3();
86 /// ```
87 pub fn v3(&self) -> AuthServiceV3 {
88 AuthServiceV3::new(self.config.clone())
89 }
90}
91
92impl Default for AuthService {
93 fn default() -> Self {
94 Self::new(Config::default())
95 }
96}
97
98#[cfg(test)]
99#[allow(unused_imports)]
100mod tests {
101 use super::*;
102
103 #[test]
104 fn test_auth_service_creation() {
105 let config = Config::default();
106 let service = AuthService::new(config.clone());
107
108 assert_eq!(service.config().app_id(), config.app_id());
109 }
110
111 #[test]
112 fn test_v3_service() {
113 let config = Config::default();
114 let service = AuthService::new(config);
115 let v3_service = service.v3();
116
117 // 验证返回的是有效的服务实例
118 let _ = format!("{v3_service:?}");
119 }
120
121 #[test]
122 fn test_default() {
123 let service = AuthService::default();
124 let _ = format!("{service:?}");
125 }
126}