sz-rust-middleware-facade 0.6.4

中间件层 facade(P3)— auth/sanctum/jwt_blacklist 等 14 个 Tower 中间件 + log 模块
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
//! SSO 中间件 — 本地验签 + 远程校验
//!
//! 对齐 spec.md FR-6 ~ FR-7,design.md §3.2。
//!
//! ## 本地验签(默认)
//!
//! 业务系统与 SSO 认证中心共享 JWT secret,本地 `SsoJwtCodec::decode` 验签,零网络开销。
//!
//! ## 远程校验(feature = "remote-validate")
//!
//! 密钥不共享场景,通过 HTTP 调用 SSO 认证中心 `/sso/validate` 端点校验。

use axum::extract::State;
use axum::http::{Request, StatusCode};
use axum::middleware::Next;
use axum::response::{IntoResponse, Response};
use std::sync::Arc;

use sz_rust_auth_facade::refresh::{
    MemoryRefreshTokenStore, MemoryTokenBlacklist, RefreshTokenStore, RefreshTokenVerifier,
    SsoJwtCodec, TokenBlacklist,
};

// ── AuthenticatedUser ──

/// 认证后的用户信息(注入 request extensions)
#[derive(Debug, Clone)]
pub struct AuthenticatedUser {
    /// 用户 ID
    pub user_id: i64,
    /// 用户名
    pub username: String,
}

// ── SsoMiddlewareConfig ──

/// SSO 中间件配置
pub struct SsoMiddlewareConfig {
    /// JWT 编解码器(本地验签)
    codec: SsoJwtCodec,
    /// Token 黑名单
    blacklist: Arc<dyn TokenBlacklist>,
    /// Token 版本存储
    store: Arc<dyn RefreshTokenStore>,
    /// JWT 签发人
    issuer: String,
    /// 白名单路由(支持 `*` 通配符)
    allow_all_action: Vec<String>,
}

impl SsoMiddlewareConfig {
    /// 创建本地验签配置
    pub fn local(
        secret: impl Into<String>,
        issuer: impl Into<String>,
        blacklist: Arc<dyn TokenBlacklist>,
        store: Arc<dyn RefreshTokenStore>,
        allow_all_action: Vec<String>,
    ) -> Self {
        Self {
            codec: SsoJwtCodec::new(secret),
            blacklist,
            store,
            issuer: issuer.into(),
            allow_all_action,
        }
    }

    /// 创建本地验签配置(内存黑名单 + 内存存储,测试用)
    pub fn local_memory(
        secret: impl Into<String>,
        issuer: impl Into<String>,
        allow_all_action: Vec<String>,
    ) -> Self {
        Self::local(
            secret,
            issuer,
            Arc::new(MemoryTokenBlacklist::new()),
            Arc::new(MemoryRefreshTokenStore::new()),
            allow_all_action,
        )
    }

    /// 检查路由是否在白名单中
    fn is_allowed(&self, path: &str) -> bool {
        for pattern in &self.allow_all_action {
            if pattern == "*" || pattern == path {
                return true;
            }
            if pattern.ends_with('*') && path.starts_with(&pattern[..pattern.len() - 1]) {
                return true;
            }
        }
        false
    }
}

impl std::fmt::Debug for SsoMiddlewareConfig {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("SsoMiddlewareConfig")
            .field("codec", &self.codec)
            .field("issuer", &self.issuer)
            .field("allow_all_action", &self.allow_all_action)
            .finish_non_exhaustive()
    }
}

// ── sso_middleware ──

/// SSO 中间件
///
/// 从 `Authorization: Bearer <token>` 提取 accessToken,
/// 执行本地验签 + 黑名单查询 + 版本校验,通过后注入 `AuthenticatedUser`。
pub async fn sso_middleware(
    State(config): State<Arc<SsoMiddlewareConfig>>,
    req: Request<axum::body::Body>,
    next: Next,
) -> Response {
    let path = req.uri().path().to_string();

    if config.is_allowed(&path) {
        return next.run(req).await;
    }

    let auth_header = match req.headers().get("authorization") {
        Some(v) => v.to_str().unwrap_or(""),
        None => return unauthorized("missing authorization header"),
    };

    let token = match auth_header.strip_prefix("Bearer ") {
        Some(t) => t,
        None => return unauthorized("invalid authorization scheme"),
    };

    let verifier = RefreshTokenVerifier::new(
        config.codec.clone(),
        config.blacklist.clone(),
        config.store.clone(),
        config.issuer.clone(),
    );

    match verifier.verify_access(token).await {
        Ok(claims) => {
            let user_id = claims.user_id.unwrap_or(0);
            let username = claims.sub.clone();
            let mut req = req;
            req.extensions_mut().insert(AuthenticatedUser { user_id, username });
            next.run(req).await
        }
        Err(e) => {
            tracing::warn!(error = %e, "SSO token validation failed");
            unauthorized(&e.to_string())
        }
    }
}

fn unauthorized(msg: &str) -> Response {
    (
        StatusCode::UNAUTHORIZED,
        [("Cache-Control", "no-store"), ("Pragma", "no-cache")],
        format!("{{\"code\":-1,\"msg\":\"{msg}\"}}"),
    )
        .into_response()
}

// ── 远程校验(feature = "remote-validate") ──

/// 连接池配置
#[cfg(feature = "remote-validate")]
#[derive(Debug, Clone)]
pub struct PoolConfig {
    /// 每个主机最大空闲连接数
    pub pool_max_idle_per_host: usize,
    /// 空闲连接超时
    pub pool_idle_timeout: Option<std::time::Duration>,
    /// TCP keepalive
    pub tcp_keepalive: Option<std::time::Duration>,
    /// TCP nodelay
    pub tcp_nodelay: bool,
}

#[cfg(feature = "remote-validate")]
impl Default for PoolConfig {
    fn default() -> Self {
        Self {
            pool_max_idle_per_host: 32,
            pool_idle_timeout: Some(std::time::Duration::from_secs(90)),
            tcp_keepalive: Some(std::time::Duration::from_secs(60)),
            tcp_nodelay: true,
        }
    }
}

/// 远程校验配置
#[cfg(feature = "remote-validate")]
pub struct RemoteValidateConfig {
    /// SSO 认证中心校验端点
    pub endpoint: String,
    /// 超时时间
    pub timeout: std::time::Duration,
    /// HTTP 客户端(单例复用连接池)
    client: reqwest::Client,
    /// 白名单路由
    pub allow_all_action: Vec<String>,
    /// 连接池配置
    pub pool_config: PoolConfig,
}

#[cfg(feature = "remote-validate")]
impl RemoteValidateConfig {
    /// 创建远程校验配置(向后兼容,内部委托 `new_checked().expect()`)
    pub fn new(
        endpoint: impl Into<String>,
        timeout: std::time::Duration,
        allow_all_action: Vec<String>,
    ) -> Self {
        Self::new_checked(endpoint, timeout, allow_all_action, PoolConfig::default())
            .expect("failed to build RemoteValidateConfig")
    }

    /// 创建远程校验配置(失败安全,返回 Result)
    pub fn new_checked(
        endpoint: impl Into<String>,
        timeout: std::time::Duration,
        allow_all_action: Vec<String>,
        pool_config: PoolConfig,
    ) -> Result<Self, sz_rust_auth_facade::refresh::RefreshTokenError> {
        let mut builder = reqwest::Client::builder()
            .timeout(timeout)
            .pool_max_idle_per_host(pool_config.pool_max_idle_per_host)
            .tcp_nodelay(pool_config.tcp_nodelay);

        if let Some(idle_timeout) = pool_config.pool_idle_timeout {
            builder = builder.pool_idle_timeout(idle_timeout);
        }
        if let Some(keepalive) = pool_config.tcp_keepalive {
            builder = builder.tcp_keepalive(keepalive);
        }

        let client = builder
            .build()
            .map_err(|e| sz_rust_auth_facade::refresh::RefreshTokenError::InvalidConfig(e.to_string()))?;

        Ok(Self {
            endpoint: endpoint.into(),
            timeout,
            client,
            allow_all_action,
            pool_config,
        })
    }

    /// 创建远程校验配置(失败回退默认 Client + warn 日志)
    pub fn new_or_default(
        endpoint: impl Into<String>,
        timeout: std::time::Duration,
        allow_all_action: Vec<String>,
        pool_config: PoolConfig,
    ) -> Self {
        match Self::new_checked(endpoint, timeout, allow_all_action, pool_config.clone()) {
            Ok(config) => config,
            Err(e) => {
                tracing::warn!(error = %e, "failed to build reqwest client, falling back to default");
                let client = reqwest::Client::new();
                Self {
                    endpoint: String::new(),
                    timeout,
                    client,
                    allow_all_action: Vec::new(),
                    pool_config,
                }
            }
        }
    }

    /// 从外部传入预配置的 reqwest::Client 创建配置
    pub fn from_client(
        endpoint: impl Into<String>,
        timeout: std::time::Duration,
        allow_all_action: Vec<String>,
        client: reqwest::Client,
    ) -> Self {
        Self {
            endpoint: endpoint.into(),
            timeout,
            client,
            allow_all_action,
            pool_config: PoolConfig::default(),
        }
    }

    /// Builder 模式创建配置
    pub fn builder(endpoint: impl Into<String>) -> RemoteValidateConfigBuilder {
        RemoteValidateConfigBuilder {
            endpoint: endpoint.into(),
            timeout: std::time::Duration::from_secs(10),
            allow_all_action: Vec::new(),
            pool_config: PoolConfig::default(),
        }
    }

    /// 检查路由是否在白名单中
    fn is_allowed(&self, path: &str) -> bool {
        for pattern in &self.allow_all_action {
            if pattern == "*" || pattern == path {
                return true;
            }
            if pattern.ends_with('*') && path.starts_with(&pattern[..pattern.len() - 1]) {
                return true;
            }
        }
        false
    }
}

/// 远程校验配置 Builder
#[cfg(feature = "remote-validate")]
pub struct RemoteValidateConfigBuilder {
    endpoint: String,
    timeout: std::time::Duration,
    allow_all_action: Vec<String>,
    pool_config: PoolConfig,
}

#[cfg(feature = "remote-validate")]
impl RemoteValidateConfigBuilder {
    pub fn timeout(mut self, timeout: std::time::Duration) -> Self {
        self.timeout = timeout;
        self
    }

    pub fn allow_all_action(mut self, actions: Vec<String>) -> Self {
        self.allow_all_action = actions;
        self
    }

    pub fn pool_config(mut self, pool_config: PoolConfig) -> Self {
        self.pool_config = pool_config;
        self
    }

    pub fn build(self) -> Result<RemoteValidateConfig, sz_rust_auth_facade::refresh::RefreshTokenError> {
        RemoteValidateConfig::new_checked(
            self.endpoint,
            self.timeout,
            self.allow_all_action,
            self.pool_config,
        )
    }
}

/// 远程校验中间件
#[cfg(feature = "remote-validate")]
pub async fn sso_middleware_remote(
    State(config): State<Arc<RemoteValidateConfig>>,
    req: Request<axum::body::Body>,
    next: Next,
) -> Response {
    let path = req.uri().path().to_string();

    if config.is_allowed(&path) {
        return next.run(req).await;
    }

    let auth_header = match req.headers().get("authorization") {
        Some(v) => v.to_str().unwrap_or(""),
        None => return unauthorized("missing authorization header"),
    };

    let token = match auth_header.strip_prefix("Bearer ") {
        Some(t) => t,
        None => return unauthorized("invalid authorization scheme"),
    };

    let validate_url = format!("{}?token={}", config.endpoint, token);
    match config.client.get(&validate_url).send().await {
        Ok(resp) if resp.status().is_success() => {
            match resp.json::<serde_json::Value>().await {
                Ok(json) => {
                    let data = json.get("data").cloned().unwrap_or_default();
                    let user_id = data.get("user_id").and_then(|v| v.as_i64()).unwrap_or(0);
                    let mut req = req;
                    req.extensions_mut().insert(AuthenticatedUser {
                        user_id,
                        username: String::new(),
                    });
                    next.run(req).await
                }
                Err(_) => service_unavailable(),
            }
        }
        Ok(resp) if resp.status() == StatusCode::UNAUTHORIZED => {
            unauthorized("token invalid or expired")
        }
        _ => service_unavailable(),
    }
}

#[cfg(feature = "remote-validate")]
fn service_unavailable() -> Response {
    (
        StatusCode::SERVICE_UNAVAILABLE,
        [("Cache-Control", "no-store"), ("Pragma", "no-cache")],
        r#"{"code":-1,"msg":"认证服务暂时不可用"}"#,
    )
        .into_response()
}

#[cfg(test)]
mod tests {
    use super::*;
    use axum::body::Body;
    use axum::http::Request;
    use axum::routing::get;
    use axum::Router;
    use sz_rust_auth_facade::refresh::{
        RefreshTokenConfig, RefreshTokenIssuer,
    };
    use tower::ServiceExt;

    async fn handler(req: Request<Body>) -> String {
        let user = req.extensions().get::<AuthenticatedUser>();
        match user {
            Some(u) => format!("user_id={}, username={}", u.user_id, u.username),
            None => "no user".to_string(),
        }
    }

    fn make_app() -> (Router, RefreshTokenIssuer) {
        let codec = SsoJwtCodec::new("test-secret");
        let blacklist: Arc<dyn TokenBlacklist> = Arc::new(MemoryTokenBlacklist::new());
        let store: Arc<dyn RefreshTokenStore> = Arc::new(MemoryRefreshTokenStore::new());
        let config = RefreshTokenConfig::default();
        let issuer = RefreshTokenIssuer::new(codec.clone(), blacklist.clone(), store.clone(), config.clone());
        let mw_config = Arc::new(SsoMiddlewareConfig::local(
            "test-secret",
            config.issuer.clone(),
            blacklist,
            store,
            vec!["/public/*".to_string()],
        ));
        let app = Router::new()
            .route("/protected", get(handler))
            .route("/public/health", get(handler))
            .layer(axum::middleware::from_fn_with_state(mw_config, sso_middleware));
        (app, issuer)
    }

    #[tokio::test]
    async fn test_middleware_allows_whitelist() {
        let (app, _) = make_app();
        let resp = app
            .oneshot(Request::builder().uri("/public/health").body(Body::empty()).unwrap())
            .await
            .unwrap();
        assert_eq!(resp.status(), StatusCode::OK);
    }

    #[tokio::test]
    async fn test_middleware_rejects_missing_token() {
        let (app, _) = make_app();
        let resp = app
            .oneshot(Request::builder().uri("/protected").body(Body::empty()).unwrap())
            .await
            .unwrap();
        assert_eq!(resp.status(), StatusCode::UNAUTHORIZED);
    }

    #[tokio::test]
    async fn test_middleware_accepts_valid_token() {
        let (app, issuer) = make_app();
        let pair = issuer.issue(42, "alice").await.unwrap();
        let resp = app
            .oneshot(
                Request::builder()
                    .uri("/protected")
                    .header("authorization", format!("Bearer {}", pair.access_token))
                    .body(Body::empty())
                    .unwrap(),
            )
            .await
            .unwrap();
        assert_eq!(resp.status(), StatusCode::OK);
        let body = axum::body::to_bytes(resp.into_body(), usize::MAX).await.unwrap();
        let text = String::from_utf8(body.to_vec()).unwrap();
        assert!(text.contains("user_id=42"));
        assert!(text.contains("username=alice"));
    }

    #[tokio::test]
    async fn test_middleware_rejects_refresh_token_as_access() {
        let (app, issuer) = make_app();
        let pair = issuer.issue(42, "alice").await.unwrap();
        let resp = app
            .oneshot(
                Request::builder()
                    .uri("/protected")
                    .header("authorization", format!("Bearer {}", pair.refresh_token))
                    .body(Body::empty())
                    .unwrap(),
            )
            .await
            .unwrap();
        assert_eq!(resp.status(), StatusCode::UNAUTHORIZED);
    }

    #[tokio::test]
    async fn test_middleware_rejects_invalid_token() {
        let (app, _) = make_app();
        let resp = app
            .oneshot(
                Request::builder()
                    .uri("/protected")
                    .header("authorization", "Bearer invalid.token.here")
                    .body(Body::empty())
                    .unwrap(),
            )
            .await
            .unwrap();
        assert_eq!(resp.status(), StatusCode::UNAUTHORIZED);
    }

    // ── T4: PoolConfig + RemoteValidateConfig 单元测试 ──

    #[cfg(feature = "remote-validate")]
    mod remote_validate_tests {
        use super::*;
        use std::time::Duration;

        #[test]
        fn test_pool_config_default() {
            let config = PoolConfig::default();
            assert_eq!(config.pool_max_idle_per_host, 32);
            assert!(config.pool_idle_timeout.is_some());
            assert!(config.tcp_keepalive.is_some());
            assert!(config.tcp_nodelay);
        }

        #[test]
        fn test_remote_validate_config_new_backward_compatible() {
            let config = RemoteValidateConfig::new(
                "http://localhost:8080/validate",
                Duration::from_secs(10),
                vec!["/public/*".to_string()],
            );
            assert_eq!(config.endpoint, "http://localhost:8080/validate");
            assert_eq!(config.timeout, Duration::from_secs(10));
            assert_eq!(config.allow_all_action, vec!["/public/*".to_string()]);
        }

        #[test]
        fn test_remote_validate_config_new_checked_success() {
            let result = RemoteValidateConfig::new_checked(
                "http://localhost:8080/validate",
                Duration::from_secs(5),
                vec![],
                PoolConfig::default(),
            );
            assert!(result.is_ok());
            let config = result.unwrap();
            assert_eq!(config.endpoint, "http://localhost:8080/validate");
        }

        #[test]
        fn test_remote_validate_config_new_or_default_success() {
            let config = RemoteValidateConfig::new_or_default(
                "http://localhost:8080/validate",
                Duration::from_secs(5),
                vec![],
                PoolConfig::default(),
            );
            assert_eq!(config.endpoint, "http://localhost:8080/validate");
        }

        #[test]
        fn test_remote_validate_config_from_client() {
            let client = reqwest::Client::new();
            let config = RemoteValidateConfig::from_client(
                "http://localhost:8080/validate",
                Duration::from_secs(10),
                vec!["*".to_string()],
                client,
            );
            assert_eq!(config.endpoint, "http://localhost:8080/validate");
            assert_eq!(config.allow_all_action, vec!["*".to_string()]);
        }

        #[test]
        fn test_remote_validate_config_builder() {
            let config = RemoteValidateConfig::builder("http://localhost:8080/validate")
                .timeout(Duration::from_secs(15))
                .allow_all_action(vec!["/api/*".to_string()])
                .build()
                .unwrap();
            assert_eq!(config.endpoint, "http://localhost:8080/validate");
            assert_eq!(config.timeout, Duration::from_secs(15));
            assert_eq!(config.allow_all_action, vec!["/api/*".to_string()]);
        }

        #[test]
        fn test_remote_validate_config_builder_default_timeout() {
            let config = RemoteValidateConfig::builder("http://localhost:8080/validate")
                .build()
                .unwrap();
            assert_eq!(config.timeout, Duration::from_secs(10));
        }

        #[test]
        fn test_pool_config_custom() {
            let config = PoolConfig {
                pool_max_idle_per_host: 64,
                pool_idle_timeout: Some(Duration::from_secs(120)),
                tcp_keepalive: None,
                tcp_nodelay: false,
            };
            assert_eq!(config.pool_max_idle_per_host, 64);
            assert!(config.tcp_keepalive.is_none());
            assert!(!config.tcp_nodelay);
        }

        #[test]
        fn test_is_allowed_wildcard() {
            let config = RemoteValidateConfig::new(
                "http://localhost:8080/validate",
                Duration::from_secs(10),
                vec!["*".to_string()],
            );
            assert!(config.is_allowed("/any/path"));
            assert!(config.is_allowed("/"));
        }

        #[test]
        fn test_is_allowed_prefix_match() {
            let config = RemoteValidateConfig::new(
                "http://localhost:8080/validate",
                Duration::from_secs(10),
                vec!["/public/*".to_string()],
            );
            assert!(config.is_allowed("/public/index"));
            assert!(!config.is_allowed("/private/data"));
        }
    }
}