garrison 0.7.2

Garrison - 面向 Rust 生态的一站式身份认证鉴权框架
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
//! Copyright (c) 2026 Kirky.X. All rights reserved.
//! See LICENSE for full license text.

//! BackendRemote — 远程认证后端实现。
//!
//! 通过 HTTP API 连接远程 Auth Server,将 AuthBackend 方法映射为 REST 请求。
//!
//! # 设计
//!
//! - **reqwest::Client**:支持 TLS/mTLS 配置,连接池复用
//! - **统一 API 包装**:`ApiResponse<T>` 包装所有响应,成功时 `data` 有值,失败时 `error_code` + `message`
//! - **X-API-Key 头**:每个请求携带 API Key 用于服务间认证
//! - **错误映射**:网络错误 → `GarrisonError::Network`,API 错误 → 根据 `error_code` 映射

use crate::error::{GarrisonError, GarrisonResult};
use async_trait::async_trait;
use std::time::Duration;

use super::types::{
    ApiResponse, CheckApiKeyRequest, CheckLoginRequest, CheckPermissionRequest, CheckRoleRequest,
    KickoutRequest, LoginParams, LoginRequest, LogoutRequest, RenewToEquivalentRequest,
    SessionData, SwitchToRequest, TokenInfo,
};
use super::AuthBackend;

/// 远程认证后端,通过 HTTP API 连接远程 Auth Server。
///
/// # 端点映射
///
/// | 方法 | 端点 | 请求体 | 响应 data |
/// |------|------|--------|-----------|
/// | login | POST /api/v1/auth/login | LoginRequest | String |
/// | logout | POST /api/v1/auth/logout | LogoutRequest | () |
/// | check_login | POST /api/v1/auth/check-login | CheckLoginRequest | bool |
/// | check_permission | POST /api/v1/auth/check-permission | CheckPermissionRequest | () |
/// | check_role | POST /api/v1/auth/check-role | CheckRoleRequest | () |
/// | check_safe | POST /api/v1/auth/check-safe | CheckLoginRequest | bool |
/// | check_disable | POST /api/v1/auth/check-disable | CheckLoginRequest | bool |
/// | check_api_key | POST /api/v1/auth/check-api-key | CheckApiKeyRequest | () |
/// | get_token_info | POST /api/v1/auth/get-token-info | CheckLoginRequest | TokenInfo |
/// | get_session | POST /api/v1/auth/get-session | CheckLoginRequest | SessionData |
/// | kickout | POST /api/v1/auth/kickout | KickoutRequest | () |
/// | switch_to | POST /api/v1/auth/switch-to | SwitchToRequest | () |
/// | renew_to_equivalent | POST /api/v1/auth/renew-to-equivalent | RenewToEquivalentRequest | String |
pub struct BackendRemote {
    client: reqwest::Client,
    base_url: String,
    api_key: String,
}

impl BackendRemote {
    /// 创建 BackendRemote 实例。
    ///
    /// # 参数
    /// - `base_url`:Auth Server 基础 URL(如 "https://auth-internal:8443")
    /// - `api_key`:服务间认证 API Key
    /// - `timeout`:请求超时
    pub fn new(
        base_url: impl Into<String>,
        api_key: impl Into<String>,
        timeout: Duration,
    ) -> GarrisonResult<Self> {
        let client = reqwest::Client::builder()
            .timeout(timeout)
            .build()
            .map_err(|e| GarrisonError::Network(format!("backend-http-client-build::{}", e)))?;
        Ok(Self {
            client,
            base_url: base_url.into(),
            api_key: api_key.into(),
        })
    }

    /// 发送 POST 请求并解析响应为 `ApiResponse<T>`。
    ///
    /// 统一处理:
    /// - 请求构建(URL + X-API-Key 头 + JSON body)
    /// - 网络错误映射
    /// - HTTP 状态码检查
    /// - 响应反序列化
    async fn post<Req, T>(&self, path: &str, req: &Req) -> GarrisonResult<ApiResponse<T>>
    where
        Req: serde::Serialize,
        T: serde::de::DeserializeOwned,
    {
        let url = format!("{}{}", self.base_url, path);
        let resp = self
            .client
            .post(&url)
            .header("X-API-Key", &self.api_key)
            .json(req)
            .send()
            .await
            .map_err(|e| GarrisonError::Network(format!("backend-http-request::{}", e)))?;

        let status = resp.status();
        if !status.is_success() {
            let body = resp.text().await.unwrap_or_default();
            return Err(GarrisonError::Network(format!(
                "HTTP {}: {}",
                status.as_u16(),
                body
            )));
        }

        resp.json::<ApiResponse<T>>()
            .await
            .map_err(|e| GarrisonError::Network(format!("backend-response-deser::{}", e)))
    }

    /// 发送 POST 请求,解析 `ApiResponse<T>` 并提取 `data`。
    ///
    /// 用于返回有数据的方法(login → String, check_login → bool 等)。
    async fn post_and_extract<Req, T>(&self, path: &str, req: &Req) -> GarrisonResult<T>
    where
        Req: serde::Serialize,
        T: serde::de::DeserializeOwned,
    {
        let api_resp = self.post::<Req, T>(path, req).await?;
        api_resp
            .into_result()
            .map_err(|(code, _msg)| GarrisonError::Network(format!("backend-api-error::{}", code)))
    }

    /// 发送 POST 请求,检查 `error_code` 判断成功/失败(无 data 提取)。
    ///
    /// 用于返回 `()` 的方法(logout, check_permission, check_role 等)。
    /// `ApiResponse<()>` 的 `data` 字段在 JSON 中为 `null`,无法通过 `into_result` 区分成功/失败,
    /// 因此直接检查 `error_code` 是否存在。
    async fn post_unit<Req>(&self, path: &str, req: &Req) -> GarrisonResult<()>
    where
        Req: serde::Serialize,
    {
        let api_resp = self.post::<Req, ()>(path, req).await?;
        if let Some(code) = api_resp.error_code {
            return Err(GarrisonError::Network(format!(
                "backend-api-error::{}",
                code
            )));
        }
        Ok(())
    }
}

#[async_trait]
impl AuthBackend for BackendRemote {
    async fn login(&self, login_id: &str, params: &LoginParams) -> GarrisonResult<String> {
        let req = LoginRequest {
            login_id: login_id.to_string(),
            params: params.clone(),
        };
        self.post_and_extract("/api/v1/auth/login", &req).await
    }

    async fn logout(&self, token: &str) -> GarrisonResult<()> {
        let req = LogoutRequest {
            token: token.to_string(),
        };
        self.post_unit("/api/v1/auth/logout", &req).await
    }

    async fn check_login(&self, token: &str) -> GarrisonResult<bool> {
        let req = CheckLoginRequest {
            token: token.to_string(),
        };
        self.post_and_extract("/api/v1/auth/check-login", &req)
            .await
    }

    async fn check_permission(&self, token: &str, permission: &str) -> GarrisonResult<()> {
        let req = CheckPermissionRequest {
            token: token.to_string(),
            permission: permission.to_string(),
        };
        self.post_unit("/api/v1/auth/check-permission", &req).await
    }

    async fn check_role(&self, token: &str, role: &str) -> GarrisonResult<()> {
        let req = CheckRoleRequest {
            token: token.to_string(),
            role: role.to_string(),
        };
        self.post_unit("/api/v1/auth/check-role", &req).await
    }

    async fn check_safe(&self, token: &str) -> GarrisonResult<bool> {
        let req = CheckLoginRequest {
            token: token.to_string(),
        };
        self.post_and_extract("/api/v1/auth/check-safe", &req).await
    }

    async fn check_disable(&self, token: &str) -> GarrisonResult<bool> {
        let req = CheckLoginRequest {
            token: token.to_string(),
        };
        self.post_and_extract("/api/v1/auth/check-disable", &req)
            .await
    }

    async fn check_api_key(&self, api_key: &str, namespace: &str) -> GarrisonResult<()> {
        let req = CheckApiKeyRequest {
            api_key: api_key.to_string(),
            namespace: namespace.to_string(),
        };
        self.post_unit("/api/v1/auth/check-api-key", &req).await
    }

    async fn get_token_info(&self, token: &str) -> GarrisonResult<TokenInfo> {
        let req = CheckLoginRequest {
            token: token.to_string(),
        };
        self.post_and_extract("/api/v1/auth/get-token-info", &req)
            .await
    }

    async fn get_session(&self, token: &str) -> GarrisonResult<SessionData> {
        let req = CheckLoginRequest {
            token: token.to_string(),
        };
        self.post_and_extract("/api/v1/auth/get-session", &req)
            .await
    }

    async fn kickout(&self, login_id: &str) -> GarrisonResult<()> {
        let req = KickoutRequest {
            login_id: login_id.to_string(),
        };
        self.post_unit("/api/v1/auth/kickout", &req).await
    }

    async fn switch_to(&self, token: &str, target_login_id: &str) -> GarrisonResult<()> {
        let req = SwitchToRequest {
            token: token.to_string(),
            target_login_id: target_login_id.to_string(),
        };
        self.post_unit("/api/v1/auth/switch-to", &req).await
    }

    async fn renew_to_equivalent(&self, token: &str) -> GarrisonResult<String> {
        let req = RenewToEquivalentRequest {
            token: token.to_string(),
        };
        self.post_and_extract("/api/v1/auth/renew-to-equivalent", &req)
            .await
    }
}

/// BackendRemote 构建器,支持 mTLS 客户端证书配置。
///
/// # 示例
///
/// ```ignore
/// use garrison::backend::BackendRemoteBuilder;
/// use std::time::Duration;
///
/// let remote = BackendRemoteBuilder::new("https://auth:8443", "api-key")
///     .with_timeout(Duration::from_secs(10))
///     .with_client_cert(cert_pem, key_pem)
///     .build()?;
/// ```
pub struct BackendRemoteBuilder {
    base_url: String,
    api_key: String,
    timeout: Duration,
    client_cert: Option<Vec<u8>>,
    client_key: Option<Vec<u8>>,
    ca_cert: Option<Vec<u8>>,
}

impl BackendRemoteBuilder {
    /// 创建构建器实例。
    pub fn new(base_url: impl Into<String>, api_key: impl Into<String>) -> Self {
        Self {
            base_url: base_url.into(),
            api_key: api_key.into(),
            timeout: Duration::from_secs(30),
            client_cert: None,
            client_key: None,
            ca_cert: None,
        }
    }

    /// 设置请求超时(默认 30 秒)。
    pub fn with_timeout(mut self, timeout: Duration) -> Self {
        self.timeout = timeout;
        self
    }

    /// 设置 mTLS 客户端证书(PEM 格式)。
    pub fn with_client_cert(mut self, cert_pem: Vec<u8>, key_pem: Vec<u8>) -> Self {
        self.client_cert = Some(cert_pem);
        self.client_key = Some(key_pem);
        self
    }

    /// 设置自定义 CA 证书(PEM 格式),用于自签名服务器证书。
    pub fn with_ca_cert(mut self, ca_pem: Vec<u8>) -> Self {
        self.ca_cert = Some(ca_pem);
        self
    }

    /// 构建 BackendRemote 实例。
    pub fn build(self) -> GarrisonResult<BackendRemote> {
        let mut builder = reqwest::Client::builder().timeout(self.timeout);

        // 加载 CA 证书(用于自签名服务器)
        if let Some(ca_pem) = self.ca_cert {
            let cert = reqwest::Certificate::from_pem(&ca_pem)
                .map_err(|e| GarrisonError::Network(format!("backend-ca-load::{}", e)))?;
            builder = builder.add_root_certificate(cert);
        }

        // 加载 mTLS 客户端证书
        if let (Some(cert_pem), Some(key_pem)) = (self.client_cert, self.client_key) {
            // reqwest::Identity::from_pem 接受包含证书+私钥的 PEM
            let mut combined = cert_pem;
            combined.extend_from_slice(&key_pem);
            let identity = reqwest::Identity::from_pem(&combined)
                .map_err(|e| GarrisonError::Network(format!("backend-client-cert-load::{}", e)))?;
            builder = builder.identity(identity);
        }

        let client = builder
            .build()
            .map_err(|e| GarrisonError::Network(format!("backend-http-client-build::{}", e)))?;

        Ok(BackendRemote {
            client,
            base_url: self.base_url,
            api_key: self.api_key,
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::Arc;
    use wiremock::matchers::{header, method, path};
    use wiremock::{Mock, MockServer, ResponseTemplate};

    /// 启动 mock server 并创建 BackendRemote 指向它。
    async fn setup_remote() -> (MockServer, BackendRemote) {
        let server = MockServer::start().await;
        let remote =
            BackendRemote::new(server.uri(), "test-api-key", Duration::from_secs(5)).unwrap();
        (server, remote)
    }

    #[tokio::test]
    async fn test_check_login_returns_true() {
        let (server, remote) = setup_remote().await;
        Mock::given(method("POST"))
            .and(path("/api/v1/auth/check-login"))
            .and(header("X-API-Key", "test-api-key"))
            .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
                "data": true,
                "error_code": null,
                "message": null
            })))
            .mount(&server)
            .await;

        let result = remote.check_login("valid-token").await.unwrap();
        assert!(result);
    }

    #[tokio::test]
    async fn test_check_login_returns_false() {
        let (server, remote) = setup_remote().await;
        Mock::given(method("POST"))
            .and(path("/api/v1/auth/check-login"))
            .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
                "data": false,
                "error_code": null,
                "message": null
            })))
            .mount(&server)
            .await;

        let result = remote.check_login("invalid-token").await.unwrap();
        assert!(!result);
    }

    #[tokio::test]
    async fn test_login_returns_token() {
        let (server, remote) = setup_remote().await;
        Mock::given(method("POST"))
            .and(path("/api/v1/auth/login"))
            .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
                "data": "generated-token-123",
                "error_code": null,
                "message": null
            })))
            .mount(&server)
            .await;

        let token = remote
            .login("user1", &LoginParams::default())
            .await
            .unwrap();
        assert_eq!(token, "generated-token-123");
    }

    #[tokio::test]
    async fn test_logout_succeeds() {
        let (server, remote) = setup_remote().await;
        Mock::given(method("POST"))
            .and(path("/api/v1/auth/logout"))
            .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
                "data": null,
                "error_code": null,
                "message": null
            })))
            .mount(&server)
            .await;

        remote.logout("some-token").await.unwrap();
    }

    #[tokio::test]
    async fn test_check_permission_succeeds() {
        let (server, remote) = setup_remote().await;
        Mock::given(method("POST"))
            .and(path("/api/v1/auth/check-permission"))
            .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
                "data": null,
                "error_code": null,
                "message": null
            })))
            .mount(&server)
            .await;

        remote.check_permission("token", "user:read").await.unwrap();
    }

    #[tokio::test]
    async fn test_check_permission_api_error() {
        let (server, remote) = setup_remote().await;
        Mock::given(method("POST"))
            .and(path("/api/v1/auth/check-permission"))
            .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
                "data": null,
                "error_code": "NOT_PERMISSION",
                "message": "无权限"
            })))
            .mount(&server)
            .await;

        let result = remote.check_permission("token", "user:read").await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_check_role_succeeds() {
        let (server, remote) = setup_remote().await;
        Mock::given(method("POST"))
            .and(path("/api/v1/auth/check-role"))
            .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
                "data": null,
                "error_code": null,
                "message": null
            })))
            .mount(&server)
            .await;

        remote.check_role("token", "admin").await.unwrap();
    }

    #[tokio::test]
    async fn test_check_safe_returns_true() {
        let (server, remote) = setup_remote().await;
        Mock::given(method("POST"))
            .and(path("/api/v1/auth/check-safe"))
            .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
                "data": true,
                "error_code": null,
                "message": null
            })))
            .mount(&server)
            .await;

        assert!(remote.check_safe("token").await.unwrap());
    }

    #[tokio::test]
    async fn test_check_disable_returns_false() {
        let (server, remote) = setup_remote().await;
        Mock::given(method("POST"))
            .and(path("/api/v1/auth/check-disable"))
            .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
                "data": false,
                "error_code": null,
                "message": null
            })))
            .mount(&server)
            .await;

        assert!(!remote.check_disable("token").await.unwrap());
    }

    #[tokio::test]
    async fn test_check_api_key_succeeds() {
        let (server, remote) = setup_remote().await;
        Mock::given(method("POST"))
            .and(path("/api/v1/auth/check-api-key"))
            .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
                "data": null,
                "error_code": null,
                "message": null
            })))
            .mount(&server)
            .await;

        remote.check_api_key("api-key", "default").await.unwrap();
    }

    #[tokio::test]
    async fn test_get_token_info() {
        let (server, remote) = setup_remote().await;
        Mock::given(method("POST"))
            .and(path("/api/v1/auth/get-token-info"))
            .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
                "data": {
                    "token": "test-token",
                    "created_at": 1000,
                    "last_active_at": 2000
                },
                "error_code": null,
                "message": null
            })))
            .mount(&server)
            .await;

        let info = remote.get_token_info("test-token").await.unwrap();
        assert_eq!(info.token, "test-token");
        assert_eq!(info.created_at, 1000);
        assert_eq!(info.last_active_at, 2000);
    }

    #[tokio::test]
    async fn test_get_session() {
        let (server, remote) = setup_remote().await;
        Mock::given(method("POST"))
            .and(path("/api/v1/auth/get-session"))
            .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
                "data": {
                    "token": "test-token",
                    "login_id": "user1",
                    "created_at": 1000,
                    "last_active_at": 2000,
                    "attrs": {},
                    "device": null,
                    "ip": null,
                    "user_agent": null,
                    "safe_services": {}
                },
                "error_code": null,
                "message": null
            })))
            .mount(&server)
            .await;

        let session = remote.get_session("test-token").await.unwrap();
        assert_eq!(session.token, "test-token");
        assert_eq!(session.login_id, "user1");
    }

    #[tokio::test]
    async fn test_kickout_succeeds() {
        let (server, remote) = setup_remote().await;
        Mock::given(method("POST"))
            .and(path("/api/v1/auth/kickout"))
            .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
                "data": null,
                "error_code": null,
                "message": null
            })))
            .mount(&server)
            .await;

        remote.kickout("user1").await.unwrap();
    }

    #[tokio::test]
    async fn test_switch_to_succeeds() {
        let (server, remote) = setup_remote().await;
        Mock::given(method("POST"))
            .and(path("/api/v1/auth/switch-to"))
            .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
                "data": null,
                "error_code": null,
                "message": null
            })))
            .mount(&server)
            .await;

        remote.switch_to("token", "user2").await.unwrap();
    }

    #[tokio::test]
    async fn test_renew_to_equivalent_returns_new_token() {
        let (server, remote) = setup_remote().await;
        Mock::given(method("POST"))
            .and(path("/api/v1/auth/renew-to-equivalent"))
            .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
                "data": "new-token-456",
                "error_code": null,
                "message": null
            })))
            .mount(&server)
            .await;

        let new_token = remote.renew_to_equivalent("old-token").await.unwrap();
        assert_eq!(new_token, "new-token-456");
    }

    // ========================================================================
    // 网络错误处理测试(T095)
    // ========================================================================

    #[tokio::test]
    async fn test_network_error_connection_refused() {
        // 指向不存在的端口,触发连接失败
        let remote =
            BackendRemote::new("http://127.0.0.1:1", "api-key", Duration::from_secs(1)).unwrap();

        let result = remote.check_login("token").await;
        assert!(result.is_err());
        match result.unwrap_err() {
            GarrisonError::Network(_) => {},
            e => panic!("期望 Network 错误,实际: {:?}", e),
        }
    }

    #[tokio::test]
    async fn test_http_500_error() {
        let (server, remote) = setup_remote().await;
        Mock::given(method("POST"))
            .and(path("/api/v1/auth/check-login"))
            .respond_with(ResponseTemplate::new(500))
            .mount(&server)
            .await;

        let result = remote.check_login("token").await;
        assert!(result.is_err());
        match result.unwrap_err() {
            GarrisonError::Network(_) => {},
            e => panic!("期望 Network 错误,实际: {:?}", e),
        }
    }

    #[tokio::test]
    async fn test_api_error_response() {
        let (server, remote) = setup_remote().await;
        Mock::given(method("POST"))
            .and(path("/api/v1/auth/check-login"))
            .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
                "data": null,
                "error_code": "INVALID_TOKEN",
                "message": "token 已过期"
            })))
            .mount(&server)
            .await;

        let result = remote.check_login("token").await;
        assert!(result.is_err());
    }

    // ========================================================================
    // Builder 测试(T098)
    // ========================================================================

    #[tokio::test]
    async fn test_builder_basic() {
        let (server, _remote) = setup_remote().await;
        let remote = BackendRemoteBuilder::new(server.uri(), "builder-key")
            .with_timeout(Duration::from_secs(10))
            .build()
            .unwrap();

        Mock::given(method("POST"))
            .and(path("/api/v1/auth/check-login"))
            .and(header("X-API-Key", "builder-key"))
            .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
                "data": true,
                "error_code": null,
                "message": null
            })))
            .mount(&server)
            .await;

        assert!(remote.check_login("token").await.unwrap());
    }

    #[tokio::test]
    async fn test_builder_with_ca_cert() {
        // 生成自签名证书用于测试
        let cert = rcgen::generate_simple_self_signed(vec!["localhost".to_string()]).unwrap();
        let ca_pem = cert.cert.der().clone();
        let _ca_cert = reqwest::Certificate::from_der(&ca_pem).unwrap();

        // 使用 CA 证书构建(验证不 panic)
        let result = BackendRemoteBuilder::new("https://localhost:8443", "key")
            .with_ca_cert(cert.cert.pem().as_bytes().to_vec())
            .build();
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_dyn_dispatch_with_backend_remote() {
        let (server, remote) = setup_remote().await;
        let backend: Arc<dyn AuthBackend> = Arc::new(remote);

        Mock::given(method("POST"))
            .and(path("/api/v1/auth/login"))
            .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
                "data": "dyn-token",
                "error_code": null,
                "message": null
            })))
            .mount(&server)
            .await;

        let token = backend
            .login("user", &LoginParams::default())
            .await
            .unwrap();
        assert_eq!(token, "dyn-token");
    }
}