1use crate::{RemoteError, RemoteResult as Result};
2use serde::{Deserialize, Serialize};
3use signer_core::SignerError;
4
5#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq)]
7pub enum AuthCodeStatus {
8 Pending,
9 Fetched,
10 Expired,
11 Completed,
12}
13
14#[derive(Serialize, Deserialize, Debug, Clone)]
15pub struct AuthCodeDetail {
16 pub code: String,
17 pub state: String,
18 pub status: AuthCodeStatus,
19 pub expire_at: i64,
20 pub jwt: Option<String>,
21}
22
23#[derive(Clone, Default, Debug, PartialEq, Serialize, Deserialize)]
24pub struct PostAuthRequest {
25 #[serde(rename = "jwt")]
26 pub jwt: String,
27 #[serde(rename = "state")]
28 pub state: String,
29}
30
31impl PostAuthRequest {
32 pub fn new(jwt: String, state: String) -> PostAuthRequest {
33 PostAuthRequest { jwt, state }
34 }
35}
36
37pub struct SignerRemoteAuth {
38 pub base_url: String,
39}
40
41impl SignerRemoteAuth {
42 pub fn from_url(url: String) -> Self {
43 Self { base_url: url }
44 }
45
46 pub async fn get_auth_detail(&self, target: &str) -> Result<AuthCodeDetail> {
48 let client = reqwest::Client::new();
49
50 let response = client.get(target).send().await?;
51
52 if !response.status().is_success() {
53 return Err(RemoteError::Signer(SignerError::Msg(
54 format!("获取认证详情失败: {}", response.status()),
55 )));
56 }
57
58 let detail: AuthCodeDetail = response.json().await?;
59 Ok(detail)
60 }
61
62 pub async fn post_auth(
63 &self,
64 target: String,
65 state: String,
66 jwt: String,
67 ) -> Result<()> {
68 let request = PostAuthRequest::new(jwt, state);
69
70 let client = reqwest::Client::new();
72
73 let response = client.post(&target).json(&request).send().await?;
74
75 if !response.status().is_success() {
76 return Err(RemoteError::Signer(SignerError::Msg(
77 format!("认证请求失败: {}", response.status()),
78 )));
79 }
80
81 Ok(())
82 }
83}