Skip to content
This repository was archived by the owner on Oct 19, 2026. It is now read-only.

Commit a467a25

Browse files
authored
Fix identity extraction in cedar_guard middleware & correct revoke_bot_token OpenAPI docs (#2028)
* #1999: rebase Signed-off-by: Hongze Gao <15101764808@163.com> * - Export bot_token_dto in jupiter model so BotTokenInfo is reachable - BotsStorage: add get_bot_by_id() for bot existence checks - BotsStorage: use update_many() in revoke_bot_tokens_by_bot to avoid N+1 - BotsStorage: validate HMAC secret (non-empty, min 32 chars) in load_bot_token_hmac_key - BotsStorage: use DateTimeWithTimeZone for expiry comparison in find_bot_by_token - AuditStorage: set created_at with DateTimeWithTimeZone (Utc::now().into()) - bot_router: ensure bot exists before create/list/revoke; return 404 when missing - bot_router: validate expires_in range (1..=10y) to avoid overflow/panic - tests: add audit_storage to AppService in test_storage() Signed-off-by: Hongze Gao <15101764808@163.com> * #1999: fix identity extraction in cedar_guard middleware and revoke_bot_token 404 OpenAPI docs - Use req.extract() to resolve LoginUser/BotIdentity instead of direct extensions access - Fix missing Bot/Human context and incorrect "reader" fallback - Update 404 description to "Bot not found" - Align docs with idempotent implementation Signed-off-by: Hongze Gao <15101764808@163.com> * #1999: remove duplicate bot token helpers and module export - remove duplicated in - deduplicate bot token HMAC helpers in to fix E0428 - short-circuit auth extraction in : try first, then only if needed Signed-off-by: Hongze Gao <15101764808@163.com> * #1999: use stateful axum extractors in cedar guard middleware - replace state-less request extraction in with for both and - fix incorrect import by removing and importing instead - keep short-circuit principal resolution: try bot identity first, then attempt login user only when bot extraction fails Signed-off-by: Hongze Gao <15101764808@163.com> * #1999: use request parts API for stateful identity extraction in cedar_guard - replace invalid usage with and extract identities from - run first, then fall back to - preserve middleware flow by reconstructing the request with before calling Signed-off-by: Hongze Gao <15101764808@163.com> * #1999: satisfy clippy by matching extractor results directly - replace + with direct matching in - keep principal resolution order unchanged: first, then , then fallback Signed-off-by: Hongze Gao <15101764808@163.com> --------- Signed-off-by: Hongze Gao <15101764808@163.com>
1 parent 94a1608 commit a467a25

2 files changed

Lines changed: 13 additions & 11 deletions

File tree

‎mono/src/api/guard/cedar_guard.rs‎

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::{collections::HashMap, path::Path, str::FromStr};
22

33
use axum::{
4-
extract::{FromRef, Request, State},
4+
extract::{FromRef, FromRequestParts, Request, State},
55
middleware::Next,
66
response::Response,
77
};
@@ -138,16 +138,16 @@ pub async fn cedar_guard(
138138
// .ok_or_else(|| MegaError::with_message(format!("Change list not found for link: {}", link)))?;
139139
// let repo_path: PathBuf = cl_model.path.into();
140140

141-
let login_user = req.extensions().get::<LoginUser>();
142-
let bot_identity = req.extensions().get::<BotIdentity>();
141+
let (mut parts, body) = req.into_parts();
143142

144-
let (principal_type, principal_id) = if let Some(bot) = bot_identity {
145-
("Bot".to_string(), bot.bot.id.to_string())
146-
} else if let Some(user) = login_user {
147-
("User".to_string(), user.username.clone())
148-
} else {
149-
("User".to_string(), "reader".to_string())
150-
};
143+
let (principal_type, principal_id) =
144+
if let Ok(bot) = BotIdentity::from_request_parts(&mut parts, &state).await {
145+
("Bot".to_string(), bot.bot.id.to_string())
146+
} else if let Ok(user) = LoginUser::from_request_parts(&mut parts, &state).await {
147+
("User".to_string(), user.username.clone())
148+
} else {
149+
("User".to_string(), "reader".to_string())
150+
};
151151

152152
// let policy_path = repo_path.join("cedar/policies.cedar");
153153
// let policy_content = get_blob_string(&state, &policy_path).await?;
@@ -169,6 +169,8 @@ pub async fn cedar_guard(
169169
MegaError::Other(format!("Guard Authorization failed: {}", e)),
170170
)
171171
})?;
172+
173+
let req = Request::from_parts(parts, body);
172174
let response = next.run(req).await;
173175

174176
if response.status().is_client_error() {

‎mono/src/api/router/bot_router.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ async fn list_bot_tokens(
314314
(status = 200, description = "Token revoked successfully"),
315315
(status = 401, description = "Unauthorized"),
316316
(status = 403, description = "Forbidden - admin only"),
317-
(status = 404, description = "Bot or token not found"),
317+
(status = 404, description = "Bot not found"),
318318
),
319319
tag = BOT_TAG
320320
)]

0 commit comments

Comments
 (0)