garrison 0.9.0

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
// Copyright (c) 2026 Kirky.X🌠
// SPDX-License-Identifier: Apache-2.0

//! /oauth2/introspect 端点 — RFC 7662 token 内省。
//!
//! 返回 token 的活跃状态与元数据。有效 token 返回 active=true + scope + client_id + exp,
//! 过期/无效 token 返回 active=false。仅内网端口 :8443 可访问。

use crate::constants::TokenType;
use crate::error::{GarrisonError, GarrisonResult};
use crate::oauth2_server::client::OAuth2ClientStore;
use crate::oauth2_server::token::{TokenHandler, TokenRecord};
use chrono::Utc;
use serde::{Deserialize, Serialize};
use std::sync::Arc;

/// /oauth2/introspect 请求参数。
#[derive(Debug, Clone, Deserialize)]
pub struct IntrospectRequest {
    /// 待查询的 token。
    pub token: String,
    /// token 类型提示("access_token" 或 "refresh_token",可选)。
    pub token_type_hint: Option<String>,
    /// 客户端 ID。
    pub client_id: String,
    /// 客户端密钥。
    pub client_secret: String,
}

/// /oauth2/introspect 响应(RFC 7662 §2.2)。
///
/// 覆盖 RFC 7662 §2.3 全部字段:username / iat / nbf / aud / iss / jti。
#[derive(Debug, Clone, Serialize, PartialEq)]
pub struct IntrospectResponse {
    /// token 是否活跃(有效且未过期)。
    pub active: bool,
    /// token 类型("Bearer")。
    #[serde(skip_serializing_if = "Option::is_none")]
    pub token_type: Option<String>,
    /// 实际授予的 scope(空格分隔)。
    #[serde(skip_serializing_if = "Option::is_none")]
    pub scope: Option<String>,
    /// 关联的客户端 ID。
    #[serde(skip_serializing_if = "Option::is_none")]
    pub client_id: Option<String>,
    /// 过期时间戳(Unix 秒)。
    #[serde(skip_serializing_if = "Option::is_none")]
    pub exp: Option<i64>,
    /// 关联的用户 ID(client_credentials 无)。
    #[serde(skip_serializing_if = "Option::is_none")]
    pub sub: Option<String>,
    /// 人类可读的用户标识(RFC 7662 §2.3,password grant type 有值)。
    #[serde(skip_serializing_if = "Option::is_none")]
    pub username: Option<String>,
    /// 签发时间(Unix 秒,RFC 7662 §2.3)。
    #[serde(skip_serializing_if = "Option::is_none")]
    pub iat: Option<i64>,
    /// 生效时间(Unix 秒,RFC 7662 §2.3,通常等于 iat)。
    #[serde(skip_serializing_if = "Option::is_none")]
    pub nbf: Option<i64>,
    /// 受众(RFC 7662 §2.3,OAuth2 中为 client_id)。
    #[serde(skip_serializing_if = "Option::is_none")]
    pub aud: Option<String>,
    /// 签发者(RFC 7662 §2.3)。
    #[serde(skip_serializing_if = "Option::is_none")]
    pub iss: Option<String>,
    /// JWT 唯一标识(RFC 7662 §2.3,对应 TokenRecord.jti)。
    #[serde(skip_serializing_if = "Option::is_none")]
    pub jti: Option<String>,
}

/// OAuth2 token 签发者标识(RFC 7662 §2.3 `iss` 字段值)。
const OAUTH2_ISSUER: &str = "garrison-oauth2-server";

impl IntrospectResponse {
    /// 创建 inactive 响应(token 无效或过期)。
    pub fn inactive() -> Self {
        Self {
            active: false,
            token_type: None,
            scope: None,
            client_id: None,
            exp: None,
            sub: None,
            username: None,
            iat: None,
            nbf: None,
            aud: None,
            iss: None,
            jti: None,
        }
    }
}

/// /oauth2/introspect handler,处理 token 内省(RFC 7662)。
pub struct IntrospectHandler {
    store: Arc<dyn OAuth2ClientStore>,
    token_handler: Arc<TokenHandler>,
}

impl IntrospectHandler {
    /// 创建 handler。
    pub fn new(store: Arc<dyn OAuth2ClientStore>, token_handler: Arc<TokenHandler>) -> Self {
        Self {
            store,
            token_handler,
        }
    }

    /// 处理 introspect 请求。
    pub async fn handle(&self, req: &IntrospectRequest) -> GarrisonResult<IntrospectResponse> {
        let client = self.store.get(&req.client_id).await?.ok_or_else(|| {
            GarrisonError::OAuth2(format!(
                "oauth2-server-introspect-invalid-client::{}",
                req.client_id
            ))
        })?;
        if !client.verify_secret(&req.client_secret)? {
            return Err(GarrisonError::OAuth2(
                "oauth2-server-introspect-invalid-client-secret".into(),
            ));
        }

        // 按 token_type_hint 选择查找顺序(RFC 7662 §2.1:hint 仅优化查找方向,查不到时可扩展搜索另一种类型);
        // refresh token 存于 `oauth2:rtoken:` 前缀 / rotation SQLite 表,须经 get_refresh_token_record 查找
        let record = match req.token_type_hint.as_deref() {
            Some("refresh_token") => {
                match self
                    .token_handler
                    .get_refresh_token_record(&req.token)
                    .await?
                {
                    Some(r) => Some(r),
                    None => {
                        self.token_handler
                            .get_access_token_record(&req.token)
                            .await?
                    },
                }
            },
            _ => {
                match self
                    .token_handler
                    .get_access_token_record(&req.token)
                    .await?
                {
                    Some(r) => Some(r),
                    None => {
                        self.token_handler
                            .get_refresh_token_record(&req.token)
                            .await?
                    },
                }
            },
        };

        // 过期判定:DAO TTL 理论上已剔除过期记录,但 rotation 路径的 expires_at 不经 TTL 控制且需防御时钟偏差,
        // 过期记录一律 inactive
        match record {
            Some(record) if record.expires_at > Utc::now() => {
                Ok(Self::response_from_record(record))
            },
            _ => Ok(IntrospectResponse::inactive()),
        }
    }

    /// 从 TokenRecord 构造 active 响应(RFC 7662 §2.3 全字段)。
    fn response_from_record(record: TokenRecord) -> IntrospectResponse {
        let scope = if record.scopes.is_empty() {
            None
        } else {
            Some(record.scopes.join(" "))
        };
        let iat_ts = record.issued_at.timestamp();
        IntrospectResponse {
            active: true,
            token_type: Some(TokenType::Bearer.to_string()),
            scope,
            client_id: Some(record.client_id.clone()),
            exp: Some(record.expires_at.timestamp()),
            sub: record.user_id.map(|id| id.to_string()),
            username: record.username,
            iat: Some(iat_ts),
            nbf: Some(iat_ts),           // OAuth2 token 签发即生效,nbf = iat
            aud: Some(record.client_id), // 受众为请求该 token 的客户端
            iss: Some(OAUTH2_ISSUER.into()),
            jti: record.jti,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::dao::{GarrisonDao, InMemoryDao};
    use crate::oauth2_server::authorize::AuthorizeHandler;
    use crate::oauth2_server::client::{DaoOAuth2ClientStore, GrantType, OAuth2Client};
    use crate::oauth2_server::token::{
        PasswordRateLimiter, TokenHandler, TokenRateLimiter, TokenRequest,
    };

    fn make_handlers() -> (
        IntrospectHandler,
        Arc<InMemoryDao>,
        Arc<TokenHandler>,
        Arc<AuthorizeHandler>,
    ) {
        let dao = Arc::new(InMemoryDao::new());
        let store = Arc::new(DaoOAuth2ClientStore::new(dao.clone()));
        let authorize_handler = Arc::new(AuthorizeHandler::new(
            store.clone(),
            dao.clone(),
            "https://auth.example.com/login".into(),
        ));
        let token_handler = Arc::new(TokenHandler::new(
            store.clone(),
            dao.clone(),
            authorize_handler.clone(),
            Arc::new(PasswordRateLimiter::new(1000, 300)),
            Arc::new(TokenRateLimiter::with_limits(100_000, 60, 100_000, 60)),
        ));
        let introspect_handler = IntrospectHandler::new(store, token_handler.clone());
        (introspect_handler, dao, token_handler, authorize_handler)
    }

    fn make_client(id: &str) -> OAuth2Client {
        OAuth2Client::new(
            id,
            "secret-123",
            vec!["https://app.example.com/cb".into()],
            vec![GrantType::ClientCredentials],
            vec!["read".into(), "write".into()],
        )
        .unwrap()
    }

    /// 创建支持 refresh_token 流程的客户端(authorization_code + refresh_token)。
    fn make_refresh_client(id: &str) -> OAuth2Client {
        OAuth2Client::new(
            id,
            "secret-123",
            vec!["https://app.example.com/cb".into()],
            vec![GrantType::AuthorizationCode, GrantType::RefreshToken],
            vec!["read".into(), "write".into()],
        )
        .unwrap()
    }

    /// 通过 authorization_code 流程签发 access_token + refresh_token。
    async fn issue_token_pair(
        authorize_handler: &AuthorizeHandler,
        token_handler: &TokenHandler,
        client_id: &str,
    ) -> (String, String) {
        let verifier = "dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk";
        let challenge = crate::oauth2_server::authorize::generate_code_challenge(verifier);
        let auth_req = crate::oauth2_server::authorize::AuthorizeRequest {
            response_type: "code".into(),
            client_id: client_id.into(),
            redirect_uri: "https://app.example.com/cb".into(),
            scope: Some("read write".into()),
            state: None,
            code_challenge: challenge,
            code_challenge_method: "S256".into(),
            prompt: None,
        };
        let resp = match authorize_handler
            .authorize(&auth_req, Some(1001))
            .await
            .unwrap()
        {
            crate::oauth2_server::authorize::AuthorizeResponse::Redirect { location } => location,
            _ => panic!("期望 Redirect"),
        };
        let code = resp
            .split("code=")
            .nth(1)
            .unwrap()
            .split('&')
            .next()
            .unwrap()
            .to_string();
        let req = TokenRequest {
            grant_type: "authorization_code".into(),
            client_id: client_id.into(),
            client_secret: "secret-123".into(),
            code: Some(code),
            redirect_uri: Some("https://app.example.com/cb".into()),
            code_verifier: Some(verifier.into()),
            refresh_token: None,
            scope: None,
            username: None,
            password: None,
        };
        let token_resp = token_handler.handle(&req).await.unwrap();
        (
            token_resp.access_token,
            token_resp.refresh_token.expect("应有 refresh_token"),
        )
    }

    async fn issue_token(token_handler: &TokenHandler, client_id: &str) -> String {
        let req = TokenRequest {
            grant_type: "client_credentials".into(),
            client_id: client_id.into(),
            client_secret: "secret-123".into(),
            code: None,
            redirect_uri: None,
            code_verifier: None,
            refresh_token: None,
            scope: Some("read write".into()),
            username: None,
            password: None,
        };
        token_handler.handle(&req).await.unwrap().access_token
    }

    #[tokio::test]
    async fn introspect_active_token() {
        let (handler, _, token_handler, _) = make_handlers();
        handler.store.create(make_client("int-001")).await.unwrap();
        let token = issue_token(&token_handler, "int-001").await;

        let req = IntrospectRequest {
            token,
            token_type_hint: Some("access_token".into()),
            client_id: "int-001".into(),
            client_secret: "secret-123".into(),
        };
        let resp = handler.handle(&req).await.expect("内省");
        assert!(resp.active);
        assert_eq!(resp.token_type.as_deref(), Some("Bearer"));
        assert_eq!(resp.client_id.as_deref(), Some("int-001"));
        assert_eq!(resp.scope.as_deref(), Some("read write"));
        assert!(resp.exp.is_some());
        assert!(resp.sub.is_none(), "client_credentials 无 user_id");
        // RFC 7662 §2.3 新增字段验证
        assert!(resp.iat.is_some(), "iat 必须有值");
        assert!(resp.nbf.is_some(), "nbf 必须有值");
        assert_eq!(resp.iat, resp.nbf, "OAuth2 token 签发即生效,nbf = iat");
        assert_eq!(resp.aud.as_deref(), Some("int-001"), "aud = client_id");
        assert_eq!(resp.iss.as_deref(), Some("garrison-oauth2-server"));
        assert!(resp.jti.is_some(), "jti 必须有值");
        assert!(resp.username.is_none(), "client_credentials 无 username");
    }

    #[tokio::test]
    async fn introspect_nonexistent_token_returns_inactive() {
        let (handler, _, _, _) = make_handlers();
        handler.store.create(make_client("int-002")).await.unwrap();

        let req = IntrospectRequest {
            token: "nonexistent".into(),
            token_type_hint: None,
            client_id: "int-002".into(),
            client_secret: "secret-123".into(),
        };
        let resp = handler.handle(&req).await.expect("内省");
        assert!(!resp.active);
        assert!(resp.token_type.is_none());
        assert!(resp.client_id.is_none());
    }

    #[tokio::test]
    async fn introspect_revoked_token_returns_inactive() {
        let (handler, _, token_handler, _) = make_handlers();
        handler.store.create(make_client("int-003")).await.unwrap();
        let token = issue_token(&token_handler, "int-003").await;

        token_handler.revoke_token(&token).await.unwrap();

        let req = IntrospectRequest {
            token,
            token_type_hint: None,
            client_id: "int-003".into(),
            client_secret: "secret-123".into(),
        };
        let resp = handler.handle(&req).await.expect("内省");
        assert!(!resp.active, "撤销后的 token 应返回 inactive");
    }

    #[tokio::test]
    async fn introspect_invalid_client_id() {
        let (handler, _, _, _) = make_handlers();
        let req = IntrospectRequest {
            token: "some-token".into(),
            token_type_hint: None,
            client_id: "no-such".into(),
            client_secret: "secret".into(),
        };
        let err = handler.handle(&req).await.unwrap_err();
        assert!(err.to_string().contains(&crate::i18n::translate_detail(
            "oauth2-server-introspect-invalid-client",
            &[("arg0", "no-such")],
        )));
    }

    #[tokio::test]
    async fn introspect_invalid_client_secret() {
        let (handler, _, _, _) = make_handlers();
        handler.store.create(make_client("int-004")).await.unwrap();
        let req = IntrospectRequest {
            token: "some-token".into(),
            token_type_hint: None,
            client_id: "int-004".into(),
            client_secret: "wrong".into(),
        };
        let err = handler.handle(&req).await.unwrap_err();
        assert!(err
            .to_string()
            .contains("oauth2-server-introspect-invalid-client"));
    }

    #[test]
    fn inactive_response_all_none() {
        let resp = IntrospectResponse::inactive();
        assert!(!resp.active);
        assert!(resp.token_type.is_none());
        assert!(resp.scope.is_none());
        assert!(resp.client_id.is_none());
        assert!(resp.exp.is_none());
        assert!(resp.sub.is_none());
        // RFC 7662 §2.3 新增字段在 inactive 响应中均为 None
        assert!(resp.username.is_none());
        assert!(resp.iat.is_none());
        assert!(resp.nbf.is_none());
        assert!(resp.aud.is_none());
        assert!(resp.iss.is_none());
        assert!(resp.jti.is_none());
    }

    /// refresh_token 内省:经 `oauth2:rtoken:` 前缀(fallback)/ rotation 表查找,使 refresh token 可返回 active=true。
    #[tokio::test]
    async fn introspect_refresh_token_returns_active() {
        let (handler, _, token_handler, authorize_handler) = make_handlers();
        handler
            .store
            .create(make_refresh_client("int-rt-001"))
            .await
            .unwrap();
        let (_access, refresh) =
            issue_token_pair(&authorize_handler, &token_handler, "int-rt-001").await;

        // hint = refresh_token
        let req = IntrospectRequest {
            token: refresh.clone(),
            token_type_hint: Some("refresh_token".into()),
            client_id: "int-rt-001".into(),
            client_secret: "secret-123".into(),
        };
        let resp = handler.handle(&req).await.expect("内省");
        assert!(resp.active, "有效的 refresh_token 应返回 active=true");
        assert_eq!(resp.client_id.as_deref(), Some("int-rt-001"));
        assert_eq!(resp.scope.as_deref(), Some("read write"));
    }

    /// token_type_hint 仅是查找提示:hint 与 token 实际类型不符时也须能查到(RFC 7662 §2.1 允许扩展搜索),
    /// 不得因 hint 错误返回 inactive。
    #[tokio::test]
    async fn introspect_hint_is_only_a_hint_fallback_still_finds() {
        let (handler, _, token_handler, authorize_handler) = make_handlers();
        handler
            .store
            .create(make_refresh_client("int-hint-001"))
            .await
            .unwrap();
        let (access, refresh) =
            issue_token_pair(&authorize_handler, &token_handler, "int-hint-001").await;

        // refresh token + access_token hint → 扩展搜索后仍应 active
        let req = IntrospectRequest {
            token: refresh,
            token_type_hint: Some("access_token".into()),
            client_id: "int-hint-001".into(),
            client_secret: "secret-123".into(),
        };
        let resp = handler.handle(&req).await.expect("内省");
        assert!(resp.active, "hint 错误时不得误报 inactive");

        // access token + refresh_token hint → 同理
        let req = IntrospectRequest {
            token: access,
            token_type_hint: Some("refresh_token".into()),
            client_id: "int-hint-001".into(),
            client_secret: "secret-123".into(),
        };
        let resp = handler.handle(&req).await.expect("内省");
        assert!(resp.active, "hint 指向 refresh 时 access token 也应可查到");
    }

    /// 过期 token 内省必须返回 active=false(即使 DAO 记录尚未被 TTL 清理)。
    #[tokio::test]
    async fn introspect_expired_token_returns_inactive() {
        let (handler, dao, _token_handler, _) = make_handlers();
        handler
            .store
            .create(make_client("int-exp-001"))
            .await
            .unwrap();

        // 直接向 DAO 写入一条已过期的 access token 记录(模拟 TTL 尚未清理的过期记录)
        let expired_record = TokenRecord {
            token: "expired-token-001".into(),
            client_id: "int-exp-001".into(),
            user_id: None,
            scopes: vec!["read".into()],
            token_type: "access".into(),
            expires_at: Utc::now() - chrono::Duration::hours(1),
            issued_at: Utc::now() - chrono::Duration::hours(2),
            jti: Some("expired-jti".into()),
            username: None,
        };
        let key = crate::constants::DaoKeyPrefix::OAuth2AccessToken.build_key("expired-token-001");
        dao.set(&key, &serde_json::to_string(&expired_record).unwrap(), 600)
            .await
            .unwrap();

        let req = IntrospectRequest {
            token: "expired-token-001".into(),
            token_type_hint: None,
            client_id: "int-exp-001".into(),
            client_secret: "secret-123".into(),
        };
        let resp = handler.handle(&req).await.expect("内省");
        assert!(!resp.active, "过期 token 应返回 active=false");
        assert!(resp.client_id.is_none(), "inactive 响应不应泄露记录字段");
    }
}