feat: support admin account login

This commit is contained in:
boos
2026-05-18 17:01:39 +08:00
parent f986fe0377
commit e9595017e9
4 changed files with 189 additions and 22 deletions
+15 -3
View File
@@ -22,7 +22,7 @@ mod sites;
mod tokens;
mod users;
/// Middleware to require admin authentication via Bearer token.
/// Middleware to require admin authentication via config token or admin JWT.
async fn require_admin_auth(
State(state): State<AppState>,
headers: HeaderMap,
@@ -35,8 +35,20 @@ async fn require_admin_auth(
.and_then(|v| v.strip_prefix("Bearer "))
.ok_or_else(|| AppError::Unauthorized("Missing or invalid Authorization header".into()))?;
if auth_header != state.config.auth.auth_token {
return Err(AppError::Unauthorized("Invalid admin token".into()));
if auth_header == state.config.auth.auth_token {
return Ok(next.run(request).await);
}
match crate::auth::jwt::validate_jwt(&state.config.auth.jwt_secret, auth_header) {
Ok(claims) if claims.role == "admin" => {}
Ok(_) => {
return Err(AppError::Forbidden(
"Admin account is required for this route".into(),
));
}
Err(_) => {
return Err(AppError::Unauthorized("Invalid admin token".into()));
}
}
Ok(next.run(request).await)