Skip to main content

agentlink_sdk/services/
auth_service.rs

1//! Authentication Service
2
3use std::sync::Arc;
4
5use crate::error::SdkResult;
6use crate::http::HttpClient;
7use crate::protocols::auth::{
8    CheckLinkIDResponse, LoginRequest, LoginResponse, SendCodeRequest, SendCodeResponse,
9    SetLinkIDRequest, SetLinkIDResponse,
10};
11
12/// 认证服务
13pub struct AuthService {
14    http: Arc<HttpClient>,
15}
16
17impl AuthService {
18    /// 创建新的认证服务
19    pub fn new(http: Arc<HttpClient>) -> Self {
20        Self { http }
21    }
22
23    /// 邮箱验证码登录
24    ///
25    /// # Arguments
26    /// * `email` - 邮箱地址
27    /// * `code` - 验证码
28    ///
29    /// # Returns
30    /// * `LoginResponse` - 包含 token 和用户信息
31    pub async fn login_with_email_code(&self, email: &str, code: &str) -> SdkResult<LoginResponse> {
32        let request = LoginRequest {
33            identifier: email.to_string(),
34            code: code.to_string(),
35        };
36        self.http.post("/auth/login/email-code", &request).await
37    }
38
39    /// 发送验证码
40    ///
41    /// # Arguments
42    /// * `email` - 邮箱地址
43    /// * `code_type` - 验证码类型 (默认 "email")
44    ///
45    /// # Returns
46    /// * `SendCodeResponse` - 发送结果
47    pub async fn send_verification_code(&self, email: &str) -> SdkResult<SendCodeResponse> {
48        let request = SendCodeRequest {
49            code_type: "email".to_string(),
50            target: email.to_string(),
51        };
52        self.http.post("/auth/send-code", &request).await
53    }
54
55    /// 检查 LinkID 是否可用(需要认证)
56    ///
57    /// # Arguments
58    /// * `linkid` - 要检查的 LinkID
59    ///
60    /// # Returns
61    /// * `CheckLinkIDResponse` - 检查结果
62    pub async fn check_linkid(&self, linkid: &str) -> SdkResult<CheckLinkIDResponse> {
63        let path = format!("/auth/linkid/check?linkid={}", linkid);
64        self.http.get(&path).await
65    }
66
67    /// 设置用户的 LinkID(需要认证)
68    ///
69    /// # Arguments
70    /// * `linkid` - 新的 LinkID
71    ///
72    /// # Returns
73    /// * `SetLinkIDResponse` - 设置结果
74    pub async fn set_linkid(&self, linkid: &str) -> SdkResult<SetLinkIDResponse> {
75        let request = SetLinkIDRequest {
76            linkid: linkid.to_string(),
77        };
78        self.http.post("/auth/linkid/set", &request).await
79    }
80}
81
82